Skip to content

Instantly share code, notes, and snippets.

@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)
@abikoushi
abikoushi / Gam_Dir_Po.R
Created January 1, 2026 00:23
Gamma-Dirichlet-Poisson-NMF (reparametrization)
NMF <- function(X, M=2,a_W=1,b_W=1,a_H=1,b_H=1,tol=1e-2,maxit=10000,
seed=1234){
set.seed(seed)
D <-nrow(X)
N <-ncol(X)
W<-matrix(rgamma(D*M,a_W,b_W),D,M)
H<-matrix(rgamma(M*N,a_H,b_H),M,N)
logW0 <-log(W)
logH0 <-log(H)
@abikoushi
abikoushi / CanonicalFormNormal.R
Created December 29, 2025 10:18
Canonical form parametrized normal distribution
rcfnorm <- function(n, shape=0, inv_scale=1){
rnorm(n, shape/inv_scale, 1/sqrt(inv_scale))
}
dcfnorm <- function(n, shape=0, inv_scale=1, ...){
dnorm(n, shape/inv_scale, 1/sqrt(inv_scale), ...)
}
pcfnorm <- function(n, shape=0, inv_scale=1, ...){
pnorm(n, shape/inv_scale, 1/sqrt(inv_scale), ...)
@abikoushi
abikoushi / readmtxfiles_asALTO.R
Last active December 26, 2025 00:51
read MM file as ALTO format
size_mtx <- function(file_path){
con <- file(file_path, open = "r") #Open for reading in text mode
size <- scan(con, what=integer(), comment.char = "%", nlines = 1, skip = 1, quiet = TRUE)
close(con)
names(size) <- c("row","column","nonzero")
return(size)
}
take_last <- function(x){
x[length(x)]