Pergunta

Model

I have the following statistical model:

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)

Where sigma is known.

Q_i and r_i (reward) are observed.

In this case, r_i and mu_i are scalars, w is 40x1, Q_i is 1x40, phi is 40x1, and Sigma is 40x40.

LaTeX formatted version: http://mathurl.com/m2utrz4

Python code

I'm trying to create a PyMC model that generates some samples and then approximates phi and 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)

However, when I run the app, I get the following error:

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,).

Am I setting up my model wrong somehow?

Foi útil?

Solução

I think you're missing the shape parameter for MvNormal. I think MvNormal(..., shape = ndim) should fix the issue. We should probably figure out a way to infer that better.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top