SlideShare a Scribd company logo
Dealing with latent
discrete parameters in
Stan
ITÔ, Hiroki
2016-06-04
Tokyo.Stan
Michael Betancourt’s Stan Lecture
About me
• An end user of statistical
software
• Researcher of forest
ecology
• Species composition of
forests
• Forest dynamics
Contents
1. Introduction
• Population Ecology
2. Examples
1. Capture-recapture data and data augmentation
2. Multistate model
Bayesian Population Analysis
using WinBUGS (BPA)
• My colleagues and I translated “Bayesian Population
Analysis using WinBUGS” by M. Kéry and M. Schaub into
Japanese.
• Many practical examples for population ecology
Stan translation of BPA models
https://github.com/stan-dev/example-models/tree/master/BPA
Population ecology
• A subfield of ecology
• Studies of population
• Changes in size (number of individuals) of
animals, plants and other organisms
• Estimation of population size, growth rate,
extinction probability, etc.
Population ecology
• Latent discrete parameters are often used.
• Unobserved status
• Present or absent
• Dead or alive
• But Stan does not support discrete parameters.
• Marginalizing out is required to deal with discrete
parameters.
Example 1
Capture-recapture data and data augmentation
(in Chapter 6 of BPA)
Capture-recapture data
Capture
Release
Recapture
🐞
①
🐞
②
🐞
③
🐞
④
🐞
⑤
🐞
⑥
🐞
⑦
🐞
⑧
🐞
⑨
🐞
①
🐞
③
🐞
④
🐞
⑧
🐞
⑩
🐞
⑪
🐞
⑫
🐞
⑬
Assume closed population: fixed size, no recruitment,
no death, no immigration nor emigration
Data
[,1] [,2] [,3]
[1,] 0 0 1
[2,] 1 1 1
[3,] 1 0 0
[4,] 1 0 1
[5,] 1 0 1
[6,] 1 0 1
:
[85,] 0 1 0
[86,] 0 1 1
[87,] 1 1 0
Individuals
Survey occasions
Estimation
• Population size (total number of individuals
including unobserved)
• Detection (capture) probability
Data augmentation
[,1] [,2] [,3]
[1,] 0 0 1
[2,] 1 1 1
[3,] 1 0 0
[4,] 1 0 1
:
[87,] 1 1 0
[88,] 0 0 0
:
[236,] 0 0 0
[237,] 0 0 0
Add 150
dummy records
Model
BUGS
model {
# Priors
omega ~ dunif(0, 1) # Inclusion probability
p ~ dunif(0, 1) # Detection probability
# Likelihood
for (i in 1:M){
z[i] ~ dbern(omega) # Inclusion indicators
for (j in 1:T) {
y[i, j] ~ dbern(p.eff[i, j])
p.eff[i, j] <- z[i] * p
}
}
# Derived quantities
N <- sum(z[])
}
Stan
data {
int<lower=0> M; // Size of augmented data set
int<lower=0> T; // Number of sampling occasions
int<lower=0,upper=1> y[M, T]; // Capture-history matrix
}
transformed data {
int<lower=0> s[M]; // Totals in each row
int<lower=0> C; // Size of observed data set
C <- 0;
for (i in 1:M) {
s[i] <- sum(y[i]);
if (s[i] > 0)
C <- C + 1;
}
}
parameters {
real<lower=0,upper=1> omega; // Inclusion probability
real<lower=0,upper=1> p; // Detection probability
}
model {
for (i in 1:M) {
real lp[2];
if (s[i] > 0) {
// Included
increment_log_prob(bernoulli_log(1, omega)
+ binomial_log(s[i], T, p));
} else {
// Included
lp[1] <- bernoulli_log(1, omega)
+ binomial_log(0, T, p);
// Not included
lp[2] <- bernoulli_log(0, omega);
increment_log_prob(log_sum_exp(lp));
}
}
}
generated quantities {
int<lower=C> N;
N <- C + binomial_rng(M, omega * pow(1 - p, T));
}
Results
Computing times
OpenBUGS Stan
Number of chains 3 4
Burn-in or warmup +
iterations / chain
500 + 1000 500 + 1000
Computing time (sec) 5.9 5.8
Effective sample size
of p
320 1867
Eff. sample size /
time (sec-1)
54.6 319.5
Environment: 2.8 GHz Xeon W3530, Ubuntu 14.04, No parallell computing.
Compilation time is not included in Stan.
Times were measured using system.time(). The values are mean of 3 measurements.
Data augmentation
Example 2
Multistate model
(in chapter 9 of BPA)
Multistate model
🐞
①
🐞
②
🐞
③
🐞
④
🐞
⑤
🐞⑥
🐞⑦
🐞⑧
🐞⑨
Site A
Site B
Capture
Release
Recapture
🐞①
🐞
②
🐞
⑤
🐞
⑥
🐞⑦
🐞⑫
🐞⑪
Site A
Site B
🐞
⑩
Data
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 3 3 2 3 1
[2,] 1 1 1 1 1 2
[3,] 1 1 1 1 3 3
[4,] 1 3 3 3 3 3
[5,] 1 3 3 3 3 3
[6,] 1 1 2 3 3 3
:
[798,] 3 3 3 3 2 3
[799,] 3 3 3 3 2 3
[800,] 3 3 3 3 2 3
Individuals
Survey occasion
Values
1: seen (captured) at site A, 2: seen (captured) at site B,
3: not seen (not captured)
Estimation
• Survival probability (at site A and B)
• Movement probability (from site A to B and B to A)
• Detection (capture) probability (at site A and B)
State transition
State at time t+1
Site A Site B Dead
Site A φA(1-ψAB) φAψAB 1-φA
State at
time t
Site B φBψBA φB(1-ψBA) 1-φB
Dead 0 0 1
φA: survival probability at site A, φB: survival probability at site B,
ψAB: movement probability from A to B,
ψBA: movement probability from B to A
Observation
Observation at time t
Site A Site B Not seen
Site A pA 0 1-pA
State at
time t
Site B 0 pB 1-pB
Dead 0 0 1
pA: detection probability at site A,
pB: detection probability at site B
Model
BUGS
model {
:
# Define probabilities of state S(t+1) given S(t)
ps[1, 1] <- phiA * (1 - psiAB)
ps[1, 2] <- phiA * psiAB
ps[1, 3] <- 1 - phiA
ps[2, 1] <- phiB * psiBA
ps[2, 2] <- phiB * (1 - psiBA)
ps[2, 3] <- 1 - phiB
ps[3, 1] <- 0
ps[3, 2] <- 0
ps[3, 3] <- 1
# Define probabilities of O(t) given S(t)
po[1, 1] <- pA
po[1, 2] <- 0
po[1, 3] <- 1 - pA
po[2, 1] <- 0
po[2, 2] <- pB
po[2, 3] <- 1 - pB
po[3, 1] <- 0
po[3, 2] <- 0
po[3, 3] <- 1
:
for (i in 1:nind) {
# Define latent state at first capture
z[i, f[i]] <- y[i, f[i]]
for (t in (f[i] + 1):n.occasions) {
# State process
z[i, t] ~ dcat(ps[z[i, t - 1], ])
# Observation process
y[i, t] ~ dcat(po[z[i, t], ])
}
}
f[]: array containing first capture occasion
ps[,]: state transition matrix
po[,]: observation matrix
Stan
Treating as a Hidden Markov model
Site B Site B Site A DeadSite A
Hidden Markov model
State
Observation
Seen at
site A
Dead
Not seen
Seen at
site B
Seen at
site A
Not seen Not seen
model {
real acc[3];
vector[3] gamma[n_occasions];
// See Stan Modeling Language User's Guide and Reference Manual
for (i in 1:nind) {
if (f[i] > 0) {
for (k in 1:3)
gamma[f[i], k] <- (k == y[i, f[i]]);
for (t in (f[i] + 1):n_occasions) {
for (k in 1:3) {
for (j in 1:3)
acc[j] <- gamma[t - 1, j] * ps[j, k]
* po[k, y[i, t]];
gamma[t, k] <- sum(acc);
}
}
increment_log_prob(log(sum(gamma[n_occasions])));
}
}
}
Results
Computing times
OpenBUGS Stan
Number of chains 3 4
Burn-in or warmup +
iterations / chain
500 + 2000 500 + 1000
Computing time (sec) 327.9 193.5
Effective sample size
of pA
140 649
Eff. sample size /
time (sec-1)
0.4 3.4
Environment: 2.8 GHz Xeon W3530, Ubuntu 14.04, No parallell computing.
Compilation time is not included in Stan.
Times were measured using system.time(). The values are mean of 3 measurements.
Binomial-mixture model
(in Chapter 12 of BPA)
Stan code
data {
int<lower=0> R; // Number of sites
int<lower=0> T; // Number of temporal replications
int<lower=0> y[R, T]; // Counts
int<lower=0> K; // Upper bounds of population size
}
model {
:
// Likelihood
for (i in 1:R) {
vector[K+1] lp;
for (n in max_y[i]:K) {
lp[n + 1] <- binomial_log(y[i], n, p)
+ poisson_log(n, lambda);
}
increment_log_prob(log_sum_exp(lp[(max_y[i] + 1):(K + 1)]));
}
:
}
Computing times
OpenBUGS
Stan
(K=100)
Number of chains 3 4
Burn-in or warmup +
iterations / chain
200 + 1000 500 + 1000
Computing time (sec) 25.4 1007.8
Effective sample size
of λ
3000 401
Eff. sample size /
time (sec-1)
118.3 0.4
Environment: 2.8 GHz Xeon W3530, Ubuntu 14.04, No parallell computing.
Compilation time is not included in Stan.
Times were measured using system.time(). The values are mean of 3 measurements.
Summary
• Stan can deal with models including discrete
parameters by marginalizing.
• Divide and sum up every cases
• However, the formulation used are less
straightforward.

More Related Content

PPT
Carcinoma oral cavity
PPTX
Management of carcinomas of urinary bladder
PPT
Carcinoma larynx recent trends in management
PPTX
Carcinoma nasopharynx
PPTX
Head and neck cancer
PPTX
Vestibular Schwannoma
PPTX
Management of Oral Cavity Cancers
PPT
Carcinoma Nasopharynx
Carcinoma oral cavity
Management of carcinomas of urinary bladder
Carcinoma larynx recent trends in management
Carcinoma nasopharynx
Head and neck cancer
Vestibular Schwannoma
Management of Oral Cavity Cancers
Carcinoma Nasopharynx

What's hot (20)

PDF
Management of Carcinoma Larynx
PPTX
Chemotherapy Induced Peripheral Neuropathy (CIPN): A Song of Ice and Fire
PPT
Carcinoma of unknown primary
PPTX
Wilms tumor and neuroblastoma
PPT
CARCINOMA OF UNKNOWN PRIMARY NECK dr mnr
PPTX
Managememt of Carcinoma Nasopharynx
DOCX
Ultimate Comprehensive ABSITE Study Guide1.docx
PPTX
7th to 8th AJCC Head and Neck
PPT
Locally Advanced Nsclc
PPTX
Management of ca larynx and hypopharynx
PPT
Brain Infections 1
PPTX
ca oropharynx
PPTX
Estabishment of Laboratory Workshop for Medical College Task Force members
PPTX
Management of ca cervix
PPTX
Management of medullary carcinoma of thyroid - based on latest NCCN and ATA g...
PPTX
Targeted cancer therapy
PPT
Management of nasopharyngeal cancer
PPTX
Chemoradiation for head and neck cancers
PPTX
Nasopharyngeal Carcinoma
PPTX
Extragonadal Germ Cells Tumors
Management of Carcinoma Larynx
Chemotherapy Induced Peripheral Neuropathy (CIPN): A Song of Ice and Fire
Carcinoma of unknown primary
Wilms tumor and neuroblastoma
CARCINOMA OF UNKNOWN PRIMARY NECK dr mnr
Managememt of Carcinoma Nasopharynx
Ultimate Comprehensive ABSITE Study Guide1.docx
7th to 8th AJCC Head and Neck
Locally Advanced Nsclc
Management of ca larynx and hypopharynx
Brain Infections 1
ca oropharynx
Estabishment of Laboratory Workshop for Medical College Task Force members
Management of ca cervix
Management of medullary carcinoma of thyroid - based on latest NCCN and ATA g...
Targeted cancer therapy
Management of nasopharyngeal cancer
Chemoradiation for head and neck cancers
Nasopharyngeal Carcinoma
Extragonadal Germ Cells Tumors
Ad

Viewers also liked (19)

PDF
Replica exchange MCMC
 
PDF
RStanとShinyStanによるベイズ統計モデリング入門
PDF
階層ベイズモデルで割安mobile PCを探す
 
PDF
Satoyama woodland management
PDF
MLPI Lecture 3: Advanced Sampling Techniques
PDF
データ解析で割安賃貸物件を探せ!(山手線沿線編) LT
 
PDF
分布から見た線形モデル・GLM・GLMM
 
PDF
100人のための統計解析 和食レストラン編
 
PDF
PCAの最終形態GPLVMの解説
PDF
順序データでもベイズモデリング
 
PDF
状態空間モデルの実行方法と実行環境の比較
PDF
変分推論法(変分ベイズ法)(PRML第10章)
PDF
StanとRでベイズ統計モデリング読書会 導入編(1章~3章)
PPTX
変分ベイズ法の説明
PDF
PyMCがあれば,ベイズ推定でもう泣いたりなんかしない
PDF
RとStanでクラウドセットアップ時間を分析してみたら #TokyoR
PDF
Stanコードの書き方 中級編
PDF
エクセルで統計分析 統計プログラムHADについて
PDF
Pythonによる機械学習入門 ~Deep Learningに挑戦~
Replica exchange MCMC
 
RStanとShinyStanによるベイズ統計モデリング入門
階層ベイズモデルで割安mobile PCを探す
 
Satoyama woodland management
MLPI Lecture 3: Advanced Sampling Techniques
データ解析で割安賃貸物件を探せ!(山手線沿線編) LT
 
分布から見た線形モデル・GLM・GLMM
 
100人のための統計解析 和食レストラン編
 
PCAの最終形態GPLVMの解説
順序データでもベイズモデリング
 
状態空間モデルの実行方法と実行環境の比較
変分推論法(変分ベイズ法)(PRML第10章)
StanとRでベイズ統計モデリング読書会 導入編(1章~3章)
変分ベイズ法の説明
PyMCがあれば,ベイズ推定でもう泣いたりなんかしない
RとStanでクラウドセットアップ時間を分析してみたら #TokyoR
Stanコードの書き方 中級編
エクセルで統計分析 統計プログラムHADについて
Pythonによる機械学習入門 ~Deep Learningに挑戦~
Ad

Similar to Dealing with latent discrete parameters in Stan (20)

PDF
Machine Learning With MapReduce, K-Means, MLE
PDF
Accelerated approximate Bayesian computation with applications to protein fol...
PDF
Bayesian Inference of deterministic population growth models -- Brazilian Mee...
PDF
variational bayes in biophysics
PDF
Final presentation2-----------------.pdf
PDF
Diffusion Schrödinger bridges for score-based generative modeling
PDF
Diffusion Schrödinger bridges for score-based generative modeling
PPTX
Error Statistics of Hidden Markov Model and Hidden Boltzmann Model Results
PPTX
Boosting probabilistic graphical model inference by incorporating prior knowl...
PDF
Complex models in ecology: challenges and solutions
ODP
Multispecies Distribution Models
PDF
Testing for mixtures at BNP 13
PDF
Jere Koskela slides
PDF
discrete-hmm
PDF
Inferring the number of components: dream or reality?
PDF
Hierarchichal species distributions model and Maxent
PDF
An Introduction to Hidden Markov Model
PDF
data-microscopes
PPTX
Viterbi algorithm
PDF
Talk by Blaise Piédallu at ISEC 2014 on improving abundance estimates by usin...
Machine Learning With MapReduce, K-Means, MLE
Accelerated approximate Bayesian computation with applications to protein fol...
Bayesian Inference of deterministic population growth models -- Brazilian Mee...
variational bayes in biophysics
Final presentation2-----------------.pdf
Diffusion Schrödinger bridges for score-based generative modeling
Diffusion Schrödinger bridges for score-based generative modeling
Error Statistics of Hidden Markov Model and Hidden Boltzmann Model Results
Boosting probabilistic graphical model inference by incorporating prior knowl...
Complex models in ecology: challenges and solutions
Multispecies Distribution Models
Testing for mixtures at BNP 13
Jere Koskela slides
discrete-hmm
Inferring the number of components: dream or reality?
Hierarchichal species distributions model and Maxent
An Introduction to Hidden Markov Model
data-microscopes
Viterbi algorithm
Talk by Blaise Piédallu at ISEC 2014 on improving abundance estimates by usin...

Recently uploaded (20)

PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPT
Quality review (1)_presentation of this 21
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
Database Infoormation System (DBIS).pptx
PDF
Fluorescence-microscope_Botany_detailed content
PPTX
Business Acumen Training GuidePresentation.pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
1_Introduction to advance data techniques.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Computer network topology notes for revision
PPTX
Introduction to Knowledge Engineering Part 1
PDF
Lecture1 pattern recognition............
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Quality review (1)_presentation of this 21
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Database Infoormation System (DBIS).pptx
Fluorescence-microscope_Botany_detailed content
Business Acumen Training GuidePresentation.pptx
Clinical guidelines as a resource for EBP(1).pdf
1_Introduction to advance data techniques.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
climate analysis of Dhaka ,Banglades.pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Computer network topology notes for revision
Introduction to Knowledge Engineering Part 1
Lecture1 pattern recognition............
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf

Dealing with latent discrete parameters in Stan

  • 1. Dealing with latent discrete parameters in Stan ITÔ, Hiroki 2016-06-04 Tokyo.Stan Michael Betancourt’s Stan Lecture
  • 2. About me • An end user of statistical software • Researcher of forest ecology • Species composition of forests • Forest dynamics
  • 3. Contents 1. Introduction • Population Ecology 2. Examples 1. Capture-recapture data and data augmentation 2. Multistate model
  • 4. Bayesian Population Analysis using WinBUGS (BPA) • My colleagues and I translated “Bayesian Population Analysis using WinBUGS” by M. Kéry and M. Schaub into Japanese. • Many practical examples for population ecology
  • 5. Stan translation of BPA models https://github.com/stan-dev/example-models/tree/master/BPA
  • 6. Population ecology • A subfield of ecology • Studies of population • Changes in size (number of individuals) of animals, plants and other organisms • Estimation of population size, growth rate, extinction probability, etc.
  • 7. Population ecology • Latent discrete parameters are often used. • Unobserved status • Present or absent • Dead or alive • But Stan does not support discrete parameters. • Marginalizing out is required to deal with discrete parameters.
  • 8. Example 1 Capture-recapture data and data augmentation (in Chapter 6 of BPA)
  • 10. Data [,1] [,2] [,3] [1,] 0 0 1 [2,] 1 1 1 [3,] 1 0 0 [4,] 1 0 1 [5,] 1 0 1 [6,] 1 0 1 : [85,] 0 1 0 [86,] 0 1 1 [87,] 1 1 0 Individuals Survey occasions
  • 11. Estimation • Population size (total number of individuals including unobserved) • Detection (capture) probability
  • 12. Data augmentation [,1] [,2] [,3] [1,] 0 0 1 [2,] 1 1 1 [3,] 1 0 0 [4,] 1 0 1 : [87,] 1 1 0 [88,] 0 0 0 : [236,] 0 0 0 [237,] 0 0 0 Add 150 dummy records
  • 13. Model
  • 14. BUGS model { # Priors omega ~ dunif(0, 1) # Inclusion probability p ~ dunif(0, 1) # Detection probability # Likelihood for (i in 1:M){ z[i] ~ dbern(omega) # Inclusion indicators for (j in 1:T) { y[i, j] ~ dbern(p.eff[i, j]) p.eff[i, j] <- z[i] * p } } # Derived quantities N <- sum(z[]) }
  • 15. Stan
  • 16. data { int<lower=0> M; // Size of augmented data set int<lower=0> T; // Number of sampling occasions int<lower=0,upper=1> y[M, T]; // Capture-history matrix } transformed data { int<lower=0> s[M]; // Totals in each row int<lower=0> C; // Size of observed data set C <- 0; for (i in 1:M) { s[i] <- sum(y[i]); if (s[i] > 0) C <- C + 1; } } parameters { real<lower=0,upper=1> omega; // Inclusion probability real<lower=0,upper=1> p; // Detection probability }
  • 17. model { for (i in 1:M) { real lp[2]; if (s[i] > 0) { // Included increment_log_prob(bernoulli_log(1, omega) + binomial_log(s[i], T, p)); } else { // Included lp[1] <- bernoulli_log(1, omega) + binomial_log(0, T, p); // Not included lp[2] <- bernoulli_log(0, omega); increment_log_prob(log_sum_exp(lp)); } } }
  • 18. generated quantities { int<lower=C> N; N <- C + binomial_rng(M, omega * pow(1 - p, T)); }
  • 20. Computing times OpenBUGS Stan Number of chains 3 4 Burn-in or warmup + iterations / chain 500 + 1000 500 + 1000 Computing time (sec) 5.9 5.8 Effective sample size of p 320 1867 Eff. sample size / time (sec-1) 54.6 319.5 Environment: 2.8 GHz Xeon W3530, Ubuntu 14.04, No parallell computing. Compilation time is not included in Stan. Times were measured using system.time(). The values are mean of 3 measurements.
  • 22. Example 2 Multistate model (in chapter 9 of BPA)
  • 23. Multistate model 🐞 ① 🐞 ② 🐞 ③ 🐞 ④ 🐞 ⑤ 🐞⑥ 🐞⑦ 🐞⑧ 🐞⑨ Site A Site B Capture Release Recapture 🐞① 🐞 ② 🐞 ⑤ 🐞 ⑥ 🐞⑦ 🐞⑫ 🐞⑪ Site A Site B 🐞 ⑩
  • 24. Data [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 3 2 3 1 [2,] 1 1 1 1 1 2 [3,] 1 1 1 1 3 3 [4,] 1 3 3 3 3 3 [5,] 1 3 3 3 3 3 [6,] 1 1 2 3 3 3 : [798,] 3 3 3 3 2 3 [799,] 3 3 3 3 2 3 [800,] 3 3 3 3 2 3 Individuals Survey occasion Values 1: seen (captured) at site A, 2: seen (captured) at site B, 3: not seen (not captured)
  • 25. Estimation • Survival probability (at site A and B) • Movement probability (from site A to B and B to A) • Detection (capture) probability (at site A and B)
  • 26. State transition State at time t+1 Site A Site B Dead Site A φA(1-ψAB) φAψAB 1-φA State at time t Site B φBψBA φB(1-ψBA) 1-φB Dead 0 0 1 φA: survival probability at site A, φB: survival probability at site B, ψAB: movement probability from A to B, ψBA: movement probability from B to A
  • 27. Observation Observation at time t Site A Site B Not seen Site A pA 0 1-pA State at time t Site B 0 pB 1-pB Dead 0 0 1 pA: detection probability at site A, pB: detection probability at site B
  • 28. Model
  • 29. BUGS
  • 30. model { : # Define probabilities of state S(t+1) given S(t) ps[1, 1] <- phiA * (1 - psiAB) ps[1, 2] <- phiA * psiAB ps[1, 3] <- 1 - phiA ps[2, 1] <- phiB * psiBA ps[2, 2] <- phiB * (1 - psiBA) ps[2, 3] <- 1 - phiB ps[3, 1] <- 0 ps[3, 2] <- 0 ps[3, 3] <- 1 # Define probabilities of O(t) given S(t) po[1, 1] <- pA po[1, 2] <- 0 po[1, 3] <- 1 - pA po[2, 1] <- 0 po[2, 2] <- pB po[2, 3] <- 1 - pB po[3, 1] <- 0 po[3, 2] <- 0 po[3, 3] <- 1 :
  • 31. for (i in 1:nind) { # Define latent state at first capture z[i, f[i]] <- y[i, f[i]] for (t in (f[i] + 1):n.occasions) { # State process z[i, t] ~ dcat(ps[z[i, t - 1], ]) # Observation process y[i, t] ~ dcat(po[z[i, t], ]) } } f[]: array containing first capture occasion ps[,]: state transition matrix po[,]: observation matrix
  • 32. Stan Treating as a Hidden Markov model
  • 33. Site B Site B Site A DeadSite A Hidden Markov model State Observation Seen at site A Dead Not seen Seen at site B Seen at site A Not seen Not seen
  • 34. model { real acc[3]; vector[3] gamma[n_occasions]; // See Stan Modeling Language User's Guide and Reference Manual for (i in 1:nind) { if (f[i] > 0) { for (k in 1:3) gamma[f[i], k] <- (k == y[i, f[i]]); for (t in (f[i] + 1):n_occasions) { for (k in 1:3) { for (j in 1:3) acc[j] <- gamma[t - 1, j] * ps[j, k] * po[k, y[i, t]]; gamma[t, k] <- sum(acc); } } increment_log_prob(log(sum(gamma[n_occasions]))); } } }
  • 36. Computing times OpenBUGS Stan Number of chains 3 4 Burn-in or warmup + iterations / chain 500 + 2000 500 + 1000 Computing time (sec) 327.9 193.5 Effective sample size of pA 140 649 Eff. sample size / time (sec-1) 0.4 3.4 Environment: 2.8 GHz Xeon W3530, Ubuntu 14.04, No parallell computing. Compilation time is not included in Stan. Times were measured using system.time(). The values are mean of 3 measurements.
  • 38. Stan code data { int<lower=0> R; // Number of sites int<lower=0> T; // Number of temporal replications int<lower=0> y[R, T]; // Counts int<lower=0> K; // Upper bounds of population size } model { : // Likelihood for (i in 1:R) { vector[K+1] lp; for (n in max_y[i]:K) { lp[n + 1] <- binomial_log(y[i], n, p) + poisson_log(n, lambda); } increment_log_prob(log_sum_exp(lp[(max_y[i] + 1):(K + 1)])); } : }
  • 39. Computing times OpenBUGS Stan (K=100) Number of chains 3 4 Burn-in or warmup + iterations / chain 200 + 1000 500 + 1000 Computing time (sec) 25.4 1007.8 Effective sample size of λ 3000 401 Eff. sample size / time (sec-1) 118.3 0.4 Environment: 2.8 GHz Xeon W3530, Ubuntu 14.04, No parallell computing. Compilation time is not included in Stan. Times were measured using system.time(). The values are mean of 3 measurements.
  • 40. Summary • Stan can deal with models including discrete parameters by marginalizing. • Divide and sum up every cases • However, the formulation used are less straightforward.