U
    Kvf                  
   @   s  d Z ddlZddl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 Z+dS )aJ  VAR and VARMA process

this does not 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
    N)signalc                 C   st   |j d }| j d }t| j }t||D ]D}|| || |ddtjf | jddjdd ||ddf< q*|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
    ----------
    https://en.wikipedia.org/wiki/Vector_Autoregression
    https://en.wikipedia.org/wiki/General_matrix_notation_of_a_VAR(p)
    r   N   Zaxis)shapenpzerosrangenewaxissum)xBconstpTxhatt r   A/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/sandbox/tsa/varma.pyVAR    s    !

Br   c                 C   s   |j d }|j d }| j d }t| j }t| j }t||}	t|	|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< qJ||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   VARMAM   s    




2..r   __main__         r   )r   g      ?)r   r   r   Zvalid)acovfacf)r   )r   ),__doc__Znumpyr   Zscipyr   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"