Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / biased_urn.R
Last active October 2, 2025 06:27
Multivariate Wallenius’ Non-Central Hypergeometric distribution in R
# https://en.wikipedia.org/wiki/Wallenius%27_noncentral_hypergeometric_distribution#Multivariate_distribution
# https://markheckmann.github.io/notes/wallenius-distribution.html
library(dplyr)
library(tibble)
library(BiasedUrn)
set.seed(1234); R = rexp(10)
ressim = t(replicate(1e5L,sort(sample.int(10, size = 3, prob = R))))
pmf = data.frame(ressim) %>%
group_by_all() %>%
@abikoushi
abikoushi / case1.tex
Created September 12, 2025 14:53
TikZ bayesnet example
\documentclass[tikz, border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{tikzpicture}
\node[obs] (x) {$X$};
\node[obs, above right = of x] (z) {$Z$};
\node[obs, below right = of z] (y) {$Y$};
\edge{x}{y};
\edge{z}{y};
@abikoushi
abikoushi / weibull_median.R
Last active September 13, 2025 01:41
相対的貧困率の例
#browseURL("https://en.wikipedia.org/wiki/Weibull_distribution")
median_weibull <- function(shape, scale=1){
ln2 = log(2)
scale * (ln2^(1/shape))
}
relativepoverty <- function(shape, scale=1){
pweibull(0.5*median_weibull(shape, scale), shape, scale)
}
@abikoushi
abikoushi / shimarisu_ch1_reproduce.R
Created September 8, 2025 09:39
佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』第1話の最初の表の数値例 (ver.2)
########
## 佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』(朝倉書店)
## Chapter 1: 再現性
########
library(dplyr)
library(ggplot2)
TPratio = function(true_ratio,
beta = 0.8,
alpha = 0.05){
study1 =cbind(true_ratio*c(beta,1-beta),
@abikoushi
abikoushi / shimarisu_ch1_repro.R
Created September 3, 2025 10:59
佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』第1話の最初の表の数値例
########
## 佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』(朝倉書店)
## Chapter 1: 再現性
########
library(dplyr)
library(ggplot2)
reproducibility = function(true_ratio,
beta = 0.8,
alpha = 0.05){
study1 =cbind(true_ratio*c(beta,1-beta),
@abikoushi
abikoushi / HDI.R
Created September 3, 2025 06:53
Highest density interval of beta distribution
# the following are used as reference
# https://stats.stackexchange.com/questions/381520/how-can-i-estimate-the-highest-posterior-density-interval-from-a-set-of-x-y-valu
hdi_beta = function(shape1, shape2, coverage, n){
x = seq(0,1, length.out=n)
best = 0.0
for (ai in 1 : (length(x) - 1)){
for (bi in (ai + 1) : length(x)){
mass = pbeta(x[bi], shape1, shape2) - pbeta(x[ai], shape1, shape2)
if (mass >= coverage && (mass / (x[bi] - x[ai])) > best){
@abikoushi
abikoushi / multiple_comparisons.R
Created September 2, 2025 16:07
Plot p-value function with multiple comparisons
library(ggplot2)
library(tidyr)
library(dplyr)
X = matrix(0,10,10)
set.seed(1234)
X[,1:5] = rnorm(50, 0)
X[,6:10] = rnorm(50, 2)
colwise_ttest <- function(X, mu, method=NULL){
@abikoushi
abikoushi / pvalfun_prop.R
Created August 30, 2025 08:14
animate p-value function of the binomial distribution
library(ggplot2)
library(dplyr)
library(animation)
library(rootSolve)
probbeta <- function(p, x, n, a=0.5, b=0.5){
ahat <- x + a
bhat <- n - x + b
pl <- pbeta(p, ahat, bhat, lower.tail=TRUE)
pu <- pbeta(p, ahat, bhat, lower.tail=FALSE)
@abikoushi
abikoushi / causalDAG.R
Created August 26, 2025 08:05
Plot Causal Diagram using DiagrammeR
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)
#https://elevanth.org/blog/2021/06/21/regression-fire-and-dangerous-things-2-3/
g <- grViz("digraph{
node[shape = plaintext]
U[label = 'U', shape=circle]
B1[label = 'B1']
B2[label = 'B2']
M[label = 'M', fontcolor='red']
@abikoushi
abikoushi / qrviz.R
Created August 23, 2025 15:39
Visualize QR algorithm for covariance matrix
library(mvtnorm)
library(reshape2)
library(ggplot2)
library(gganimate)
library(dplyr)
Sig = matrix(c(1,0.8,0.8,1),2,2)
scatters = vector("list", 5)
heats = vector("list", 5)
X = rmvnorm(5000, sigma=Sig)