Using Gamma distribution parameters from SciPy

I don’t know what is the goal but if it is just parameterization, you could use the ShiftedGamma that was defined here (code below). It extends the PyMC3 Gamma distribution to be able to receive a parameter equivalent to loc for SciPy.

class ShiftedGamma(pm.Gamma):
    def __init__(self, alpha, beta, shift, *args, **kwargs):
        transform = pm.distributions.transforms.lowerbound(shift)
        super().__init__(alpha=alpha, beta=beta, *args, **kwargs,
                         transform=transform)
        self.shift = shift
        self.mean += shift
        self.mode += shift
        
    def random(self):
        return super().random() + self.shift
    
    def logp(self, x):
        return super().logp(x - self.shift)

Then just make sure that you transform scale to beta and you are good to go.

with pm.Model() as model:
    
    alpha = pm.HalfNormal('alpha', 1)
    scale = pm.HalfNormal('scale', 1)
    loc = pm.HalfNormal('loc', 10)
    
    beta = 1/scale
    
    # likelihood
    y_g = ShiftedGamma("y_g", 
                          alpha=alpha, 
                          beta=beta, 
                          shift=loc, observed=y)
    
    trace = pm.sample(
        2000,
        tune=2000
    )

Hope it helps!

3 Likes