斯坦语中的冒号是什么意思?

时间:2018-09-16 12:31:28

标签: stan rstan

我是斯坦(Stan)编程的新手,正在尝试研究我在网上找到的一些代码: https://modernstatisticalworkflow.blogspot.com/2017/11/bayesian-instrumental-variables-with.html

LocalFree

这是工具变量模型的开始。在“转换后的参数”部分,我不确定行中“:”的作用:

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;

}

它是否告诉Stan,这应该遍历现有的行/列?

1 个答案:

答案 0 :(得分:2)

通常,冒号表示连续整数的序列(除非它用作三元运算符的一部分)。通常,您会在颜色的两边看到整数,例如for (n in 1:N) {...}。但是,《 Stan用户手册》第27.2节描述了以“单边”整数序列为子集的数组,向量,矩阵等,

  

也可以仅提供一个下限或一个上限。编写c[3:]只是c[3:size(c)]的简写。编写c[:5]只是c[1:5]的简写。

此外,Stan用户手册将带有“零边”整数序列的子集描述为

  

最后,可以通过只包含范围符号(:作为索引,或将索引位置保留为空来编写覆盖整个数组范围的范围索引。在这两种情况下,c[]c[:]等于c[1:size(c)],而后者又等于c

因此,mu[:,2:] =等效于mu[ , 2:cols(mu)] =,并用赋值运算符右侧的(sub)矩阵填充除第一列以外的所有行。

相关问题