How to get error estimates for fit parameters in scipy.stats.gamma.fit?
我有一些,我用scipy.stats拟合伽马分布。我能够提取形状、位置和比例参数,它们看起来与我期望的数据范围是合理的。
我的问题是:有没有办法同时得到参数中的错误?类似于曲线拟合的输出。注意:我不直接使用曲线拟合,因为它不能正常工作,而且大多数时间无法计算伽马分布的参数。另一方面,scipy.stats.gamma.fit工作正常。
这是我正在做的事情的一个例子(不是我的实际数据)。
1 2 3 4 5 | from scipy.stats import gamma shape = 12; loc = 0.71; scale = 0.0166 data = gamma.rvs(shape, loc=loc, scale=scale, size=1000) params = gamma.fit(data) # params close to but not the same as (shape, loc, scale) # HOW TO ESTIMATE/GET ERRORS FOR EACH PARAM? |
提前谢谢
编辑警告:以下说明了在问题示例之后使用
StatsModels有一个用于最大似然估计的通用类,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import numpy as np from statsmodels.base.model import GenericLikelihoodModel from scipy.stats import gamma shape = 12; loc = 0.71; scale = 0.0166 data = gamma.rvs(shape, loc=loc, scale=scale, size=1000) params = gamma.fit(data) # params close to but not the same as (shape, loc, scale) # HOW TO ESTIMATE/GET ERRORS FOR EACH PARAM? print(params) print(' ') class Gamma(GenericLikelihoodModel): nparams = 3 def loglike(self, params): return gamma.logpdf(self.endog, *params).sum() res = Gamma(data).fit(start_params=params) res.df_model = len(params) res.df_resid = len(data) - len(params) print(res.summary()) |
这打印了以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | (10.31888758604304, 0.71645502437403186, 0.018447479022445423) Optimization terminated successfully. Current function value: -1.439996 Iterations: 69 Function evaluations: 119 Gamma Results ============================================================================== Dep. Variable: y Log-Likelihood: 1440.0 Model: Gamma AIC: -2872. Method: Maximum Likelihood BIC: -2852. Date: Sun, 12 Jul 2015 Time: 04:00:05 No. Observations: 1000 Df Residuals: 997 Df Model: 3 ============================================================================== coef std err z P>|z| [95.0% Conf. Int.] ------------------------------------------------------------------------------ par0 10.3187 2.242 4.603 0.000 5.925 14.712 par1 0.7165 0.019 37.957 0.000 0.679 0.753 par2 0.0184 0.002 8.183 0.000 0.014 0.023 ============================================================================== |
号
基于最大似然估计的其他结果也可用,例如,第一个参数为10的z-检验可以通过指定限制矩阵或使用相等的字符串表达式来执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> res.t_test(([1, 0, 0], [10])) <class 'statsmodels.stats.contrast.ContrastResults'> Test for Constraints ============================================================================== coef std err z P>|z| [95.0% Conf. Int.] ------------------------------------------------------------------------------ c0 10.3187 2.242 0.142 0.887 5.925 14.712 ============================================================================== >>> res.t_test('par0=10') <class 'statsmodels.stats.contrast.ContrastResults'> Test for Constraints ============================================================================== coef std err z P>|z| [95.0% Conf. Int.] ------------------------------------------------------------------------------ c0 10.3187 2.242 0.142 0.887 5.925 14.712 ============================================================================== |