#NEWLINE# def vargenerate( ar , u , initvalues = None ) : #NEWLINE# #INDENT# 'generate an VAR process with errors u\n\n    similar to gauss\n    uses loop\n\n    Parameters\n    ----------\n    ar : array (nlags,nvars,nvars)\n        matrix lagpolynomial\n    u : array (nobs,nvars)\n        exogenous variable, error term for VAR\n\n    Returns\n    -------\n    sar : array (1+nobs,nvars)\n        sample of var process, inverse filtered u\n        does not trim initial condition y_0 = 0\n\n    Examples\n    --------\n    # generate random sample of VAR\n    nobs, nvars = 10, 2\n    u = numpy.random.randn(nobs,nvars)\n    a21 = np.array([[[ 1. ,  0. ],\n                     [ 0. ,  1. ]],\n\n                    [[-0.8,  0. ],\n                     [ 0.,  -0.6]]])\n    vargenerate(a21,u)\n\n    # Impulse Response to an initial shock to the first variable\n    imp = np.zeros((nobs, nvars))\n    imp[0,0] = 1\n    vargenerate(a21,imp)\n\n    ' #NEWLINE# ( nlags , nvars , nvarsex ) = ar . shape #NEWLINE# nlagsm1 = ( nlags - 1 ) #NEWLINE# nobs = u . shape [ 0 ] #NEWLINE# if ( nvars != nvarsex ) : #NEWLINE# #INDENT# print ( 'exogenous variables not implemented not tested' ) #NEWLINE# #UNINDENT# if ( u . shape [ 1 ] != nvars ) : #NEWLINE# #INDENT# raise ValueError ( 'u needs to have nvars columns' ) #NEWLINE# #UNINDENT# if ( initvalues is None ) : #NEWLINE# #INDENT# sar = np . zeros ( ( ( nobs + nlagsm1 ) , nvars ) ) #NEWLINE# start = nlagsm1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# start = max ( nlagsm1 , initvalues . shape [ 0 ] ) #NEWLINE# sar = np . zeros ( ( ( nobs + start ) , nvars ) ) #NEWLINE# sar [ ( start - initvalues . shape [ 0 ] ) : start ] = initvalues #NEWLINE# #UNINDENT# sar [ start : ] = initvalues #NEWLINE# for i in range ( start , ( start + nobs ) ) : #NEWLINE# #INDENT# for p in range ( 1 , nlags ) : #NEWLINE# #INDENT# sar [ i ] += np . dot ( sar [ ( i - p ) , : ] , ( - ar [ p ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# return sar