
    M/Ph+                         d Z ddlmZ ddlmZ ddlmZmZ ddlZ	ddl
ZddlmZmZ ddlmc mZ dd	Zdd
ZddZd Zd ZddZd ZddZd Zd Zd ZddZdS )z/
Miscellaneous utility code for VAR estimation
    )frequencies)asbytes)
array_likeint_likeN)statslinalgcskipc                      t                     }t          j         fdt          |          D                       }|dk    rt	          j        |d||          }|S )u   
    Make predictor matrix for VAR(p) process

    Z := (Z_0, ..., Z_T).T (T x Kp)
    Z_t = [1 y_t y_{t-1} ... y_{t - p + 1}] (Kp x 1)

    Ref: Lütkepohl p.70 (transposed)

    has_constant can be 'raise', 'add', or 'skip'. See add_constant.
    c                 `    g | ]*}|z
  |         d d d                                          +S )N)ravel).0tlagsys     ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/vector_ar/util.py
<listcomp>z!get_var_endog.<locals>.<listcomp>   s<    III!!AdFQJ-"%++--III    nT)prependtrendhas_constant)lennparrayrangetsa	add_trend)r   r   r   r   nobsZs   ``    r   get_var_endogr"      ss     q66D
IIIIIuT47H7HIIIJJA ||M!T'35 5 5 Hr   c                 n    | dk    rd}n+| dv rd}n$| dk    rd}n| dk    rd}nt          d	|            |S )
Nr	      )r   ncr   ct   ctt   zUnkown trend: )
ValueError)r   
trendorders     r   get_trendorderr,   (   sa    ||

	+		

	$

	%

1%11222r   r$   c                    g }t          | t                    r| g} t          d|dz             D ]V}| D ]Q}t          |t                    st          |          }|                    dt          |          z   dz   |z              RW|dk    r|                    dd           |dk    r|                    dd           |dk    r|                    dd           |t          |t
          j                  rt          j        |          }n$t          |d
          st          j
        |          }|j        dk    r|d	d	d	f         }t          |j        d                   D ]b}t          |t
          j                  rt          |j        |                   }ndt          |          z   }|                    ||z   |           c|S )z
    Produce list of lag-variable names. Constant / trends go at the beginning

    Examples
    --------
    >>> make_lag_names(['foo', 'bar'], 2, 1)
    ['const', 'L1.foo', 'L1.bar', 'L2.foo', 'L2.bar']
    r$   L.r   constr   r'   ztrend**2Nndimexog)
isinstancestrr   appendinsertpdSeries	DataFramehasattrr   asarrayr1   shapecolumns)names	lag_orderr+   r2   	lag_namesiname	exog_names           r   make_lag_namesrD   7   s    I%  1i!m$$ 2 2 	2 	2DdC(( !4yySQZ^D01111	2 QG$$$A~~G$$$A~~J'''dBI&& 	$<%%DDv&& 	$:d##D9>>4=Dtz!}%% 	8 	8A$-- ,Q00		"SVVO	Z!^Y7777r   c                    | j         \  }}}||k    rt          d          ||z  }t          j        ||f          }t          j        | d          |d|<   |dk    r/d|t          j        ||          t          j        ||z
            f<   |S )z
    Return compansion matrix for the VAR(1) representation for a VAR(p) process
    (companion form)

    A = [A_1 A_2 ... A_p-1 A_p
         I_K 0       0     0
         0   I_K ... 0     0
         0 ...       I_K   0]
    z'coefs must be 3-d with shape (p, k, k).r$   )axisN)r<   r*   r   zerosconcatenatearange)coefspk1k2kpresults         r   comp_matrixrP   b   s     IAr2	RxxBCCC	aBXr2hF.Q///F3B3K 	1uu67ryR  ")BrE"2"223Mr   c                 "   ddl m} ddlm} ddl}|                    t          d                    }t          | d          5 } ||          }ddd           n# 1 swxY w Y   d}t          d          |                                vr(|dz  }t          d          |                                v(	 |dz  }|                                }|                    |          }	|	r|		                                \  }
}}nJt          j        | d
|dz                                 d          }t          |          }t          |          }t          |
          }
t          d          t          j                    t          d          t          j                    t          d          t          j                    i}||         }||dz
  z  }|                     ||
dd                    |z   }||         }t          j        |||          }||fS )u`   
    Parse data files from Lütkepohl (2005) book

    Source for data files: www.jmulti.de
    r   )deque)datetimeNz<(.*) (\w)([\d]+)>.*rbz*/r$   Tz\s+)	delimiterheaderF)indexQMA)startfreqperiods)collectionsrR   rS   recompiler   openpopleftmatchgroupsr7   read_csv
