<s> """<STR_LIT>""" <EOL> from statsmodels . compat . python import range , string_types , asbytes <EOL> import numpy as np <EOL> import scipy . stats as stats <EOL> import scipy . linalg as L <EOL> import scipy . linalg . decomp as decomp <EOL> import statsmodels . tsa . tsatools as tsa <EOL> from scipy . linalg import cholesky <EOL> def get_var_endog ( y , lags , trend = '<STR_LIT:c>' , has_constant = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> nobs = len ( y ) <EOL> Z = np . array ( [ y [ t - lags : t ] [ : : - <NUM_LIT:1> ] . ravel ( ) for t in range ( lags , nobs ) ] ) <EOL> if trend != '<STR_LIT>' : <EOL> Z = tsa . add_trend ( Z , prepend = True , trend = trend , <EOL> has_constant = has_constant ) <EOL> return Z <EOL> def get_trendorder ( trend = '<STR_LIT:c>' ) : <EOL> if trend == '<STR_LIT:c>' : <EOL> trendorder = <NUM_LIT:1> <EOL> elif trend == '<STR_LIT>' : <EOL> trendorder = <NUM_LIT:0> <EOL> elif trend == '<STR_LIT>' : <EOL> trendorder = <NUM_LIT:2> <EOL> elif trend == '<STR_LIT>' : <EOL> trendorder = <NUM_LIT:3> <EOL> return trendorder <EOL> def make_lag_names ( names , lag_order , trendorder = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> lag_names = [ ] <EOL> if isinstance ( names , string_types ) : <EOL> names = [ names ] <EOL> for i in range ( <NUM_LIT:1> , lag_order + <NUM_LIT:1> ) : <EOL> for name in names : <EOL> if not isinstance ( name , string_types ) : <EOL> name = str ( name ) <EOL> lag_names . append ( '<STR_LIT:L>' + str ( i ) + '<STR_LIT:.>' + name ) <EOL> if trendorder != <NUM_LIT:0> : <EOL> lag_names . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> if trendorder > <NUM_LIT:1> : <EOL> lag_names . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> if trendorder > <NUM_LIT:2> : <EOL> lag_names . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return lag_names <EOL> def comp_matrix ( coefs ) : <EOL> """<STR_LIT>""" <EOL> p , k , k2 = coefs . shape <EOL> assert ( k == k2 ) <EOL> kp = k * p <EOL> result = np . zeros ( ( kp , kp ) ) <EOL> result [ : k ] = np . concatenate ( coefs , axis = <NUM_LIT:1> ) <EOL> if p > <NUM_LIT:1> : <EOL> result [ np . arange ( k , kp ) , np . arange ( kp - k ) ] = <NUM_LIT:1> <EOL> return result <EOL> def parse_lutkepohl_data ( path ) : <EOL> """<STR_LIT>""" <EOL> from collections import deque <EOL> from datetime import datetime <EOL> import pandas <EOL> import pandas . core . datetools as dt <EOL> import re <EOL> regex = re . compile ( asbytes ( '<STR_LIT>' ) ) <EOL> with open ( path , '<STR_LIT:rb>' ) as f : <EOL> lines = deque ( f ) <EOL> to_skip = <NUM_LIT:0> <EOL> while asbytes ( '<STR_LIT>' ) not in lines . popleft ( ) : <EOL> to_skip += <NUM_LIT:1> <EOL> while True : <EOL> to_skip += <NUM_LIT:1> <EOL> line = lines . popleft ( ) <EOL> m = regex . match ( line ) <EOL> if m : <EOL> year , freq , start_point = m . groups ( ) <EOL> break <EOL> data = np . genfromtxt ( path , names = True , skip_header = to_skip + <NUM_LIT:1> ) <EOL> n = len ( data ) <EOL> start_point = int ( start_point ) <EOL> year = int ( year ) <EOL> offsets = { <EOL> asbytes ( '<STR_LIT>' ) : dt . BQuarterEnd ( ) , <EOL> asbytes ( '<STR_LIT:M>' ) : dt . BMonthEnd ( ) , <EOL> asbytes ( '<STR_LIT:A>' ) : dt . BYearEnd ( ) <EOL> } <EOL> offset = offsets [ freq ] <EOL> inc = offset * ( start_point - <NUM_LIT:1> ) <EOL> start_date = offset . rollforward ( datetime ( year , <NUM_LIT:1> , <NUM_LIT:1> ) ) + inc <EOL> offset = offsets [ freq ] <EOL> from pandas import DatetimeIndex <EOL> date_range = DatetimeIndex ( start = start_date , freq = offset , periods = n ) <EOL> return data , date_range <EOL> def get_logdet ( m ) : <EOL> from statsmodels . tools . linalg import logdet_symm <EOL> return logdet_symm ( m ) <EOL> get_logdet = np . deprecate ( get_logdet , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def norm_signif_level ( alpha = <NUM_LIT> ) : <EOL> return stats . norm . ppf ( <NUM_LIT:1> - alpha / <NUM_LIT:2> ) <EOL> def acf_to_acorr ( acf ) : <EOL> diag = np . diag ( acf [ <NUM_LIT:0> ] ) <EOL> return acf / np . sqrt ( np . outer ( diag , diag ) ) <EOL> def varsim ( coefs , intercept , sig_u , steps = <NUM_LIT:100> , initvalues = None , seed = None ) : <EOL> """<STR_LIT>""" <EOL> if seed is not None : <EOL> np . random . seed ( seed = seed ) <EOL> from numpy . random import multivariate_normal as rmvnorm <EOL> p , k , k = coefs . shape <EOL> ugen = rmvnorm ( np . zeros ( len ( sig_u ) ) , sig_u , steps ) <EOL> result = np . zeros ( ( steps , k ) ) <EOL> result [ p : ] = intercept + ugen [ p : ] <EOL> for t in range ( p , steps ) : <EOL> ygen = result [ t ] <EOL> for j in range ( p ) : <EOL> ygen += np . dot ( coefs [ j ] , result [ t - j - <NUM_LIT:1> ] ) <EOL> return result <EOL> def get_index ( lst , name ) : <EOL> try : <EOL> result = lst . index ( name ) <EOL> except Exception : <EOL> if not isinstance ( name , int ) : <EOL> raise <EOL> result = name <EOL> return result <EOL> def eigval_decomp ( sym_array ) : <EOL> """<STR_LIT>""" <EOL> eigva , W = decomp . eig ( sym_array , left = True , right = False ) <EOL> k = np . argmax ( eigva ) <EOL> return W , eigva , k <EOL> def vech ( A ) : <EOL> """<STR_LIT>""" <EOL> length = A . shape [ <NUM_LIT:1> ] <EOL> vechvec = [ ] <EOL> for i in range ( length ) : <EOL> b = i <EOL> while b < length : <EOL> vechvec . append ( A [ b , i ] ) <EOL> b = b + <NUM_LIT:1> <EOL> vechvec = np . asarray ( vechvec ) <EOL> return vechvec </s>
