1
from cStringIO import StringIO
2
Miscellaneous utility code for VAR estimation
6
import scipy.stats as stats
8
def interpret_data(data, names):
10
Convert passed data structure to form required by VAR estimation classes
19
if isinstance(data, np.ndarray):
20
provided_names = data.dtype.names
22
# structured array type
25
names = provided_names
27
assert(len(names) == len(provided_names))
29
Y = struct_to_ndarray(data)
33
raise Exception('cannot handle other input types at the moment')
37
def struct_to_ndarray(arr):
38
return arr.view((float, len(arr.dtype.names)))
5
40
def parse_data(path):
56
91
return data, date_range
59
def pprint_matrix(values, rlabels, clabels, col_space=None):
62
T, K = len(rlabels), len(clabels)
66
col_space = [max(len(str(c)) + 2, min_space) for c in clabels]
68
col_space = (col_space,) * K
70
row_space = max([len(str(x)) for x in rlabels]) + 2
72
head = _pfixed('', row_space)
74
for j, h in enumerate(clabels):
75
head += _pfixed(h, col_space[j])
79
for i, rlab in enumerate(rlabels):
80
line = ('%s' % rlab).ljust(row_space)
83
line += _pfixed(values[i,j], col_space[j])
89
def _pfixed(s, space, nanRep=None, float_format=None):
90
if isinstance(s, float):
92
formatted = float_format(s)
94
formatted = "%#8.6F" % s
96
return formatted.rjust(space)
98
return ('%s' % s)[:space].rjust(space)
94
def comp_matrix(coefs):
96
Return compansion matrix for the VAR(1) representation for a VAR(p) process
99
A = [A_1 A_2 ... A_p-1 A_p
104
p, k, k2 = coefs.shape
109
result = np.zeros((kp, kp))
110
result[:k] = np.concatenate(coefs, axis=1)
114
result[np.arange(k, kp), np.arange(kp-k)] = 1
119
from scikits.statsmodels.compatibility import np_slogdet
120
logdet = np_slogdet(m)
123
raise ValueError("Matrix is not positive definite")
125
raise ValueError("Matrix is singluar")
131
def norm_signif_level(alpha=0.05):
132
return stats.norm.ppf(1 - alpha / 2)
135
def acf_to_acorr(acf):
136
diag = np.diag(acf[0])
137
# numpy broadcasting sufficient
138
return acf / np.sqrt(np.outer(diag, diag))