B
    ZU             	   @   s  d Z ddlmZmZmZ ddlZddlmZ ddl	Z
ddlmZ ddlZdadaejejejejdZi Zi Zi Zi Zi Zi Zi Zi Zi Zi Zi Zi Z dLd	d
Z!e!dd yddl"m#Z# W n, e$k
r   ddddddZ%dd Z#Y nX dd Z&dMddZ'dNddZ(dOddZ)dPddZ*d d! Z+d"d# Z,dQd$d%Z-dRd&d'Z.dSd(d)Z/tr`dTd*d+Z0e/j e0_ ne/Z0dUd,d-Z1d.d/ Z2dVd0d1Z3d2d3 Z4dWd4d5Z5dXd6d7Z6d8d9 Z7d:d; Z8d<d= Z9dYd>d?Z:dZd@dAZ;d[dBdCZ<d\dDdEZ=d]dFdGZ>d^dHdIZ?dJdK Z@dS )_z?
Statespace Tools

Author: Chad Fulton
License: Simplified-BSD
    )divisionabsolute_importprint_functionN)solve_sylvester)_is_using_pandasFT)sdczc       
      C   s  | d kr6yddl m} d} W n tk
r4   d} Y nX | snyddl m} W n" tk
rl   td d} Y nX | sddl m} ddlm}m}m}m	}m
} dat|j|j|j|jd t|j|j|j|jd t|j|j|j|jd t|j|j|j|j d t!|j"|j#|j$|j%d t&|j'|j(|j)|j*d t+|j,|j-|j.|j/d t0|j1|j2|j3|j4d t5|j6|j7|j8|j9d t:|j;|j<|j=|j>d t?|j@|jA|jB|jCd tD|jE|jF|jG|jHd ndd	lmI} dd
lJmK} dayddlLmM}	 W n tk
r:   daNY nX t|j|j|j|jd t|j|j|j|jd t||||d td d d d d tNrt!|j"|j#|j$|j%d t&|j'|j(|j)|j*d t+|j,|j-|j.|j/d t0|j1|j2|j3|j4d t5|j6|j7|j8|j9d t:|j;|j<|j=|j>d t?|j@|jA|jB|jCd tD|jE|jF|jG|jHd d S )Nr   )cython_blasFTz9Minimum dependencies not met. Compatibility mode enabled.   )_representation_kalman_filter_kalman_smoother_simulation_smoother_tools)r   r   r	   r
   )_statespace)_KalmanSmoother)dtrmm)Oscipy.linalgr   ImportErrorwarningswarn r   r   r   r   r   compatibility_modeprefix_statespace_mapupdateZsStatespaceZdStatespaceZcStatespaceZzStatespaceprefix_kalman_filter_mapZsKalmanFilterZdKalmanFilterZcKalmanFilterZzKalmanFilterprefix_kalman_smoother_mapZsKalmanSmootherZdKalmanSmootherZcKalmanSmootherZzKalmanSmootherprefix_simulation_smoother_mapZsSimulationSmootherZdSimulationSmootherZcSimulationSmootherZzSimulationSmootherprefix_pacf_mapZ-_scompute_coefficients_from_multivariate_pacfZ-_dcompute_coefficients_from_multivariate_pacfZ-_ccompute_coefficients_from_multivariate_pacfZ-_zcompute_coefficients_from_multivariate_pacfprefix_sv_mapZ_sconstrain_sv_less_than_oneZ_dconstrain_sv_less_than_oneZ_cconstrain_sv_less_than_oneZ_zconstrain_sv_less_than_one!prefix_reorder_missing_matrix_mapZsreorder_missing_matrixZdreorder_missing_matrixZcreorder_missing_matrixZzreorder_missing_matrix!prefix_reorder_missing_vector_mapZsreorder_missing_vectorZdreorder_missing_vectorZcreorder_missing_vectorZzreorder_missing_vectorprefix_copy_missing_matrix_mapZscopy_missing_matrixZdcopy_missing_matrixZccopy_missing_matrixZzcopy_missing_matrixprefix_copy_missing_vector_mapZscopy_missing_vectorZdcopy_missing_vectorZccopy_missing_vectorZzcopy_missing_vectorprefix_copy_index_matrix_mapZscopy_index_matrixZdcopy_index_matrixZccopy_index_matrixZzcopy_index_matrixprefix_copy_index_vector_mapZscopy_index_vectorZdcopy_index_vectorZccopy_index_vectorZzcopy_index_vectorr   Z_pykalman_smootherr   scipy.linalg.blasr   has_trmm)
compatibilityr   r   r   r   r   r   r   r   r    r+   ?lib/python3.7/site-packages/statsmodels/tsa/statespace/tools.pyset_mode$   s
   



r-   )r*   )find_best_blas_typer   r   r	   r
   )fr   FDGc             C   s2   t dd t| D \}}t|jd}||d fS )Nc             S   s   g | ]\}}|j |fqS r+   )dtype).0iZarr+   r+   r,   
