Python 均值和协方差未知的PyMC模型层次回归 模型
我有以下统计模型:Python 均值和协方差未知的PyMC模型层次回归 模型,python,bayesian,pymc,Python,Bayesian,Pymc,我有以下统计模型: 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格式的版本: Python代码 我试图创建一个PyMC模型,该模型生成
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格式的版本:
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,).
回溯(最近一次呼叫最后一次):
文件“test_pymc.py”,第46行,在
phi=pm.MvNormal('phi',np.zero((ndims)),np.identity((ndims))*2)
TypeError:尺寸编号错误:应为0,得到1,形状为(40,)。
不知为什么,我的模型设置错误了吗?我认为您缺少MvNormal的形状参数。我认为MvNormal(…,shape=ndim)应该解决这个问题。我们或许应该想出一个更好的推断方法