Log likelihood not found in inference data object

With this model:

import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pytensor.tensor as pt
from scipy.special import expit as logistic
import pymc as pm
 
def custom_sigmoid(x):

    return pm.math.exp(x) / (1 + pm.math.exp(x))
 
def create_model(data_matrix, num_nodes, num_blocks, a_alpha, b_alpha, a_tau, b_tau, mu_zeta, sigma_zeta):
 
    data_matrix = data_matrix.reshape(num_nodes * num_nodes).T
    mask = np.triu(np.ones((num_nodes, num_nodes)), k=1).astype(bool)
    data_vector = data_matrix.reshape(num_nodes, num_nodes)[mask]
  
 
    with pm.Model() as SBMmodel:
    
        alpha = pm.Gamma("alpha", alpha=a_alpha, beta=b_alpha)       
        omega = pm.Dirichlet("omega", a=np.ones(num_blocks) * (alpha / num_blocks), shape=num_blocks)          
        E_row = pm.Categorical('E_vector', p=omega, shape=num_nodes)           
        tau2 = pm.InverseGamma("tau2", alpha=a_tau, beta=b_tau)
        zeta_raw = pm.Normal("zetafull", mu=mu_zeta, sigma=np.sqrt(sigma_zeta))
        zeta = pm.Deterministic("zeta", custom_sigmoid(zeta_raw))
        
        Theta_kl_raw = pm.Normal('Theta_klfull', mu=zeta, sigma=np.sqrt(tau2),
                                shape=(num_blocks * (num_blocks + 1)) // 2)
        Theta_kl = pm.Deterministic("Theta_kl", custom_sigmoid(Theta_kl_raw))
        theta_matrix_np = np.zeros((num_blocks, num_blocks))
        Theta_matrix = pm.Data("theta_matrix", theta_matrix_np)
        index = 0
        for i in range(num_blocks):
            for j in range(i, num_blocks):
                theta_val = Theta_kl[index].eval()
                theta_matrix_np[i,j] = theta_val
                theta_matrix_np[j,i] = theta_val
                index += 1
        
        E_matrix_np = np.zeros((num_blocks, num_nodes))
        E_row_value = E_row.eval()
        for node_index in range(num_nodes):
            block_index = E_row_value[node_index]
            E_matrix_np[block_index, node_index] = 1
        
        E_matrix = pm.Data("E_matrix", E_matrix_np)
         
        def calculate_lambda(E_matrix, Theta_matrix):
            full_matrix = pt.dot(pt.dot(E_matrix.T, Theta_matrix), E_matrix)
            indices = np.where(mask)
            edge_probs = full_matrix[indices]
            return edge_probs
        
        
        Lambda = pm.Deterministic('Lambda', calculate_lambda(E_matrix, Theta_matrix))
        Lambda1 = pm.Deterministic("Lambda1", custom_sigmoid(Lambda))
      
        Likelihood = pm.Bernoulli("Y_obs", p=Lambda1, observed=data_vector)

    return SBMmodel

Where Likelihood is explicit, I am trying to calculate the WAIC by doing:

waic = az.waic(trace)
waics[num_blocks] = waic
print(f"WAIC for num_blocks = {num_blocks}: {waic.waic}"). 

But I got this error:

1620 inference_data = convert_to_inference_data(data) -> 1621 log_likelihood = _get_log_likelihood(inference_data, var_name=var_name) 1622 scale = rcParams["stats.ic_scale"] if scale is None else scale.lower() 1623 pointwise = rcParams["stats.ic_pointwise"] if pointwise is None else pointwise File [/opt/anaconda3/envs/aesara_env/lib/python3.10/site-packages/arviz/stats/stats_utils.py:429](http://localhost:8888/opt/anaconda3/envs/aesara_env/lib/python3.10/site-packages/arviz/stats/stats_utils.py#line=428), in get_log_likelihood(idata, var_name, single_var) 427 return idata.sample_stats.log_likelihood 428 if not hasattr(idata, "log_likelihood"): --> 429 raise TypeError("log likelihood not found in inference data object") 430 if var_name is None: 431 var_names = list(idata.log_likelihood.data_vars) TypeError: log likelihood not found in inference data object.    

Which I don’t understand since the Likelihood was specified.

Calling pm.sample no longer computes the log-likelihood by default. You can call pm.compute_log_likelihood after you sample: pymc.compute_log_likelihood — PyMC 5.19.1 documentation

2 Likes

Alternatively you can add the option to do this via idata_kwargs parameter:

with model:
    idata = pm.sample(cores=1,chains=4,draws=3000,
                      idata_kwargs={"log_likelihood": True}, # here
                      )

This works for me, I’m using pymc=5.10.4 though (i.e. a slightly old version). The parameters that idata_kwargs will accept are those described here: pymc.to_inference_data — PyMC 5.19.1 documentation, as linked to from the docs for pm.sample().

2 Likes