Inference with a bounded exponential distribution

I am new to PyMC. I have been going through some examples and getting started with a few simple problems. I am trying to implement a model with a bounded/truncated exponential distribution.

The data which I have is { 1.5, 2, 3, 4, 5, 12 }

and I would like to model this using the bounded exponential distribution

P(x|\lambda) \propto \begin{cases} e^{-x/\lambda} \qquad &1< x < 20 \\ 0 &\text{otherwise} \end{cases}

I choose a flat/uniform prior for \lambda

P(\lambda) = \begin{cases} 1/(20-1) \qquad &1< x < 20 \\ 0 &\text{otherwise} \end{cases}

This is based on modelling particle decays; see MacKay’s textbook, Sec 3.1.

import numpy as np
import pymc as pm

decay_data = np.array([1.5, 2, 3, 4, 5, 12])

with pm.Model() as model:
    # specify priors 
    λ = pm.Uniform("λ", lower=1.0, upper=20.0, initval = 1/5)
    # our model is a bounded exponential distribution from [1,20]   
    trunc_exp = pm.Bound('trunc_exp', pm.Exponential.dist(lam=1/λ), 
                          lower=1, upper=20) 
    # feed observations to the model
    obs = trunc_exp("obs", λ, observed=decay_data)
    # inference 
    trace = pm.sample(1000)

Is the above implementation correct? I am getting the error: TypeError: 'TensorVariable' object is not callable and the error points to the line where obs is defined.

Any help would be greatly appreciated.

1 Like

Bound does not work like that in V4, but regardless you cannot observe it because it does not include the normalization term.

There is a draft PR opened recently: Implement Truncated distributions by ricardoV94 · Pull Request #6113 · pymc-devs/pymc · GitHub

In the meantime you can implement a truncated likelihood by adding a Potential term with the normalization constant.There is a v3 example here: Google Colab which came from Help with pm.Potential - #12 by sspickle