Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / gd_norm.R
Created May 15, 2024 08:54
Gradient Discent normal MLE
library(ggplot2)
library(dplyr)
library(numDeriv)
lp <- function(x,mu,u){
-exp(u)*sum((x-mu)^2)/2 + length(x)*u
}
dlp_mu <- function(x,mu,u){
exp(u)*sum((x-mu))
}
@abikoushi
abikoushi / inner_prod.tex
Created May 10, 2024 07:38
tikz example (inner product)
\documentclass[dvipdfmx, dvipsnames]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes, decorations.pathreplacing, backgrounds, positioning}
\begin{document}
\frame{
\begin{figure}
\begin{tabular}{ccc}
inner product of \Huge $($
\begin{tikzpicture}
@abikoushi
abikoushi / poker.R
Created May 8, 2024 10:07
Monte-Carlo simulation of the five cards
drawfive <- function(iter){
three = 0L
four = 0L
onepair = 0L
twopair = 0L
fullhouse = 0L
straight = 0L
flush = 0L
#iter <- 100000
dec <- expand.grid(suit=1L:4L,rank=1L:13L)
@abikoushi
abikoushi / app.R
Last active May 7, 2024 02:06
Shiny application to learn confidence interval (t-dist)
##
p_stat_t <- function(v, df = 1,
alternative = "two.sided"){
if (!alternative %in% c("two.sided", "less", "greater")) {
stop("alternative must be either \"two.sided\", \"less\", or \"greater\".")
}
if(alternative == "two.sided"){
p0 <- 2*pt(abs(v), df, lower.tail = FALSE)
}else if(alternative == "less"){
p0 <- pt(v, df)
@abikoushi
abikoushi / app.r
Last active May 5, 2024 07:23
Shiny application to learn p-value
##
p_stat_t <- function(v, df = 1,
alternative = "two.sided"){
if (!alternative %in% c("two.sided", "less", "greater")) {
stop("alternative must be either \"two.sided\", \"less\", or \"greater\".")
}
if(alternative == "two.sided"){
p0 <- 2*pt(abs(v), df, lower.tail = FALSE)
}else if(alternative == "less"){
p0 <- pt(v, df)
@abikoushi
abikoushi / dotdiagram.R
Last active May 2, 2024 05:10
dot diagram (visualization of `formula`)
library(reshape2)
library(ggplot2)
theme_dotdiagram <- function(...){
theme_minimal(...)%+%
theme(axis.text.y = element_text(colour="black"),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
panel.grid.minor = element_blank(),
@abikoushi
abikoushi / hclust_hull.R
Created April 29, 2024 10:56
Comparison of the methods of `hclust`
library(dplyr)
library(ggplot2)
x <- c("ward.D","ward.D2", "single","complete",
"average","mcquitty","median","centroid")
X <- log(as.matrix(iris[,1:2]))
df <- data.frame(X)
d <- dist(X)
@abikoushi
abikoushi / hclust_distplot.R
Created April 29, 2024 09:25
Comparison of the methods of `hclust`
library(dplyr)
library(ggplot2)
x <- c("ward.D","ward.D2", "single","complete",
"average","mcquitty","median","centroid")
X <- log(as.matrix(iris[,1:4]))
d <- dist(X)
dm <- as.matrix(d)
@abikoushi
abikoushi / mat_anno.tex
Created April 24, 2024 06:48
TikZ example: matrix with annotation
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing, backgrounds, positioning}
\begin{document}
\begin{tikzpicture}[
%Styles
Matrix/.style={
matrix of nodes,
text height=2.5ex,
@abikoushi
abikoushi / heatmap.r
Created April 19, 2024 04:15
pheatmap: colored row labels text-by-text
library(pheatmap)
library(grid)
mid0scale <- function(test, paletteLength=120){
myColor <- colorRampPalette(c("orange", "white", "royalblue"))(paletteLength)
myBreaks <- c(seq(min(test), 0, length.out=ceiling(paletteLength/2) + 1),
seq(max(test)/paletteLength, max(test), length.out=floor(paletteLength/2)))
return(list(color=myColor, breaks=myBreaks))
}
set.seed(123)