I’m building a model where the likelihood is gamma and the alpha and beta priors have a LaPlace and beta distribution, respectively.
I need to specify the beta distribution with the loc and scale parameters that are specified in scipy, in addition to ‘a’ and ‘b’.
I found a post that allows me to specify the beta distribution with those additional parameters. However, when I specify as is, I get the following error:
SamplingError : Initial evaluation of model at starting point failed! Starting values: {‘alpha_lowerbound__’: array(0.), ‘beta_testing_interval__’: array(nan)}
So I attempted to do something similar to what I do with the LaPlace, placing a bound on the custom distribution:
import numpy as np
import pymc3 as pm
class SSBeta(pm.Beta):
def __init__(self, alpha, beta, loc, scale, *args, **kwargs):
transform = pm.distributions.transforms.interval(loc, loc + scale)
super().__init__(alpha=alpha, beta=beta, *args, **kwargs, transform=transform)
self.scale = scale
self.loc = loc
self.mean += loc
def random(self):
return super().random()*self.scale + self.loc
def logp(self, x):
return super().logp((x - self.loc)/self.scale)
with pm.Model() as model1:
bounded_laplace = pm.Bound(pm.Laplace, lower=0.0)
alpha = bounded_laplace('alpha', mu = 51704326, b = 767593)
bounded_beta = pm.Bound(SSBeta, lower=0.0)
beta_param = bounded_beta(name = 'beta_testing',
alpha = 1.578,
beta = 0.855,
loc = 0.0004,
scale = 7.338e-5)
pred = pm.Gamma("test_var", alpha = alpha, beta = beta_param,
observed=np.array([1.72e6, 1.58e6, 1.64e6, 1.59e6]))
trace = pm.sample(400)
This is the error I get:
TypeError : pymc3.distributions.continuous.Beta.init() got multiple values for keyword argument ‘transform’
It does appear that when I change the scale to 1 or above, the model runs.