~vincent-vincentdavis/statsmodels/sum-stats-devel3

« back to all changes in this revision

Viewing changes to scikits/statsmodels/tsa/mlemodel.py

  • Committer: Vincent Davis
  • Date: 2010-10-17 03:41:15 UTC
  • mfrom: (2020.1.11 statsmodels-devel)
  • Revision ID: vincent@vincentdavis.net-20101017034115-w4ycsu053uy5i706
merged with devel

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Base Classes for Likelihood Models in time series analysis
 
2
 
 
3
Warning: imports numdifftools
 
4
 
 
5
 
 
6
 
 
7
Created on Sun Oct 10 15:00:47 2010
 
8
 
 
9
Author: josef-pktd
 
10
License: BSD
 
11
 
 
12
"""
 
13
 
 
14
import numpy as np
 
15
 
 
16
import numdifftools as ndt
 
17
 
 
18
from scikits.statsmodels.model import LikelihoodModel
 
19
 
 
20
#copied from sandbox/regression/mle.py
 
21
#TODO: I take it this is only a stub and should be included in another
 
22
# model class?
 
23
class TSMLEModel(LikelihoodModel):
 
24
    """
 
25
    univariate time series model for estimation with maximum likelihood
 
26
 
 
27
    Note: This is not working yet
 
28
    """
 
29
 
 
30
    def __init__(self, endog, exog=None):
 
31
        #need to override p,q (nar,nma) correctly
 
32
        super(TSMLEModel, self).__init__(endog, exog)
 
33
        #set default arma(1,1)
 
34
        self.nar = 1
 
35
        self.nma = 1
 
36
        #self.initialize()
 
37
        
 
38
    def geterrors(self, params):
 
39
        raise NotImplementedError
 
40
 
 
41
    def loglike(self, params):
 
42
        """
 
43
        Loglikelihood for timeseries model
 
44
 
 
45
        Notes
 
46
        -----
 
47
        needs to be overwritten by subclass
 
48
        """
 
49
        raise NotImplementedError
 
50
 
 
51
 
 
52
    def score(self, params):
 
53
        """
 
54
        Score vector for Arma model
 
55
        """
 
56
        #return None
 
57
        #print params
 
58
        jac = ndt.Jacobian(self.loglike, stepMax=1e-4)
 
59
        return jac(params)[-1]
 
60
 
 
61
    def hessian(self, params):
 
62
        """
 
63
        Hessian of arma model.  Currently uses numdifftools
 
64
        """
 
65
        #return None
 
66
        Hfun = ndt.Jacobian(self.score, stepMax=1e-4)
 
67
        return Hfun(params)[-1]
 
68
 
 
69
 
 
70
    def fit(self, start_params=None, maxiter=5000, method='fmin', tol=1e-08):
 
71
        '''estimate model by minimizing negative loglikelihood
 
72
        
 
73
        does this need to be overwritten ?
 
74
        '''
 
75
        if start_params is None and hasattr(self, '_start_params'):
 
76
            start_params = self._start_params
 
77
        #start_params = np.concatenate((0.05*np.ones(self.nar + self.nma), [1]))
 
78
        mlefit = super(TSMLEModel, self).fit(start_params=start_params, 
 
79
                maxiter=maxiter, method=method, tol=tol)
 
80
        return mlefit
 
 
b'\\ No newline at end of file'