B
    ZX              
   @   s  d Z ddlmZ ddlZddlmZ ddlmZm	Z	 dddZ
ddd	Zed
krdZdZdZeeege ZeeeefZddgddgddggedddddf< e
eeZeeeeddf eedddf eed k dZdZdZdZdZeeege ZeeeefZddgddgddggedddddf< eeeefZe
eeedZeeeeed\ZZeeeek eeeeddf eedddf eed e k ded< eeee\Z Z!ej"eeefef Zeeee\Z#Z$ded< ddgddgddggedddddf< eeee\Z%Z&eeedee gZ'eeefedddddf< eeefedddddf< ded< e
e'eZ(ee'edddddf Z)ee'edddddf Z*eeee'edddddf ddddf e(eddf k eeee'edddddf ddddf e(eddf k ddl+m,Z,m-Z- e,edddf Z.ee.d e/edddf k e-edddf Z0dS )aI  VAR and VARMA process

this doesn't actually do much, trying out a version for a time loop

alternative representation:
* textbook, different blocks in matrices
* Kalman filter
* VAR, VARX and ARX could be calculated with signal.lfilter
  only tried some examples, not implemented

TODO: try minimizing sum of squares of (Y-Yhat)

Note: filter has smallest lag at end of array and largest lag at beginning,
    be careful for asymmetric lags coefficients
    check this again if it is consistently used


changes
2009-09-08 : separated from movstat.py

Author : josefpkt
License : BSD
    )print_functionN)signal)assert_array_equalassert_array_almost_equalc             C   sx   |j d }| j d }t| j }xRt||D ]D}|| || |ddtjf | jddjdd ||ddf< q,W |S )a   multivariate linear filter

    Parameters
    ----------
    x: (TxK) array
        columns are variables, rows are observations for time period
    B: (PxKxK) array
        b_t-1 is bottom "row", b_t-P is top "row" when printing
        B(:,:,0) is lag polynomial matrix for variable 1
        B(:,:,k) is lag polynomial matrix for variable k
        B(p,:,k) is pth lag for variable k
        B[p,:,:].T corresponds to A_p in Wikipedia
    const: float or array (not tested)
        constant added to autoregression

    Returns
    -------
    xhat: (TxK) array
        filtered, predicted values of x array

    Notes
    -----
    xhat(t,i) = sum{_p}sum{_k} { x(t-P:t,:) .* B(:,:,i) }  for all i = 0,K-1, for all t=p..T

    xhat does not include the forecasting observation, xhat(T+1),
    xhat is 1 row shorter than signal.correlate

    References
    ----------
    http://en.wikipedia.org/wiki/Vector_Autoregression
    http://en.wikipedia.org/wiki/General_matrix_notation_of_a_VAR(p)
    r   N   )axis)shapenpzerosrangenewaxissum)xBconstpTxhatt r   <lib/python3.7/site-packages/statsmodels/sandbox/tsa/varma.pyVAR#   s    !

Dr   c             C   s   |j d }|j d }| j d }t| j }t| j }t||}	xt|	|D ]}
|| |
| |
ddtjf | jddjdd ||
| |
ddtjf | jddjdd ||
ddf< | |
ddf ||
ddf  ||
ddf< qLW ||fS )z multivariate linear filter

    x (TxK)
    B (PxKxK)

    xhat(t,i) = sum{_p}sum{_k} { x(t-P:t,:) .* B(:,:,i) } +
                sum{_q}sum{_k} { e(t-Q:t,:) .* C(:,:,i) }for all i = 0,K-1

    r   Nr   )r   )r   r	   r
   maxr   r   r   )r   r   Cr   PQr   r   estartr   r   r   r   VARMAP   s    




2>0r   __main__         r   )r   g      ?)r   r   r   Zvalid)acovfacf)r   )r   )1__doc__Z
__future__r   Znumpyr	   Zscipyr   Znumpy.testingr   r   r   r   __name__r   Kr   Zcolumn_stackZaranger   Zonesr   r   printallZ	correlater   r   r
   r   Zxhat1Zxhat2Zerr2Zxhat3Zerr3Zr_Zxhat4Zerr4Zxhat5Zerr5Zx0Zxhat0Zxcorr00Zxcorr01Zstatsmodels.tsa.stattoolsr$   r%   ZaavvarZaacr   r   r   r   <module>   s`   
-

(
<(@(  
FF"