本文整理汇总了Python中statsmodels.discrete.discrete_model.Poisson类的典型用法代码示例。如果您正苦于以下问题:Python Poisson类的具体用法?Python Poisson怎么用?Python Poisson使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Poisson类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: junk
def junk():
# Singular Matrix in mod1a.fit()
formula1 = 'deaths ~ smokes + C(agecat)'
formula2 = 'deaths ~ C(agecat) + C(smokes) : C(agecat)' # same as Stata default
mod = Poisson.from_formula(formula2, data=data, exposure=data['pyears'].values)
res0 = mod.fit()
constraints = 'C(smokes)[T.1]:C(agecat)[3] = C(smokes)[T.1]:C(agecat)[4]'
import patsy
lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constraints)
R, q = lc.coefs, lc.constants
resc = mod.fit_constrained(R,q, fit_kwds={'method':'bfgs'})
# example without offset
formula1a = 'deaths ~ logpyears + smokes + C(agecat)'
mod1a = Poisson.from_formula(formula1a, data=data)
print(mod1a.exog.shape)
res1a = mod1a.fit()
lc_1a = patsy.DesignInfo(mod1a.exog_names).linear_constraint('C(agecat)[T.4] = C(agecat)[T.5]')
resc1a = mod1a.fit_constrained(lc_1a.coefs, lc_1a.constants, fit_kwds={'method':'newton'})
print(resc1a[0])
print(resc1a[1])
示例2: setup_class
def setup_class(cls):
expected_params = [1, 1, 0.5]
np.random.seed(987123)
nobs = 500
exog = np.ones((nobs, 2))
exog[:nobs//2, 1] = 0
# offset is used to create misspecification of the model
# for predicted probabilities conditional moment test
#offset = 0.5 * np.random.randn(nobs)
#range_mix = 0.5
#offset = -range_mix / 2 + range_mix * np.random.rand(nobs)
offset = 0
mu_true = np.exp(exog.dot(expected_params[:-1]) + offset)
endog_poi = np.random.poisson(mu_true / 5)
# endog3 = distr.zigenpoisson.rvs(mu_true, 0,
# 2, 0.01, size=mu_true.shape)
model_poi = Poisson(endog_poi, exog)
res_poi = model_poi.fit(method='bfgs', maxiter=5000, maxfun=5000)
cls.exog = exog
cls.endog = endog_poi
cls.res = res_poi
cls.nobs = nobs
示例3: _initialize
def _initialize(cls):
y, x = cls.y, cls.x
modp = Poisson(y, x)
cls.res2 = modp.fit(disp=0)
mod = PoissonPenalized(y, x)
mod.pen_weight = 0
cls.res1 = mod.fit(method='bfgs', maxiter=100, disp=0)
cls.atol = 5e-6
示例4: setup_class
def setup_class(cls):
# here we don't need to check convergence from default start_params
start_params = [14.1709, 0.7085, -3.4548, -0.539, 3.2368, -7.9299,
-5.0529]
mod_poi = Poisson(endog, exog)
res_poi = mod_poi.fit(start_params=start_params)
marge_poi = res_poi.get_margeff(dummy=True)
cls.res = res_poi
cls.margeff = marge_poi
cls.res1_slice = [0, 1, 2, 3, 5, 6]
cls.res1 = res_stata.results_poisson_margins_dummy
示例5: setup_class
def setup_class(cls):
cls.res2 = results.results_noexposure_constraint
cls.idx = [7, 3, 4, 5, 6, 0, 1] # 2 is dropped baseline for categorical
# example without offset
formula = 'deaths ~ logpyears + smokes + C(agecat)'
mod = Poisson.from_formula(formula, data=data)
#res1a = mod1a.fit()
# get start_params, example fails to converge on one py TravisCI
k_vars = len(mod.exog_names)
start_params = np.zeros(k_vars)
start_params[0] = np.log(mod.endog.mean())
# if we need it, this is desired params
p = np.array([-3.93478643, 1.37276214, 2.33077032, 2.71338891,
2.71338891, 0.57966535, 0.97254074])
constr = 'C(agecat)[T.4] = C(agecat)[T.5]'
lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constr)
cls.res1 = fit_constrained(mod, lc.coefs, lc.constants,
start_params=start_params,
fit_kwds={'method': 'bfgs',
'disp': 0})
# TODO: Newton fails
# test method of Poisson, not monkey patched
cls.res1m = mod.fit_constrained(constr, start_params=start_params,
method='bfgs', disp=0)
示例6: test_compare_glm_poisson
def test_compare_glm_poisson(self):
res1 = self.res1m
res2 = self.res2
formula = 'deaths ~ smokes + C(agecat)'
mod = Poisson.from_formula(formula, data=data,
exposure=data['pyears'].values)
#offset=np.log(data['pyears'].values))
constr = 'C(agecat)[T.4] = C(agecat)[T.5]'
res2 = mod.fit_constrained(constr, start_params=self.res1m.params,
method='newton', warn_convergence=False,
disp=0)
# we get high precision because we use the params as start_params
# basic, just as check that we have the same model
assert_allclose(res1.params, res2.params, rtol=1e-12)
assert_allclose(res1.bse, res2.bse, rtol=1e-12)
# check predict, fitted, ...
predicted = res1.predict()
assert_allclose(predicted, res2.predict(), rtol=1e-10)
assert_allclose(res1.mu, predicted, rtol=1e-10)
assert_allclose(res1.fittedvalues, predicted, rtol=1e-10)
assert_allclose(res2.predict(linear=True), res2.predict(linear=True),
rtol=1e-10)
示例7: __init__
def __init__(self, endog, exog, exog_infl=None, offset=None, exposure=None,
inflation='logit', missing='none', **kwargs):
super(ZeroInflatedPoisson, self).__init__(endog, exog, offset=offset,
inflation=inflation,
exog_infl=exog_infl,
exposure=exposure,
missing=missing, **kwargs)
self.model_main = Poisson(self.endog, self.exog, offset=offset,
exposure=exposure)
self.distribution = zipoisson
self.result_class = ZeroInflatedPoissonResults
self.result_class_wrapper = ZeroInflatedPoissonResultsWrapper
self.result_class_reg = L1ZeroInflatedPoissonResults
self.result_class_reg_wrapper = L1ZeroInflatedPoissonResultsWrapper
示例8: setup_class
def setup_class(cls):
from statsmodels.discrete.discrete_model import Poisson
import statsmodels.stats.tests.test_anova as ttmod
test = ttmod.TestAnova3()
test.setup_class()
cls.data = test.data.drop([0,1,2])
mod = Poisson.from_formula("Days ~ C(Duration) + C(Weight)", cls.data)
cls.res = mod.fit(cov_type='HC0')
cls.term_name = "C(Weight)"
cls.constraints = ['C(Weight)[T.2]',
'C(Weight)[T.3]',
'C(Weight)[T.3] - C(Weight)[T.2]']
示例9: setup_class
def setup_class(cls):
cls.res2 = results.results_exposure_constraint
#cls.idx = [3, 4, 5, 6, 0, 1] # 2 is dropped baseline for categorical
cls.idx = [6, 2, 3, 4, 5, 0] # 2 is dropped baseline for categorical
# example without offset
formula = 'deaths ~ smokes + C(agecat)'
mod = Poisson.from_formula(formula, data=data,
offset=np.log(data['pyears'].values))
#res1a = mod1a.fit()
constr = 'C(agecat)[T.4] = C(agecat)[T.5]'
lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constr)
cls.res1 = fit_constrained(mod, lc.coefs, lc.constants,
fit_kwds={'method':'newton'})
cls.constraints = lc
# TODO: bfgs fails
# test method of Poisson, not monkey patched
cls.res1m = mod.fit_constrained(constr, method='newton')
示例10: setup_class
def setup_class(cls):
cls.res2 = results.results_exposure_constraint2
#cls.idx = [3, 4, 5, 6, 0, 1] # 2 is dropped baseline for categorical
cls.idx = [6, 2, 3, 4, 5, 0] # 2 is dropped baseline for categorical
# example without offset
formula = 'deaths ~ smokes + C(agecat)'
mod = Poisson.from_formula(formula, data=data,
#offset=np.log(data['pyears'].values))
exposure=data['pyears'].values)
#res1a = mod1a.fit()
constr = 'C(agecat)[T.5] - C(agecat)[T.4] = 0.5'
lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constr)
cls.res1 = fit_constrained(mod, lc.coefs, lc.constants,
fit_kwds={'method': 'newton',
'disp': 0})
cls.constraints = lc
# TODO: bfgs fails to converge. overflow somewhere?
# test method of Poisson, not monkey patched
cls.res1m = mod.fit_constrained(constr, method='bfgs', disp=0,
start_params=cls.res1[0])
示例11: ZeroInflatedPoisson
class ZeroInflatedPoisson(GenericZeroInflated):
__doc__ = """
Poisson Zero Inflated model for count data
%(params)s
%(extra_params)s
Attributes
-----------
endog : array
A reference to the endogenous response variable
exog : array
A reference to the exogenous design.
exog_infl: array
A reference to the zero-inflated exogenous design.
""" % {'params' : base._model_params_doc,
'extra_params' : _doc_zi_params + base._missing_param_doc}
def __init__(self, endog, exog, exog_infl=None, offset=None, exposure=None,
inflation='logit', missing='none', **kwargs):
super(ZeroInflatedPoisson, self).__init__(endog, exog, offset=offset,
inflation=inflation,
exog_infl=exog_infl,
exposure=exposure,
missing=missing, **kwargs)
self.model_main = Poisson(self.endog, self.exog, offset=offset,
exposure=exposure)
self.distribution = zipoisson
self.result_class = ZeroInflatedPoissonResults
self.result_class_wrapper = ZeroInflatedPoissonResultsWrapper
self.result_class_reg = L1ZeroInflatedPoissonResults
self.result_class_reg_wrapper = L1ZeroInflatedPoissonResultsWrapper
def _hessian_main(self, params):
params_infl = params[:self.k_inflate]
params_main = params[self.k_inflate:]
y = self.endog
w = self.model_infl.predict(params_infl)
w = np.clip(w, np.finfo(float).eps, 1 - np.finfo(float).eps)
score = self.score(params)
zero_idx = np.nonzero(y == 0)[0]
nonzero_idx = np.nonzero(y)[0]
mu = self.model_main.predict(params_main)
hess_arr = np.zeros((self.k_exog, self.k_exog))
coeff = (1 + w[zero_idx] * (np.exp(mu[zero_idx]) - 1))
#d2l/dp2
for i in range(self.k_exog):
for j in range(i, -1, -1):
hess_arr[i, j] = ((
self.exog[zero_idx, i] * self.exog[zero_idx, j] *
mu[zero_idx] * (w[zero_idx] - 1) * (1 / coeff -
w[zero_idx] * mu[zero_idx] * np.exp(mu[zero_idx]) /
coeff**2)).sum() - (mu[nonzero_idx] * self.exog[nonzero_idx, i] *
self.exog[nonzero_idx, j]).sum())
return hess_arr
def _predict_prob(self, params, exog, exog_infl, exposure, offset):
params_infl = params[:self.k_inflate]
params_main = params[self.k_inflate:]
counts = np.atleast_2d(np.arange(0, np.max(self.endog)+1))
if len(exog_infl.shape) < 2:
transform = True
w = np.atleast_2d(
self.model_infl.predict(params_infl, exog_infl))[:, None]
else:
transform = False
w = self.model_infl.predict(params_infl, exog_infl)[:, None]
w = np.clip(w, np.finfo(float).eps, 1 - np.finfo(float).eps)
mu = self.model_main.predict(params_main, exog,
offset=offset)[:, None]
result = self.distribution.pmf(counts, mu, w)
return result[0] if transform else result
def _get_start_params(self):
start_params = self.model_main.fit(disp=0, method="nm").params
start_params = np.append(np.ones(self.k_inflate) * 0.1, start_params)
return start_params
示例12: initialize
def initialize(cls):
from statsmodels.discrete.discrete_model import Poisson
mod = Poisson.from_formula("Days ~ C(Duration, Sum)*C(Weight, Sum)", cls.data)
cls.res = mod.fit(cov_type='HC0')
示例13: PredictPlayerStats
class PredictPlayerStats(ConvertMixin):
def __init__(self, engine, player_name, stat_to_predict, opposing_team_name,
predictor_stats=('csum_min_kills', 'csum_min_minions_killed'),
defense_predictor_stats=('csum_prev_min_allowed_kills', 'csum_prev_min_allowed_assists'),
game_range=None):
self.engine = engine
self.player_name = player_name
self.stat_to_predict = stat_to_predict
if predictor_stats:
self.predictor_stats = ('csum_prev_min_kills', 'csum_prev_min_minions_killed')
else:
self.predictor_stats = ('csum_prev_min_kills', 'csum_prev_min_minions_killed')
role_stats = ('Jungler', 'Mid', 'Coach', 'Support', 'AD', 'Sub', 'Top')
self.predictor_stats = self.predictor_stats + defense_predictor_stats + role_stats
self.opposing_team_name = opposing_team_name
self.player_stats_table_name = 'player_stats_df'
self.processed_player_stars_table_name = 'processed_player_stats_df'
self.key_stats = ('kills', 'deaths', 'assists', 'minions_killed', 'gold',
'k_a', 'a_over_k')
self.game_range = game_range
self._process_player_stats_and_train()
def _process_player_stats_and_train(self):
processed_player_stats_df = self._get_processed_player_stats_in_df()
self.latest_predictor_numpy_array = self._get_latest_player_stats_numpy_array(processed_player_stats_df)
print('latest predictors numpy array {}'.format(self.latest_predictor_numpy_array))
predictors, y_array = self._get_predictors_in_numpy_arrays(processed_player_stats_df)
self._train_model(predictors, y_array)
def _get_latest_player_stats_numpy_array(self, processed_player_stats_df):
player_id = self._get_player_id_by_player_name(self.player_name)
player_stats_df = processed_player_stats_df[processed_player_stats_df['player_id'] == player_id]
latest_player_stats_df = player_stats_df.sort(['game_id'], ascending=False).head(1)
dict_player = latest_player_stats_df.to_dict('records')[0]
player_predictor_stats = []
for predictor_stat in self.predictor_stats:
# print('processing predictor stat {}'.format(predictor_stat))
player_predictor_stats.append(dict_player[predictor_stat])
latest_predictor_numpy_array = numpy.array([player_predictor_stats])
return latest_predictor_numpy_array
def _get_predictors_in_numpy_arrays(self, processed_player_stats_df):
player_game_records = self._get_predictors(processed_player_stats_df)
game_list = []
y_array_list = []
for player_game_record in player_game_records:
game_predictor_stats = []
if not (numpy.isnan(player_game_record['csum_prev_min_kills'])
or numpy.isnan(player_game_record['csum_prev_min_allowed_kills'])):
if player_game_record['csum_prev_min_assists'] != 0:
prev_predictor_stats = self._convert_predictors_to_prev_csum(self.predictor_stats)
for prev_predictor_stat in prev_predictor_stats:
game_predictor_stats.append(player_game_record[prev_predictor_stat])
game_list.append(game_predictor_stats)
y_array_list.append(player_game_record['y_element'])
predictors = numpy.array(game_list)
y_array = numpy.array([y_array_list])
return predictors, y_array
def _get_predictors(self, processed_player_stats_df):
player_game_records = processed_player_stats_df.to_dict('records')
player_game_records.sort(key=itemgetter('game_id'))
for player_game_record in player_game_records:
player_game_record['y_element'] = player_game_record[self.stat_to_predict]
return player_game_records
def _train_model(self, predictors, y_array):
y_1darray = numpy.squeeze(y_array)
self.poisson = Poisson(y_1darray, predictors)
self.pos_result = self.poisson.fit(method='bfgs')
def _get_game_ids_from_database(self):
game_ids_row = Game.objects.values_list('id', flat=True)
game_ids = [game for game in game_ids_row]
return game_ids
def _get_lastest_processed_team_stats_by_name(self):
return ProcessedTeamStatsDf.objects.filter(name=self.opposing_team_name).order_by('-id').first()
def _get_game_by_ids(self, game_ids):
return Game.objects.filter(id__in=game_ids)
def _get_player_id_by_player_name(self, player_name):
player = Player.objects.filter(name=player_name)
return player[0].id
def _get_processed_player_stats_in_df(self):
game_ids = self._get_game_ids_from_database()
last_game_number = game_ids[-1]
has_processed_team_stats_table = self.engine.has_table(self.processed_player_stars_table_name)
if has_processed_team_stats_table:
df_game_stats = pandas.read_sql(self.player_stats_table_name, self.engine)
df_game_stats_all = df_game_stats[df_game_stats.game_id.isin(game_ids)]
# Using game_numbers here since we need the last few games to check.
max_game_id_cached = df_game_stats_all['game_id'].max()
max_index_cached = df_game_stats_all['index'].max()
if pandas.isnull(max_game_id_cached):
max_game_id_cached = game_ids[0]
# Check if all the game numbers have been cached,
#.........这里部分代码省略.........
示例14: _train_model
def _train_model(self, predictors, y_array):
y_1darray = numpy.squeeze(y_array)
self.poisson = Poisson(y_1darray, predictors)
self.pos_result = self.poisson.fit(method='bfgs')
示例15: ols
from statsmodels.formula.api import ols, glm, poisson
from statsmodels.discrete.discrete_model import Poisson
import statsmodels.stats.tests.test_anova as ttmod
test = ttmod.TestAnova3()
test.setupClass()
data = test.data.drop([0,1,2])
res_ols = ols("np.log(Days+1) ~ C(Duration, Sum)*C(Weight, Sum)", data).fit(use_t=False)
res_glm = glm("np.log(Days+1) ~ C(Duration, Sum)*C(Weight, Sum)",
data).fit()
res_poi = Poisson.from_formula("Days ~ C(Weight) * C(Duration)", data).fit(cov_type='HC0')
res_poi_2 = poisson("Days ~ C(Weight) + C(Duration)", data).fit(cov_type='HC0')
print('\nOLS')
print(res_ols.wald_test_terms())
print('\nGLM')
print(res_glm.wald_test_terms(skip_single=False, combine_terms=['Duration', 'Weight']))
print('\nPoisson 1')
print(res_poi.wald_test_terms(skip_single=False, combine_terms=['Duration', 'Weight']))
print('\nPoisson 2')
print(res_poi_2.wald_test_terms(skip_single=False))
from statsmodels.discrete.discrete_model import NegativeBinomial
res_nb2 = NegativeBinomial.from_formula("Days ~ C(Weight) * C(Duration)", data).fit()
print('\nNegative Binomial nb2')
print(res_nb2.wald_test_terms(skip_single=False))