# Fitting Custom Distributions and Likelihoods

**URL:** <https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730>\
**Category:** Questions\
**Tags:** theano\
**Created:** [July 12, 2021, 8:03pm UTC](https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730 "2021-07-12T20:03:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Melo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juan_melo/32/3330_2.png) [@Juan\_Melo](https://discourse.pymc.io/u/Juan_Melo)\
**Post date:** [July 12, 2021, 8:03pm UTC](https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730/1 "2021-07-12T20:03:15Z")

</div>

I’m working on building a framework to fit custom densities to data in PyMC3.

This is my starting set-up, where I try to do it with a simple Normal. What am I missing ? Does the custom density logp\_norm need to be expressed in Theano ? Is there a simple way to use the @as\_op decorator ?

```auto
## LOG DENSITY FOR NORMAL DISTRIBUTION 
def logp_norm(x, μ, σ):
    """Normal distribtuion."""
    return -0.5*np.log(2*np.pi) - np.log(σ) - (x-μ)**2/(2*σ**2)

## PYMC3 PARAMETER ESTIMATION 
with pm.Model() as LogoNormo:
    σ = pm.HalfNormal('μ',sd=1)
    μ = pm.Normal('σ', mu = 0, sd=1)
    

    "Custom Density"
    # Note that by calling the observed here, you declare the likelihood 
    custom_pdf = pm.DensityDist('custom_pdf', logp_norm(obs_y,μ, σ), observed = obs_y)

    trace = pm.sample(2000, chains=3)
    
    pm.traceplot(trace, ['μ', 'σ']);

```

Would appreciate some clues.  
Thank you!

---

<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:** [July 13, 2021, 5:57am UTC](https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730/2 "2021-07-13T05:57:28Z")

</div>

Your `logp_norm` should return a callable. Something along the lines of:

```python
def logp_norm(μ, σ):
    """Normal distribtuion."""
    def logp (x):
        return -0.5*np.log(2*np.pi) - np.log(σ) - (x-μ)**2/(2*σ**2)
    return logp

...

custom_pdf = pm.DensityDist('custom_pdf', logp_norm(μ, σ), observed = obs_y)

```

---

<div class="post-metadata">

**Author:** ![lucianopaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lucianopaz/32/2186_2.png) [@lucianopaz](https://discourse.pymc.io/u/lucianopaz)\
**Post date:** [July 13, 2021, 3:53pm UTC](https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730/3 "2021-07-13T15:53:13Z")

</div>

The answer is slightly different if you are using pymc3 version 3 (not the pre-release of version 4). Could you say which version you’re using, @Juan_Melo

---

<div class="post-metadata">

**Author:** ![Juan\_Melo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juan_melo/32/3330_2.png) [@Juan\_Melo](https://discourse.pymc.io/u/Juan_Melo)\
**Post date:** [July 13, 2021, 4:15pm UTC](https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730/4 "2021-07-13T16:15:07Z")

</div>

I’m currently using pymc3 3.10.0, and Python 3.7.3 on a Jupyter notebook.

Would you suggest installing the pre-release of version 4? I haven’t looked at it.

---

<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:** [July 13, 2021, 5:38pm UTC](https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730/5 "2021-07-13T17:38:27Z")

</div>

> [@Juan\_Melo](#):
>
> Would you suggest installing the pre-release of version 4? I haven’t looked at it.

No, the `DensityDist` has not been implemented in version 4 yet

---

<div class="post-metadata">

**Author:** ![Juan\_Melo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/juan_melo/32/3330_2.png) [@Juan\_Melo](https://discourse.pymc.io/u/Juan_Melo)\
**Post date:** [July 14, 2021, 1:11am UTC](https://discourse.pymc.io/t/fitting-custom-distributions-and-likelihoods/7730/6 "2021-07-14T01:11:50Z")

</div>

Thanks Ricardo, and Luciano! I was able to fit the distribution with the above code.
