Skip to content

Instantly share code, notes, and snippets.

@MatsuuraKentaro
MatsuuraKentaro / model1.stan
Last active June 26, 2017 03:35
Comparison of LOOCV and WAIC
data {
int D;
int N;
matrix[N,D] X;
vector[N] Y;
}
parameters {
vector[D] b;
real<lower=0> sigma;
@MatsuuraKentaro
MatsuuraKentaro / model.stan
Created December 27, 2016 06:08
Spot detection using Markov field model with UBN (Uniformly Boosted Normal) distribution
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 / model.stan
Last active May 13, 2017 22:48
Aizu Online Judge
data {
int Q;
int N;
vector[Q] D;
int Solved[Q, N];
vector[N] D_max;
real D_range;
}
parameters {
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)
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)
@MatsuuraKentaro
MatsuuraKentaro / data.csv
Last active December 9, 2018 08:59
statistical modeling with TensorFlow
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
@MatsuuraKentaro
MatsuuraKentaro / DP.csv
Last active April 4, 2020 22:50
COVID-19: estimate total number of positive persons in Japan
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 / model1.stan
Last active July 11, 2020 18:36
COVID-19: estimate effective reproduction number
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 / calculate-WAIC.R
Created December 17, 2021 13:33
Calculate WAIC using Simpson's rule and Monte Carlo integration (2D version)
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 / iterfuns.hpp
Last active November 30, 2023 09:23
Test of Bayesian IPW implemented by A. Jordan Nafa
#include <ostream>
static int iteration_index = 1;
inline void add_iter(std::ostream* pstream__) {
iteration_index += 1;
}
inline int get_iter(std::ostream* pstream__) {
return iteration_index;