60
60
def score(self, params):
62
Score function of model = gradient of logL with respect to
62
Score function of model.
64
The gradient of logL with respect to params.
65
raise NotImplementedError
66
return derivative(self.loglike, params, dx=1e-04, n=1, order=3)
67
68
def information(self, params):
69
70
Fisher information function of model = - Hessian of logL with respect
84
85
# so supplied or default guess?
85
86
def newton(self, params):
86
87
#JP this is not newton, it's fmin
87
# optimize.newton is only for singlevariate?
88
# fmin can take multivariate
89
# probably called newton bc it's the well known
90
# root finding for MLE
88
# it also doesn't work
89
# tried fmin, fmin_ncg, fmin_powell
90
# converges to wrong estimates
91
# will probably need to write own root-finding routine
92
#FIXME: should we call it MLE for Maximum Likelihood Estimator?
91
93
# SS no this isn't used anywhere right now, but needs attention for
93
95
# is this used anywhere
94
96
# results should be attached to self
95
97
f = lambda params: -self.loglike(params)
96
xopt, fopt, iter, funcalls, warnflag =\
97
optimize.fmin(f, params, full_output=True)
98
score = lambda params: -self.score(params)
99
# xopt, fopt, iter, funcalls, warnflag =\
100
# optimize.fmin(f, params, full_output=True)
101
# xopt, fopt, fcalls, gcalls, hcalls, warnflag = \
102
# optimize.fmin_ncg(f, params, score)
98
103
converge = not warnflag
99
104
extras = dict(iter=iter, evaluations=funcalls, converge=converge)
100
105
# return LikelihoodModelResults(self, params, llf=fopt, **extras)
222
227
r_matrix : array-like
223
228
Can be 1d, or 2d. Can be used alone or with other.
229
column : array-like, optional
225
230
Must be used on its own. Can be 0d or 1d see below.
231
scale : float, optional
227
232
Can be specified or not. Default is None, which means that
228
233
the scale argument is taken from the model.
234
other : array-like, optional
230
235
Can be used when r_matrix is specified.
239
244
(scale)*(X.T X)^(-1)
241
246
If contrast is specified it pre and post-multiplies as follows
242
(scale) * contrast (X.T X)^(-1) contrast.T
247
(scale) * r_matrix (X.T X)^(-1) r_matrix.T
244
249
If contrast and other are specified returns
245
(scale) * contrast (X.T X)^(-1) other.T
250
(scale) * r_matrix (X.T X)^(-1) other.T
247
252
If column is specified returns
248
253
(scale) * (X.T X)^(-1)[column,column] if column is 0d
338
346
if self.normalized_cov_params is None:
339
347
raise ValueError, 'Need covariance of parameters for computing \
341
if r_matrix.shape[-1] != self.params.shape[0]:
342
raise ValueError, 'r_matrix and params are not aligned'
349
if r_matrix.ndim == 1:
350
if r_matrix.shape[0] != self.params.shape[0]:
351
raise ValueError, 'r_matrix and params are not aligned'
352
elif r_matrix.ndim >1:
353
if r_matrix.shape[1] != self.params.shape[0]:
354
raise ValueError, 'r_matrix and params are not aligned'
366
378
r_matrix : array-like
367
379
q x p array where q is the number of restrictions to test and
368
380
p is the number of regressors in the full model fit.
369
If q is 1 then f_test is equivalent to the square of t_test.
381
If q is 1 then f_test(r_matrix).fvalue is equivalent to the square of
383
scale : float, optional
371
384
Default is 1.0 for no scaling.
373
Optional, a qxq matrix to specify an inverse covariance
385
invcov : array-like, optional
386
A qxq matrix to specify an inverse covariance
374
387
matrix based on a restrictions matrix.
431
444
def conf_int(self, alpha=.05, cols=None):
433
Returns the confidence interval of the specified params estimates.
446
Returns the confidence interval of the fitted parameters.
437
450
alpha : float, optional
438
451
The `alpha` level for the confidence interval.
439
ie., `alpha` = .05 returns a 95% confidence interval.
440
cols : tuple, optional
452
ie., The default `alpha` = .05 returns a 95% confidence interval.
453
cols : array-like, optional
441
454
`cols` specifies which confidence intervals to return
444
Each item contains [lower, upper]
459
Each row contains [lower, upper] confidence interval
448
>>>import numpy as np
449
>>>from numpy.random import standard_normal as stan
450
>>>import nipy.fixes.scipy.stats.models as SSM
451
>>>x = np.hstack((stan((30,1)),stan((30,1)),stan((30,1))))
452
>>>params=np.array([3.25, 1.5, 7.0])
453
>>>y = np.dot(x,params) + stan((30))
454
>>>model = SSM.regression.OLSModel(x, hascons=False).fit(y)
455
>>>model.conf_int(cols=(1,2))
464
>>>from models.datasets.longley.data import load
466
>>>data.exog = models.tools.add_constant(data.exog)
467
>>>results = models.OLS(data.endog, data.exog).fit()
468
>>>results.conf_int()
469
array([[ -1.77029035e+02, 2.07152780e+02],
470
[ -1.11581102e-01, 3.99427438e-02],
471
[ -3.12506664e+00, -9.15392966e-01],
472
[ -1.51794870e+00, -5.48505034e-01],
473
[ -5.62517214e-01, 4.60309003e-01],
474
[ 7.98787515e+02, 2.85951541e+03],
475
[ -5.49652948e+06, -1.46798779e+06]])
477
>>>results.conf_int(cols=(1,2))
478
array([[-0.1115811 , 0.03994274],
479
[-3.12506664, -0.91539297]])
460
tails : string, optional
461
`tails` can be "two", "upper", or "lower"
483
The confidence interval is based on Student's t distribution for all
484
models except RLM and GLM, which uses the standard normal distribution.
463
486
if self.__class__.__name__ in ['RLMResults','GLMResults']:
473
496
lower = self.params - dist.ppf(1-alpha/2)*self.bse
474
497
upper = self.params + dist.ppf(1-alpha/2)*self.bse
475
498
elif cols is not None and dist == t:
479
lower.append(self.params[i] - dist.ppf(1-\
480
alpha/2,self.df_resid) *self.bse)
481
upper.append(self.params[i] + dist.ppf(1-\
482
alpha/2,self.df_resid) *self.bse)
499
cols = np.asarray(cols)
500
lower = self.params[cols] - dist.ppf(1-\
501
alpha/2,self.df_resid) *self.bse[cols]
502
upper = self.params[cols] + dist.ppf(1-\
503
alpha/2,self.df_resid) *self.bse[cols]
483
504
elif cols is not None and dist == norm:
484
lower = self.params - dist.ppf(1-alpha/2)*self.bse
485
upper = self.params + dist.ppf(1-alpha/2)*self.bse
505
cols = np.asarray(cols)
506
lower = self.params[cols] - dist.ppf(1-alpha/2)*self.bse[cols]
507
upper = self.params[cols] + dist.ppf(1-alpha/2)*self.bse[cols]
486
508
return np.asarray(zip(lower,upper))