Hi everyone,
I’m quite new using PyMC3 and currently trying to implement the following model of predictive election. However, I’m receiving this error from
pymc3.exceptions.SamplingError: Initial evaluation of model at starting point failed!
Do you have a tip on this? Below is the following code with the dataset.
# Explore and Sample the Parameter Space
import pandas as pd
import numpy as np
import pymc3 as pm
from pymc3.math import invlogit
import theano.tensor as tt
import xarray as xr
import arviz as az
PRIOR_N = 50
# results used as prior
b_pct = .25
l_pct = .35
# normalize the split because
b_pct_norm = b_pct / (b_pct + l_pct)
l_pct_norm = l_pct / (b_pct + l_pct)
b_pct_norm, l_pct_norm
alpha = int(l_pct_norm * PRIOR_N)
beta = PRIOR_N - alpha
alpha, beta
data = [['A', 243.945, 30.896],
['B', 381.126, 186.751],
['C', 596.776, 301.596],
['D', 880.126, 449.231],
['E', 477.420, 238.710],
['F', 425.333, 232.001],
['G', 418.260, 215.598]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['pollster', 'samplesize', 'num_votes'])
print(df)
with pm.Model() as model:
phi = pm.Beta('phi', alpha=alpha, beta=beta)
kappa_log = pm.HalfNormal('kappa_log', sigma=10)
kappa = pm.Deterministic('kappa', tt.exp(kappa_log))
thetas = pm.Beta(
'thetas',
alpha=phi*kappa,
beta=(1.0-phi)*kappa,
shape=len(df)
)
y = pm.Binomial(
'y',
n=df['samplesize'],
p=thetas,
observed=df['num_votes']
)
with model:
poll_samples = pm.sample(5000, tune=5000, cores=2, target_accept=0.98)