I’m trying to build the following model in PyMC3:
I have a separately defined Generate() function with some hyperparameters, to generate data according to the model shown.The X_1 and X_2 variables are observed vectors/matrices.
The following code generates a TypeError for wrong dimensions of “mu” for variable “C”. Why is it acceptable to pass a multi-dim array to “mu” of a likelihood distribution, but not for a latent variable? What if my system has a deterministic input vector that feeds into several layers of distributions and transformations before being observed as a likelihood?
from PGM_RandData import Generate
import pymc3 as pm
import numpy as np
import matplotlib.pyplot as plt
# Generate random data, size = number of points
size = int(40)
data = Generate(size)
test_model2 = pm.Model()
with test_model2:
### LEFT SIDE #
# Assign a prior for hyperparameters
mu_A = pm.Normal('muA', sd=50)
sig_A = pm.HalfNormal('sigA', sd=5)
sig_C = pm.HalfNormal('sigC', sd=5)
# Define conditional probabilities
A = pm.Normal('A', mu=mu_A, sd=sig_A)
mu_C = A * data.X_1**2
C = pm.Normal('C', mu=mu_C, sd=sig_C)
# Create likelihood
# A_obs = pm.Normal('A_obs', mu=mu_A, sd=sig_A, observed=np.array(data.A))
C_obs = pm.Normal('C_obs', mu=mu_C, sd=sig_C, observed=np.array(data.C))
### RIGHT SIDE #
# Priors
mu_B = pm.Normal('muB', sd=50)
sig_B = pm.HalfNormal('sigB',sd=5)
sig_D = pm.HalfNormal('sigD', sd=5)
# Conditional probabilities
B = pm.Normal('B', mu=mu_B, sd=sig_B)
mu_D = 1 / (1 + np.exp(-B * data.X_2))
# D = pm.Normal('D', mu=mu_D, sd=sig_D)
# Likelihood
# B_obs = pm.Normal('B_obs', mu=mu_B, sd=sig_B, observed=np.array(data.B))
D_obs = pm.Normal('D_obs', mu=mu_D, sd=sig_D, observed=np.array(data.D))
trace1 = pm.sampling.sample(draws=2000, cores=1)
# trace2 = pm.sampling.sample_prior_predictive(5000, var_names=["A", "B", "C", "D"])
pm.plots.traceplot(trace1)
# pm.plots.traceplot(trace2)
plt.show()
Error:
TypeError: For compute_test_value, one input test value does not have the requested type.
The error when converting the test value to that variable type:
Wrong number of dimensions: expected 0, got 1 with shape (40,).