Stan 中的冒号是什么意思?
What does the colon mean in Stan?
我是 Stan 编程的新手,正在尝试处理我在网上找到的一些代码:
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;
}
这是工具变量模型的开始。在 "transformed parameters" 部分,我不太确定“:”在行中的作用:
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;
它是否告诉 Stan 这应该迭代现有的 rows/columns?
一般来说,冒号表示一系列连续的整数(除非它被用作三元运算符的一部分)。通常,你会看到它的颜色两边都有整数,比如for (n in 1:N) {...}
。但是,Stan 用户手册的第 27.2 节将 "one-sided" 整数序列的子集数组、向量、矩阵等描述为
It is also possible to supply just a lower bound, or just an upper bound. Writing c[3:]
is just shorthand for c[3:size(c)]
. Writing c[:5]
is just shorthand for c[1:5]
.
此外,Stan 用户手册将 "zero-sided" 整数序列的子集描述为
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[]
and c[:]
are equal to c[1:size(c)]
, which in turn is just equal to c
.
因此,mu[:,2:] =
等同于 mu[ , 2:cols(mu)] =
,并用赋值运算符右侧的(子)矩阵填充除第一列以外的所有行。
我是 Stan 编程的新手,正在尝试处理我在网上找到的一些代码: 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;
}
这是工具变量模型的开始。在 "transformed parameters" 部分,我不太确定“:”在行中的作用:
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;
它是否告诉 Stan 这应该迭代现有的 rows/columns?
一般来说,冒号表示一系列连续的整数(除非它被用作三元运算符的一部分)。通常,你会看到它的颜色两边都有整数,比如for (n in 1:N) {...}
。但是,Stan 用户手册的第 27.2 节将 "one-sided" 整数序列的子集数组、向量、矩阵等描述为
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]
.
此外,Stan 用户手册将 "zero-sided" 整数序列的子集描述为
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
.
因此,mu[:,2:] =
等同于 mu[ , 2:cols(mu)] =
,并用赋值运算符右侧的(子)矩阵填充除第一列以外的所有行。