-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussian_innovation.py
214 lines (173 loc) · 8.41 KB
/
gaussian_innovation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from sys import exit
import numpy as np
from regime_switching_VARM import RSVARM
############################################################################
## @class Gaussian
# Regime switching vectoc auto-regressive model with gaussian innovation
#
class Gaussian_X(RSVARM):
## @fn
# @brief
#
# @param dimension Data dimension
# @param order Autoregressive process order
# @param nb_regime Number of regimes
# @param data Vector of length S, where S is the numbers of observed
# sequences, data[s] is s^th sequence, is a matrix T_s x dimension.
# @param initial_values Matrix orderx1, initial values of X.
# @param init_method Initialization method to be considered. Can take two
# values :
# * "datadriven" for datadriven initialization. See function
# datadriven_init for further details.
# * "rand1"
# * "rand2"
#
#
def __init__(self, dimension, order, nb_regime, data, initial_values, \
init_method):
#---assertion
assert (dimension > 0)
assert (order >= 0)
assert (nb_regime > 0)
assert (initial_values[0].shape[1] == data[0].shape[1])
if (dimension != data[0].shape[1]):
print()
print("ERROR: class Gaussian_X: dimension must be equal to the number of column within data!\n")
exit(1)
#---model dimension and data
self.innovation = "gaussian"
self.dimension = dimension
self.order = order
self.nb_regime = nb_regime
self.data = data
self.initial_values = initial_values
#---initial law parameters
# The initial_values[.][order-i] for i = 1, ..., order, follows
# a multi-variate normal distribution of mean self.psi["means"][i] and
# covariance matrix self.psi["covar"])
self.psi = {}
self.psi["means"] = []
for _ in range(self.order):
self.psi["means"].append(np.zeros(dtype=np.float64, \
shape=self.dimension))
self.psi["covar"] = np.zeros(dtype=np.float64, \
shape=(self.dimension,self.dimension))
#---VAR parameters initialization
self.intercept = []
self.sigma = []
self.coefficients = []
if(init_method == "datadriven"):
self.datadriven_init()
elif(init_method == "rand1" or init_method == "rand2"):
self.random_init(init_method)
else:
print("ERROR: in class Gaussian_X, unkown initialization method!\n")
exit(1)
return
## @fn datadriven_init
# @brief VAR Covariance matrices are set at time series's empirical
# covariance matrix; coefficients are set at zeros; and intercepts are
# randomly chosen within interval [m-2s, m+2s] where m is time series'
# empirical mean and s is their standard deviation.
#
def datadriven_init(self):
#---compute the unconditional mean and covariance of data
unc_mean = np.zeros(dtype=np.float64, shape=self.dimension)
unc_covar = np.zeros(dtype=np.float64, \
shape=(self.dimension, self.dimension))
S = len(self.data)
for i in range(S):
unc_mean = unc_mean + np.mean(self.data[i], axis=0)
unc_covar = unc_covar + np.cov(self.data[i], rowvar=False)
unc_mean = unc_mean / S
unc_covar = unc_covar / S
#---parameters initialization begins
for k in range(self.nb_regime):
#-intercepts are randomly chosen within 2-standard deviations
#arround the unconditional mean
two_std = 2*np.sqrt(np.diag(unc_covar))
self.intercept.append( \
np.random.uniform(unc_mean-two_std, unc_mean+two_std))
#-covariance matrices are set at the unconditional covariance matrix
self.sigma.append( np.array(unc_covar) )
#--autoregressive coefficients are set at zero in order that
#the conditional means are equal to the conditional mean
self.coefficients.append([])
for i in range(self.order):
self.coefficients[k].append( np.zeros(dtype=np.float64, \
shape=(self.dimension, self.dimension)) )
return
## @fn random_one_init
# @brief VAR coefficients are randomly chosen within [-1,1] and
# covariance matrices are set at data empirical covariance matrix.
# Intercepts initialization depends of init_method:
# * if init_method == "rand1" then intercepts are set at zeros
# * if init_method == "rand2", b_k = unc_mean - sum_i=1..order C_i^k * X_{t-i}
#
# NB: ADDED 2022/05/25
#
def random_init(self, init_method):
#---compute the unconditional mean and covariance of data
unc_mean = np.zeros(dtype=np.float64, shape=self.dimension)
unc_covar = np.zeros(dtype=np.float64, \
shape=(self.dimension, self.dimension))
S = len(self.data)
for i in range(S):
unc_mean = unc_mean + np.mean(self.data[i], axis=0)
unc_covar = unc_covar + np.cov(self.data[i], rowvar=False)
unc_mean = unc_mean / S
unc_covar = unc_covar / S
two_std = 2*np.sqrt(np.diag(unc_covar))
#---parameters initialization begins
for k in range(self.nb_regime):
#----autoregressive coefficients are randomly chosen within [-1,1]
self.coefficients.append([])
for i in range(self.order):
self.coefficients[k].append( np.random.uniform(-1.0, 1.0, \
(self.dimension, self.dimension)))
#----covariance matrices are set to random diagonal matrices
self.sigma.append( np.array(unc_covar) )
#----intercepts are initialization
if(init_method == "rand1"):
self.intercept.append(np.zeros(dtype=np.float64, \
shape=self.dimension))
else:
# choose state k conditional mean within 2-standard
# deviations arround the unconditional mean
mean_k = np.random.uniform(unc_mean-two_std, unc_mean+two_std)
# choose the time series used to initialize intercepts
chosen_s = np.random.choice([s for s in range(S)])
T_s = self.data[chosen_s].shape[0]
tmp_intecept_k = []
for t in range(self.order, T_s):
tmp = np.zeros(dtype=np.float64, shape=self.dimension)
for i in range(1, self.order+1):
tmp = tmp + np.matmul(self.coefficients[k][i-1], \
self.data[chosen_s][t-i, :])
tmp_intecept_k.append(mean_k - tmp)
tmp_intecept_k = np.array(tmp_intecept_k)
self.intercept.append(np.mean(tmp_intecept_k, axis=0))
return
## @fn
# @brief
#
# @param intercept
# @param coefficients
# @param sigma
#
def set_parameters(self, intercept, coefficients, sigma):
#NB: object intercept, coefficients and sigma are not copied
self.intercept = intercept
self.coefficients = coefficients
self.sigma = sigma
return
def compute_means_k(ar_coefficients, ar_intercepts, previous_vals, K, order, dim):
assert((previous_vals.shape[0] == order) and (previous_vals.shape[1] == dim))
#output
means = np.zeros(shape=(K,dim), dtype=np.float64)
for i in range(K):
for j in range(1, order+1):
means[i,:] = means[i,:] + \
np.matmul(ar_coefficients[i][j-1], previous_vals[order-j, :])
means[i,:] = means[i,:] + ar_intercepts[i]
return means