Some questions about defining a custom distribution

Hello,everyone! I am a rookie about pymc,and I’m trying to define a three-parameter log-logistic distribution in pymc.I have referred to the official documents in pymc3.But I have encountered a problem.This is my code:

from pymc3.distributions.dist_math import bound, logpow
import numpy as np
from scipy.stats import fisk

def loglogistic_logp(alpha,beta,r,value):
alpha_r_value=(value-r)/alpha
log_prob=np.log(beta/alpha)+logpow(alpha_r_value,beta-1)+logpow(1+alpha_r_value**beta,-2)
return log_prob

def loglogistic_rvs(alpha,beta,r,size):
return fisk.rvs(c=beta,loc=r,scale=alpha,size=size)

class CustomLogLogistic(pm.Continuous):
def init(self,alpha,beta,r,*args,**kwargs):
super(CustomLogLogistic,self).init(*args, **kwargs)
self.alpha=alpha
self.beta=beta
self.r=r

def logp(self,value):
    alpha=self.alpha
    beta=self.beta
    r=self.r
    return loglogistic_logp(alpha,beta,r,value)

def random(self,alpha,beta,r):
    return fisk.rvs(c=beta,loc=r,scale=alpha,size=size)

then i tried this:

b=CustomLogLogistic.dist(beta=3.05,r=0,alpha=1).random()
print(b)

i got a error:
init() got an unexpected keyword argument ‘beta’

and i don’t know how to fix it.
English is not my native language, forgive me for my poor expression.
Thank you all!

1 Like

Since the fisk is just an exponentiated Logistic, in the new version of PyMC you can actually define it easily with a CustomDist like this:

import pymc as pm

def log_logistic(alpha, beta, size):
  mu = pm.math.log(alpha)
  s = 1/beta
  return pm.math.exp(pm.Logistic.dist(mu=mu, s=s, size=size))

with pm.Model() as m:
  ...
  x = pm.CustomDist("x", alpha, beta, dist=log_logistic)

https://www.pymc.io/projects/docs/en/stable/api/distributions/generated/pymc.CustomDist.html

Thank you sooooo much!
But I want to use a three-parameter log-logistic distribution,which has another parameter : location parameter,so I still don’t know how to do.
This is its link,and I’m using its " Alternate parameterization" PDF
https://en.wikipedia.org/wiki/Shifted_log-logistic_distribution

You can just add a loc parameter!

import pymc as pm

def log_logistic(alpha, beta,  loc, size):
  mu = pm.math.log(alpha)
  s = 1/beta
  return pm.math.exp(pm.Logistic.dist(mu=mu, s=s, size=size)) + loc

with pm.Model() as m:
  ...
  x = pm.CustomDist("x", alpha, beta, loc, dist=log_logistic)

Thank you again! :smiley:
I have studied the knowledge of distribution too poorly, and I have studied it carefully again, understanding the relationship between them.