Ë
    £�DjI  ã                   ó¬   — 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      €úaC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/miscmodels/tmodel.pyr   zTLinearModel.initialize>   s	  ø€ ÜÐ)Ô*à—i‘i—o‘o aÑ(ˆŒÜ�t˜XÔ&ØˆDŒKà�;‰;˜%Ñà $ˆDÔØ$(ˆDÔ!Ø ŸI™IŸO™O¨AÑ.°Ñ2ˆDŒMØ"&¨ Ñð !ŸI™IŸO™O¨AÑ.°Ñ2ˆDŒMÜ—F‘FœRŸX™X d§i¡i§o¡o°aÑ&8¸1Ñ&<Ó=Ñ=ˆEØŸ™ˆE�"‰IØ %ˆDÔÜ$&§H¡H¨U£OˆDÔ!Ø") Ðä‰ÑÔð
 	×$Ñ$Ð%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Õå?Ù˜$Ÿ*™* d§i¡iÓ0×4Ñ4Ó6ˆGØœrŸw™w t§}¡}Ó5Ñ5ˆLØ)0¯©ˆL˜˜$Ÿ+™+Ð&à�{‰{˜eÑ#áÜ Ÿ>™>¨'¯-©-Ó8�DØ˜D™ 1™‘Bà�Bà#%�˜RÑ ä#%§7¡7¨7¯=©=Ó#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à�c�rˆ{ˆØ�B‰ZˆÜ—‘�v˜b‘zÓ"ˆÜ�f‰f�T—Y‘Y Ó%ˆØ—
‘
ˆØ�S‰[˜%Ñˆä˜˜A™˜q™Ó!¤I¨b°©eÓ$4Ñ4ˆØˆs”6˜"œU™(Ó#Ñ# r¨!¡t¨R¡i´°q¸!¸Q¹$À¹±{Ó0CÑ&CÑCÑCˆØŒv�e‹}ÑˆØˆ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   € Øˆ<Ø—9‘9ˆDÜ�v‰v�d˜FÐ#6 D§I¡I§O¡O°AÑ$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^   € ð —N‘N 6¨#¨2 ;Ó/ˆ	ð
 �B‰ZˆÜ—‘�v˜b‘zÓ"ˆÜ—7‘7—?‘? 9¨U¡?°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Ñ+BÀQÈÀFÐ*KÓLˆLô ‰g‰oð : EØ6BØ06ÀØ-0ñ:ð 59ñ:ˆð
 ˆ
r%   )NÚnmiˆ  g:Œ0âŽyE>)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Ý )ð 
�‰€Ø
�‰€Ø�O‰O€	ôx9Ð)ô x9ôv:ˆDõ :r%   