Has anyone developed a custom tweedie distribution for sampling? How to implement once developed?


I’d like to model insurance pure premium which is usually known to have a tweedie distribution. Has any of you pymc3-ers ever developed a custom tweedie distribution to sample from?

Update: I found a comparable function for a tweedie distribution (see below). How would I actually use this function as a sampling method like pm.Normal/pm.StudentT/etc.?

def tweedie(n,p,mu,phi):

np.random.seed(seed = 32 )

#checking the value of variance power between 1-2

if (p = 2 ):

print ( 'p must be between (1,2)' )


else :

rt = np.full(n,np.nan)

# calculating mean of poisson distribution

lambdaa = mu * * ( 2 - p) / (phi * ( 2 - p))

# shape parameter of gamma distribution

alpha = ( 2 - p) / ( 1 - p)

# scale parameter of gamma distribution

gam = phi * (p - 1 ) * (mu * * (p - 1 ))

# Generating Poisson random sample

N = np.random.poisson(lambdaa,n)

for i in range (n):

# Generate single data point of gamma distribution using poisson random variable

rt[i] = np.random.gamma(N[i] * np. abs (alpha),gam, 1 )

return (rt)

I suggest that you read this introduction to pymc3 distributions. It introduces the two alternatives that you have: define a class that inherits from Distribution and write down both the logp and random methods, or wrap a single logp definition with a DensityDist.

The most important thing needed by pymc3 to do inference is the logp function. If you can write that down you’ll be good to go.