<listcomp>   s    z'find_best_blas_type.<locals>.<listcomp>r   )max	enumerate
_type_convgetchar)Zarraysr3   indexprefixr+   r+   r,   r.      s    r.   c             C   s  d}t | tr| }d}d} nt| d }|dk r8tdt | tsLt | tryt| d }W n tk
rt   d}Y nX |dkrt| } q| d dkrt	|| d< d}nd}t| } tj
|| || ft| jd}t|d | }|d |d | f}d||< | dk	r|dkr|dkrJ| dd  | d  |dddf< n|rxt|D ]2}| |d  j ||| |d | d|f< qZW nXtj| d }xFt|D ]:}t|| |d  j ||| |d | d|f< qW |S )a	  
    Create a companion matrix

    Parameters
    ----------
    polynomial : array_like or list
        If an iterable, interpreted as the coefficients of the polynomial from
        which to form the companion matrix. Polynomial coefficients are in
        order of increasing degree, and may be either scalars (as in an AR(p)
        model) or coefficient matrices (as in a VAR(p) model). If an integer,
        it is interpereted as the size of a companion matrix of a scalar
        polynomial, where the polynomial coefficients are initialized to zeros.
        If a matrix polynomial is passed, :math:`C_0` may be set to the scalar
        value 1 to indicate an identity matrix (doing so will improve the speed
        of the companion matrix creation).

    Returns
    -------
    companion_matrix : array

    Notes
    -----
    Given coefficients of a lag polynomial of the form:

    .. math::

        c(L) = c_0 + c_1 L + \dots + c_p L^p

    returns a matrix of the form

    .. math::
        \begin{bmatrix}
            \phi_1 & 1      & 0 & \cdots & 0 \\
            \phi_2 & 0      & 1 &        & 0 \\
            \vdots &        &   & \ddots & 0 \\
                   &        &   &        & 1 \\
            \phi_n & 0      & 0 & \cdots & 0 \\
        \end{bmatrix}

    where some or all of the :math:`\phi_i` may be non-zero (if `polynomial` is
    None, then all are equal to zero).

    If the coefficients provided are scalars :math:`(c_0, c_1, \dots, c_p)`,
    then the companion matrix is an :math:`n \times n` matrix formed with the
    elements in the first column defined as
    :math:`\phi_i = -\frac{c_i}{c_0}, i \in 1, \dots, p`.

    If the coefficients provided are matrices :math:`(C_0, C_1, \dots, C_p)`,
    each of shape :math:`(m, m)`, then the companion matrix is an
    :math:`nm \times nm` matrix formed with the elements in the first column
    defined as :math:`\phi_i = -C_0^{-1} C_i', i \in 1, \dots, p`.

    It is important to understand the expected signs of the coefficients. A
    typical AR(p) model is written as:

    .. math::
        y_t = a_1 y_{t-1} + \dots + a_p y_{t-p} + \varepsilon_t

    This can be rewritten as:

    .. math::
        (1 - a_1 L - \dots - a_p L^p )y_t = \varepsilon_t \\
        (1 + c_1 L + \dots + c_p L^p )y_t = \varepsilon_t \\
        c(L) y_t = \varepsilon_t

    The coefficients from this form are defined to be :math:`c_i = - a_i`, and
    it is the :math:`c_i` coefficients that this function expects to be
    provided.

    Fr   Nz=Companion matrix polynomials must include at least two terms.r   T)r3   )
isinstanceintlen
ValueErrorlisttuple	TypeErrornp
asanyarrayeyezerosr3   Zdiag_indicesrangeTlinalginvdot)
polynomialZidentity_matrixnmmatrixidxr5   rL   r+   r+   r,   companion_matrix   sD    G


"
$4:rS   r   c             C   s   t | d}|st| n| }|dk	rlxF|dkrj|sN||d |d|   }n|||d }|d8 }q&W |stj||dd}n&x$|dkr| dd }|d8 }qW |S )az  
    Difference a series simply and/or seasonally along the zero-th axis.

    Given a series (denoted :math:`y_t`), performs the differencing operation

    .. math::

        \Delta^d \Delta_s^D y_t

    where :math:`d =` `diff`, :math:`s =` `seasonal_periods`,
    :math:`D =` `seasonal\_diff`, and :math:`\Delta` is the difference
    operator.

    Parameters
    ----------
    series : array_like
        The series to be differenced.
    diff : int, optional
        The number of simple differences to perform. Default is 1.
    seasonal_diff : int or None, optional
        The number of seasonal differences to perform. Default is no seasonal
        differencing.
    seasonal_periods : int, optional
        The seasonal lag. Default is 1. Unused if there is no seasonal
        differencing.

    Returns
    -------
    differenced : array
        The differenced array.
    Nr   r   )axis)r   rE   rF   diff)seriesZk_diffZk_seasonal_diffZseasonal_periodspandasZdifferencedr+   r+   r,   rU   X  s     


rU   c             C   sZ   t jdd | D  }t |r.tj| |d}n(t | s>|rNt j| |d}ntd|S )a{  
    Concatenate a set of series.

    Parameters
    ----------
    series : iterable
        An iterable of series to be concatenated
    axis : int, optional
        The axis along which to concatenate. Default is 1 (columns).
    allow_mix : bool
        Whether or not to allow a mix of pandas and non-pandas objects. Default
        is False. If true, the returned object is an ndarray, and additional
        pandas metadata (e.g. column names, indices, etc) is lost.

    Returns
    -------
    concatenated : array or pd.DataFrame
        The concatenated array. Will be a DataFrame if series are pandas
        objects.
    c             S   s   g | ]}t |d qS )N)r   )r4   r   r+   r+   r,   r6     s    zconcat.<locals>.<listcomp>)rT   zWAttempted to concatenate Pandas objects with non-Pandas objects with `allow_mix=False`.)rE   Zr_allpdconcatconcatenaterA   )rV   rT   Z	allow_mixZ	is_pandasZconcatenatedr+   r+   r,   rZ     s    
