Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / TFIDF.R
Created October 31, 2025 10:00
An example of TF-IDF
library(janeaustenr)
library(tidytext)
library(dplyr)
library(ggplot2)
book_words <- austen_books() %>%
unnest_tokens(word, text) %>%
count(book, word, sort = TRUE) %>%
group_by(book) %>%
mutate(total = sum(n)) %>%
@abikoushi
abikoushi / MatExp.md
Last active October 31, 2025 07:26
行列の指数関数と常微分方程式についてのイントロ

行列の指数関数と常微分方程式についてのイントロ

はしがき

行列の指数関数と常微分方程式について入門するためのちょっとしたメモです.独学者が行列の指数関数にいきなり出会ってしまっても怯まないように,という願いが込められています.

例によってあまり self-contained な書き方でないので文献を紹介しておきます.行列の指数関数と微分方程式については

  1. 長谷川浩司 線形代数
  2. Hirsch・Smale・Devaney 力学系入門
@abikoushi
abikoushi / MatrixExp.R
Last active October 31, 2025 07:00
Solving ordinary differential equations using matrix exponentials
library(deSolve)
modLinear <- function(Time, State, A) {
return(list(A%*%State))
}
a = 0.2
b = 0.1
A = matrix(c(-a, 0,
a, -b), 2, 2, byrow = TRUE)
yini <- c(X = 1, Y = 0)
@abikoushi
abikoushi / Density_FMVNCHypergeo.R
Last active October 27, 2025 05:04
Calculate density of the multivariate Fisher's noncentral hypergeometric distribution
library(BiasedUrn)
softmax <- function(x){
mx = max(x)
ex = exp(x-mx)
ex/sum(ex)
}
MVNCH_Fisher <- function (x, m, weight) {
@abikoushi
abikoushi / Density_FMVNCHypergeo.R
Last active October 22, 2025 09:54
Check probability function of a multivariate Fisher's noncentral hypergeometric distribution
library(BiasedUrn)
softmax <- function(x){
mx = max(x)
ex = exp(x-mx)
ex/sum(ex)
}
MVNCH_Fisher <- function(K,M,OR){
CB = combn(K,M)
nr = ncol(CB)
@abikoushi
abikoushi / MVNCNHypergeo.R
Created October 21, 2025 03:53
Try multivariate noncentral hypergeometric distribution in BiasedUrn package (without replacement)
library(BiasedUrn)
library(dplyr)
K=10
set.seed(1234); OR = rexp(K)
M=3
ressim = replicate(100000, sort(sample.int(K, size = M, prob = OR, replace = FALSE))) %>%
t() %>%
as.data.frame() %>%
group_by_all() %>%
tally()
@abikoushi
abikoushi / MVNCNHypergeo.R
Created October 20, 2025 08:24
Try multivariate noncentral hypergeometric distribution in BiasedUrn package
library(BiasedUrn)
library(dplyr)
K=10
set.seed(1234); OR = rexp(K)
ressim = sample.int(K, size = 10000, prob = OR, replace = TRUE) |>
table() |>
as.data.frame()
ressim = mutate(ressim, prob = Freq/sum(Freq))
@abikoushi
abikoushi / try_brunnermunzel.R
Created October 20, 2025 07:20
Comparison Brunner-Munzel and Wilcoxon rank sum test (my first try)
library(brunnermunzel)
#https://cran.r-project.org/web/packages/brunnermunzel/vignettes/usage.html
## median of the difference: point a s.t. P(X-Y<a)+.5*P(X-Y=a) = 0.5
conv <- function(x){
#numerical convolution
integrate(function(y){dnorm(x+y,mu)*dexp(y)},-Inf,0)$value +
integrate(function(y){dnorm(x+y,mu)*dexp(y)},0,Inf)$value
}
conv <- Vectorize(conv)
@abikoushi
abikoushi / simIV.R
Last active October 13, 2025 14:38
A simulation of the method of Instrumental Variables
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)
library(dplyr)
library(dqrng)
library(ggplot2)
g <- grViz("digraph{
graph[rankdir = TB]
node[shape = none]
@abikoushi
abikoushi / DAG.R
Created October 13, 2025 05:23
Try `dagitty` and `ggdag`
library(dagitty)
library(ggdag)
library(ggplot2)
library(gridExtra)
g1 <- dagitty("dag{U -> X; U ->Y; X -> Y}")
exposures(g1) <- "X"
outcomes(g1) <- "Y"
plot(g1)