U
    Kvf  ã                   @   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krXd | _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 )	Nzrunning Tmodel initializeé   Úfix_dfFé   ÚdfÚscaleéþÿÿÿ)ÚprintÚexogÚshapeÚk_varsÚhasattrr   Úfixed_paramsZfixed_paramsmaskÚk_paramsÚnpÚnanÚzerosÚisnanÚsuperr   Ú
initializeZ_set_extra_params_namesÚ_set_start_params)ÚselfZextra_params_namesZfixdf©Ú	__class__© úA/tmp/pip-unpacked-wheel-2v6byqio/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   r   ZkurtosisZresidÚsqrtr   )r   r$   Zuse_kurtosisr    Zres_olsZkurtr
   r   r   r   r   ]   s    
zTLinearModel._set_start_paramsc                 C   s   |   |¡ d¡ S ©Nr   ©ÚnloglikeobsÚsum©r   r'   r   r   r   Úloglikew   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 : 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'   Úbetar
   r   Úlocr%   ÚxZlPxr   r   r   r+   z   s    

4zTLinearModel.nloglikeobsc                 C   s*   |d kr| j }t ||d | j jd … ¡S )Nr   )r   r   r0   r   )r   r'   r   r   r   r   Úpredict§   s    zTLinearModel.predict)NF)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r.   r+   r7   Ú__classcell__r   r   r   r   r   2   s   
-r   c                       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 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_logpdfr2   )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   r=   Úfit_mle)
r   rB   r$   rC   rD   rE   ÚkwdsZnarZnmaÚresr   r   r   rH   Ø   s    
  ýýzTArma.fit_mle)Nr?   r@   rA   )r8   r9   r:   r;   r.   r+   rH   r<   r   r   r   r   r=   ­   s     ÿr=   )r;   Znumpyr   Zscipyr   r   Zstatsmodels.base.modelr   Zstatsmodels.tsa.arma_mler   Úlogr2   Úpir3   Zgammalnr1   r   r=   r   r   r   r   Ú<module>   s   ${