rZ   A?c             C   s$   t jt| }t t ||k S )a  
    Determine if a polynomial is invertible.

    Requires all roots of the polynomial lie inside the unit circle.

    Parameters
    ----------
    polynomial : array_like or tuple, list
        Coefficients of a polynomial, in order of increasing degree.
        For example, `polynomial=[1, -0.5]` corresponds to the polynomial
        :math:`1 - 0.5x` which has root :math:`2`. If it is a matrix
        polynomial (in which case the coefficients are coefficient matrices),
        a tuple or list of matrices should be passed.
    threshold : number
        Allowed threshold for `is_invertible` to return True. Default is 1.

    Notes
    -----

    If the coefficients provided are scalars :math:`(c_0, c_1, \dots, c_n)`,
    then the corresponding polynomial is :math:`c_0 + c_1 L + \dots + c_n L^n`.


    If the coefficients provided are matrices :math:`(C_0, C_1, \dots, C_n)`,
    then the corresponding polynomial is :math:`C_0 + C_1 L + \dots + C_n L^n`.

    There are three equivalent methods of determining if the polynomial
    represented by the coefficients is invertible:

    The first method factorizes the polynomial into:

    .. math::

        C(L) & = c_0 + c_1 L + \dots + c_n L^n \\
             & = constant (1 - \lambda_1 L)
                 (1 - \lambda_2 L) \dots (1 - \lambda_n L)

    In order for :math:`C(L)` to be invertible, it must be that each factor
    :math:`(1 - \lambda_i L)` is invertible; the condition is then that
    :math:`|\lambda_i| < 1`, where :math:`\lambda_i` is a root of the
    polynomial.

    The second method factorizes the polynomial into:

    .. math::

        C(L) & = c_0 + c_1 L + \dots + c_n L^n \\
             & = constant (L - \zeta_1) (L - \zeta_2) \dots (L - \zeta_3)

    The condition is now :math:`|\zeta_i| > 1`, where :math:`\zeta_i` is a root
    of the polynomial with reversed coefficients and
    :math:`\lambda_i = \frac{1}{\zeta_i}`.

    Finally, a companion matrix can be formed using the coefficients of the
    polynomial. Then the eigenvalues of that matrix give the roots of the
    polynomial. This last method is the one actually used.

    See Also
    --------
    companion_matrix
    )rE   rK   eigvalsrS   rX   abs)rN   Z	thresholdr]   r+   r+   r,   is_invertible  s    Cr_   c          	   C   s   t j| jd | jd}|s|   }t j|| }t || |}dt t t j| | || }t	|  || S |  }t j|| }t || |}dt t t j| | || }t	| || S dS )a  
    Solves the discrete Lyapunov equation using a bilinear transformation.

    Notes
    -----
    This is a modification of the version in Scipy (see
    https://github.com/scipy/scipy/blob/master/scipy/linalg/_solvers.py)
    which allows passing through the complex numbers in the matrix a
    (usually the transition matrix) in order to allow complex step
    differentiation.
    r   )r3      N)
rE   rG   shaper3   ZconjZ	transposerK   rL   rM   r   )aqZcomplex_steprG   ZaHZaHI_invbr	   r+   r+   r,   solve_discrete_lyapunov  s    $$re   c             C   s   | j d }tj||f| jd}| d| d  d  }xjt|D ]^}xHt|D ]<}||d |f || ||d || d f   |||f< qJW || |||f< q<W ||d ddf  S )a  
    Transform unconstrained parameters used by the optimizer to constrained
    parameters used in likelihood evaluation

    Parameters
    ----------
    unconstrained : array
        Unconstrained parameters used by the optimizer, to be transformed to
        stationary coefficients of, e.g., an autoregressive or moving average
        component.

    Returns
    -------
    constrained : array
        Constrained parameters of, e.g., an autoregressive or moving average
        component, to be transformed to arbitrary parameters used by the
        optimizer.

    References
    ----------
    .. [*] Monahan, John F. 1984.
       "A Note on Enforcing Stationarity in
       Autoregressive-moving Average Models."
       Biometrika 71 (2) (August 1): 403-404.
    r   )r3   r   r`   g      ?N)ra   rE   rH   r3   rI   )unconstrainedrO   yrkr5   r+   r+   r,   constrain_stationary_univariate  s    
<rj   c             C   s   | j d }tj||f| jd}|  ||d d< xvt|d ddD ]b}x\t|D ]P}|||f |||f |||| d f   d|||f d   ||d |f< qPW qBW | }|d|d  d  }|S )a  
    Transform constrained parameters used in likelihood evaluation
    to unconstrained parameters used by the optimizer

    Parameters
    ----------
    constrained : array
        Constrained parameters of, e.g., an autoregressive or moving average
        component, to be transformed to arbitrary parameters used by the
        optimizer.

    Returns
    -------
    unconstrained : array
        Unconstrained parameters used by the optimizer, to be transformed to
        stationary coefficients of, e.g., an autoregressive or moving average
        component.

    References
    ----------
    .. [*] Monahan, John F. 1984.
       "A Note on Enforcing Stationarity in
       Autoregressive-moving Average Models."
       Biometrika 71 (2) (August 1): 403-404.
    r   )r3   r   Nr`   g      ?)ra   rE   rH   r3   rI   Zdiagonal)constrainedrO   rg   ri   r5   rh   xr+   r+   r,   !unconstrain_stationary_univariate9  s    
Trn   c       
      C   s   ddl m} g }|dkr t| }|dkr6| d jd }t|}xNt|D ]B}| | }|j|t||j	 dd\}}	|
|j|||	d qJW |S )a  
    Transform arbitrary matrices to matrices with singular values less than
    one.

    Parameters
    ----------
    unconstrained : list
        Arbitrary matrices. Should be a list of length `order`, where each
        element is an array sized `k_endog` x `k_endog`.
    order : integer, optional
        The order of the autoregression.
    k_endog : integer, optional
        The dimension of the data vector.

    Returns
    -------
    constrained : list
        Partial autocorrelation matrices. Should be a list of length
        `order`, where each element is an array sized `k_endog` x `k_endog`.

    Notes
    -----
    Corresponds to Lemma 2.2 in Ansley and Kohn (1986). See
    `constrain_stationary_multivariate` for more details.

    There is a Cython implementation of this function that can be much faster,
    but which requires SciPy 0.14.0 or greater. See
    `constrain_stationary_multivariate` for details.

    r   )rK   NT)lower)scipyrK   r@   ra   rE   rG   rI   
