o
    þ0Gfb  ã                   @   sj   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„ deƒZG d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                       sD   e Zd ZdZ‡ fdd„Zddd„Zdd	„ Zd
d„ Zddd„Z‡  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                    sÈ   t dƒ | jjd | _t| dƒsd| _| jdu r,d | _d | _| jjd d | _ddg}n(| jjd d | _t	j
t	 | jjd d ¡ }| j|d< || _t	 |¡| _dg}tƒ  ¡  |  |¡ |  ¡  d S )	Nzrunning Tmodel initializeé   Úfix_dfFé   ÚdfÚscaleéþÿÿÿ)ÚprintÚexogÚshapeÚk_varsÚhasattrr   Úfixed_paramsZfixed_paramsmaskÚk_paramsÚnpÚnanZzerosZisnanÚsuperÚ
initializeZ_set_extra_params_namesÚ_set_start_params)ÚselfZextra_params_namesZfixdf©Ú	__class__© ú=lib/python3.10/site-packages/statsmodels/miscmodels/tmodel.pyr   >   s$   





zTLinearModel.initializeNFc                 C   sž   |d ur	|| _ d S ddlm} || j| jƒ ¡ }dt | j¡ }|j	|d | j
…< | jdu rJ|r<t |j¡}d| d }nd}||d< t |j¡|d	< || _ d S )
Nr   )ÚOLSgš™™™™™¹?Fg      @é   é   r   éÿÿÿÿ)Ústart_paramsZ#statsmodels.regression.linear_modelr   Úendogr   Zfitr   Úonesr   Úparamsr   r   r   ZkurtosisZresidZsqrtr   )r   r"   Zuse_kurtosisr   Zres_olsZkurtr
   r   r   r   r   ]   s   


zTLinearModel._set_start_paramsc                 C   ó   |   |¡ d¡ S ©Nr   ©ÚnloglikeobsÚsum©r   r%   r   r   r   Úloglikew   ó   zTLinearModel.loglikec           	      C   s¸   | j dur
|  |¡}|dd… }|d }t |d ¡}t | j|¡}| j}|| | }t|d d ƒt|d ƒ }|dt|t	 ƒ |d d td|d |  ƒ  8 }|t|ƒ8 }| 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   Zexpandparamsr   ÚabsÚdotr   r#   Ú	sps_gamlnÚnp_logÚnp_pi)	r   r%   Zbetar
   r   Úlocr#   ÚxZlPxr   r   r   r)   z   s   

4zTLinearModel.nloglikeobsc                 C   s*   |d u r| j }t ||d | j jd … ¡S )Nr   )r   r   r/   r   )r   r%   r   r   r   r   Úpredict§   s   zTLinearModel.predict)NF)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r,   r)   r5   Ú__classcell__r   r   r   r   r   2   s    
-r   c                       s6   e Zd ZdZdd„ Zdd„ Z			d‡ fd
d„	Z‡  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                 C   r&   r'   r(   r+   r   r   r   r,   ¾   r-   zTArma.loglikec                 C   sH   |   |dd… ¡}|d }t |d ¡}tj || |¡ t|ƒ }|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!   )Z	geterrorsr   r.   r   ÚtZ_logpdfr1   )r   r%   Z	errorsestr
   r   Zlliker   r   r   r)   Ã   s
   
zTArma.nloglikeobsNÚnméˆ  ç:Œ0âŽyE>c           
         sp   |\}}|d urt |ƒ|| d krtdƒ‚nt dt || ¡ ddgf¡}tƒ jd|||||dœ|¤Ž}	|	S )Nr	   z(start_param need sum(order) + 2 elementsgš™™™™™©?r    r   )Úorderr"   ÚmethodÚmaxiterÚtolr   )ÚlenÚ
ValueErrorr   Zconcatenater$   r   Úfit_mle)
r   r@   r"   rA   rB   rC   ÚkwdsZnarZnmaÚresr   r   r   rF   Ø   s   ÿ 
ýýzTArma.fit_mle)Nr=   r>   r?   )r6   r7   r8   r9   r,   r)   rF   r:   r   r   r   r   r;   ­   s    ÿr;   )r9   Znumpyr   Zscipyr   r   Zstatsmodels.base.modelr   Zstatsmodels.tsa.arma_mler   Úlogr1   Zpir2   Zgammalnr0   r   r;   r   r   r   r   Ú<module>   s    ${