1

モデル

次の統計モデルがあります。

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_ir_i(報酬)が観察されます。

この場合、r_imu_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,).

どういうわけかモデルを間違って設定していますか?

4

1 に答える 1

3

MvNormal の形状パラメーターが欠落していると思います。MvNormal(..., shape = ndim) で問題が解決すると思います。おそらく、それをより適切に推測する方法を考え出す必要があります。

于 2013-11-12T08:18:36.877 に答える