ó
áp7]c           @  s2  d  Z  d d l m Z d d l m Z d d l Z d d l m Z d d l	 m
 Z
 m Z d d l j Z d d l Z d d l m Z m Z d d l m Z d d	 l m Z d
 d „ Z d
 d e d
 d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z  d e f d „  ƒ  YZ! d e f d „  ƒ  YZ" d d „ Z# d d d  „ Z$ d d d d! „ Z& d" „  Z' d# „  Z( d$ „  Z) d% d& „ Z* e+ d' k r.d( g Z, d) e, k r¨e j- j. ƒ  Z/ e0 d* ƒ d d+ g Z1 d d, g Z2 e/ j3 e1 e2 d- d ƒ Z4 e4 e4 j5 ƒ  8Z4 e" e4 ƒ Z6 d e6 _7 d e6 _8 e6 j9 d. d/ ƒ Z: e0 e: j; ƒ e" e4 ƒ Z< e< j9 d. d0 ƒ Z= e0 e= j; ƒ e0 e= j> j? e= j; ƒ ƒ e0 e j@ e6 jA d1 d2 ƒe= j; ƒ ƒ e/ j9 e4 d d ƒ ZB e0 eB d
 ƒ e0 eB d ƒ e0 d3 ƒ e0 d4 ƒ e0 d+ d, d g ƒ e0 d5 ƒ e0 e: j; ƒ e0 d6 ƒ e0 e= j; ƒ e0 d7 ƒ e/ jC ZD e0 eB d
 e jE e jF eD eD ƒ eD jG d
 ƒ ƒ e6 jH e= j; ƒ ZI e0 d8 ƒ e0 e jF eD eD ƒ eD jG d
 eB d ƒ e0 d9 ƒ e0 e= j> jJ d: d d; … d d; … f ƒ e0 d< ƒ e0 e jK jL e j@ e6 jA d1 d2 ƒe= j; ƒ ƒ d d; … d d; … f ƒ e" e4 d; ƒ ZM eM j9 d. d0 ƒ ZN e0 eN j; ƒ n  d- ZO d( e, k rŠe# eO d d= g d d g d> d, ƒ\ ZI ZP e jQ ƒ  e jR d? ƒ e jS eI ƒ e jR d@ ƒ e jS eP ƒ dA ZT e jU jV dB ƒ ZT e0 dC eT ƒ e jU jT eT ƒ dD ZW e$ eO d eW g d d, g d> dE dF d ƒ\ ZI ZP e* eI eP ƒ e* eI dG eP dG ƒ e jU jT eT ƒ e& eO d eW g d d
 g d, d
 g g d> dE dF d ƒ\ ZX ZY ZZ e* eX eO  eY eO  dH ƒ e* eX dG eO !eY dG eO !dH ƒ e jU jT eT ƒ e& eO d eW g d d
 g d dI g g d> dE dF d ƒ\ Z[ Z\ ZZ e* e[ eO  e\ eO  dJ ƒ e* e[ dG eO !e\ dG eO !dJ ƒ e jU jT eT ƒ e& eO d eW g d d
 g d dI g d dI g d dI g g d> dE dF d ƒ\ Z] Z^ Z_ e* e] eO  e^ eO  dK ƒ e* e] dG eO !e^ dG eO !dK ƒ e jU jT eT ƒ e& eO d eW g d d d
 g d
 d dI g d
 d dI g d
 d dI g g d> dE dF d ƒ\ Z` Za Zb e* e` eO  ea eO  dK ƒ e* e` dG eO !ea dG eO !dK ƒ e jc dL ƒ Zd d ed d
 <e& dL d dM g d d d
 g d
 d dN g d
 dO dP g d
 d2 dQ g g d> dE dF d dR ed ƒ\ Ze Zf Zg e* ee dS  ef dS  dK ƒ n  e jU jV dB ƒ ZT e0 dC eT ƒ e jh dS ƒ ji dT d; ƒ Zj e jk e jl ej jG d
 d f ƒ ej f ƒ Zm e d d
 g e jn dU dV d g dE dE d
 g g ƒ em ƒ \ Zo Zp d- ZO d- Zq e jU jT eT ƒ d dW g Z1 d d
 d
 g d
 dX dE g g Z2 e& eq eO e1 e2 d> dX dF dY ƒ\ Z` Za Zb e` eq ea eq eb eq Z` Za Zb e* e` eO  ea eO  dK ƒ e e` e` j5 ƒ  ƒ Zr d er _7 d er _8 e jn dZ d d[ dE g ƒ er _s er j9 d\ e jn dZ d d[ dE g ƒ d] d- ƒ Zt e0 d^ et j; ƒ e* er ju er jP ƒ e0 d_ ƒ e0 e
 jv d` „  da dI d[ g ƒ ƒ e e` e` j5 ƒ  ƒ Zw d ew _7 d er _8 e jn dZ d[ d g ƒ Zx ex ew _s ew j9 d\ ex d] db ƒ Zy e0 dc ey j; ƒ e e` e` j5 ƒ  ƒ Zw d ew _7 d er _8 e jn dZ d[ d g ƒ Zx ex ew _s ew j9 d\ ex d. d0 d] db ƒ Zy e0 dc ey j; ƒ dd e, k ryd de lz m{ Z{ e{ j| df ƒ Z} e{ j~ dg dh ƒ Zj e0 di e j e j€ ej d; ƒ ƒ dj  ƒ e" e j€ ej d; ƒ ƒ ZM eM j9 d\ dk d d, g d] dl ƒ Z e0 e j; ƒ e" e j€ ej d; ƒ ƒ Z‚ e‚ j9 d\ dk d d, g d] dl d. d0 ƒ Zƒ e0 eƒ j; ƒ n  e' da dI d[ g e` ƒ Z„ e0 e„ d
 ƒ e& dS e1 e2 d> dm dF d2 dR e jl dS ƒ ƒ\ Z… Z† Z‡ e jU jˆ dS ƒ Zo da dI d[ g Z; e' e; eo ƒ \ Z‰ ZŠ Z‹ e‹ ZŒ d
 Z eŽ e‹ ƒ ZO dn e j e j eŒ ƒ e ƒ e j eo d; eŒ e ƒ eO e j d; e j‘ ƒ Z’ e0 e‰ e’ ƒ e jU jT d ƒ e jc do ƒ Z“ xD e” d do ƒ D]3 Z• e jU jˆ ƒ  Z– dI e“ e• d d2 e– e“ e• <q±We“ dL Z“ e! e“ ƒ Z— e— j9 d. dp dq e˜ ƒ e0 dr ƒ e0 e— j; ƒ n  d S(s   sQ	  general non-linear MLE for time series analysis

idea for general version
------------------------

subclass defines geterrors(parameters) besides loglike,...
and covariance matrix of parameter estimates (e.g. from hessian
or outerproduct of jacobian)
update: I don't really need geterrors directly, but get_h the conditional
    variance process

new version Garch0 looks ok, time to clean up and test
no constraints yet
in some cases: "Warning: Maximum number of function evaluations has been exceeded."

Notes
-----

idea: cache intermediate design matrix for geterrors so it doesn't need
    to be build at each function call

superclass or result class calculates result statistic based
on errors, loglike, jacobian and cov/hessian
  -> aic, bic, ...
  -> test statistics, tvalue, fvalue, ...
  -> new to add: distribution (mean, cov) of non-linear transformation
  -> parameter restrictions or transformation with corrected covparams (?)
  -> sse, rss, rsquared  ??? are they defined from this in general
  -> robust parameter cov ???
  -> additional residual based tests, NW, ... likelihood ratio, lagrange
     multiplier tests ???

how much can be reused from linear model result classes where
   `errorsest = y - X*beta` ?

for tsa: what's the division of labor between model, result instance
    and process

examples:
 * arma: ls and mle look good
 * arimax: add exog, especially mean, trend, prefilter, e.g. (1-L)
 * arma_t: arma with t distributed errors (just a change in loglike)
 * garch: need loglike and (recursive) errorest
 * regime switching model without unobserved state, e.g. threshold


roadmap for garch:
 * simple case
 * starting values: garch11 explicit formulas
 * arma-garch, assumed separable, blockdiagonal Hessian
 * empirical example: DJI, S&P500, MSFT, ???
 * other standard garch: egarch, pgarch,
 * non-normal distributions
 * other methods: forecast, news impact curves (impulse response)
 * analytical gradient, Hessian for basic garch
 * cleaner simulation of garch
 * result statistics, AIC, ...
 * parameter constraints
 * try penalization for higher lags
 * other garch: regime-switching

for pgarch (power garch) need transformation of etax given
   the parameters, but then misofilter should work
   general class aparch (see garch glossary)

References
----------

see notes_references.txt


Created on Feb 6, 2010
@author: "josef pktd"
iÿÿÿÿ(   t   print_function(   t   zipN(   t   assert_almost_equal(   t   optimizet   signal(   t   Modelt   LikelihoodModelResults(   t   miso_lfilter(   t   tsai    c         C  s   t  j |  d d | ƒS(   s<   Helper function to calculate sum of squares along first axisi   t   axis(   t   npt   sum(   t   xR	   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   sumofsq[   s    i   c         C  s  t  j |  ƒ }  t  j |  ƒ }  | d  k r9 |  j ƒ  }  n  |  j d k rX |  j | } n t |  ƒ } |  | }  | rÃ d t  j d t  j	 ƒ t  j | ƒ |  d | } t  j
 | | ƒ } | | f Sd t  j
 t  j | ƒ | ƒ t  j
 |  d | | ƒ | t  j d t  j	 ƒ } | Sd  S(   Ni   g      à¿i   (   R
   t   asarrayt
   atleast_1dt   Nonet   ravelt   ndimt   shapet   lent   logt   piR   (   R   t   mut   sigma2t	   returnllsR	   t   nobst   llst   LL(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   normloglike`   s    
3
Ot   LikelihoodModelc           B  s\   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d d d	 d
 „ Z
 RS(   s2   
    Likelihood model is a subclass of Model.
    c         C  s'   t  t |  ƒ j | | ƒ |  j ƒ  d  S(   N(   t   superR   t   __init__t
   initialize(   t   selft   endogt   exog(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR       s    c         C  s   d S(   s¸   
        Initialize (possibly re-initialize) a Model instance. For
        instance, the design matrix of a linear model may change
        and some things must be recomputed.
        N(    (   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR!   ƒ   s    c         C  s
   t  ‚ d S(   s*   
        Log-likelihood of model.
        N(   t   NotImplementedError(   R"   t   params(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   loglike   s    c         C  s
   t  ‚ d S(   sf   
        Score vector of model.

        The gradient of logL with respect to each parameter.
        N(   R%   (   R"   R&   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   score“   s    c         C  s
   t  ‚ d S(   sn   
        Fisher information matrix of model

        Returns -Hessian of loglike evaluated at params.
        N(   R%   (   R"   R&   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   information›   s    c         C  s
   t  ‚ d S(   s1   
        The Hessian matrix of the model
        N(   R%   (   R"   R&   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   hessian£   s    t   newtoni#   g:Œ0âŽyE>c           sØ  d d d d d d g } | d k r> d g ˆ  j j d } n  | | k r] t d	 | ƒ ‚ n  ‡  f d
 †  } ‡  f d †  } d } | j ƒ  d k rld }	 t j | ƒ }
 t j |
 g } x— |	 | k  rPt j t j	 | d | d ƒ | k ƒ rPˆ  j
 | d ƒ } | d t j t j j | ƒ ˆ  j | d ƒ ƒ } | j | ƒ |	 d 7}	 qº Wt ˆ  | ƒ } |	 | _ n_| d k rd } t j | | | d d d | d | ƒ\ } } } } } } } | } t ˆ  | ƒ } d } t t | j d ƒ | | | | | | | g ƒ ƒ ˆ  _ n¼ | d k rst j | | | d | d d d | d | ƒ\ } } } } } } t ˆ  | ƒ } | } nX | d k rËt j | | d d d | d | ƒ\ } } } } } t ˆ  | ƒ } | } n  | ˆ  _ | S(   sK  
        Fit method for likelihood based models

        Parameters
        ----------
        start_params : array-like, optional
            An optional

        method : str
            Method can be 'newton', 'bfgs', 'powell', 'cg', or 'ncg'.
            The default is newton.  See scipy.optimze for more information.
        R+   t   bfgst   powellt   cgt   ncgt   fmini    i   s   Unknown fit method %sc           s   ˆ  j  |  ƒ S(   N(   R'   (   R&   (   R"   (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   <lambda>»   t    c           s   ˆ  j  |  ƒ S(   N(   R(   (   R&   (   R"   (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR1   ¼   R2   iÿÿÿÿiþÿÿÿt   full_outputt   maxitert   gtols8   xopt, fopt, gopt, Hopt, func_calls, grad_calls, warnflags   , t   fhesst   avextolt   xtolN(   R   R$   R   t
   ValueErrort   lowerR
   t   arrayt   inft   allt   absR*   t   dott   linalgt   invR(   t   appendR   t	   iterationR   t	   fmin_bfgst   dictR   t   splitt   optimresultst   fmin_ncgR0   t   _results(   R"   t   start_paramst   methodR4   t   tolt   methodst   fR(   t   hessRC   t   startt   historyt   Ht	   newparamst   mlefitt   xoptt   foptt   goptt   Hoptt
   func_callst
   grad_callst   warnflagt   converget   optrest   fcallst   gcallst   hcallst   nitert   funcalls(    (   R"   s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   fit©   sT    "$''
$
	N(   t   __name__t
   __module__t   __doc__R   R    R!   R'   R(   R)   R*   Rc   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR   z   s   	
				t
   TSMLEModelc           B  sS   e  Z d  Z d
 d „ Z d „  Z d „  Z d „  Z d „  Z d
 d d d d	 „ Z	 RS(   sp   
    univariate time series model for estimation with maximum likelihood

    Note: This is not working yet
    c         C  s/   t  t |  ƒ j | | ƒ d |  _ d |  _ d  S(   Ni   (   R   Rg   R    t   nart   nma(   R"   R#   R$   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR    ó   s    	c         C  s
   t  ‚ d  S(   N(   R%   (   R"   R&   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt	   geterrorsû   s    c         C  s
   t  ‚ d S(   s}   
        Loglikelihood for timeseries model

        Notes
        -----
        needs to be overwritten by subclass
        N(   R%   (   R"   R&   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR'   þ   s    c         C  s&   t  j |  j d d ƒ} | | ƒ d S(   s-   
        Score vector for Arma model
        t   stepMaxg-Cëâ6?iÿÿÿÿ(   t   ndtt   JacobianR'   (   R"   R&   t   jac(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR(   	  s    c         C  s&   t  j |  j d d ƒ} | | ƒ d S(   sE   
        Hessian of arma model.  Currently uses numdifftools
        Rk   g-Cëâ6?iÿÿÿÿ(   Rl   Rm   R(   (   R"   R&   t   Hfun(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR*     s    iˆ  R0   g:Œ0âŽyE>c      	   C  sX   | d k r' t |  d ƒ r' |  j } n  t t |  ƒ j d | d | d | d | ƒ } | S(   sh   estimate model by minimizing negative loglikelihood

        does this need to be overwritten ?
        t   _start_paramsRJ   R4   RK   RL   N(   R   t   hasattrRp   R   Rg   Rc   (   R"   RJ   R4   RK   RL   RT   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRc     s
    N(
   Rd   Re   Rf   R   R    Rj   R'   R(   R*   Rc   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRg   ì   s   						t   Garch0c           B  s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s@  Garch model,

    still experimentation stage:
    simplified structure, plain garch, no constraints
    still looking for the design of the base class

    serious bug:
    ar estimate looks ok, ma estimate awful
    -> check parameterization of lagpolys and constant
    looks ok after adding missing constant
        but still difference to garch11 function
    corrected initial condition
    -> only small differences left between the 3 versions
    ar estimate is close to true/DGP model
    note constant has different parameterization
    but design looks better

    c         C  sW   t  t |  ƒ j | | ƒ d |  _ d |  _ | d |  _ t j |  j j ƒ  ƒ |  _	 d  S(   Ni   i   (
   R   Rr   R    Rh   Ri   t   _etaxR
   R   t   meant   _icetax(   R"   R#   R$   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR    :  s
    		c         C  s   d  S(   N(    (   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR!   E  s    c   
      C  sj   | \ } } } |  j  | } |  j } | j d } t j | | | ƒ } t j | | | d | ƒd }	 |	 S(   sä   

        Parameters
        ----------
        params : tuple, (ar, ma)
            try to keep the params conversion in loglike

        copied from generate_gjrgarch
        needs to be extracted to separate function
        i    t   zi(   Rs   Ru   R   R   t   lfiltict   lfilter(
   R"   R&   t   art   maR   t   etaxt   icetaxR   Rv   t   h(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   gethH  s    	c         C  s  |  j  |  j } } t j d g | |  f ƒ } t j d g | | | | !f ƒ } | d } | | | f } |  j | ƒ } | |  _ | |  _ t j | d ƒ } d }	 t | ƒ }
 d }	 d t j	 t j
 | ƒ |	 ƒ t j	 |  j d | |	 ƒ |
 t j
 d t j ƒ } | S(   sY  
        Loglikelihood for timeseries model

        Notes
        -----
        needs to be overwritten by subclass

        make more generic with using function _convertparams
        which could also include parameter transformation
        _convertparams_in, _convertparams_out

        allow for different distributions t, ged,...
        i   i    iÿÿÿÿgíµ ÷Æ°>g      à¿i   (   Rh   Ri   R
   t   concatenateR~   t   params_convertedR}   t   maximumR   R   R   R#   R   (   R"   R&   t   pt   qRy   Rz   R   R}   R   R	   R   t   llike(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR'   g  s    #
		ON(   Rd   Re   Rf   R   R    R!   R~   R'   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRr   '  s
   		t   GarchXc           B  s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sø  Garch model,

    still experimentation stage:
    another version, this time with exog and miso_filter
    still looking for the design of the base class

    not done yet, just a design idea
    * use misofilter as in garch (gjr)
    * but take etax = exog
      this can include constant, asymetric effect (gjr) and
      other explanatory variables (e.g. high-low spread)

    todo: renames
    eta -> varprocess
    etax -> varprocessx
    icetax -> varprocessic (is actually ic of eta/sigma^2)
    c         C  su   t  t |  ƒ j | | ƒ d |  _ d |  _ t j t j t d f ƒ | d | ƒ |  _	 t j
 |  j	 j ƒ  ƒ |  _ d  S(   Ni   i   (   R   Rr   R    Rh   Ri   R
   t   column_stackt   onesR   Rs   R   Rt   Ru   (   R"   R#   R$   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR    ¨  s
    		+c         C  s   d  S(   N(    (   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR!   ·  s    c         C  s   d  S(   N(    (   Ry   Rz   R   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   convert_mod2paramsº  s    c   
      C  s{   | \ } } } |  j  | } |  j } |  j } t | | | d |  j ƒd } | d k  }	 |	 j ƒ  rw t j | ƒ } n  | S(   sä   

        Parameters
        ----------
        params : tuple, (ar, ma)
            try to keep the params conversion in loglike

        copied from generate_gjrgarch
        needs to be extracted to separate function
        t   useici    (   Rs   Ru   R   R   t   anyR
   R>   (
   R"   R&   Ry   Rz   R   R{   R|   R   R}   t   hneg(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR~   ½  s    		c         C  s  |  j  |  j } } t j d g | |  f ƒ } t j d g | | | | !f ƒ } | d } | | | f } |  j | ƒ } | |  _ | |  _ t j | d ƒ } d }	 t | ƒ }
 d }	 d t j	 t j
 | ƒ |	 ƒ t j	 |  j d | |	 ƒ |
 t j
 d t j ƒ } | S(   sY  
        Loglikelihood for timeseries model

        Notes
        -----
        needs to be overwritten by subclass

        make more generic with using function _convertparams
        which could also include parameter transformation
        _convertparams_in, _convertparams_out

        allow for different distributions t, ged,...
        i   i    iÿÿÿÿgíµ ÷Æ°>g      à¿i   (   Rh   Ri   R
   R   R~   R€   R}   R   R   R   R   R#   R   (   R"   R&   R‚   Rƒ   Ry   Rz   R   R}   R   R	   R   R„   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR'   å  s    #
		ON(	   Rd   Re   Rf   R   R    R!   Rˆ   R~   R'   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR…   –  s   			(t   Garchc           B  s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   sO   Garch model gjrgarch (t-garch)

    still experimentation stage, try with

    c         C  s/   t  t |  ƒ j | | ƒ d |  _ d |  _ d  S(   Ni   (   R   RŒ   R    Rh   Ri   (   R"   R#   R$   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR      s    	c         C  s   d  S(   N(    (   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR!   #  s    c   
   	   C  s  | \ } } |  j  } | j d } t j | d f ƒ } d | d d … d f <| d d d … d f | d d … d d … f <d | | d k d f <t | | | d t j | d d … d f j ƒ  ƒ ƒd } | d k  } | j ƒ  rø t j	 | ƒ } n  t j
 | ƒ | }	 |	 | | f S(   sè   

        Parameters
        ----------
        params : tuple, (mu, ar, ma)
            try to keep the params conversion in loglike

        copied from generate_gjrgarch
        needs to be extracted to separate function
        i    i   i   Ni   R‰   (   R#   R   R
   t   emptyR   R   R   Rt   RŠ   R>   t   sqrt(
   R"   R&   Ry   Rz   t   etaR   R{   R}   R‹   t   err(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRj   &  s    	0;c         C  s¤  |  j  |  j } } t j d g | |  f ƒ } t j | d d f ƒ } | d | d	 <t j d g | | | | !f ƒ | d d … d f <t j d g | | | | d | !f ƒ | d d … d f <| d } | | f } |  j | ƒ \ } } }	 | |  _ | | |	 |  _ |  _ |  _	 t j
 | d ƒ }
 d } t | ƒ } d } | j ƒ  } d t j t j |
 ƒ | ƒ t j |  j d |
 | ƒ | t j d t j ƒ } | S(
   s}   
        Loglikelihood for timeseries model

        Notes
        -----
        needs to be overwritten by subclass
        i   i   iÿÿÿÿi    Ni   gíµ ÷Æ°>g      à¿(   i    i    (   Rh   Ri   R
   R   t   zerosRj   R€   t	   errorsestR}   R{   R   R   Rt   R   R   R#   R   (   R"   R&   R‚   Rƒ   Ry   Rz   R   R’   R}   R{   R   R	   R   t   muyR„   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR'   J  s&    3;
		ON(   Rd   Re   Rf   R   R    R!   Rj   R'   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRŒ     s
   		$c   
      C  sØ   | | } } t  j d g | |  f ƒ } t  j | d d f ƒ } | d | d <t  j d g | | | | !f ƒ | d d … d f <t  j d g | | | | d | !f ƒ | d d … d f <| d } | | f }	 t S(   sU   
    flat to matrix

    Notes
    -----
    needs to be overwritten by subclass
    i   i   iÿÿÿÿi    Ni   (   i    i    (   R
   R   R‘   t   paramsclass(
   R"   R&   Rh   Ri   R‚   Rƒ   Ry   Rz   R   t   params2(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   gjrconvertparams  s    3;
t   ARc           B  sb   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d d	 d
 e
 d „ Z RS(   s   
    Notes
    -----
    This is not general, only written for the AR(1) case.

    Fit methods that use super and broyden do not yet work.
    i   c         C  sP   | d  k r | |  } n  | | } t t |  ƒ j | | ƒ |  j | 7_ d  S(   N(   R   R   R—   R    R   (   R"   R#   R$   t   nlags(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR    ¦  s
    
c         C  s   d  S(   N(    (   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR!   ¯  s    c         C  so  |  j  } |  j } |  j } |  j } t | t ƒ rE t j | ƒ } n  t } t j	 t j
 | ƒ d k  ƒ r‘ | r‘ | } t j d g ƒ } t } n  t | t j | | ƒ ƒ } d | | | d d d | d }	 | d t j d t j ƒ | d t j |	 ƒ d t j d | d ƒ d | |	 | d d d | d d |	 }
 | rk|
 d | d d 8}
 n  |
 S(   s‚   
        The unconditional loglikelihood of an AR(p) process

        Notes
        -----
        Contains constant term.
        i   g§èH.ÿï?i    i   g      à?iè  (   R   R#   R$   t   penaltyt
   isinstancet   tupleR
   R   t   FalseR=   R>   R;   t   TrueR   R?   R   R   (   R"   R&   R   t   yt   ylagR™   t
   usepenaltyt	   oldparamst	   diffsumsqR   R'   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR'   ²  s$    				%	&V#c   
      C  s„  |  j  } |  j } |  j } t | t j | | ƒ ƒ } d | d t j | | t j | | ƒ d d … d f ƒ d | | d d } d | | | d d d | d } | d | | | d | d d | t j | | t j | | ƒ d d … d f ƒ d | d | | | d d | | | d d d | d d | d | } |  j rhn  t	 j
 |  j ƒ }	 |	 | ƒ S(   s¾   
        Notes
        -----
        Need to generalize for AR(p) and for a constant.
        Not correct yet.  Returns numerical gradient.  Depends on package
        numdifftools.
        i   iþÿÿÿNi   i    g      à?(   R#   R$   R   R   R
   R?   R   R   R™   Rl   Rm   R'   (
   R"   R&   Rž   RŸ   R   R¢   t   dsdrR   t   gradientt   j(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR(   Ñ  s    			<&+	c         C  s   d S(   s%   
        Not Implemented Yet
        N(    (   R"   R&   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR)   ì  s    c         C  s   t  j |  j ƒ } | | ƒ S(   sN   
        Returns numerical hessian for now.  Depends on numdifftools.
        (   Rl   t   HessianR'   (   R"   R&   R}   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR*   ò  s    R,   i#   g:Œ0âŽyE>c   	   	     s:  | ˆ  _  | j ƒ  } ‡  f d †  } | d k r^ t t ˆ  ƒ j d | d | d | d | ƒ nØ d g } | d k rˆ t j d g ƒ } n  | d k rÎ t j	 | | d t
 d | ƒ} | d d !\ ˆ  _ ˆ  _ n  | d k rt j | | d t
 d | ƒ} | d ˆ  _ n  | d k r6t j | | ƒ } | d ˆ  _ n  d S(   sÚ  
        Fit the unconditional maximum likelihood of an AR(p) process.

        Parameters
        ----------
        start_params : array-like, optional
            A first guess on the parameters.  Defaults is a vector of zeros.
        method : str, optional
            Unconstrained solvers:
                Default is 'bfgs', 'newton' (newton-raphson), 'ncg'
                (Note that previous 3 are not recommended at the moment.)
                and 'powell'
            Constrained solvers:
                'bfgs-b', 'tnc'
            See notes.
        maxiter : int, optional
            The maximum number of function evaluations. Default is 35.
        tol = float
            The convergence tolerance.  Default is 1e-08.
        penalty : bool
            Whether or not to use a penalty function.  Default is False,
            though this is ignored at the moment and the penalty is always
            used if appropriate.  See notes.

        Notes
        -----
        The unconstrained solvers use a quadratic penalty (regardless if
        penalty kwd is True or False) in order to ensure that the solution
        stays within (-1,1).  The constrained solvers default to using a bound
        of (-.999,.999).
        c           s   ˆ  j  |  ƒ S(   N(   R'   (   R&   (   R"   (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR1   "  R2   R+   R,   R/   RJ   RK   R4   RL   g+‡ÙÎ÷ï¿g+‡ÙÎ÷ï?i    s   bfgs-bt   approx_gradt   boundsi   t   tncR-   N(   R+   R,   R/   (   g+‡ÙÎ÷ï¿g+‡ÙÎ÷ï?(   R™   R:   R   R—   Rc   R   R
   R;   R   t   fmin_l_bfgs_bR   R&   t   llft   fmin_tnct   fmin_powell(	   R"   RJ   RK   R4   RL   R™   t   minfuncR¨   t   retval(    (   R"   s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRc   ú  s(    !		N(   Rd   Re   Rf   R   R    R!   R'   R(   R)   R*   Rœ   Rc   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR—   ž  s   						t   Armac           B  s\   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d d d	 d
 „ Z
 RS(   sˆ   
    univariate Autoregressive Moving Average model

    Note: This is not working yet, or does it
    this can subclass TSMLEModel
    c         C  s/   t  t |  ƒ j | | ƒ d |  _ d |  _ d  S(   Ni   (   R   R°   R    Rh   Ri   (   R"   R#   R$   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR    E  s    	c         C  s   d  S(   N(    (   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR!   M  s    c         C  sn   |  j  |  j } } t j d g | |  f ƒ } t j d g | | | | !f ƒ } t j | | |  j ƒ } | S(   Ni   (   Rh   Ri   R
   R   R   Rx   R#   (   R"   R&   R‚   Rƒ   t   rhoyt   rhoeR’   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRj   P  s
    #c         C  s†   |  j  | ƒ } t j | d d d ƒ } d } t | ƒ } d | t j | ƒ t j | d | | ƒ | t j d t j ƒ } | S(   sª   
        Loglikelihood for arma model

        Notes
        -----
        The ancillary parameter is assumed to be the last element of
        the params vector
        iÿÿÿÿi   gíµ ÷Æ°>i    g      à¿(   Rj   R
   R   R   R   R   R   (   R"   R&   R’   R   R	   R   R„   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR'   X  s    Dc         C  s&   t  j |  j d d ƒ} | | ƒ d S(   s-   
        Score vector for Arma model
        Rk   g-Cëâ6?iÿÿÿÿ(   Rl   Rm   R'   (   R"   R&   Rn   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR(   u  s    c         C  s&   t  j |  j d d ƒ} | | ƒ d S(   sE   
        Hessian of arma model.  Currently uses numdifftools
        Rk   g-Cëâ6?iÿÿÿÿ(   Rl   Rm   R(   (   R"   R&   Ro   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR*   €  s    iˆ  R0   g:Œ0âŽyE>c      	   C  so   | d  k r> t j d t j |  j |  j ƒ d g f ƒ } n  t t |  ƒ j d | d | d | d | ƒ } | S(   Ngš™™™™™©?i   RJ   R4   RK   RL   (	   R   R
   R   R‡   Rh   Ri   R   R°   Rc   (   R"   RJ   R4   RK   RL   RT   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyRc   ‰  s
    2N(   Rd   Re   Rf   R   R    R!   Rj   R'   R(   R*   Rc   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR°   =  s   						g      ð?c         C  sk   d d l  m } | | | |  d ƒ } | | d } t j | ƒ } t j | ƒ t j j |  ƒ } | | f S(   sr   simulate garch like process but not squared errors in arma
    used for initial trial but produces nice graph
    iÿÿÿÿ(   t   arma_generate_samplegš™™™™™¹?i   (   t   statsmodels.tsa.arima_processR³   R
   t   expRŽ   t   randomt   randn(   R   Ry   Rz   R   R³   R}   R   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   generate_kindofgarch  s    gš™™™™™¹?c         C  sL   | t  j j |  ƒ } t j | | | d ƒ } t  j | ƒ | } | | f S(   sv   simulate standard garch

    scale : float
       scale/standard deviation of innovation process in GARCH process
    i   (   R
   R¶   R·   R   Rx   RŽ   (   R   Ry   Rz   R   t   scaleR   R}   R   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   generate_garch¨  s    c   
      C  sÜ   | d k r% | t j j |  ƒ } n | } t j |  d f ƒ } | | d d … d f <| d d d … d f | d d … d d … f <d | | d k d f <t | | | ƒ d } t j | t | ƒ  ƒ | }	 |	 | | f S(   s¿  simulate gjr garch process

    Parameters
    ----------
    ar : array_like, 1d
        autoregressive term for variance
    ma : array_like, 2d
        moving average term for variance, with coefficients for negative
        shocks in second column
    mu : float
        constant in variance law of motion
    scale : float
       scale/standard deviation of innovation process in GARCH process

    Returns
    -------
    err : array 1d, (nobs+?,)
        simulated gjr-garch process,
    h : array 1d, (nobs+?,)
        simulated variance
    etax : array 1d, (nobs+?,)
        data matrix for constant and ma terms in variance equation

    Notes
    -----

    References
    ----------



    i   Ni    i   i   (   R   R
   R¶   R·   R   R   RŽ   R   (
   R   Ry   Rz   R   R¹   t   varinnovationR   R{   R}   R   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   generate_gjrgarch½  s    "0c         C  s÷   |  d } |  d } |  d } | d } | j  d } t j | ƒ } | j ƒ  | d <xA t d | ƒ D]0 } | | | | d | | | d | | <qd Wt j | ƒ }	 | |	 }
 d t j d t j ƒ t j |	 ƒ d |
 d } | j ƒ  | | f S(   Ni    i   i   g      à¿g      à?(	   R   R
   R‘   Rt   t   rangeRŽ   R   R   R   (   R&   Rž   t   wt   alphat   betat   y2R   t   htt   it   sqrthtR   t   llvalues(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   loglike_GARCH11ó  s    



.
3c          C  sŽ  t  j d ƒ j d d ƒ }  t d d g d d g d d g g |  ƒ \ } } t | d  |  j d ƒ j ƒ  d d	 ƒt j t  j d ƒ t  j	 d ƒ ƒ d d  d … } t | d  | d d	 ƒt j t  j d ƒ t  j	 d
 ƒ ƒ d d  d … } t d d g d d g d d g g |  ƒ \ } } t | | j ƒ  d d	 ƒt | | d d	 ƒt d d g d d g d d g g |  ƒ \ } } t | | d d	 ƒt | | d d	 ƒt  j
 t  j	 |  j d d f ƒ |  f ƒ } t d d g t  j d d d g d d d g g ƒ | ƒ \ } } | t  j d d d g ƒ j d ƒ } t | d  | d d	 ƒt | | d d	 ƒ| j ƒ  } | d c | d  d … d f 7)t d d g t  j d d d g d d d g g ƒ | ƒ \ } } t | d  | d d	 ƒt | | d d	 ƒ| j ƒ  } | d c | d  d … d f 7)t d d g t  j d d d g d d d g g ƒ | ƒ \ } } t | d  | d d	 ƒt | | d d	 ƒt d d g t  j d d d g d d d g g ƒ | ƒ \ } } t | d  | j ƒ  d d	 ƒ| j ƒ  } | d c | d  d … d f 7)t d d g t  j d d d g d d d g g ƒ | ƒ \ } } t | d  | d d	 ƒt | | d d	 ƒt d d g t  j d d d g d d d g g ƒ | ƒ \ } } t | d  | j ƒ  d d	 ƒt d d g d d g d d g d d g g |  ƒ \ } } t  j |  d  d  … d f d d d g ƒ } t | | d d	 ƒt | | d d	 ƒt d d g d d g d d g d d g g |  ƒ \ } } t  j |  d  d  … d f d d d g ƒ } t | | d d	 ƒt | | d d	 ƒt d d g d d g d d g d d g g |  ƒ \ } } t  j |  d  d  … d f d d d g ƒ } | d c |  d  d  … d f 7)t | | d d	 ƒt | | d d	 ƒd  S(   Ni   i
   i   g      ð?iÿÿÿÿi   i    t   decimali   i   g       Ài   g        iþÿÿÿ(   R
   t   aranget   reshapeR   R   R   t   cumsumR   t   convolveR‡   R†   R   R;   t   copy(   R   Rž   t   inpt   inp2t   x3t   y3t   y4t   yt(    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   test_misofilter
  sb    0&1100+?% ? ?? ??9+9+9+ c       
   C  sÆ  t  j d ƒ }  d |  d <t d d d g d d d g d d d g d d d g d d	 d
 g g d d d d d |  ƒ\ } } } t  j d d d d	 d d g ƒ } t | d  | d d ƒt d d d g d d d g d d d g d d d g d d	 d
 g g d d d d d |  ƒ\ } } } t | d  | j ƒ  d d ƒt d d d g d d d g d d d g d d d g d d	 d
 g g d d d d d |  ƒ\ } } } d g } x# | D] } | j | | d ƒ q§Wt | d  | d d d ƒt  j d ƒ }  d |  d <t d d d g d d d g d d d g d d d g d d	 d
 g g d d d d d |  ƒ\ } } } t  j d d d d d d g ƒ } t | d  | d d ƒt d d d g d d d g d d d g d d d g d d	 d
 g g d d d d d |  ƒ\ } } } t | d  | j ƒ  d d ƒt d d d g d d d g d d d g d d d g d d	 d
 g g d d d d d |  ƒ\ } } } d g } x# | D] } | j | | d ƒ qˆWt | d  | d d d ƒd  S(   Nid   g      ð?i    i   gš™™™™™¹?gš™™™™™é?gš™™™™™©?gffffffæ?g{®Gáz„?g333333ã?R   g        R¹   R»   i   RÇ   i   g      ð¿iÿÿÿÿgÍÌÌÌÌÌì?g      è?g…ëQ¸…ã?(   R
   R‘   R¼   R;   R   RÊ   RB   (   t   varinnot   errgjr5t   hgjr5t   etax5RÂ   t   ht1R}   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   test_gjrgarchA  sH    
6!66	 
6!66	 s   Garch simulationc         C  s”   t  j ƒ  t  j d ƒ t  j |  ƒ t  j | ƒ t  j d ƒ t  j d ƒ t  j |  d ƒ t  j d ƒ t  j d ƒ t  j | ƒ t  j d ƒ d  S(   Ni7  Rž   i8  i   s   $y^2$i9  s   conditional variance(   t   pltt   figuret   subplott   plott   titlet   ylabel(   R   R}   RÞ   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt	   garchplott  s    
t   __main__t   garcht   armas
   
Example 1gš™™™™™é¿g      à?iè  RK   R0   R,   Rk   g{®Gáz„?s   
parameter estimates*   parameter of DGP ar(1), ma(1), sigma_errors   mle with fmins   mle with bfgss(   cond. least squares uses optim.leastsq ?s    cond least squares parameter covs   bfgs hessianRX   i   s   numdifftools inverse hessiangffffffî¿R   iÓ   iÔ   iÖ¢: i–˜ t   seedgÍÌÌÌÌÌì¿g        R¹   ipþÿÿs%   GJR-GARCH(1,1) Simulation - symmetricgÍÌÌÌÌÌì?s   GJR-GARCH(1,1) Simulations   GJR-GARCH(1,3) Simulationid   g       €gš™™™™™é?gš™™™™™©?gffffffæ?g333333ã?R»   i   i
   g       Ài   gffffffæ¿gš™™™™™Ù?g)\Âõ(ð?g333333ã¿gš™™™™™É?RJ   R4   s   ggres.paramst   Garch11c         C  s   t  |  t t j ƒ  ƒ d S(   Ni    (   RÆ   t   errgjr4Rt   (   R&   (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyR1     R2   gÃõ(\Âí?iÐ  s   ggres0.paramst   rpy(   t   rs   ~garch(1, 1)t   niô  s   R acfi   gš™™™™™É¿iˆ  g{®Gáz¤?g      à¿iÈ   R-   R™   s5   Unconditional MLE for AR(1) y_t = .9*y_t-1 +.01 * err(™   Rf   t
   __future__R    t   statsmodels.compat.pythonR   t   numpyR
   t   numpy.testingR   t   scipyR   R   t   matplotlib.pyplott   pyplotRÚ   t   numdifftoolsRl   t   statsmodels.base.modelR   R   t#   statsmodels.tsa.filters.filtertoolsR   t   statsmodels.sandboxR   R   Rœ   R   R   Rg   Rr   R…   RŒ   R–   R—   R°   R¸   Rº   R   R¼   RÆ   RÓ   RÙ   Rà   Rd   t   examplest   arimat   ARIMAt   arestt   printRy   Rz   t   generate_samplet   y1Rt   t   arma1Rh   Ri   Rc   t   arma1resR&   t   arma2t   res2t   modelR*   R¦   R'   t   reslst   error_estimatet   errlsRŽ   R?   R   Rj   R   RG   R@   RA   t   arma3t   res3R   R}   RÛ   RÜ   RÝ   Rä   R¶   t   randintt   ar1t   errgjrt   hgjrR{   t   errgjr2t   hgjr2t   errgjr3t   hgjr3t   etax3Ræ   t   hgjr4t   etax4R‘   RÔ   RÕ   RÖ   R×   RÈ   RÉ   R   R†   R‡   RÏ   R;   Rž   RÍ   t   warmupt   ggmodRp   t   ggresR’   R0   t   ggmod0RJ   t   ggres0Rç   Rè   t   formulaRN   t   garchSimt   acft   powert   arma3rest   arma3bt	   arma3bresR«   t   errot   hot   etaxoR·   R   t   lltRÂ   R   R	   R   R   R   R   R„   t   tseriesR½   RÃ   t   errort   armodelR   (    (    (    s<   lib/python2.7/site-packages/statsmodels/sandbox/tsa/garch.pyt   <module>J   sn  r;oj	ŸS6		7	3	
		%




	1
)
*
H*
000B6
6+? 		-
"						&!'eL"

