Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / asymp_meanvar.R
Created February 20, 2026 04:11
plot sample mean and variance
moments_4 <- function(truedens){
k1 <- integrate(function(x){x*truedens(x)}, lower=-Inf, upper=Inf)
k2 <- integrate(function(x){(x-k1$value)^2*truedens(x)}, lower=-Inf, upper=Inf)
k3 <- integrate(function(x){(x-k1$value)^3*truedens(x)}, lower=-Inf, upper=Inf)
k4 <- integrate(function(x){(x-k1$value)^4*truedens(x)}, lower=-Inf, upper=Inf)
c(k1$value, k2$value,
k3$value, k4$value)
}
Mu4 <- moments_4(truedens = function(x){dgamma(x,2,1)})
@abikoushi
abikoushi / simCLT.R
Created February 18, 2026 07:20
A demonstration of central limit theorem
library(ggplot2)
library(dplyr)
library(tidyr)
# integrate(f = function(x)x*(0.6*dnorm(x, -10/6)+0.4*dnorm(x, 10/4)), lower = -Inf, upper = Inf)
scaled_mean <- function(x){sqrt(length(x))* mean(x) / ifelse(length(x)==1L,1,sd(x))}
CLTsim <- function(n, iter){
mu <- c(-10/6, 10/4)
mixture <- numeric(iter)
uniform <- numeric(iter)
@abikoushi
abikoushi / BayesianLogistic_polyagamma.R
Created February 17, 2026 01:30
Gibbs sampler of the Bayesian logistic regression via Polya-gamma distribution
library(BayesLogit)
library(ggplot2)
library(dplyr)
#########
#Bayesian inference for logistic models using Polya-Gamma latent variables (2013)
#Nicholas G. Polson, James G. Scott, Jesse Windle
#https://arxiv.org/abs/1205.0310
#########
#Y: response variable
#X: explanatory design matrix
@abikoushi
abikoushi / mixnorm.R
Created January 23, 2026 00:33
An animation of posterior distribution with mixture normal model
library(dplyr)
library(tidyr)
library(ggplot2)
library(gganimate)
logsumexp2 =function (logx1,logx2){
logx1 + log1p(exp(logx2-logx1))
}
llmixnorm <- function(par, y){
@abikoushi
abikoushi / TryRcppParallel.R
Created January 13, 2026 23:52
Try RcppParallel
library(Rcpp)
library(RcppParallel)
sourceCpp("R/cpp/parallelreduce.cpp")
# defaultNumThreads()
setThreadOptions(numThreads = 4)
m <- rnorm(1e+8)
# ensure that serial and parallel versions give the same result
# compare performance of serial and parallel
library(rbenchmark)
@abikoushi
abikoushi / Try_roll.R
Created January 13, 2026 19:10
Try multi-threads `roll` package
library(roll)
library(RcppParallel)
RcppParallel::defaultNumThreads()
x <- rnorm(1e8)
setThreadOptions(numThreads = 1L)
t1 <- system.time({
roll_mean(x, 1000)
})
@abikoushi
abikoushi / gist:aa8b941c69ff1a1656f951fc1a37aabd
Last active January 13, 2026 20:26
スティーブン・ピンカー『人はどこまで合理的か 上』5章のタクシー問題に出てくるような図
##参考文献:
##スティーブン・ピンカー『人はどこまで合理的か 上』,5章,訳:橘明美,草思社
#devtools::install_github("abikoushi/ggsomestat")
library(ggsomestat)
library(dplyr)
taxi <- matrix(c(0.68, 0.03,
0.17, 0.12), byrow = TRUE, nrow = 2)
gb=c("green","blue")
@abikoushi
abikoushi / overparametrize_withoutintercept.R
Created January 8, 2026 08:18
冗長性のあるワンホットエンコーディングによるパラメータ化の係数をフルランクの計画行列の係数に変換する(切片項なし)
library(moltenNMF)
library(Matrix)
df = expand.grid(letters[1:3], LETTERS[1:2])
X = sparse_onehot(~., data = df)
A = matrix(c(1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,0,
@abikoushi
abikoushi / GamDirPoiMul.tex
Last active January 7, 2026 01:58
TikZ Picture of Poisson, Gamma, Dirichlet, and Multinomial distributions
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[
node distance=30mm,
box/.style={draw, rounded corners, align=center, inner sep=6pt, minimum height=5em ,minimum width=7em},
arrow/.style={->, thick}
@abikoushi
abikoushi / Gam_Dir_Po.R
Created January 3, 2026 08:28
reparameterized Poisson NMF
## reparameterized Poisson NMF
NMF_re <- function(X,
M=2,
a_W=1,
a_H=1,
alpha_b=1, beta_b=1,
tol=1e-3, maxit=100){
D <- nrow(X)
N <- ncol(X)
b_W <- rgamma(1, 1, 1)