模型
我有以下统计模型:
r_i ~ N(r | mu_i, sigma)
mu_i = w . Q_i
w ~ N(w | phi, Sigma)
prior(phi, Sigma) = NormalInvWishart(0, 1, k+1, I_k)
哪里
sigma
是已知的。观察到
Q_i
和r_i
(奖励)。在这种情况下,
r_i
和mu_i
是标量,w
是40x1,Q_i
是1x40,phi
是40x1,Sigma
是40x40。LaTeX格式化的版本:http://mathurl.com/m2utrz4
Python代码
我正在尝试创建一个PyMC模型,该模型生成一些样本,然后近似
phi
和Sigma
。import pymc as pm
import numpy as np
SAMPLE_SIZE = 100
q_samples = ... # Q created elsewhere
reward_sigma = np.identity(SAMPLE_SIZE) * 0.1
phi_true = (np.random.rand(40)+1) * -2
sigma_true = np.random.rand(40, 40) * 2. - 1.
weights_true = np.random.multivariate_normal(phi_true, sigma_true)
reward_true = np.random.multivariate_normal(np.dot(q_samples,weights_true), reward_sigma)
with pm.Model() as model:
phi = pm.MvNormal('phi', np.zeros((ndims)), np.identity((ndims)) * 2)
sigma = pm.InverseWishart('sigma', ndims+1, np.identity(ndims))
weights = pm.MvNormal('weights', phi, sigma)
rewards = pm.Normal('rewards', np.dot(weights, q_samples), reward_sigma, observed=reward_true)
with model:
start = pm.find_MAP()
step = pm.NUTS()
trace = pm.sample(3000, step, start)
pm.traceplot(trace)
但是,当我运行该应用程序时,出现以下错误:
Traceback (most recent call last):
File "test_pymc.py", line 46, in <module>
phi = pm.MvNormal('phi', np.zeros((ndims)), np.identity((ndims)) * 2)
TypeError: Wrong number of dimensions: expected 0, got 1 with shape (40,).
我是否以某种方式设置了模型错误?
最佳答案
我认为您缺少MvNormal的shape参数。我认为MvNormal(...,shape = ndim)应该可以解决此问题。我们可能应该想出一种更好的推断方法。
关于python - 未知均值和协方差的PyMC建模层次回归,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19870637/