
    M/Phb                         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dS )
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                 v   t          d           | j        j        d         | _        t	          | d          sd| _        | j        du r-d | _        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                                                       |                     |           |                                  d S )	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      ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/miscmodels/tmodel.pyr   zTLinearModel.initialize>   s%   )***ioa(tX&& 	 DK;% $D$(D! IOA.2DM"& !IOA.2DMFRXdioa&81&<===EE"I %D$&HUOOD!")
 	$$%7888         NFc                    |	|| _         d S ddlm}  || j        | j                                                  }dt          j        | j                  z  }|j	        |d | j
        <   | j        du rG|r"t          j        |j                  }d|z  dz   }nd}||d<   t          j        |j                  |d	<   || _         d S )
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??????c$*di004466Grwt}555L)0L$+&{e##  >'-88DD1BBB#%R #%77=#9#9R  ,Dr%   c                 T    |                      |                              d           S Nr   nloglikeobssumr    r0   s     r$   loglikezTLinearModel.loglikew   '      ((,,Q////r%   c                    | 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crc{BZvbz""fTY%%
S[%Aq!!Ibe$4$44s6"U(###r!tRiq!Q${0C0C&CCCve}}tr%   c                 n    || j         }t          j        ||d | j         j        d                            S )Nr
   )r   r   rB   r   )r    r0   r   s      r$   predictzTLinearModel.predict   s4    <9DvdF#6DIOA$6#67888r%   )NF)N)
__name__
__module____qualname____doc__r   r   r=   r:   rK   __classcell__r#   s   @r$   r   r   2   s        	 	! ! ! ! !>- - - -40 0 0+ + +Z9 9 9 9 9 9 9 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                 T    |                      |                              d           S r8   r9   r<   s     r$   r=   zTArma.loglike   r>   r%   c                     |                      |dd                   }|d         }t          j        |d                   }t          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   sg     NN6#2#;//	
 BZvbz""7??9U?B777&--Gr%   Nnm  :0yE>c           	         |\  }}|)t          |          ||z   dz   k    rt          d          n0t          j        dt          j        ||z             z  ddgf          } t                      j        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`   ra   rb   kwdsnarnmaresr#   s             r$   rg   zTArma.fit_mle   s    S#<  C#IM11 !KLLL 2 >4c	0B0B+BQF*KLLL eggo :E6B06-0: : 59: :
 
r%   )Nr[   r\   r]   )rL   rM   rN   rO   r=   r:   rg   rP   rQ   s   @r$   rS   rS      so          0 0 0
  * FJ         r%   rS   )rO   numpyr   scipyr   r   statsmodels.base.modelr   statsmodels.tsa.arma_mler   logrD   pirE   gammalnrC   r   rS   rc   r%   r$   <module>rs      s   ! !H                     9 9 9 9 9 9 ) ) ) ) ) ) 

O	x9 x9 x9 x9 x9) x9 x9 x9v: : : : :D : : : : :r%   