Comments (5)
I don't remember actually adding this to the repo; to the extent I'm working on DLM stuff I'm working on a complete replacement. So I'd probably delete these examples before going back and figuring out how I did it.
from example-models.
@jrnold Do you have the current stan models for those two examples (nile & fx rates) somewhere else so I can follow along? I am trying to write dlm code for my work but there are limited examples on the internet.
from example-models.
I can't seem to find any examples for writing a generated quantities block for a Gaussian Dynamic Linear Model. I wrote the one shown below but stan produces an error. Any help will be much appreciated. @jrnold
stan_model1 <- "
// Kalman Filter (Multivariate Form)
// - no missing values
data {
int n_rows; // the number of rows/variables
int n_cols; // the number of columns/observations
matrix[n_rows, n_cols] y; // the observation matrix
vector[n_rows] mu_theta_init; // the mean at the initial state
cov_matrix[n_rows] sigma_theta_init; // covariance at the initial state
// holdout dataset
int n_rows_h;
int n_cols_h;
matrix[n_rows_h, n_cols_h] y_h;
vector[n_rows_h] mu_theta_init_h;
cov_matrix[n_rows_h] sigma_theta_init_h;
}
transformed data {
vector[n_rows] ones;
matrix[n_rows, n_rows] trans_matrix; // transition matrix
matrix[n_rows, n_rows] design_matrix; // design matrix
for (i in 1:n_rows) {
ones[i] <- 1.0;
}
trans_matrix <- diag_matrix(ones);
design_matrix <- trans_matrix;
}
parameters {
real<lower=-1.0, upper=1.0> rho;
vector<lower=0.0>[n_rows] sigma;
vector<lower=0.0>[n_rows] W_diag;
}
transformed parameters {
cov_matrix[n_rows] sigma_obs; //diagonal of observation covariance matrix of the y observations
cov_matrix[n_rows] sigma_theta; // state(system) covariance matrix of the states
sigma_theta <- diag_matrix(W_diag);
for (i in 1:n_rows) {
sigma_obs[i, i] <- pow(sigma[i], 2);
for (j in 1:(i - 1)) {
sigma_obs[i, j] <- sigma[i] * sigma[j] * rho;
sigma_obs[j, i] <- sigma_obs[i, j];
}
}
}
model {
y ~ gaussian_dlm_obs(design_matrix, trans_matrix, sigma_obs, sigma_theta, mu_theta_init, sigma_theta_init); // likelihood for gaussian dlm
}
generated quantities {
matrix[n_rows_h, n_cols_h] log_lik_h;
log_lik_h <- gaussian_dlm_obs_lpdf(y_h, design_matrix, trans_matrix, sigma_obs, sigma_theta, mu_theta_init_h, sigma_theta_init_h);
}
"
HERE IS THE ERROR MESSAGE:
ERROR at line 52
50: matrix[n_rows_h, n_cols_h] log_lik_h;
51:
52: log_lik_h <- gaussian_dlm_obs_lpdf(y_h, design_matrix, trans_matrix, sigma_obs, sigma_theta, mu_theta_init_h, sigma_theta_init_h);
^
53: }
PARSER EXPECTED:
Error in stanc(file = file, model_code = model_code, model_name = model_name, : failed to parse Stan model 'traffic-prediction1' due to the above error.
from example-models.
from example-models.
The answer is that you need to use "|" in place of the first comma in an lpdf. I'm going to change that error message for the next release as per stan-dev/stan#1940
I'm closing this issue as it looks like @jrnold has an alternative fix in mind. Re-open if I'm closing in error.
from example-models.
Related Issues (20)
- `stan_demo` example 500 not working HOT 4
- Missing function in Introduction
- Error in BPA Ch.10 js_super.stan with possible fix HOT 1
- rats ref link at top of stan files is broken
- Add .data.R files for knitr models HOT 1
- Rdump format
- Reorganize the stan example repository HOT 13
- sampling() fails in Lotka-Volterra example HOT 3
- Lotka-Volterra example not running HOT 1
- BUGS Background Information for Litter Example HOT 1
- Failed to build Lotka Vollterra on Mac OS / clang HOT 9
- reduce_sum case study will be out of date once new cmdstanr pull gets merged
- Example script `fit_nyc_bym2.R` to use model `bym2_offset_only.stan` doesn't work HOT 1
- Subtle error in some capture-recapture likelihoods HOT 3
- [Error] Validate transformed params
- Survey.stan example from Cognitive Modeling - Expected end of file after end of generated quantities block. HOT 9
- Which book/chapter HOT 1
- updates to ARM model for normal_id_glm - variable name "cov" should be "x_as_mat" or something like that HOT 6
- add Python scripts to BYM12 case study
- Parameter names for stan file HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from example-models.