Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / MontyHall_problem.R
Created November 24, 2025 08:46
モンティ・ホール問題のシミュレーション
library(dplyr)
library(gt)
doors = LETTERS[1:3]
policy1 = 0L
policy2 = 0L
iter = 100000
res_raw = matrix(NA_character_, iter, 4)
set.seed(1124)
system.time({
@abikoushi
abikoushi / draw_utilityfun.R
Created November 23, 2025 00:15
draw utility (or expectation) function
Ut <- function(x,alpha,beta,lambda){
ifelse(x>0, x^alpha, -lambda*((-x)^beta))
}
params <- list(
list(alpha=1,beta=1,lambda=1),
list(alpha=1,beta=1,lambda=2),
list(alpha=1/2,beta=1/2,lambda=2),
list(alpha=2,beta=2,lambda=1)
)
@abikoushi
abikoushi / heatmap.R
Created November 20, 2025 06:37
Try ComplexHeatmap
#BiocManager::install("ComplexHeatmap")
library(tidyr)
library(dplyr)
library(tibble)
library(ComplexHeatmap)
df0 <- rownames_to_column(mtcars, var="car") %>%
separate(car, into=c("name","model"), extra="merge")
head(df0)
@abikoushi
abikoushi / crossing_border.R
Created November 16, 2025 04:36
2点を結ぶとき線分が多角形の境界をまたぐかの判定
#ref https://qiita.com/zu_rin/items/e04fdec4e3dec6072104
Judge <- function(a, b, c, d) {
s = (a$x - b$x) * (c$y - a$y) - (a$y - b$y) * (c$x - a$x);
t = (a$x - b$x) * (d$y - a$y) - (a$y - b$y) * (d$x - a$x);
if (s * t > 0){
return(FALSE)
}
s = (c$x - d$x) * (a$y - c$y) - (c$y - d$y) * (a$x - c$x);
t = (c$x - d$x) * (b$y - c$y) - (c$y - d$y) * (b$x - c$x);
if (s * t > 0){
@abikoushi
abikoushi / crossing_number_algorithm.R
Created November 15, 2025 13:27
多角形領域の内外判定
#https://www.nttpc.co.jp/technology/number_algorithm.html
cn <- function(polygon, point) {
cn <- 0
n <- nrow(polygon)
for (i in 1:(n - 1)) {
y0 <- polygon$y[i]
y1 <- polygon$y[i + 1]
x0 <- polygon$x[i]
x1 <- polygon$x[i + 1]
# ルール1(上向きの辺)
@abikoushi
abikoushi / try_ggalluvial.R
Last active November 11, 2025 03:50
Try ggalluvial (Sankey Chart)
library(ggalluvial)
library(dplyr)
titanic_wide <- data.frame(Titanic)
titanic1 = group_by(titanic_wide,Class, Age, Survived) %>%
summarise(Freq = sum(Freq)) %>%
ungroup() %>%
mutate(dummy=1L)
@abikoushi
abikoushi / bigram.R
Last active November 10, 2025 01:42
Simple frequency plot of the bigram
library(dplyr)
library(ggplot2)
#see following
#browseURL("https://rekihaku.pref.hyogo.lg.jp/curator/20165/")
wazauta = scan("wazauta.txt", what = character())
n = nchar(wazauta)
unigram = character(n)
for( i in 1:n ){
unigram[i] = substr(wazauta, i, i)
@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)