斯坦。使用目标 += 语法
Stan. Using target += syntax
我开始学习 Stan。
谁能解释一下何时以及如何使用诸如...的语法?
target +=
而不仅仅是:
y ~ normal(mu, sigma)
例如,在 Stan 手册中,您可以找到以下示例。
model {
real ps[K]; // temp for log component densities
sigma ~ cauchy(0, 2.5);
mu ~ normal(0, 10);
for (n in 1:N) {
for (k in 1:K) {
ps[k] = log(theta[k])
+ normal_lpdf(y[n] | mu[k], sigma[k]);
}
target += log_sum_exp(ps);
}
}
我认为目标线增加了目标值,我认为是后验密度的对数。
但是什么参数的后验密度?
什么时候更新和初始化?
Stan 完成(并收敛)后,你如何访问它的值以及我如何使用它?
其他示例:
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
vector[J] theta;
theta = mu + tau * eta;
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
上面的例子使用了 target 两次而不是一次。
另一个例子。
data {
int<lower=0> N;
vector[N] y;
}
parameters {
real mu;
real<lower=0> sigma_sq;
vector<lower=-0.5, upper=0.5>[N] y_err;
}
transformed parameters {
real<lower=0> sigma;
vector[N] z;
sigma = sqrt(sigma_sq);
z = y + y_err;
}
model {
target += -2 * log(sigma);
z ~ normal(mu, sigma);
}
最后一个例子甚至混合了这两种方法。
要做到更难,我读过
y ~ normal(0,1);
与
效果相同
increment_log_prob(normal_log(y,0,1));
谁能解释一下为什么?
谁能提供一个用两种不同方式编写的简单示例,使用 "target +=" 和更简单的常规 "y ~" 方式,好吗?
此致
语法
target += u;
将 u 添加到目标对数密度。
目标密度是采样器从中采样的密度,它需要等于给定数据直至常数的所有参数的联合密度(这通常通过贝叶斯规则通过编码作为联合来实现参数和建模数据的密度达到一个常数)。您在后面以 lp__ 的形式访问它,但要小心,因为它还包含由约束产生的雅可比行列式并在采样语句中删除常量——您不想将它用于模型比较。
从抽样的角度,写作
target += normal_lpdf(y | mu, sigma);
与
效果相同
y ~ normal(mu, sigma);
_lpdf 表示它是法线的对数概率密度函数,这隐含在采样符号中。对于目标 += 语法,采样符号只是 shorthand,此外,在对数密度中删除常数项。
它在语言参考(手册的第二部分)的语句部分进行了解释,并通过程序员指南(手册的第一部分)在多个示例中使用。
刚开始学习Stan和贝叶斯统计,主要靠John Kruschke的书"Doing Bayesian Data Analysis"。在这里,在第 14.3.3 章中,他解释说:
Thus, the essence of computation in Stan is dealing with the logarithm
of the posterior probability density and its gradient; there is no
direct random sampling of parameters from distributions.
结果(仍然改写 Kruschke),
model [...] like y ∼ normal(mu,sigma)
[actuallly] means to multiply the current posterior probability by the density of the normal distribution at the datum value y.
按照对数计算规则,这个乘法等于给定数据y
的对数概率密度加上当前对数概率。 (log(a*b) = log(a) + log(b)
,因此乘法和求和相等)。
我承认我没有完全理解它的含义,但我认为它指出了正确的方向,从数学上讲,targer +=
所做的事情。
我开始学习 Stan。
谁能解释一下何时以及如何使用诸如...的语法?
target +=
而不仅仅是:
y ~ normal(mu, sigma)
例如,在 Stan 手册中,您可以找到以下示例。
model {
real ps[K]; // temp for log component densities
sigma ~ cauchy(0, 2.5);
mu ~ normal(0, 10);
for (n in 1:N) {
for (k in 1:K) {
ps[k] = log(theta[k])
+ normal_lpdf(y[n] | mu[k], sigma[k]);
}
target += log_sum_exp(ps);
}
}
我认为目标线增加了目标值,我认为是后验密度的对数。
但是什么参数的后验密度?
什么时候更新和初始化?
Stan 完成(并收敛)后,你如何访问它的值以及我如何使用它?
其他示例:
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
vector[J] theta;
theta = mu + tau * eta;
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
上面的例子使用了 target 两次而不是一次。
另一个例子。
data {
int<lower=0> N;
vector[N] y;
}
parameters {
real mu;
real<lower=0> sigma_sq;
vector<lower=-0.5, upper=0.5>[N] y_err;
}
transformed parameters {
real<lower=0> sigma;
vector[N] z;
sigma = sqrt(sigma_sq);
z = y + y_err;
}
model {
target += -2 * log(sigma);
z ~ normal(mu, sigma);
}
最后一个例子甚至混合了这两种方法。
要做到更难,我读过
y ~ normal(0,1);
与
效果相同increment_log_prob(normal_log(y,0,1));
谁能解释一下为什么?
谁能提供一个用两种不同方式编写的简单示例,使用 "target +=" 和更简单的常规 "y ~" 方式,好吗?
此致
语法
target += u;
将 u 添加到目标对数密度。
目标密度是采样器从中采样的密度,它需要等于给定数据直至常数的所有参数的联合密度(这通常通过贝叶斯规则通过编码作为联合来实现参数和建模数据的密度达到一个常数)。您在后面以 lp__ 的形式访问它,但要小心,因为它还包含由约束产生的雅可比行列式并在采样语句中删除常量——您不想将它用于模型比较。
从抽样的角度,写作
target += normal_lpdf(y | mu, sigma);
与
效果相同y ~ normal(mu, sigma);
_lpdf 表示它是法线的对数概率密度函数,这隐含在采样符号中。对于目标 += 语法,采样符号只是 shorthand,此外,在对数密度中删除常数项。
它在语言参考(手册的第二部分)的语句部分进行了解释,并通过程序员指南(手册的第一部分)在多个示例中使用。
刚开始学习Stan和贝叶斯统计,主要靠John Kruschke的书"Doing Bayesian Data Analysis"。在这里,在第 14.3.3 章中,他解释说:
Thus, the essence of computation in Stan is dealing with the logarithm of the posterior probability density and its gradient; there is no direct random sampling of parameters from distributions.
结果(仍然改写 Kruschke),
model [...] like
y ∼ normal(mu,sigma)
[actuallly] means to multiply the current posterior probability by the density of the normal distribution at the datum value y.
按照对数计算规则,这个乘法等于给定数据y
的对数概率密度加上当前对数概率。 (log(a*b) = log(a) + log(b)
,因此乘法和求和相等)。
我承认我没有完全理解它的含义,但我认为它指出了正确的方向,从数学上讲,targer +=
所做的事情。