问题描述
我想指定一个随机变量数组,其中索引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',10000)
tau=pm.Deterministic('tau',pm.math.sqrt(tausq))
h0=pm.normal('h0',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),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),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));
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)