使用 statsmodels 计算 Gamma GLM 的 scale/dispersion

Calculating scale/dispersion of Gamma GLM using statsmodels

我在使用 statsmodels 的 GLM 函数获取模拟数据的分散参数时遇到问题。

import statsmodels.api as sm
import matplotlib.pyplot as plt 
import scipy.stats as stats 
import numpy as np

np.random.seed(1)

# Generate data
x=np.random.uniform(0, 100,50000)
x2 = sm.add_constant(x)
a = 0.5
b = 0.2
y_true = 1/(a+(b*x))
# Add error 
scale = 2 # the scale parameter I'm trying to obtain
shape = y_true/scale # given that, for Gamma, mu = scale*shape
y = np.random.gamma(shape=shape, scale=scale)

# Run model
model = sm.GLM(y, x2, family=sm.families.Gamma()).fit() 

model.summary()

这是上面的总结:

请注意,系数估计值是正确的(0.5 和 0.2),但比例 (21.995) 与我设置的比例 (2) 相去甚远。

有人可以指出我 misunderstanding/doing 错在哪里吗?谢谢!

正如 Josef 在评论中指出的那样,statsmodels 使用了一种不同的参数化。