-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgeneric_mle.py
61 lines (36 loc) · 1.08 KB
/
generic_mle.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
import numpy as np
from scipy import stats
import statsmodels.api as sm
from statsmodels.base.model import GenericLikelihoodModel
# <codecell>
print sm.datasets.spector.NOTE
# <codecell>
data = sm.datasets.spector.load_pandas()
exog = sm.add_constant(data.exog, prepend=True)
endog = data.endog
# <codecell>
sm_probit = sm.Probit(endog, exog).fit()
# <rawcell>
# * To create your own Likelihood Model, you just need to overwrite the loglike method.
# <codecell>
class MyProbit(GenericLikelihoodModel):
def loglike(self, params):
exog = self.exog
endog = self.endog
q = 2 * endog - 1
return stats.norm.logcdf(q*np.dot(exog, params)).sum()
# <codecell>
my_probit = MyProbit(endog, exog).fit()
# <codecell>
print sm_probit.params
# <codecell>
print sm_probit.cov_params()
# <codecell>
print my_probit.params
# <rawcell>
# You can get the variance-covariance of the parameters. Notice that we didn't have to provide Hessian or Score functions.
# <codecell>
print my_probit.cov_params()