Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / MCMC_exp.R
Created February 24, 2025 05:44
The random walk that converges to exponential distribution
library(animation)
make_initial <- function(np, S){
X = rep(S/np, np)
return(X)
}
randomwalk <- function(X){
np = length(X)
i = sample.int(np, 1)
@abikoushi
abikoushi / OsawaRyuTedukuri.R
Created February 22, 2025 01:45
simulate Boltzmann distribution
#参考文献:永井佑紀『1週間で学べる!Julia数値計算プログラミング』(講談社)
library(animation)
library(reshape2)
make_initial <- function(n_people,n_chip){
basket = integer(n_people)
for(i in 1:n_chip){
target = sample.int(n_people, 1)
basket[target] = basket[target] + 1
}
@abikoushi
abikoushi / Osaka_survey.R
Created February 19, 2025 13:35
pdftools example
library(pdftools)
library(tidyr)
library(dplyr)
library(ggplot2)
browseURL("https://www.jstage.jst.go.jp/article/jph/50/8/50_686/_article/-char/ja/")
text = pdf_text("~/Downloads/50_686.pdf")
tab2raw = unlist(strsplit(text[grep("表2", text)]," "))
tab2raw = tab2raw[sapply(tab2raw, nchar) > 0]
tab2raw = tab2raw[16:145]
@abikoushi
abikoushi / sim_exponential.R
Created February 7, 2025 13:37
Geometric and binomial distribution -> exponential and Poisson distribution
library(animation)
sim_pois_1d <- function(t_n, g_n, p, seed){
set.seed(seed)
counts <- integer(t_n)
x <- seq(0, 1, length.out=g_n)
delta_list <- vector("list", t_n)
hit <- matrix(0, t_n, g_n)
pud = 10
for(i in 1:t_n){
@abikoushi
abikoushi / OUprocess.R
Created January 25, 2025 05:09
Euler–Maruyama method
library(ggplot2)
library(dplyr)
#browseURL("https://en.wikipedia.org/wiki/Euler–Maruyama_method")
mu <- function(y, t, mod){
mod$THETA * (mod$MU - y)
}
sigma <- function(y, t, mod){
mod$SIGMA
@abikoushi
abikoushi / rowfilter_mtx.cpp
Created January 16, 2025 08:14
filtering rows in mtx file using Rcpp
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace Rcpp;
// [[Rcpp::export]]
@abikoushi
abikoushi / rowmeanvar_mtx.cpp
Created January 16, 2025 06:06
rowwise mean and variance from mtx file using Rcpp
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
#include <iostream>
#include <string>
#include <vector>
using namespace Rcpp;
// [[Rcpp::export]]
List rowmeanvar_mtx(const int & n_row, const int & n_col,
@abikoushi
abikoushi / radar_iris.r
Created January 15, 2025 04:54
radar chart using package "see"
require("tidyr")
require("dplyr")
require("ggplot2")
require("see")
dfiris <- mutate(iris, id=row_number()) %>%
pivot_longer(Sepal.Length:Petal.Width)
#head(dfiris)
@abikoushi
abikoushi / statfuns.R
Created December 30, 2024 13:34
An example of line chart with markers
library(ggplot2)
library(dplyr)
lr_default <- function(t, delay, forgetting){
(t+delay)^(-forgetting)
}
linedf <- expand.grid(delay=c(1,2), forgetting=c(0.6,0.9), t=seq(0,20, length.out=50)) %>%
mutate(lr=lr_default(t, delay, forgetting)) %>%
mutate(param = paste0("delay=",delay, ", ", "forgetting=", forgetting)) %>%
@abikoushi
abikoushi / bump.R
Created December 25, 2024 14:52
bump plot with jitter
library(ggplot2)
library(magrittr)
library(tidyr)
library(dplyr)
sigmoid2 <- function(x_from, x_to, y_from, y_to, smooth = 5, n = 100, direction = "x", location = 0, scale = 1) {
if(!direction %in% c("x", "y")) {stop("Only the directions x or y is allowed.")}
if(direction == "x") {
x <- seq(-smooth, smooth, length = n)