Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / gr_japan.R
Created November 3, 2026 02:29
国債の金利と名目GDP成長率のグラフ
library(readr)
library(ggplot2)
library(dplyr)
#data are available in:
#https://www.esri.cao.go.jp/jp/sna/menu.html
#https://japanyieldcurve.com/data/
gdp <- read_csv("~/Downloads/ritu-mfy2622.csv", skip = 6)
gdp <- mutate(gdp, year = as.integer(gsub("/4-3\\.","",`Fiscal Year`))) |>
rename(g = ...2)
@abikoushi
abikoushi / Domar_eq_desc.R
Created November 2, 2026 12:45
飯田泰之『財政・金融政策の転換点』(中公新書)図3-1の再現(離散時間)
debt <- function(t, r, g, d, b0){
b <- numeric(t)
b[1] <- b0
A <- (1+r)/(1+g)
for(i in 1:t){
b[i+1] <- A*b[i] + d
}
return(b)
}
@abikoushi
abikoushi / Domar_eq.R
Last active November 2, 2026 12:46
飯田泰之『財政・金融政策の転換点』(中公新書)図3-1の再現(連続時間)
library(deSolve)
Domer_eq <- function(Time, State, Pars) {
with(as.list(c(State, Pars)), {
db <- (r-g)*b + d
return(list(c(db)))
})
}
pars <- c(r=0.01, g=0.02, d=0.02)
@abikoushi
abikoushi / taylor_log1p.R
Created October 31, 2026 05:59
animation for Taylor's expansion of log1p function
poly_n_log1p <- function(x,n){
x+sapply(x, function(xv){sum(((-1)^(1:(n-1))*(xv^(2:n)))/(2:n))})
}
saveGIF({
for(i in 2:50){
curve(log1p(x), 0, 1, main = paste0("n=",i))
curve(poly_n_log1p(x,i), add=TRUE, col="orangered", lty=2)
}
}, movie.name = "taylor_log1p.gif", interval=0.2)
@abikoushi
abikoushi / taylor_exp.R
Created October 18, 2026 04:37
animation for Taylor's expansion of exp function
library(animation)
poly_n <- function(x,n){
1+sapply(x, function(xv){sum((xv^(1:n))/factorial(1:n))})
}
saveGIF({
for(i in 2:20){
curve(exp(x), 0, 10, main = paste0("n=",i))
curve(poly_n(x,i), add=TRUE, col="orangered", lty=2)
@abikoushi
abikoushi / digamma.R
Created September 24, 2026 14:31
Test Rcpp digamma function
library(Rcpp)
sourceCpp(code='
#include <Rcpp.h>
// [[Rcpp::export]]
int digamma_cpp(const double x) {
return R::digamma(x);
}'
)
print(digamma(1e-16))
@abikoushi
abikoushi / thinning.R
Created September 7, 2026 04:36
Simulate Nonhomogeneous Poisson process using thinning
library(dplyr)
library(ggplot2)
library(patchwork)
get_NHPP <-function(Tmax, lambda, ...,
lambda2=NULL, maxit=10000){
if(is.null(lambda2)){
opt <- optimise(lambda, lower = 0, upper = Tmax, maximum = TRUE,
...)
lambda2 <- opt$objective
@abikoushi
abikoushi / toshinosa.R
Last active September 3, 2026 13:25
夫婦の歳の差(estatapiのデモ)
library(estatapi)
library(tidyverse)
myappId <- scan("appId.txt", what = character()) #ここには自分のアプリケーションIDを入れる
# list1 <- estat_getStatsList(appId = myappId, searchWord = "人口動態調査")
# view(list1)
dat1 <-estat_getStatsData(appId = myappId, statsDataId = "0003411856")
dat_filt <- dplyr::filter(dat1, `夫の初婚・再婚`=="夫_総数", `妻の初婚・再婚`=="妻_総数") |>
@abikoushi
abikoushi / sir_ctmc.R
Last active September 7, 2026 06:13
SIR model as continuous-time Markov chain
library(ggplot2)
library(tidyr)
library(dplyr)
library(deSolve)
sir_ctmc <- function(S0, I0, R0,
beta, gamma,
t_max = 100) {
# 初期状態
@abikoushi
abikoushi / CTMC_SIR.R
Created September 1, 2026 07:49
SIR model as continuous-time Markov chain with reward
library(ggplot2)
library(tidyr)
library(dplyr)
library(deSolve)
sir_ctmc <- function(S0, I0, R0,
beta, gamma,
t_max = 100) {
# 初期状態