Skip to content

Instantly share code, notes, and snippets.

@MatsuuraKentaro
MatsuuraKentaro / model.stan
Last active March 5, 2024 06:08
Tweedie distribution in Stan
data {
int N;
int M;
real<lower=0> Y[N];
}
parameters {
real<lower=0> mu;
real<lower=0> phi;
real<lower=1, upper=2> theta;
@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;
@MatsuuraKentaro
MatsuuraKentaro / model.stan
Created November 24, 2023 09:21
『Pythonではじめる数理最適化』の7章「商品推薦のための興味のスコアリング」をStanで解く
data {
int I; // number of data
int R; // number of Rcen
int F; // number of Freq
array[I] int Rcen; // value of Rcen
array[I] int Freq; // value of Freq
array[I] int N;
array[I] int PV;
}
@MatsuuraKentaro
MatsuuraKentaro / model1-add1-speedup.stan
Last active August 16, 2022 09:46
WAIC with hierarchical models
functions {
real f(int Y, real[] theta, real x) {
return(gamma_lpdf(x | theta[2], theta[2]/theta[1]) + poisson_lpmf(Y | x));
}
real log_lik_Simpson(int Y, real[] theta, real a, real b, int M) {
vector[M+1] lp;
real h;
h = (b-a)/M;
lp[1] = f(Y, theta, a);
@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 / 0-replica-exchange-mcmc.R
Last active December 17, 2020 02:21
replica exchange MCMC
library(rstan)
library(doParallel)
replica.exchange.mcmc <- function (inv_T, n_ex, stanmodel, data, par_list, init, iter, warmup) {
n_rep <- length(inv_T)
len <- iter - warmup
n_param <- sum(unlist(lapply(par_list, prod))) + 2 # number of parameters included E and lp__
ms_T1 <- matrix(0, len*n_ex, n_param) # MCMC samples at inv_T=1
idx_tbl <- matrix(0, n_ex, n_rep) # index table of (exchange time, replica)
E_tbl <- matrix(0, n_ex, n_rep) # E table along idx_tbl
@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 / 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 / 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 / model.stan
Last active October 21, 2017 02:29
correction of age heaping
data {
int A;
int Y[A];
int J;
int From[J];
int To[J];
}
parameters {
simplex[A] q;