I'm brand new at programming in Stan, and trying to work through some code that I found on the web: https://modernstatisticalworkflow.blogspot.com/2017/11/bayesian-instrumental-variables-with.html
data {
int N;
int PX; // dimension of exogenous covariates
int PN; // dimension of endogenous covariates
int PZ; // dimension of instruments
matrix[N, PX] X_exog; // exogenous covariates
matrix[N, PN] X_endog; // engogenous covariates
matrix[N, PZ] Z; // instruments
vector[N] Y_outcome; // outcome variable
int<lower=0,upper=1> run_estimation; // simulate (0) or estimate (1)
}
transformed data {
matrix[N, 1 + PN] Y;
Y[,1] = Y_outcome;
Y[,2:] = X_endog;
}
parameters {
vector[PX + PN] gamma1;
matrix[PX + PZ, PN] gamma2;
vector[PN + 1] alpha;
vector<lower = 0>[1 + PN] scale;
cholesky_factor_corr[1 + PN] L_Omega;
}
transformed parameters {
matrix[N, 1 + PN] mu; // the conditional means of the process
mu[:,1] = rep_vector(alpha[1], N) + append_col(X_endog,X_exog)*gamma1;
mu[:,2:] = rep_matrix(alpha[2:]', N) + append_col(X_exog, Z)*gamma2;
}
This is the start to an instrumental variables model. In the "transformed parameters" section, I'm not exactly sure what the ":" is doing in the lines:
mu[:,1] = rep_vector(alpha[1], N) + append_col(X_endog,X_exog)*gamma1;
mu[:,2:] = rep_matrix(alpha[2:]', N) + append_col(X_exog, Z)*gamma2;
Does it tell Stan that this should iterate over the existing rows/columns?
In general, a colon indicates a sequence of consecutive integers (unless it is used as part of a ternary operator). Usually, you will see it with integers on both sides of the color, such as for (n in 1:N) {...}
. However, section 27.2 of the Stan User Manual describes subsetting arrays, vectors, matrices, etc. with "one-sided" integer sequences as
It is also possible to supply just a lower bound, or just an upper bound. Writing
c[3:]
is just shorthand forc[3:size(c)]
. Writingc[:5]
is just shorthand forc[1:5]
.
Also, the Stan User Manual describes subsetting with "zero-sided" integer sequences as
Finally, it is possible to write a range index that covers the entire range of an array, either by including just the range symbol (
:
) as the index or leaving the index position empty. In both cases,c[]
andc[:]
are equal toc[1:size(c)]
, which in turn is just equal toc
.
So, mu[:,2:] =
is equivalent to mu[ , 2:cols(mu)] =
and fills all rows for all but the first column with the (sub)matrix on the right-hand side of the assignment operator.