cho_factorrM   rJ   appendsolve_triangular)
rf   orderk_endogrK   rl   rG   r5   ABro   r+   r+   r,   "_constrain_sv_less_than_one_python^  s    !
 rx   c             C   s  ddl m} |dkrt| }|dkr2| d jd }|sP|}t||| d  }|g}|g}|g}	g }
g }|j|dd}|g}|g}xt|D ]}|
}|}g }
g }|
|j	|| | | j
ddd t|| |
d j
|
d< ||j	|| | | ddd t|| |d j
|d< t|
d || }|	| j
 xt|D ]}|
||| t|
d	 |||d
     |||| t|d	 |||d
     |	|d
   t|	|d
  |||d
   j
7  < qJW ||| t||
| j
  ||| tt|| || || j
  ||j||d
  dd ||j||d
  dd qW |d	 }|stj|}tj|}t|tj|}tj|}x.t|D ]"}tt||
| ||
|< qW |
|fS )aC  
    Transform matrices with singular values less than one to matrices
    corresponding to a stationary (or invertible) process.

    Parameters
    ----------
    partial_autocorrelations : list
        Partial autocorrelation matrices. Should be a list of length `order`,
        where each element is an array sized `k_endog` x `k_endog`.
    error_variance : array
        The variance / covariance matrix of the error term. Should be sized
        `k_endog` x `k_endog`. This is used as input in the algorithm even if
        is not transformed by it (when `transform_variance` is False). The
        error term variance is required input when transformation is used
        either to force an autoregressive component to be stationary or to
        force a moving average component to be invertible.
    transform_variance : boolean, optional
        Whether or not to transform the error variance term. This option is
        not typically used, and the default is False.
    order : integer, optional
        The order of the autoregression.
    k_endog : integer, optional
        The dimension of the data vector.

    Returns
    -------
    coefficient_matrices : list
        Transformed coefficient matrices leading to a stationary VAR
        representation.

    Notes
    -----
    Corresponds to Lemma 2.1 in Ansley and Kohn (1986). See
    `constrain_stationary_multivariate` for more details.

    There is a Cython implementation of this function that can be much faster,
    but which requires SciPy 0.14.0 or greater. See
    `constrain_stationary_multivariate` for details.
    r   )rK   N
   T)ro   rJ   )ro   Ztransrk   r   )rp   rK   r@   ra   rE   rG   choleskyrI   rr   rs   rJ   rM   copyinsertrL   )partial_autocorrelationserror_variancetransform_variancert   ru   rK   Zinitial_varianceforward_variancesbackward_variancesautocovariancesforwards	backwardsZerror_variance_factorforward_factorsbackward_factorsr   prev_forwardsprev_backwardsZtmpri   varianceZinitial_variance_factorZtransformed_variance_factorZ	transformZinv_transformr5   r+   r+   r,   3_compute_coefficients_from_multivariate_pacf_python  sz    *	"r   c       	         s   t tk}|s:j\ }|  } fddt|D t}d jd  t| }t|||| \}}|stj|dd	  | }||fS )a  
    Transform unconstrained parameters used by the optimizer to constrained
    parameters used in likelihood evaluation for a vector autoregression.

    Parameters
    ----------
    unconstrained : array or list
        Arbitrary matrices to be transformed to stationary coefficient matrices
        of the VAR. If a list, should be a list of length `order`, where each
        element is an array sized `k_endog` x `k_endog`. If an array, should be
        the matrices horizontally concatenated and sized
        `k_endog` x `k_endog * order`.
    error_variance : array
        The variance / covariance matrix of the error term. Should be sized
        `k_endog` x `k_endog`. This is used as input in the algorithm even if
        is not transformed by it (when `transform_variance` is False). The
        error term variance is required input when transformation is used
        either to force an autoregressive component to be stationary or to
        force a moving average component to be invertible.
    transform_variance : boolean, optional
        Whether or not to transform the error variance term. This option is
        not typically used, and the default is False.
    prefix : {'s','d','c','z'}, optional
        The appropriate BLAS prefix to use for the passed datatypes. Only
        use if absolutely sure that the prefix is correct or an error will
        result.

    Returns
    -------
    constrained : array or list
        Transformed coefficient matrices leading to a stationary VAR
        representation. Will match the type of the passed `unconstrained`
        variable (so if a list was passed, a list will be returned).

    Notes
    -----
    In the notation of [1]_, the arguments `(variance, unconstrained)` are
    written as :math:`(\Sigma, A_1, \dots, A_p)`, where :math:`p` is the order
    of the vector autoregression, and is here determined by the length of
    the `unconstrained` argument.

    There are two steps in the constraining algorithm.

    First, :math:`(A_1, \dots, A_p)` are transformed into
    :math:`(P_1, \dots, P_p)` via Lemma 2.2 of [1]_.

    Second, :math:`(\Sigma, P_1, \dots, P_p)` are transformed into
    :math:`(\Sigma, \phi_1, \dots, \phi_p)` via Lemmas 2.1 and 2.3 of [1]_.

    If `transform_variance=True`, then only Lemma 2.1 is applied in the second
    step.

    While this function can be used even in the univariate case, it is much
    slower, so in that case `constrain_stationary_univariate` is preferred.

    References
    ----------
    .. [1] Ansley, Craig F., and Robert Kohn. 1986.
       "A Note on Reparameterizing a Vector Autoregressive Moving Average Model
       to Enforce Stationarity."
       Journal of Statistical Computation and Simulation 24 (2): 99-106.
    .. [*] Ansley, Craig F, and Paul Newbold. 1979.
       "Multivariate Partial Autocorrelations."
       In Proceedings of the Business and Economic Statistics Section, 349-53.
       American Statistical Association

    c                s,   g | ]$}d  |  |d   f qS )Nr   r+   )r4   r5   )ru   rf   r+   r,   r6     s   z<constrain_stationary_multivariate_python.<locals>.<listcomp>r   r   )rT   )
