Exponential likelihood at different location

@harcel I agree with @ricardoV94 that your solution is a simple and clean one. But I agree with your third point—I’ve been wanting an offset parameter (akin to scipy loc) for some of the distributions, including the Exponential class, and was wondering if this is something the developers were considering.

But to address your question, I was trying to think of a way of doing it that is built in and in line with your Normal example. The solution I came up with was to have the exponentially distributed error as an additive term to your mu= in your pm.Normal() likelihood definition and using a small and constant value for sigma=, as shown below.

First some test data:

x = np.array(range(0, 100))
m = 0.1
b = 10
scale = 2
err = sp.stats.expon.rvs(scale=scale, size=len(x), random_state=42)

y = m*x + b + err

plt.plot(x, y, '.')
plt.plot(x, m*x + b, '-')

example
I model this by creating a hierarchical parameter for the exponential decay (lam) but then use that to generate a bunch of independent error variables err for each of your data points. Like so:

 sigma_vals = np.array([0.1]*len(x))

with pm.Model() as m:
    lam = pm.HalfCauchy('err_lam', beta=10)
    intercept = pm.Normal('intercept', mu=0, sigma=20)
    slope = pm.Normal('slope', mu=0, sigma=20)
    
    err = pm.Exponential('errors', lam = lam, shape=[len(x),])
    
    y_vals = slope*x + intercept + err
    obs = pm.Normal('y', mu = y_vals, sigma=sigma_vals, observed = y)
    
    samps = pm.sample(1000)

Which returns the correct posteriors:
arviz.plot_posterior(samps, var_names=['intercept', 'slope', 'err_lam']);

The downside to this approach seems two-fold:

  1. One needs to specify an arbitrary value for sigma in the model (i.e. sigma_vals)
  2. One ends up with a bunch of extraneous variables associated with the error on each data point (i.e. err) (which I have omitted in the plot_posterior call)

I’m curious if there’s anything else people think is particularly wrong-headed about this approach or if they have suggestions for improvements.

1 Like