Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nile.stan file missing #73

Closed
Emaasit opened this issue Aug 14, 2016 · 5 comments
Closed

nile.stan file missing #73

Emaasit opened this issue Aug 14, 2016 · 5 comments

Comments

@Emaasit
Copy link

Emaasit commented Aug 14, 2016

@jrnold It appears the "nile.stan" file is missing from the repo "stan-dev/example-models/misc/dlm". Could you upload it or paste the stan code here so that I can replicate your results? thanks.

@jrnold
Copy link
Contributor

jrnold commented Aug 14, 2016

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.

@Emaasit
Copy link
Author

Emaasit commented Aug 14, 2016

@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.

@Emaasit
Copy link
Author

Emaasit commented Aug 19, 2016

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.

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Aug 19, 2016 via email

@bob-carpenter
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants