我想指定一个随机变量数组,其中索引I处的随机变量有一个sigma,它是随机变量I-1的函数,对于I=2,3,4。。。N.我不知道如何在pymc3中做到这一点。以下是我的尝试:
import pymc3 as pm
with pm.Model() as model:
mu=pm.Normal('mu',-10,5)
phi=pm.Beta('phi',1,1)
tausq=pm.InverseGamma('tausq', 0,10000)
tau=pm.Deterministic('tau', pm.math.sqrt(tausq))
h0=pm.Normal('h0', 0,10000)
h=pm.Normal('h',mu+phi*(h0-mu),tau, shape=len(returns))
for i in range(2,len(returns)):
h[i]=pm.Normal('h'+str(i),mu+phi*(h[i-1]-mu), tau)
for i in range(1,len(returns)):
r[i]=pm.Normal('r'+str(i),0, pm.math.exp(h[i]/2), observed=returns[i])
trace_mdl=pm.sample(draws=2000,tune=2000,cores=10,init='adapt_diag')
此操作失败并出现错误:
Traceback (most recent call last):
File "<ipython-input-7-011e4fcf40bd>", line 11, in <module>
h[i]=pm.Normal('h'+str(i),mu+phi*(h[i-1]-mu), tau)
TypeError: 'FreeRV' object does not support item assignment
我有点理解Pymc3有我在这里使用的模型规范组件,也许我应该使用FreeRV类,但这是我所能做到的。FWIW,这是原始文件在rstan中所说的:
parameters {
real mu;
real<lower=0,upper=1> phi;
real<lower=0.01> tausq;
real h0;
vector[T] h;
}
transformed parameters {
real<lower=0> tau;
tau <- sqrt(tausq);
}
model {
mu ~ normal(-10,5);
phi ~ beta(1, 1);
tausq ~ inv_gamma(2.5, 0.025);
h0 ~ normal(0, 10000);
h[1] ~ normal(mu + phi*(h0-mu), tau);
for (t in 2:T)
h[t] ~ normal(mu + phi*(h[t-1]-mu), tau);
for (t in 1:T)
y[t] ~ normal(0,exp(h[t]/2));
}
目前没有回答
相关问题 更多 >
编程相关推荐