【问题标题】:What does the colon mean in Stan?斯坦的冒号是什么意思?
【发布时间】:2018-09-16 12:31:27
【问题描述】:

我是 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;

}

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

 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 这应该遍历现有的行/列?

【问题讨论】:

    标签: stan rstan


    【解决方案1】:

    一般来说,冒号表示一个连续整数的序列(除非它被用作三元运算符的一部分)。通常,您会看到它的颜色两侧带有整数,例如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)] =,并使用赋值运算符右侧的(子)矩阵填充除第一列之外的所有行。

    【讨论】:

      猜你喜欢
      • 2012-10-09
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 2020-05-21
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      相关资源
      最近更新 更多