
    >hI                         S r SSKrSSKJrJr  SSKJr  SSKJ	r	  \R                  r\R                  r\R                  r " S S\5      r " S S	\	5      rg)
a  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 does not 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


    N)specialstats)GenericLikelihoodModel)Armac                   L   ^  \ rS rSrSrU 4S jrS	S jrS rS rS
S jr	Sr
U =r$ )TLinearModel2   a'  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                   > [        S5        U R                  R                  S   U l        [	        U S5      (       d  SU l        U R
                  SL a4  S U l        S U l        U R                  R                  S   S-   U l        SS/nOU R                  R                  S   S-   U l        [        R                  [        R                  " U R                  R                  S   S-   5      -  nU R
                  US'   X l        [        R                  " U5      U l        S/n[        TU ]9  5         U R                  U5        U R!                  5         g )	Nzrunning Tmodel initialize   fix_dfF   dfscale)printexogshapek_varshasattrr   fixed_paramsfixed_paramsmaskk_paramsnpnanzerosisnansuper
initialize_set_extra_params_names_set_start_params)selfextra_params_namesfixdf	__class__s      pC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/miscmodels/tmodel.pyr   TLinearModel.initialize>   s
   )*iiooa(tX&&DK;;% $D$(D! IIOOA.2DM"& !IIOOA.2DMFFRXXdiiooa&81&<==EE"I %$&HHUOD!")
 	$$%78     c                    Ub  Xl         g SSKJn  U" U R                  U R                  5      R                  5       nS[        R                  " U R                  5      -  nUR                  US U R                  & U R                  SL aY  U(       a)  [        R                  " UR                  5      nSU-  S-   nOSnXaS'   [        R                  " UR                   5      US	'   Xl         g )
Nr   )OLSg?Fg      @      r   )start_params#statsmodels.regression.linear_modelr)   endogr   fitr   onesr   paramsr   r   r   kurtosisresidsqrtr   )r!   r-   use_kurtosisr)   res_olskurtr   s          r%   r    TLinearModel._set_start_params]   s    # ,?$**dii0446Grwwt}}55L)0L$++&{{e# >>'--8DD1BB#%R #%777==#9R  ,r'   c                 D    U R                  U5      R                  S5      * $ Nr   nloglikeobssumr!   r2   s     r%   loglikeTLinearModel.loglikew   !      (,,Q///r'   c                    U R                   b  U R                  U5      nUSS nUS   n[        R                  " US   5      n[        R                  " U R
                  U5      nU R                  nXe-
  U-  n[        US-   S-  5      [        US-  5      -
  nUS[        U[        -  5      -  US-   S-  [        SUS-  U-  -   5      -  -   -  nU[        U5      -  nU* $ )a  
Loglikelihood of linear model with t distributed errors.

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

Returns
-------
loglike : ndarray
    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.)
Nr   r,   r   r   g       @g      ?)
r   expandparamsr   absdotr   r/   	sps_gamlnnp_lognp_pi)	r!   r2   betar   r   locr/   xlPxs	            r%   r=   TLinearModel.nloglikeobsz   s    : (&&v.Fcr{BZvbz"ffTYY%

[%Aq!Ibe$44s6"U(##r!tRiq!Q${0C&CCCve}tr'   c                     Uc  U R                   n[        R                  " X!S U R                   R                  S    5      $ )Nr   )r   r   rF   r   )r!   r2   r   s      r%   predictTLinearModel.predict   s4    <99Dvvd#6DIIOOA$6788r'   )r   r   r   r   r   r-   )NF)N)__name__
__module____qualname____firstlineno____doc__r   r    r@   r=   rP   __static_attributes____classcell__r$   s   @r%   r   r   2   s'    	!>-40+Z9 9r'   r   c                   @   ^  \ rS rSrSrS rS r  SU 4S jjrSrU =r	$ )TArma   ag  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                 D    U R                  U5      R                  S5      * $ r;   r<   r?   s     r%   r@   TArma.loglike   rB   r'   c                     U R                  USS 5      nUS   n[        R                  " US   5      n[        R                  R                  X$-  U5      * [        U5      -   nU$ )z
Loglikelihood for arma model for each observation, t-distribute

Notes
-----
The ancillary parameter is assumed to be the last element of
the params vector
Nr   r,   )	geterrorsr   rE   r   t_logpdfrH   )r!   r2   	errorsestr   r   llikes         r%   r=   TArma.nloglikeobs   s\     NN6#2;/	
 BZvbz"77??9?B77&-Gr'   c           	         > Uu  pxUb   [        U5      Xx-   S-   :w  a  [        S5      eO3[        R                  " S[        R                  " Xx-   5      -  SS/45      n[
        T
U ]  " SUUX4US.UD6n	U	$ )Nr   z(start_param need sum(order) + 2 elementsg?r+   r   )orderr-   methodmaxitertol )len
ValueErrorr   concatenater1   r   fit_mle)r!   rg   r-   rh   ri   rj   kwdsnarnmaresr$   s             r%   ro   TArma.fit_mle   s    #< CIM1 !KLL 2 >>4	0B+BQF*KLL go :E6B06-0: 59:
 
r'   rk   )Nnmi  g:0yE>)
rR   rS   rT   rU   rV   r@   r=   ro   rW   rX   rY   s   @r%   r[   r[      s&     0
* FJ r'   r[   )rV   numpyr   scipyr   r   statsmodels.base.modelr   statsmodels.tsa.arma_mler   logrH   pirI   gammalnrG   r   r[   rk   r'   r%   <module>r}      sR   !H    9 ) 

OO	x9) x9v:D :r'   