typerB   ra   rI   r@   rx   r   rE   r[   Zreshape)	rf   r~   r   r=   use_listrt   sv_constrainedrl   varr+   )ru   rf   r,   (constrain_stationary_multivariate_python<  s     G

r   c       	         s   t | tk}|rtj| dd} | j\}| }|dk r@tddk rPtd|d krjt| |g\}}}t| }tj| |d} tj||d}t	| | |}t
| ||||\ }tj |d tj||d}|r fddt|D   |fS )Nr   )rT   zMust have order at least 1z(Must have at least 1 endogenous variable)r3   c                s,   g | ]$} d | |d  f qS )Nr   r+   )r4   r5   )rl   ru   r+   r,   r6     s   z5constrain_stationary_multivariate.<locals>.<listcomp>)r   rB   rE   r[   ra   rA   r.   prefix_dtype_mapasfortranarrayr!   r    arrayrI   )	rf   r   r   r=   r   rt   r3   _r   r+   )rl   ru   r,   !constrain_stationary_multivariate  s0    
r   c       
      C   s   ddl m} g }|dkr t| }|dkr6| d jd }t|}xNt|D ]B}| | }|j|t||j	 dd\}}	|
|j|||	d qJW |S )a  
    Transform matrices with singular values less than one to arbitrary
    matrices.

    Parameters
    ----------
    constrained : list
        The partial autocorrelation matrices. Should be a list of length
        `order`, where each element is an array sized `k_endog` x `k_endog`.
    order : integer, optional
        The order of the autoregression.
    k_endog : integer, optional
        The dimension of the data vector.

    Returns
    -------
    unconstrained : list
        Unconstrained matrices. A list of length `order`, where each element is
        an array sized `k_endog` x `k_endog`.

    Notes
    -----
    Corresponds to the inverse of Lemma 2.2 in Ansley and Kohn (1986). See
    `unconstrain_stationary_multivariate` for more details.

    r   )rK   NT)ro   )rp   rK   r@   ra   rE   rG   rI   rq   rM   rJ   rr   rs   )
rl   rt   ru   rK   rf   rG   r5   PZB_invro   r+   r+   r,   _unconstrain_sv_less_than_one  s    
 r   c             C   s   t | } | jdkr&| ddt jf } | t j| dd8 } | j\}}g }xrt|d D ]b}|t ||f x8t|| D ](}||  t 	| | | ||  7  < qzW ||  |  < qTW |S )a|  
    Computer multivariate sample autocovariances

    Parameters
    ----------
    endog : array_like
        Sample data on which to compute sample autocovariances. Shaped
        `nobs` x `k_endog`.

    Returns
    -------
    sample_autocovariances : list
        A list of the first `maxlag` sample autocovariance matrices. Each
        matrix is shaped `k_endog` x `k_endog`.

    Notes
    -----
    This function computes the forward sample autocovariances:

    .. math::

        \hat \Gamma(s) = rac{1}{n} \sum_{t=1}^{n-s}
        (Z_t - ar Z) (Z_{t+s} - ar Z)'

    See page 353 of Wei (1990). This function is primarily implemented for
    checking the partial autocorrelation functions below, and so is quite slow.

    References
    ----------
    .. [*] Wei, William. 1990.
        Time Series Analysis : Univariate and Multivariate Methods.
       Boston: Pearson.
    r   Nr   )rT   )
rE   r   ndimZnewaxisZmeanra   rI   rr   rH   Zouter)endogmaxlagnobsru   sample_autocovariancesr   tr+   r+   r,   "_compute_multivariate_sample_acovf
  s    #


(r   c       
         sL  ddl m} t tkr0t } d jd n* j\}| } fddt|D  |dkrj|d }tdg fddt|D  j}t	
|j}||ddf< |||fddtt||d D }x@t||d  D ],}	t	||d df g7 }qW |rHx$tt|D ]}	||	 j||	< q0W |S )	a  
    Compute multivariate autocovariances from vector autoregression coefficient
    matrices

    Parameters
    ----------
    coefficients : array or list
        The coefficients matrices. If a list, should be a list of length
        `order`, where each element is an array sized `k_endog` x `k_endog`. If
        an array, should be the coefficient matrices horizontally concatenated
        and sized `k_endog` x `k_endog * order`.
    error_variance : array
        The variance / covariance matrix of the error term. Should be sized
        `k_endog` x `k_endog`.
    maxlag : integer, optional
        The maximum autocovariance to compute. Default is `order`-1. Can be
        zero, in which case it returns the variance.
    forward_autocovariances : boolean, optional
        Whether or not to compute forward autocovariances
        :math:`E(y_t y_{t+j}')`. Default is False, so that backward
        autocovariances :math:`E(y_t y_{t-j}')` are returned.

    Returns
    -------
    autocovariances : list
        A list of the first `maxlag` autocovariance matrices. Each matrix is
        shaped `k_endog` x `k_endog`.

    Notes
    -----
    Computes

    .. math::

        \Gamma(j) = E(y_t y_{t-j}')

    for j = 1, ..., `maxlag`, unless `forward_autocovariances` is specified,
    in which case it computes:

    .. math::

        E(y_t y_{t+j}') = \Gamma(j)'

    Coefficients are assumed to be provided from the VAR model:

    .. math::
        y_t = A_1 y_{t-1} + \dots + A_p y_{t-p} + \varepsilon_t

    Autocovariances are calculated by solving the associated discrete Lyapunov
    equation of the state space representation of the VAR process.

    r   )rK   c                s,   g | ]$} d | |d  f qS )Nr   r+   )r4   r5   )coefficientsru   r+   r,   r6     s   zA_compute_multivariate_acovf_from_coefficients.<locals>.<listcomp>Nr   c                s   g | ]} |  qS r+   r+   )r4   r5   )r   r+   r,   r6     s    c                s,   g | ]$}d  |  |d   f qS )Nr   r+   )r4   r5   )ru   stacked_covr+   r,   r6     s   )rp   rK   r   rB   r@   ra   rI   rS   rJ   rE   rH   re   minrM   )
