U
    Ãmœd3  ã                   @   sj   d Z ddlZddlZddlmZ ddlmZmZ ddl	m
Z
mZmZ ddlmZmZ G dd„ dejƒZdS )	zs
(Internal) AR(1) model for monthly growth rates aggregated to quarterly freq.

Author: Chad Fulton
License: BSD-3
é    N)ÚBunch)ÚmlemodelÚinitialization)ÚSMOOTHER_STATEÚSMOOTHER_STATE_COVÚSMOOTHER_STATE_AUTOCOV)Úconstrain_stationary_univariateÚ!unconstrain_stationary_univariatec                       sˆ   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZ‡ fdd	„Zddd„Z	ddd„Z
d dd„Zd!dd„Zdd„ Zdd„ Z‡ fdd„Z‡  ZS )"ÚQuarterlyAR1a‹  
    AR(1) model for monthly growth rates aggregated to quarterly frequency

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`

    Notes
    -----
    This model is internal, used to estimate starting parameters for the
    DynamicFactorMQ class. The model is:

    .. math::

        y_t & = \begin{bmatrix} 1 & 2 & 3 & 2 & 1 \end{bmatrix} \alpha_t \\
        \alpha_t & = \begin{bmatrix}
            \phi & 0 & 0 & 0 & 0 \\
               1 & 0 & 0 & 0 & 0 \\
               0 & 1 & 0 & 0 & 0 \\
               0 & 0 & 1 & 0 & 0 \\
               0 & 0 & 0 & 1 & 0 \\
        \end{bmatrix} +
        \begin{bmatrix} 1 \\ 0 \\ 0 \\ 0 \\ 0 \end{bmatrix} \varepsilon_t

    The two parameters to be estimated are :math:`\phi` and :math:`\sigma^2`.

    It supports fitting via the usual quasi-Newton methods, as well as using
    the EM algorithm.

    c                    sN   t ƒ j|dddd� dddddg| d< t d¡| d	dd …d d
…f< d| d< d S )Né   é   Z
stationary)Úk_statesZk_posdefr   é   é   Zdesigné   Ú
transitionéÿÿÿÿg      ð?)Z	selectionr   r   )ÚsuperÚ__init__ÚnpÚeye)ÚselfÚendog©Ú	__class__© úb/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/tsa/statespace/_quarterly_ar1.pyr   3   s    ÿzQuarterlyAR1.__init__c                 C   s   ddgS )NÚphiZsigma2r   ©r   r   r   r   Úparam_names:   s    zQuarterlyAR1.param_namesc                 C   s   t  dt  | j¡d g¡S )Nr   é   )r   ÚarrayZnanvarr   r   r   r   r   Ústart_params>   s    zQuarterlyAR1.start_paramsc              	      s0   t  ¡ � t  d¡ tƒ j||Ž}W 5 Q R X |S )NÚignore)ÚwarningsÚcatch_warningsÚsimplefilterr   Úfit)r   ÚargsÚkwargsÚoutr   r   r   r'   B   s    

zQuarterlyAR1.fitNTÚnoneéô  ç�íµ ÷Æ°>Fc                 C   sº  | j rtdƒ‚|rtdƒ‚|d kr.| j}d}ntj|dd�}|sJ|  |¡}g }|g}d }d}d}||k �r"|dk s|||k�r"| j|d ||d	�}| |d j	 
¡ ¡ | |d ¡ |rÞtj| jd
|d jd |d jd d�}|dk�rd|d |d   t |d ¡t |d ¡  }|d7 }q`|
�r2|d }n„|�rH| jj}|| j_| j|d d||d�}|�rl|| j_|	�r¢tf t |¡t |¡|dœŽ}tf ||dœŽ}nd }d }||_||_|S )NzFCannot fit using the EM algorithm while holding some parameters fixed.z?Cannot fit using the EM algorithm when using low_memory option.Tr   )Zndminr   r   r   )ÚinitÚmstep_methodZknown).r   )ZconstantZstationary_covéþÿÿÿ)ÚtransformedÚcov_typeÚcov_kwds)ÚparamsÚllfÚiter)Ú	toleranceÚmaxiter)Z_has_fixed_paramsÚNotImplementedErrorÚ
ValueErrorr"   r   r!   Útransform_paramsÚ_em_iterationÚappendÚllf_obsÚsumr   ZInitializationr   Úsmoothed_stateÚsmoothed_state_covÚabsÚssmÚsmoothr   Zmle_retvalsZmle_settings)r   r"   r1   r2   r3   r8   r7   Zem_initializationr/   Zfull_outputZreturn_paramsZ
low_memoryr5   r4   r.   ÚiÚdeltar*   ÚresultÚ	base_initZ
em_retvalsZem_settingsr   r   r   Úfit_emI   sr    
ÿ ý
ÿ

 ÿþÿ
zQuarterlyAR1.fit_emc                 C   s&   | j ||d�}| j|||d�}||fS )N)r.   )r/   )Ú_em_expectation_stepÚ_em_maximization_step)r   Úparams0r.   r/   ÚresÚparams1r   r   r   r<   �   s
    ÿzQuarterlyAR1._em_iterationc                 C   sd   |   |¡ |d k	r"| jj}|| j_| jjttB tB dd�}tj| jj	j
dd�|_|d k	r`|| j_|S )NF)Zupdate_filterT)Úcopy)ÚupdaterC   r   rD   r   r   r   r   r!   Z_kalman_filterZloglikelihoodr>   )r   rL   r.   rH   rM   r   r   r   rJ   ™   s    

þ ÿz!QuarterlyAR1._em_expectation_stepc              	   C   s0  |j jd }|j ddd¡}|j ddd¡}| ¡ t || ddd¡¡ }|d d… t |dd … |d d…  ddd¡¡ }|d d…d d…d d…f jdd�}	|d d …d d…d d…f jdd�}
|dd …d d…d d…f jdd�}|j	d d }|
|	 }|||
j  | }t 
|¡}|d |d< |d |d< |S )N).Nr   r   r   r   )Zaxis)r   r   )r@   ÚTrA   Z	transposeZsmoothed_state_autocovrO   r   Úmatmulr?   ÚshapeZ
zeros_like)r   rM   rL   r/   ÚaZcov_aZacov_aZEaaZEaa1ÚAÚBÚCZnobsZf_AZf_QrN   r   r   r   rK   ¬   s    2"""
z"QuarterlyAR1._em_maximization_stepc                 C   s"   t  t|d d… ƒ|d d g¡S )Nr   r   )r   Úhstackr   )r   Zunconstrainedr   r   r   r;   Ä   s    
þzQuarterlyAR1.transform_paramsc                 C   s"   t  t|d d… ƒ|d d g¡S )Nr   g      à?)r   rX   r	   )r   Zconstrainedr   r   r   Úuntransform_paramsÊ   s    
þzQuarterlyAR1.untransform_paramsc                    s,   t ƒ j|f|Ž |d | d< |d | d< d S )Nr   )r   r   r   r   )Z	state_covr   r   )r   rP   )r   r4   r)   r   r   r   rP   Ð   s    zQuarterlyAR1.update)NTr+   Nr,   r-   TNTFF)NN)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r"   r'   rI   r<   rJ   rK   r;   rY   rP   Ú__classcell__r   r   r   r   r
      s.   

              ý
F



r
   )r]   r$   Únumpyr   Zstatsmodels.tools.toolsr   Zstatsmodels.tsa.statespacer   r   Z*statsmodels.tsa.statespace.kalman_smootherr   r   r   Z statsmodels.tsa.statespace.toolsr   r	   ZMLEModelr
   r   r   r   r   Ú<module>   s   