Skip to content

ENH: GenericLikelihoodWrapper for scipy.stats distribution fit #2527

Open

Description

@josef-pkt

see http://stackoverflow.com/questions/31359017/how-to-get-error-estimates-for-fit-parameters-in-scipy-stats-gamma-fit/31365801#31365801

my current script:

# -*- coding: utf-8 -*-
"""
Created on Sun Jul 12 03:44:40 2015

Author: Josef Perktold
License: BSD-3

"""

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('\n')


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())
print('\n')

res.t_test(([1, 0, 0], [10]))
res.t_test('par0=10')

import statsmodels.api as sm
res_glm = sm.GLM(data, exog=np.ones(len(data)), 
                 family=sm.families.Gamma(link=sm.families.links.identity)).fit()
print(res_glm.summary())

print(gamma.mean(*params))
print(gamma.var(*params))

GLM Gamma only has mean parameter and separately estimated scale parameter. Note: Pearson chisquare as scale estimate does not correspond to the variance of endog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions