# Log likelihood not found in inference data object

**URL:** <https://discourse.pymc.io/t/log-likelihood-not-found-in-inference-data-object/16276>\
**Category:** v5\
**Tags:** modeling\
**Created:** [December 20, 2024, 7:12am UTC](https://discourse.pymc.io/t/log-likelihood-not-found-in-inference-data-object/16276 "2024-12-20T07:12:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrea\_Catalina\_Garc](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/andrea_catalina_garc/32/8817_2.png) [@Andrea\_Catalina\_Garc](https://discourse.pymc.io/u/Andrea_Catalina_Garc)\
**Post date:** [December 20, 2024, 7:12am UTC](https://discourse.pymc.io/t/log-likelihood-not-found-in-inference-data-object/16276/1 "2024-12-20T07:12:00Z")

</div>

With this model:

```py
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:

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

```

But I got this error:

```auto
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.

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [December 20, 2024, 7:46am UTC](https://discourse.pymc.io/t/log-likelihood-not-found-in-inference-data-object/16276/2 "2024-12-20T07:46:12Z")

</div>

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](https://www.pymc.io/projects/docs/en/stable/api/generated/pymc.compute_log_likelihood.html)

---

<div class="post-metadata">

**Author:** ![cowirihy](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cowirihy/32/7259_2.png) [@cowirihy](https://discourse.pymc.io/u/cowirihy)\
**Post date:** [December 20, 2024, 9:07am UTC](https://discourse.pymc.io/t/log-likelihood-not-found-in-inference-data-object/16276/3 "2024-12-20T09:07:58Z")

</div>

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

```auto
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](https://www.pymc.io/projects/docs/en/stable/api/generated/pymc.to_inference_data.html#pymc.to_inference_data), as linked to from the docs for `pm.sample()`.
