[英]Using pymc3 to fit lomax model
I have a pretty simple example that doesn't seem to work. 我有一个非常简单的示例,似乎没有用。 My goal is to build a Lomax model, and since PyMC3 doesn't have a Lomax distribution I use the fact that an Exponential mixed with a Gamma is a Lomax (see here ):
我的目标是建立Lomax模型,并且由于PyMC3没有Lomax分布,因此我使用了一个事实,即与Gamma混合的指数就是Lomax(请参见此处 ):
import pymc3 as pm
from scipy.stats import lomax
# Generate artificial data with a shape and scale parameterization
data = lomax.rvs(c=2.5, scale=3, size=1000)
# if t ~ Exponential(lamda) and lamda ~ Gamma(shape, rate), then t ~ Lomax(shape, rate)
with pm.Model() as hierarchical:
shape = pm.Uniform('shape', 0, 10)
rate = pm.Uniform('rate', 0 , 10)
lamda = pm.Gamma('lamda', alpha=shape, beta=rate)
t = pm.Exponential('t', lam=lamda, observed=data)
trace = pm.sample(1000, tune=1000)
The summary is: 摘要是:
>>> pm.summary(trace)
mean sd mc_error hpd_2.5 hpd_97.5 n_eff Rhat
shape 4.259874 2.069418 0.060947 0.560821 8.281654 1121.0 1.001785
rate 6.532874 2.399463 0.068837 2.126299 9.998271 1045.0 1.000764
lamda 0.513459 0.015924 0.000472 0.483754 0.545652 1096.0 0.999662
I would expect the shape and rate estimates to be close to 2.5 and 3 respectively. 我希望形状和速率估计分别接近2.5和3。 I tried various non-informative priors for shape and rate, including
pm.HalfFlat()
and pm.Uniform(0, 100)
but both resulted in worse fits. 我尝试了各种形状和费率的非先验先验,包括
pm.HalfFlat()
和pm.Uniform(0, 100)
但两者均导致拟合度较差。 Any ideas? 有任何想法吗?
弄清楚了:为了从指数-伽玛混合物中得出lomax,我需要为数据集中的每个示例指定一个lamda
( lamda = pm.Gamma('lamda', alpha=shape, beta=rate, shape=len(data)
)。这是因为该模型假设数据中的每个主题都有其自己的lamda_i
,其中lamda_i ~ Gamma(shape, rate)
每i
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.