您可以按照https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_continuous.html 构建自己的发行版。你定义的私有函数越多,分布就越好。至少,您需要定义 _pdf() 和 _argcheck,其他所有内容都将在运行中(慢慢地!)计算。
基本上,如果您提供 _pdf(),则父 pdf() 调用将 jusr 重定向到 _pdf()。如果您提供 _cdf(),则 cdf() 将调用 _cdf()。
但如果没有提供 _ppf() 实现,父类会从 _pdf()、_cdf() 和您提供的所有其他部分为您补上。
示例代码(经过轻微测试!)帮助您入门
import numpy as np
from scipy.stats import rv_continuous
from scipy.special import erf
SQRT2 = np.float64(1.4142135623730951)
def logit(x):
return np.log(x/(1.0-x))
class logit_norm(rv_continuous):
def _pdf(self, x, μ, σ):
if x == 0.0:
return 0.0
if x == 1.0:
return 0.0
return 1.0 / (σ * np.sqrt(2.0*np.pi)) * np.exp(-0.5 * ((logit(x) - μ)/σ)**2)/x/(1.0-x)
def _cdf(self, x, μ, σ):
if x == 0.0:
return 0.0
if x == 1.0:
return 1.0
return 0.5*(1.0 + erf((logit(x)-μ)/SQRT2/σ))
def _argcheck(self, μ, σ):
s = σ > 0.0
return s
np.random.seed(seed=111)
lgtn = logit_norm(name='logit_norm', a=0.0, b=1.0)
test = lgtn.rvs(μ=2.0,σ=1.0,loc=0.0,scale=1.0, size=100000)