to_recordsr   intr   BQuarterEnd	BMonthEndBYearEndrollforward
date_range)pathrR   rS   r_   regexflinesto_skiplinemyearr\   start_pointdatar   offsetsoffsetinc
start_daterl   s                       r   parse_lutkepohl_datar{      sQ    "!!!!!!!!!!!IIIJJw67788E	dD		 Qa               G
$--u}}
.
.1 $--u}}
.
.1}}KK 	&'hhjj#D$ KwqyAAAZeZ$$ 	 	D		A k""Kt99D 	k-//k+--k*,,G T]F
K!O
$C##HHT1a$8$899C?JT]FZfaHHHJs   AA"A皙?c                 L    t           j                            d| dz  z
            S )Nr$   r'   )r   normppf)alphas    r   norm_signif_levelr      s    :>>!eai-(((r   c                     t          j        | d                   }| t          j        t          j        ||                    z  S )Nr   )r   diagsqrtouter)acfr   s     r   acf_to_acorrr      s4    73q6??D$--....r   d   c           
      t   t           j                            |          }|j        }| j        \  }	}
}
t          |dd          }t          |t                    r|dk    rt          d          |||
f}d}n|||
f}|t          j	        |
          } |t          j
        t          |                    |||z                                |||
          }t          j
        |||
f          }|mt          j        |          dk    r-t          |          |j        d         k    st          d	          ||z  }|dd|	dfxx         |dd|	df         z  cc<   n|dd|	df         |dd|	df<   t          |d
dd          }|3|j        |	|
fk    s|j        |
fk    st          d          ||ddd|	f<   t          |	|          D ]X}|dd|f         }t          |	          D ]:}|t          j        | |         |dd||z
  dz
  f         j                  j        z  };Y|                    |          S )a  
    Simulate VAR(p) process, given coefficients and assuming Gaussian noise

    Parameters
    ----------
    coefs : ndarray
        Coefficients for the VAR lags of endog.
    intercept : None or ndarray 1-D (neqs,) or (steps, neqs)
        This can be either the intercept for each equation or an offset.
        If None, then the VAR process has a zero intercept.
        If intercept is 1-D, then the same (endog specific) intercept is added
        to all observations.
        If intercept is 2-D, then it is treated as an offset and is added as
        an observation specific intercept to the autoregression. In this case,
        the intercept/offset should have same number of rows as steps, and the
        same number of columns as endogenous variables (neqs).
    sig_u : ndarray
        Covariance matrix of the residuals or innovations.
        If sig_u is None, then an identity matrix is used.
    steps : {None, int}
        number of observations to simulate, this includes the initial
        observations to start the autoregressive process.
        If offset is not None, then exog of the model are used if they were
        provided in the model
    initial_values : array_like, optional
        Initial values for use in the simulation. Shape should be
        (nlags, neqs) or (neqs,). Values should be ordered from less to
        most recent. Note that this values will be returned by the
        simulation as the first values of `endog_simulated` and they
        will count for the total number of steps.
    seed : {None, int}
        If seed is not None, then it will be used with for the random
        variables generated by numpy.random.
    nsimulations : {None, int}
        Number of simulations to perform. If `nsimulations` is None it will
        perform one simulation and return value will have shape (steps, neqs).

    Returns
    -------
    endog_simulated : nd_array
        Endog of the simulated VAR process. Shape will be (nsimulations, steps, neqs)
        or (steps, neqs) if `nsimulations` is None.
    )seednsimulationsT)optionalr   z3nsimulations must be a positive integer if providedNr$   z*2-D intercept needs to have length `steps`initial_valuesr'   )r   maxdimzoinitial_values should have shape (p, k) or (k,) where p is the number of lags and k is the number of equations.)r   randomRandomStatemultivariate_normalr<   r   r3   rg   r*   eyerG   r   reshaper1   r   r   dotT)rJ   	interceptsig_ustepsr   r   r   rsrmvnormrK   kresult_shapeugenrO   r   ygenjs                    r   varsimr      s   X 
		D		)	)B$GkGAq!<$GGGL,$$ P):):NOOOqz$eQ/}q		728CJJ''l0BCCKKLZ_abccDX|UA.//F79!!y>>TZ]22 !MNNN)qqqtQQQqrrT
