I’m fitting a reinforcement learning model to human behavior and would like to estimate marginal likelihoods and then Bayes factors for competing model. I’m able to compute the posterior distributions of model parameters using:
with pm.Model() as mod:
alpha1 = pm.Beta('alpha1', 1.1, 1.1)
alpha2 = pm.Beta('alpha2', 1.1, 1.1)
Lambda = pm.Beta('Lambda', 1.1, 1.1)
w = pm.Beta('w', 1.1, 1.1)
beta1 = pm.Gamma('beta1', 1.2, 1/5)
beta2 = pm.Gamma('beta2', 1.2, 1/5)
params = [alpha1, alpha2, Lambda, w, beta1, beta2]
like = pm.Potential('like', likelihood(params, stateActions1, stateActions2, rewards))
tr = pm.sample(return_inferencedata=True)
But I get this error when trying to use arviz:
az.loo(tr, mod)
*** TypeError: log likelihood not found in inference data object
It has been noted elsewhere (see https://discourse.pymc.io/t/log-likelihood-not-found-in-inferencedata/8169) that returning the arviz InferenceData object is not possible when using a blackbox likelihood function with pm.Potential, because the conversion requires an “observed=” argument. So I was able to replace the like=pm.Potential… line of code with this:
prob_action = likelihood(params, stateActions1, stateActions2, rewards)
nr_actions = prob_action.shape[0]
stateActions1_binary = np.zeros((len(stateActions1),max(stateActions1)+1))
stateActions2_binary = np.zeros((len(stateActions2),max(stateActions2)+1))
stateActions1_binary[np.arange(len(stateActions1)),stateActions1] = 1
stateActions2_binary[np.arange(len(stateActions2)),stateActions2] = 1
like = pm.Multinomial('like', n=nr_actions, p=prob_action, observed=np.concatenate([stateActions1_binary[1:,:], stateActions2_binary[1:,:]]))
However, this leads to the following error:
pymc3.exceptions.SamplingError: Initial evaluation of model at starting point failed!
Starting values:
{‘alpha1_logodds__’: array(0.), ‘alpha2_logodds__’: array(0.), ‘Lambda_logodds__’: array(0.), ‘beta1_log__’: array(1.79175947), ‘beta2_log__’: array(1.79175947)}Initial evaluation results:
alpha1_logodds__ -1.33
alpha2_logodds__ -1.33
Lambda_logodds__ -1.33
beta1_log__ -0.90
beta2_log__ -0.90
like -inf
Modifying the start values with the sample argument start={‘alpha1_logodds__’: np.array(0.5)…. does not improve the result, and I don’t know why my likelihood function would be producing -inf values.