r   r~   r   Zforward_autocovariancesrK   rt   Z	companionZselected_variancer   r5   r+   )r   ru   r   r,   -_compute_multivariate_acovf_from_coefficients?  s2    7
 r   c             C   s   t | |}t|S )a  
    Computer multivariate sample partial autocorrelations

    Parameters
    ----------
    endog : array_like
        Sample data on which to compute sample autocovariances. Shaped
        `nobs` x `k_endog`.
    maxlag : integer
        Maximum lag for which to calculate sample partial autocorrelations.

    Returns
    -------
    sample_pacf : list
        A list of the first `maxlag` sample partial autocorrelation matrices.
        Each matrix is shaped `k_endog` x `k_endog`.

    )r   /_compute_multivariate_pacf_from_autocovariances)r   r   r   r+   r+   r,   !_compute_multivariate_sample_pacf  s    
r   c             C   s  ddl m} |dkr t| d }|dkr6| d jd }g }g }g }g }g }g }	g }
x6t|D ](}t|}t|}g }g }| d  }| d j }xJt|D ]>}|t	|| | |d  8 }|t	|| | |d  j8 }qW |
| |
| |
|j|| dd |	
|j|| dd |dkrt|
||d df| d j |
||	d df| d jj n~| |d  j }x0t|D ]$}|t	|| | ||  j8 }qW |
||	| df|jj |
||| df|j xjt|D ]^}|||| t	|d |||d     |||| t	|d |||d     qW |

|j|| t	|| |	| dd q^W |
S )aa  
    Compute multivariate partial autocorrelations from autocovariances.

    Parameters
    ----------
    autocovariances : list
        Autocorrelations matrices. Should be a list of length `order` + 1,
        where each element is an array sized `k_endog` x `k_endog`.
    order : integer, optional
        The order of the autoregression.
    k_endog : integer, optional
        The dimension of the data vector.

    Returns
    -------
    pacf : list
        List of first `order` multivariate partial autocorrelations.

    Notes
    -----
    Note that this computes multivariate partial autocorrelations.

    Corresponds to the inverse of Lemma 2.1 in Ansley and Kohn (1986). See
    `unconstrain_stationary_multivariate` for more details.

    Notes
    -----
    Computes sample partial autocorrelations if sample autocovariances are
    given.

    r   )rK   Nr   T)ro   rk   )rp   rK   r@   ra   rI   rB   r{   rJ   rE   rM   rr   rz   Z	cho_solver|   rs   )r   rt   ru   rK   r   r   r   r   r   r   r}   r   r   r   Zforward_varianceZbackward_varianceri   Ztmp_sumr+   r+   r,   r     sf    !



$
"r   c             C   sZ   t | tkr$t| }| d jd }n| j\}}|| }t}dd || ||dD }t|S )a  
    Transform matrices corresponding to a stationary (or invertible) process
    to matrices with singular values less than one.

    Parameters
    ----------
    constrained : array or list
        The coefficients matrices. If a list, should be a list of length
        `order`, where each element is an array sized `k_endog` x `k_endog`. If
        an array, should be the coefficient matrices horizontally concatenated
        and sized `k_endog` x `k_endog * order`.
    error_variance : array
        The variance / covariance matrix of the error term. Should be sized
        `k_endog` x `k_endog`.
    order : integer, optional
        The order of the autoregression.
    k_endog : integer, optional
        The dimension of the data vector.

    Returns
    -------
    pacf : list
        List of first `order` multivariate partial autocorrelations.

    Notes
    -----
    Note that this computes multivariate partial autocorrelations.

    Corresponds to the inverse of Lemma 2.1 in Ansley and Kohn (1986). See
    `unconstrain_stationary_multivariate` for more details.

    Notes
    -----

    Coefficients are assumed to be provided from the VAR model:

    .. math::
        y_t = A_1 y_{t-1} + \dots + A_p y_{t-p} + arepsilon_t
    r   c             S   s   g | ]
}|j qS r+   )rJ   )r4   Zautocovariancer+   r+   r,   r6     s    z@_compute_multivariate_pacf_from_coefficients.<locals>.<listcomp>)r   )r   rB   r@   ra   r   r   )rl   r~   rt   ru   Z_acovfr   r+   r+   r,   ,_compute_multivariate_pacf_from_coefficientsd  s    *
r   c                s   t  tk}|s< j\}| } fddt|D  nt } d jd t ||}t||}|s~tj|dd}||fS )a  
    Transform constrained parameters used in likelihood evaluation
    to unconstrained parameters used by the optimizer

    Parameters
    ----------
    constrained : array or list
        Constrained parameters of, e.g., an autoregressive or moving average
        component, to be transformed to arbitrary parameters used by the
        optimizer. If a list, should be a list of length `order`, where each
        element is an array sized `k_endog` x `k_endog`. If an array, should be
        the coefficient matrices horizontally concatenated and sized
        `k_endog` x `k_endog * order`.
    error_variance : array
        The variance / covariance matrix of the error term. Should be sized
        `k_endog` x `k_endog`. This is used as input in the algorithm even if
        is not transformed by it (when `transform_variance` is False).

    Returns
    -------
    unconstrained : array
        Unconstrained parameters used by the optimizer, to be transformed to
        stationary coefficients of, e.g., an autoregressive or moving average
        component. Will match the type of the passed `constrained`
        variable (so if a list was passed, a list will be returned).

    Notes
    -----
    Uses the list representation internally, even if an array is passed.

    References
    ----------
    .. [*] Ansley, Craig F., and Robert Kohn. 1986.
       "A Note on Reparameterizing a Vector Autoregressive Moving Average Model
       to Enforce Stationarity."
       Journal of Statistical Computation and Simulation 24 (2): 99-106.

    c                s,   g | ]$} d | |d  f qS )Nr   r+   )r4   r5   )rl   ru   r+   r,   r6     s   z7unconstrain_stationary_multivariate.<locals>.<listcomp>r   r   )rT   )	r   rB   ra   rI   r@   r   r   rE   r[   )rl   r~   r   rt   r}   rf   r+   )rl   ru   r,   #unconstrain_stationary_multivariate  s    '

