Skip to content

Instantly share code, notes, and snippets.

@MatsuuraKentaro
MatsuuraKentaro / calculate-WAIC.R
Created Dec 17, 2021
Calculate WAIC using Simpson's rule and Monte Carlo integration (2D version)
View calculate-WAIC.R
library(cmdstanr)
waic <- function(log_likelihood) {
training_error <- - mean(log(colMeans(exp(log_likelihood))))
functional_variance_div_N <- mean(colMeans(log_likelihood^2) - colMeans(log_likelihood)^2)
waic <- training_error + functional_variance_div_N
return(waic)
}
model1_Si <- cmdstan_model('model1-addG-Simpson.stan')
@MatsuuraKentaro
MatsuuraKentaro / model1.stan
Last active Jul 11, 2020
COVID-19: estimate effective reproduction number
View model1.stan
functions {
// calculating the convolutions
vector convolution(vector x, vector y_rev) {
int T = num_elements(x);
vector[T-1] res;
for (t in 2:T) {
res[t-1] = dot_product(x[1:(t-1)], y_rev[(T-t+2):T]);
}
return res;
}
@MatsuuraKentaro
MatsuuraKentaro / DP.csv
Last active Apr 4, 2020
COVID-19: estimate total number of positive persons in Japan
View DP.csv
Age10 N Y
1 1 0
2 5 2
3 28 25
4 34 27
5 27 19
6 59 28
7 177 76
8 234 95
9 52 27
@MatsuuraKentaro
MatsuuraKentaro / data.csv
Last active Dec 9, 2018
statistical modeling with TensorFlow
View data.csv
Time Date Y
0 2012-03-01 28140
1 2012-03-08 28850
2 2012-03-15 34230
3 2012-03-22 29260
4 2012-03-29 29200
5 2012-04-05 33940
7 2012-04-19 24800
8 2012-04-26 26060
9 2012-05-03 26490
View dynoload.js
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML")
document.getElementsByTagName("head")[0].appendChild(fileref)
View calc-scale_best-example.R
library(ggjoy)
library(dplyr)
library(purrr)
# yutani data ------------------------------------------------------------
set.seed(10)
l <- rerun(26, rnorm(1000, mean = runif(1), sd = sqrt(runif(1))))
names(l) <- LETTERS
l[[2]] <- c(rnorm(500, mean = -1, sd = 0.3), rnorm(500, mean = 1, sd = 0.3))
d <- as.data.frame(l) %>% tidyr::gather(id, value)
@MatsuuraKentaro
MatsuuraKentaro / model.stan
Last active May 13, 2017
Aizu Online Judge
View model.stan
data {
int Q;
int N;
vector[Q] D;
int Solved[Q, N];
vector[N] D_max;
real D_range;
}
parameters {
@MatsuuraKentaro
MatsuuraKentaro / model.stan
Created Dec 27, 2016
Spot detection using Markov field model with UBN (Uniformly Boosted Normal) distribution
View model.stan
data {
int I;
int J;
matrix[I,J] Y;
real<lower=0> U;
}
parameters {
matrix<lower=-5, upper=5>[I,J] mu;
real<lower=0> s_mu;
@MatsuuraKentaro
MatsuuraKentaro / model1.stan
Last active Jun 26, 2017
Comparison of LOOCV and WAIC
View model1.stan
data {
int D;
int N;
matrix[N,D] X;
vector[N] Y;
}
parameters {
vector[D] b;
real<lower=0> sigma;
View model.stan
data {
int N;
int K;
int D;
vector[N] Y[K];
}
transformed data {
vector[N] Mu;
Mu = rep_vector(0, N);