
p7]c           @   s   d  Z  d d l Z d d l m Z m Z d d l m Z d d l m	 Z	 e j
 Z e j Z e j Z d e f d     YZ d e	 f d	     YZ d S(
   s  Linear Model with Student-t distributed errors

Because the t distribution has fatter tails than the normal distribution, it
can be used to model observations with heavier tails and observations that have
some outliers. For the latter case, the t-distribution provides more robust
estimators for mean or mean parameters (what about var?).



References
----------
Kenneth L. Lange, Roderick J. A. Little, Jeremy M. G. Taylor (1989)
Robust Statistical Modeling Using the t Distribution
Journal of the American Statistical Association
Vol. 84, No. 408 (Dec., 1989), pp. 881-896
Published by: American Statistical Association
Stable URL: http://www.jstor.org/stable/2290063

not read yet


Created on 2010-09-24
Author: josef-pktd
License: BSD

TODO
----
* add starting values based on OLS
* bugs: store_params doesn't seem to be defined, I think this was a module
        global for debugging - commented out
* parameter restriction: check whether version with some fixed parameters works


iN(   t   specialt   stats(   t   GenericLikelihoodModel(   t   Armat   TLinearModelc           B   sD   e  Z d  Z d   Z d e d  Z d   Z d   Z d d  Z	 RS(   s?  Maximum Likelihood Estimation of Linear Model with t-distributed errors

    This is an example for generic MLE.

    Except for defining the negative log-likelihood method, all
    methods and results are generic. Gradients and Hessian
    and all resulting statistics are based on numerical
    differentiation.

    c         C   s  |  j  j d |  _ t |  d  s. t |  _ n  |  j t k ru d  |  _ d  |  _ |  j  j d d |  _	 d d g } nl |  j  j d d |  _	 t
 j t
 j |  j  j d d  } |  j | d <| |  _ t
 j |  |  _ d g } |  j |  |  j   t t |   j   d  S(   Ni   t   fix_dfi   t   dft   scalei(   t   exogt   shapet   k_varst   hasattrt   FalseR   t   Nonet   fixed_paramst   fixed_paramsmaskt   k_paramst   npt   nant   zerost   isnant   _set_extra_params_namest   _set_start_paramst   superR   t
   initialize(   t   selft   extra_params_namest   fixdf(    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyR   >   s"    		$		
c         C   s   | d  k	 r | |  _ n d d l m } | |  j |  j  j   } d t j |  j	  } | j
 | |  j *|  j t k r | r t j | j  } d | d } n d } | | d <t j | j  | d <n  | |  _ d  S(   Ni(   t   OLSg?g      @i   i   i(   R   t   start_paramst#   statsmodels.regression.linear_modelR   t   endogR   t   fitR   t   onesR   t   paramsR
   R   R   R   t   kurtosist   residt   sqrtR   (   R   R   t   use_kurtosisR   t   res_olst   kurtR   (    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyR   X   s    
c         C   s   |  j  |  j d  S(   Ni    (   t   nloglikeobst   sum(   R   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyt   logliker   s    c   	      C   s   |  j  d k	 r! |  j |  } n  | d  } | d } t j | d  } t j |  j |  } |  j } | | | } t | d d  t | d  } | d t	 | t
  | d d t	 d | d |  8} | t	 |  8} | S(   s  
        Loglikelihood of linear model with t distributed errors.

        Parameters
        ----------
        params : array
            The parameters of the model. The last 2 parameters are degrees of
            freedom and scale.

        Returns
        -------
        loglike : array
            The log likelihood of the model evaluated at `params` for each
            observation defined by self.endog and self.exog.

        Notes
        -----
        .. math:: \ln L=\sum_{i=1}^{n}\left[-\lambda_{i}+y_{i}x_{i}^{\prime}\beta-\ln y_{i}!\right]

        The t distribution is the standard t distribution and not a standardized
        t distribution, which means that the scale parameter is not equal to the
        standard deviation.

        self.fixed_params and self.expandparams can be used to fix some
        parameters. (I doubt this has been tested in this model.)

        iii   i   g       @g      ?N(   R   R   t   expandparamsR   t   abst   dotR   R   t	   sps_gamlnt   np_logt   np_pi(	   R   R"   t   betaR   R   t   locR   t   xt   lPx(    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyR)   u   s    

	":c         C   s6   | d  k r |  j } n  t j | | |  j j d   S(   Ni   (   R   R   R   R.   R	   (   R   R"   R   (    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyt   predict   s    N(
   t   __name__t
   __module__t   __doc__R   R   R   R   R+   R)   R6   (    (    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyR   2   s   
			.t   TArmac           B   s5   e  Z d  Z d   Z d   Z d d d d d  Z RS(   s  Univariate Arma Model with t-distributed errors

    This inherit all methods except loglike from tsa.arma_mle.Arma

    This uses the standard t-distribution, the implied variance of
    the error is not equal to scale, but ::

        error_variance = df/(df-2)*scale**2

    Notes
    -----
    This might be replaced by a standardized t-distribution with scale**2
    equal to variance

    c         C   s   |  j  |  j d  S(   Ni    (   R)   R*   (   R   R"   (    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyR+      s    c         C   sX   |  j  | d   } | d } t j | d  } t j j | | |  t |  } | S(   s   
        Loglikelihood for arma model for each observation, t-distribute

        Notes
        -----
        The ancillary parameter is assumed to be the last element of
        the params vector
        ii(   t	   geterrorsR   R-   R   t   tt   _logpdfR0   (   R   R"   t	   errorsestR   R   t   llike(    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyR)      s
    

$t   nmi  g:0yE>c   
      K   s   | \ } } | d  k	 rD t |  | | d k rp t d   qp n, t j d t j | |  d d g f  } t t |   j d | d | d | d	 | d
 | |  }	 |	 S(   Ni   s(   start_param need sum(order) + 2 elementsg?i   i   t   orderR   t   methodt   maxitert   tol(	   R   t   lent
   ValueErrorR   t   concatenateR!   R   R:   t   fit_mle(
   R   RA   R   RB   RC   RD   t   kwdst   nart   nmat   res(    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyRH      s    ,N(   R7   R8   R9   R+   R)   R   RH   (    (    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyR:      s
   			(   R9   t   numpyR   t   scipyR    R   t   statsmodels.base.modelR   t   statsmodels.tsa.arma_mleR   t   logR0   t   piR1   t   gammalnR/   R   R:   (    (    (    s<   lib/python2.7/site-packages/statsmodels/miscmodels/tmodel.pyt   <module>"   s   			w