r   c             C   s   t |}|dkr td| |f |d |ksBtd| ||d f |d |ksdtd| ||d f |dkr|dks|d	 dkstd
|  |dkr|dk	r|d	 d|gkrtd| |t|f dS )a  
    Validate the shape of a possibly time-varying matrix, or raise an exception

    Parameters
    ----------
    name : str
        The name of the matrix being validated (used in exception messages)
    shape : array_like
        The shape of the matrix to be validated. May be of size 2 or (if
        the matrix is time-varying) 3.
    nrows : int
        The expected number of rows.
    ncols : int
        The expected number of columns.
    nobs : int
        The number of observations (used to validate the last dimension of a
        time-varying matrix)

    Raises
    ------
    ValueError
        If the matrix is not of the desired shape.
    )r`      zTInvalid value for %s matrix. Requires a 2- or 3-dimensional array, got %d dimensionsr   z:Invalid dimensions for %s matrix: requires %d rows, got %dr   z=Invalid dimensions for %s matrix: requires %d columns, got %dNr`   rk   zInvalid dimensions for %s matrix: time-varying matrices cannot be given unless `nobs` is specified (implicitly when a dataset is bound or else set explicity)r   zNInvalid dimensions for time-varying %s matrix. Requires shape (*,*,%d), got %s)r@   rA   str)namera   nrowsZncolsr   r   r+   r+   r,   validate_matrix_shape  s      r   c             C   s   t |}|dkr td| |f |d |ksBtd| ||d f |dkrj|dksj|d dksjtd|  |d	kr|d d|gkrtd
| |t|f dS )av  
    Validate the shape of a possibly time-varying vector, or raise an exception

    Parameters
    ----------
    name : str
        The name of the vector being validated (used in exception messages)
    shape : array_like
        The shape of the vector to be validated. May be of size 1 or (if
        the vector is time-varying) 2.
    nrows : int
        The expected number of rows (elements of the vector).
    nobs : int
        The number of observations (used to validate the last dimension of a
        time-varying vector)

    Raises
    ------
    ValueError
        If the vector is not of the desired shape.
    )r   r`   zTInvalid value for %s vector. Requires a 1- or 2-dimensional array, got %d dimensionsr   z:Invalid dimensions for %s vector: requires %d rows, got %dNr   rk   zInvalid dimensions for %s vector: time-varying vectors cannot be given unless `nobs` is specified (implicitly when a dataset is bound or else set explicity)r`   zLInvalid dimensions for time-varying %s vector. Requires shape (*,%d), got %s)r@   rA   r   )r   ra   r   r   r   r+   r+   r,   validate_vector_shape  s    r   c             C   sJ   |dkrt | fd }t| }|s0tj| dd} || t|||| | S )a  
    Reorder the rows or columns of a time-varying matrix where all non-missing
    values are in the upper left corner of the matrix.

    Parameters
    ----------
    matrix : array_like
        The matrix to be reordered. Must have shape (n, m, nobs).
    missing : array_like of bool
        The vector of missing indices. Must have shape (k, nobs) where `k = n`
        if `reorder_rows is True` and `k = m` if `reorder_cols is True`.
    reorder_rows : bool, optional
        Whether or not the rows of the matrix should be re-ordered. Default
        is False.
    reorder_cols : bool, optional
        Whether or not the columns of the matrix should be re-ordered. Default
        is False.
    is_diagonal : bool, optional
        Whether or not the matrix is diagonal. If this is True, must also have
        `n = m`. Default is False.
    inplace : bool, optional
        Whether or not to reorder the matrix in-place.
    prefix : {'s', 'd', 'c', 'z'}, optional
        The Fortran prefix of the vector. Default is to automatically detect
        the dtype. This parameter should only be used with caution.

    Returns
    -------
    reordered_matrix : array_like
        The reordered matrix.

    Nr   r0   )rt   )r.   r"   rE   r{   r   )rQ   missingZreorder_rowsZreorder_colsis_diagonalinplacer=   reorderr+   r+   r,   reorder_missing_matrixJ  s    #r   c             C   sD   |dkrt | fd }t| }|s0tj| dd} || t| | S )a  
    Reorder the elements of a time-varying vector where all non-missing
    values are in the first elements of the vector.

    Parameters
    ----------
    vector : array_like
        The vector to be reordered. Must have shape (n, nobs).
    missing : array_like of bool
        The vector of missing indices. Must have shape (n, nobs).
    inplace : bool, optional
        Whether or not to reorder the matrix in-place. Default is False.
    prefix : {'s', 'd', 'c', 'z'}, optional
        The Fortran prefix of the vector. Default is to automatically detect
        the dtype. This parameter should only be used with caution.

    Returns
    -------
    reordered_vector : array_like
        The reordered vector.

    Nr   r0   )rt   )r.   r#   rE   r{   r   )Zvectorr   r   r=   r   r+   r+   r,   reorder_missing_vectorz  s    r   c       	      C   sx   |dkrt | |fd }t| }|s2tj|dd}y|  sBt W n   t| } Y nX || |t|||| |S )aU  
    Copy the rows or columns of a time-varying matrix where all non-missing
    values are in the upper left corner of the matrix.

    Parameters
    ----------
    A : array_like
        The matrix from which to copy. Must have shape (n, m, nobs) or
        (n, m, 1).
    B : array_like
        The matrix to copy to. Must have shape (n, m, nobs).
    missing : array_like of bool
        The vector of missing indices. Must have shape (k, nobs) where `k = n`
        if `reorder_rows is True` and `k = m` if `reorder_cols is True`.
    missing_rows : bool, optional
        Whether or not the rows of the matrix are a missing dimension. Default
        is False.
    missing_cols : bool, optional
        Whether or not the columns of the matrix are a missing dimension.
        Default is False.
    is_diagonal : bool, optional
        Whether or not the matrix is diagonal. If this is True, must also have
        `n = m`. Default is False.
    inplace : bool, optional
        Whether or not to copy to B in-place. Default is False.
    prefix : {'s', 'd', 'c', 'z'}, optional
        The Fortran prefix of the vector. Default is to automatically detect
        the dtype. This parameter should only be used with caution.

    Returns
    -------
    copied_matrix : array_like
        The matrix B with the non-missing submatrix of A copied onto it.

    Nr   r0   )rt   )r.   r$   rE   r{   is_f_contigrA   r   )	rv   rw   r   Zmissing_rowsZmissing_colsr   r   r=   r{   r+   r+   r,   copy_missing_matrix  s    %
r   c             C   sr   |dkrt | |fd }t| }|s2tj|dd}y|  sBt W n   t| } Y nX || |t| |S )aK  
    Reorder the elements of a time-varying vector where all non-missing
    values are in the first elements of the vector.

    Parameters
    ----------
    a : array_like
        The vector from which to copy. Must have shape (n, nobs) or (n, 1).
    b : array_like
        The vector to copy to. Must have shape (n, nobs).
    missing : array_like of bool
        The vector of missing indices. Must have shape (n, nobs).
    inplace : bool, optional
        Whether or not to copy to b in-place. Default is False.
    prefix : {'s', 'd', 'c', 'z'}, optional
        The Fortran prefix of the vector. Default is to automatically detect
        the dtype. This parameter should only be used with caution.

    Returns
    -------
    copied_vector : array_like
        The vector b with the non-missing subvector of b copied onto it.

    Nr   r0   )rt   )r.   r%   rE   r{   r   rA   r   )rb   rd   r   r   r=   r{   r+   r+   r,   copy_missing_vector  s    
r   c       	      C   sx   |dkrt | |fd }t| }|s2tj|dd}y|  sBt W n   t| } Y nX || |t|||| |S )aE  
    Copy the rows or columns of a time-varying matrix where all non-index
    values are in the upper left corner of the matrix.

    Parameters
    ----------
    A : array_like
        The matrix from which to copy. Must have shape (n, m, nobs) or
        (n, m, 1).
    B : array_like
        The matrix to copy to. Must have shape (n, m, nobs).
    index : array_like of bool
        The vector of index indices. Must have shape (k, nobs) where `k = n`
        if `reorder_rows is True` and `k = m` if `reorder_cols is True`.
    index_rows : bool, optional
        Whether or not the rows of the matrix are a index dimension. Default
        is False.
    index_cols : bool, optional
        Whether or not the columns of the matrix are a index dimension.
        Default is False.
    is_diagonal : bool, optional
        Whether or not the matrix is diagonal. If this is True, must also have
        `n = m`. Default is False.
    inplace : bool, optional
        Whether or not to copy to B in-place. Default is False.
    prefix : {'s', 'd', 'c', 'z'}, optional
        The Fortran prefix of the vector. Default is to automatically detect
        the dtype. This parameter should only be used with caution.

    Returns
    -------
    copied_matrix : array_like
        The matrix B with the non-index submatrix of A copied onto it.

    Nr   r0   )rt   )r.   r&   rE   r{   r   rA   r   )	rv   rw   r<   Z