"AAAabbDzqqqt0@4XYZZZN!$A...2F1$2N2N  O  P  P  P%qqq!t 1e__ : :aaac{q 	: 	:ABF58VAAAac!eG_%67799DD	: >>,'''r   c                     	 |                      |          }n(# t          $ r t          |t                    s |}Y nw xY w|S )N)rW   	Exceptionr3   rg   )lstrB   rO   s      r   	get_indexr     sX    4   $$$ 	 Ms    "==c                 h    t          j        | dd          \  }}t          j        |          }|||fS )zt
    Returns
    -------
    W: array of eigenvectors
    eigva: list of eigenvalues
    k: largest eigenvector
    TF)leftright)r   eigr   argmax)	sym_arrayeigvaWr   s       r   eigval_decompr      s9     z)$e<<<HE1
	%AeQ;r   c                     | j         d         }g }t          |          D ]2}|}||k     r(|                    | ||f                    |dz   }||k     (3t          j        |          }|S )zp
    Simple vech operator
    Returns
    -------
    vechvec: vector of all elements on and below diagonal
    r$   )r<   r   r5   r   r;   )rZ   lengthvechvecrA   bs        r   vechr   .  s|     71:FG6]]  
&jjNN1QqS6"""cA &jj JwGNr   Fc                     | dk    rt          j        |df          S | dk    rJt          j        || dz
  f          }t          | dz
            D ]}d|||z
  | z  d| |f<   |r|d| z  z  }|S dS )a  

    Parameters
    ----------
    n_seasons : int >= 0
        Number of seasons (e.g. 12 for monthly data and 4 for quarterly data).
    len_endog : int >= 0
        Total number of observations.
    first_period : int, default: 0
        Season of the first observation. As an example, suppose we have monthly
        data and the first observation is in March (third month of the year).
        In this case we pass 2 as first_period. (0 for the first season,
        1 for the second, ..., n_seasons-1 for the last season).
        An integer greater than n_seasons-1 are treated in the same way as the
        integer modulo n_seasons.
    centered : bool, default: False
        If True, center (demean) the dummy variables. That is useful in order
        to get seasonal dummies that are orthogonal to the vector of constant
        dummy variables (a vector of ones).

    Returns
    -------
    seasonal_dummies : ndarray (len_endog x n_seasons-1)
    r   r$   N)r   emptyrG   r   )	n_seasons	len_endogfirst_periodcenteredseason_exogrA   s         r   seasonal_dummiesr   A  s    2 A~~xA'''1}}h	9q=9::y1}%% 	H 	HAFGK<94?i?BCC 	)1y=(K }r   )r	   r
   )r	   )r$   N)r|   )r   NNN)r   F)__doc__statsmodels.compat.pandasr   statsmodels.compat.pythonr   statsmodels.tools.validationr   r   numpyr   pandasr7   scipyr   r   statsmodels.tsa.tsatoolsr   tsatoolsr"   r,   rD   rP   r{   r   r   r   r   r   r   r    r   r   <module>r      s{    2 1 1 1 1 1 - - - - - - = = = = = = = =                 & & & & & & & & &
   .   ( ( ( (V  :4 4 4n) ) ) )/ / /R( R( R( R(j      &" " " " " "r   