B
    ¤ŠãZµ  ã               @   sr   d Z ddlZddlmZ ejZejZej	Z
ddlmZ G dd„ deƒZddlmZ ddlm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 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


é    N)Úspecial)ÚGenericLikelihoodModelc                   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Ä   | j jd | _t| dƒsd| _| jdkrPd | _d | _| j jd d | _ddg}nP| j jd d | _tj	t 
| j jd d ¡ }| j|d< || _t |¡| _dg}|  |¡ |  ¡  tt| ƒ ¡  d S )Né   Úfix_dfFé   ÚdfÚscaleéþÿÿÿ)ÚexogÚshapeÚk_varsÚhasattrr   Úfixed_paramsZfixed_paramsmaskÚk_paramsÚnpÚnanZzerosZisnanZ_set_extra_params_namesÚ_set_start_paramsÚsuperr   Ú
initialize)ÚselfZextra_params_namesZfixdf)Ú	__class__© ú<lib/python3.7/site-packages/statsmodels/miscmodels/tmodel.pyr   ;   s"    




zTLinearModel.initializeNFc             C   sœ   |d k	r|| _ nˆddlm} || j| jƒ ¡ }dt | j¡ }|j	|d | j
…< | jdkr’|rvt |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   ÚstatsZkurtosisZresidZsqrtr	   )r   r   Zuse_kurtosisr   Zres_olsZkurtr   r   r   r   r   U   s    
zTLinearModel._set_start_paramsc             C   s   |   |¡ d¡ S )Nr   )ÚnloglikeobsÚsum)r   r!   r   r   r   Úloglikeo   s    zTLinearModel.loglikec       	      C   s¸   | j dk	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 : 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.)

        Nr
   r   r   r   g       @g      à?)
r   Zexpandparamsr   ÚabsÚdotr   r   Ú	sps_gamlnÚnp_logÚnp_pi)	r   r!   Zbetar   r	   Zlocr   ÚxZlPxr   r   r   r#   r   s    

4zTLinearModel.nloglikeobsc             C   s*   |d k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#   r,   Ú__classcell__r   r   )r   r   r   /   s   

.r   )r"   )ÚArmac                   s2   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   s   |   |¡ d¡ S )Nr   )r#   r$   )r   r!   r   r   r   r%   º   s    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_logpdfr)   )r   r!   Z	errorsestr   r	   Zlliker   r   r   r#   ¿   s
    
zTArma.nloglikeobsNÚnméˆ  ç:Œ0âŽyE>c       
         st   |\}}|d k	r.t |ƒ|| d krNtdƒ‚n t dt || ¡ ddgf¡}tt| ƒjf |||||dœ|—Ž}	|	S )Nr   z(start_param need sum(order) + 2 elementsgš™™™™™©?r   r   )Úorderr   ÚmethodÚmaxiterÚtol)ÚlenÚ
ValueErrorr   Zconcatenater    r   r3   Úfit_mle)
r   r8   r   r9   r:   r;   ÚkwdsZnarZnmaZres)r   r   r   r>   Ô   s    
 zTArma.fit_mle)Nr5   r6   r7   )r-   r.   r/   r0   r%   r#   r>   r1   r   r   )r   r   r3   ©   s
    r3   )r0   Znumpyr   Zscipyr   Úlogr)   Zpir*   Zgammalnr(   Zstatsmodels.base.modelr   r   r"   Zstatsmodels.tsa.arma_mler2   r3   r   r   r   r   Ú<module>"   s   w