# Custom likelihood function giving TypeError

**URL:** <https://discourse.pymc.io/t/custom-likelihood-function-giving-typeerror/6712>\
**Category:** Questions\
**Created:** [February 3, 2021, 7:55pm UTC](https://discourse.pymc.io/t/custom-likelihood-function-giving-typeerror/6712 "2021-02-03T19:55:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drg5](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/drg5/32/3627_2.png) [@drg5](https://discourse.pymc.io/u/drg5)\
**Post date:** [February 3, 2021, 7:55pm UTC](https://discourse.pymc.io/t/custom-likelihood-function-giving-typeerror/6712/1 "2021-02-03T19:55:09Z")

</div>

Hi,

New to Bayesian modeling, tried to find an answer from previous posts but failed.

Each point of my observed data has two entries: years of work experience, and a boolean about whether they moved to a different state since they got their first job(0 if they did, 1 if they did not). For the people who stayed in the same state, the likelihood function is:

P(t|a,b) =e^{f1+f2}\\ f1=\frac{-b}{a^2}\\ f2=\frac{be^{-at}(1+at)}{a^2}

The likelihood function of people who have moved is obviously 1 minus this. My code is:

```
import pymc3 as pm
import theano.tensor as tt

yr = [20, 34, 8, 15, 38] #years of work experience
ss = [1, 1, 1, 1, 0] #0: didn't move states, 1: moved states

def likelihood(a, b, t, ss):
    f1 = -b/(a**2)
    f2 = (b*tt.exp(-a*t)*(1+a*t))/(a**2)
    return ss*(f1+f2)+(1-ss)*(tt.log(1-tt.exp(f1+f2))) #logp

with pm.Model() as model:    
    a = pm.Gamma('a', alpha=1, beta=1)
    b = pm.Gamma('b', alpha=1, beta=1)
    like = pm.DensityDist('like', likelihood, observed={'a':a,'b':b,'t':yr,'ss':ss})
    trace = pm.sample()

```

This gives me an error:

```
Unknown parameter type: <class 'theano.tensor.var.TensorVariable'>

```

What am I doing wrong here? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rosgori](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rosgori/32/1077_2.png) [@rosgori](https://discourse.pymc.io/u/rosgori)\
**Post date:** [February 4, 2021, 2:25am UTC](https://discourse.pymc.io/t/custom-likelihood-function-giving-typeerror/6712/2 "2021-02-04T02:25:32Z")

</div>

I am getting another error:

```auto
MissingInputError: Input 0 of the graph (indices start from 0), 
used to compute Elemwise{exp,no_inplace}(a_log__), 
was not provided and not given a value. Use the Theano flag 
exception_verbosity='high', for more information on this error.

```

---

<div class="post-metadata">

**Author:** ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)\
**Post date:** [February 4, 2021, 9:07am UTC](https://discourse.pymc.io/t/custom-likelihood-function-giving-typeerror/6712/3 "2021-02-04T09:07:11Z")

</div>

Try updating to latest arviz version and use `idata_kwargs={"density_dist_obs": False}` in `pm.sample`.

* * *

**Note:** I also recommend using `return_inferencedata=True`. Otherwise, this same error will be triggered any time you call plotting functions such as `plot_trace` or stats functions such as `summary` with this trace object.

The error is in the conversion to InferenceData step, because the variables passed as `observed` to `DensityDist` are not observed data, there are model parameters which works for sampling but ends up breaking things down the line. You may also want to track [[WIP] Add givens argument to DensityDist by OriolAbril · Pull Request #4433 · pymc-devs/pymc3 · GitHub](https://github.com/pymc-devs/pymc3/pull/4433).

All plotting and stats functions do this conversion under the hood before doing the actual calculations or plots and do not allow to use extra arguments in the conversion step. Converting to inferencedata and calling plots and stats functions with an inferencedata object avoids first the conversion overhead and possible errors such as this.