index_rowsZ
index_colsr   r   r=   r{   r+   r+   r,   copy_index_matrix  s    %
r   c             C   sr   |dkrt | |fd }t| }|s2tj|dd}y|  sBt W n   t| } Y nX || |t| |S )aC  
    Reorder the elements of a time-varying vector where all non-index
    values are in the first elements of the vector.

    Parameters
    ----------
    a : array_like
        The vector from which to copy. Must have shape (n, nobs) or (n, 1).
    b : array_like
        The vector to copy to. Must have shape (n, nobs).
    index : array_like of bool
        The vector of index indices. Must have shape (n, nobs).
    inplace : bool, optional
        Whether or not to copy to b in-place. Default is False.
    prefix : {'s', 'd', 'c', 'z'}, optional
        The Fortran prefix of the vector. Default is to automatically detect
        the dtype. This parameter should only be used with caution.

    Returns
    -------
    copied_vector : array_like
        The vector b with the non-index subvector of b copied onto it.

    Nr   r0   )rt   )r.   r'   rE   r{   r   rA   r   )rb   rd   r<   r   r=   r{   r+   r+   r,   copy_index_vector>  s    
r   c             C   s`   d}| d k	rXt | d }|s$t| } | jdkrN|sD| d d d f } n
t| } | jd }|| fS )Nr   r   )r   rE   Zasarrayr   rY   Z	DataFramera   )ZexogZk_exogZexog_is_using_pandasr+   r+   r,   prepare_exogk  s    




r   )N)r   Nr   )r   F)r\   )F)NN)FNN)FN)FN)NN)NF)NN)NN)FFFFN)FN)FFFFN)FN)FFFFN)FN)A__doc__Z
__future__r   r   r   ZnumpyrE   r   r   rW   rY   Zstatsmodels.tools.datar   r   r   r)   Zfloat32Zfloat64Z	complex64Z
complex128r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r-   r(   r.   r   r9   rS   rU   rZ   r_   re   rj   rn   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r+   r+   r+   r,   <module>   s   
 ,
y
8
"
G
%% 
1 
 - 
f 
-

.6 
k
  
<E5/  
.
# 
9
- 
9
-