
    'YHhb                         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 G d de      Z G d d	e	      Zy)
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                   >     e Zd ZdZ fdZddZd Zd ZddZ xZ	S )	TLinearModela?  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                    t        d       | j                  j                  d   | _        t	        | d      sd| _        | j
                  du r4d | _        d | _        | j                  j                  d   dz   | _        ddg}n| j                  j                  d   dz   | _        t        j                  t        j                  | j                  j                  d   dz         z  }| j
                  |d<   || _        t        j                  |      | _        dg}t        | 9          | j                  |       | j!                          y )	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      V/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/miscmodels/tmodel.pyr   zTLinearModel.initialize>   s	   )*iiooa(tX&DK;;% $D$(D! IIOOA.2DM"& !IIOOA.2DMFFRXXdiiooa&81&<==EE"I %D$&HHUOD!")
 	$$%78     c                    ||| _         y ddlm}  || j                  | j                        j                         }dt        j                  | j                        z  }|j                  |d | j                   | j                  du rS|r(t        j                  |j                        }d|z  dz   }nd}||d<   t        j                  |j                         |d	<   || _         y )
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   zTLinearModel._set_start_params]   s    # ,D?$**dii0446Grwwt}}55L)0L$++&{{e# >>'--8DD1BB#%R #%777==#9R  ,Dr%   c                 D    | j                  |      j                  d       S Nr   nloglikeobssumr    r0   s     r$   loglikezTLinearModel.loglikew   !      (,,Q///r%   c                    | j                   | j                  |      }|dd }|d   }t        j                  |d         }t        j                  | j
                  |      }| j                  }||z
  |z  }t        |dz   dz        t        |dz        z
  }|dt        |t        z        z  |dz   dz  t        d|dz  |z  z         z  z   z  }|t        |      z  }| S )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    r0   betar   r   locr-   xlPxs	            r$   r:   zTLinearModel.nloglikeobsz   s    : (&&v.Fcr{BZvbz"ffTYY%

S[%Aq!Ibe$44s6"U(##r!tRiq!Q${0C&CCCve}tr%   c                 ~    || j                   }t        j                  ||d | j                   j                  d          S )Nr
   )r   r   rB   r   )r    r0   r   s      r$   predictzTLinearModel.predict   s6    <99DvvdF#6DIIOOA$6788r%   )NF)N)
__name__
__module____qualname____doc__r   r   r=   r:   rK   __classcell__r#   s   @r$   r   r   2   s"    	!>-40+Z9r%   r   c                   4     e Zd ZdZd Zd Z	 	 d fd	Z xZS )TArmaa  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    | j                  |      j                  d       S r8   r9   r<   s     r$   r=   zTArma.loglike   r>   r%   c                     | j                  |dd       }|d   }t        j                  |d         }t        j                  j                  ||z  |       t        |      z   }|S )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   rA   r   t_logpdfrD   )r    r0   	errorsestr   r   llikes         r$   r:   zTArma.nloglikeobs   s^     NN6#2;/	
 BZvbz"77??9U?B77&-Gr%   c           	          |\  }}|t        |      ||z   dz   k7  r=t        d      t        j                  dt        j                  ||z         z  ddgf      }t        
|   d|||||d|}	|	S )Nr   z(start_param need sum(order) + 2 elementsg?r)   r
   )orderr+   methodmaxitertol )len
ValueErrorr   concatenater/   r   fit_mle)r    r\   r+   r]   r^   r_   kwdsnarnmaresr#   s             r$   rd   zTArma.fit_mle   s    S#< C#IM1 !KLL>>4c	0B+BQF*KLL go :E6B06-0: 59:
 
r%   )Nnmi  g:0yE>)rL   rM   rN   rO   r=   r:   rd   rP   rQ   s   @r$   rS   rS      s&     0
* FJ r%   rS   )rO   numpyr   scipyr   r   statsmodels.base.modelr   statsmodels.tsa.arma_mler   logrD   pirE   gammalnrC   r   rS   r`   r%   r$   <module>rq      sR   !H    9 ) 

OO	x9) x9v:D :r%   