-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRarmaFcns.m
372 lines (315 loc) · 11.7 KB
/
RarmaFcns.m
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
classdef RarmaFcns
%% Class implementing many of the helper functions required for
%% regularized ARMA
% Assumes
% A = [A^(1) ... A^(p)];
% B = [B^(1); ...; B^(q)];
% Phi = [phi_1 ... phi_t];
% Always start from the back first, and goes backwards numsamples
% Xminusone = [X1 ... X_t-1], samples as columns
methods(Static)
%%%%%%%%%%%%%%%%%%%%%%%%% RARMA BASIC MODEL COMPUTATIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function result = computeARMA(A, Xminusone, B, Epsilon, ardim, madim, xdim, numsamples)
result = RarmaFcns.computeAR(A, Xminusone, ardim, xdim, numsamples) + ...
RarmaFcns.computeMA(B, Epsilon, madim, xdim, numsamples);
end
function result = computeAR(A, Xminusone, ardim, xdim, numsamples)
% returns result =
% [sum_i A^(i) X_{ardim+1-i}, ..., sum_i A^(i) X_{t-i}] = A X_hist
% if numsamples is less than full matrix, then returns the last
% numsamples of the array
% % Treat numsamples = 1 specially; for others, inefficiently
% % computes the entire AR and then returns a subset
% if numsamples == 1
% result = A * Xminusone(:,end-ardim+1);
% return;
% end
if ardim > 0
Xhist = RarmaFcns.generate_history(Xminusone, ardim);
result = A*Xhist;
result = [zeros(xdim, ardim) result];
if numsamples < size(result,2), result = result(:, end-numsamples+1:end); end
else
result = zeros(xdim, numsamples);
end
end
function result = computeMA(B, Epsilon, madim, xdim, numsamples)
if isempty(Epsilon)
% result = sum_j Z^(j)_{:,t-j}
% Does not use Z^(j)_{:, T-madim+j+1:T}
result = zeros(xdim, numsamples);
T = size(B,2);
for j = 1:madim
idx = RarmaFcns.blockInd(j,xdim);
Tidx = numsamples:-1:max(1,numsamples-T+j);
result(:,Tidx) = result(:,Tidx) + B(idx, Tidx-numsamples+T-j+1);
end
else
result = zeros(xdim, numsamples);
T = size(Epsilon,2);
% T-madim+1 is the maximum # of samples that can be generated
% E.g., q=4, T=9, numsamples=14
% 00000000XXXXXX (0: padding zero, X: generated point)
% =====---------
% q-1 T
% numsamples
endpoint = max(1, numsamples-T+madim);
Tidx = numsamples:-1:endpoint;
for j = 1:madim
idx = RarmaFcns.blockInd(j,xdim);
result(:,Tidx) = result(:,Tidx) + B(idx,:) * Epsilon(:,Tidx-numsamples+T-j+1);
end
end
end
function Xiterated = iterateModel(Xstart, A, B, Epsilon, ardim, madim, xdim, numsamples)
Xiterated = [];
Xminusone = Xstart(:, end-ardim+1:end);
if isempty(Epsilon)
for i = 1:numsamples
xt = RarmaFcns.computeAR(A, Xminusone, ardim, xdim, 1);
Xiterated = [Xiterated xt];
Xminusone = [Xminusone(:, 2:end) xt];
end
else
if madim <= ardim
Epsilon = Epsilon(:,ardim-madim+1:end);
else
Epsilon = [zeros(size(Epsilon,1), madim-ardim) Epsilon];
end
for i = 1:numsamples
xt = RarmaFcns.computeARMA(A, Xminusone, B, Epsilon(:,i:i+madim-1), ardim, madim, xdim, 1);
Xiterated = [Xiterated xt];
Xminusone = [Xminusone(:, 2:end) xt];
end
end
end
function ind = blockInd(j, xdim)
indStart = xdim*(j-1) + 1;
ind = indStart:(indStart+xdim-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%% Prediction Functions %%%%%%%%%%%%%%%%%%%%%%%%%%
% Model contains
% model.Aall, model.B, model.z and model.zparam for the exponential scalar variable
% Phistart = [Phi_1, ..., Phi_startnum]
function Xiterated = iterate_predict(Xstart, Epsilonstart, model, horizon, opts)
%% ITERATE_PREDICT iteratively applies the ARMA model
% If Epsilonstart is empty, only uses the autoregressive part
% Otherwise, uses Epsilonstart for as many steps as possible, until
% pass the end of it and then only using autoregressive part
% Epsilonstart can either be Zstart or the epsilon; this is
% determined by the size of the matrix
if isempty(Epsilonstart)
Xiterated = RarmaFcns.iterate_predict_ar(Xstart, model, horizon, opts);
return;
end
% Use models A and B to predict the next point from Xstart
r = opts.ardim;
xdim = size(Xstart,1);
if size(Xstart, 2) < r
Xminusone = [zeros(size(Xstart,1), r-size(Xstart,2)) Xstart];
else
Xminusone = Xstart(:, (end-r+1):end);
end
Zq = Epsilonstart(:, (end-opts.madim+1):end);
if size(Zq, 1) ~= opts.madim*xdim
if isempty(model.B)
Zq = zeros(opts.madim*xdim, size(Xstart,2));
else
Zq = model.B*Epsilonstart(:, (end-opts.madim+1):end);
end
end
xt = RarmaFcns.computeARMA(model.A, Xminusone, Zq, [], opts.ardim, opts.madim, xdim, 1);
Xiterated = xt;
Xminusone = [Xminusone xt];
ldim = size(Zq,1);
for i = 2:horizon
Zq = [Zq(:,2:end) zeros(ldim,1)];
xt = RarmaFcns.computeARMA(model.A, Xminusone, Zq, [], opts.ardim, opts.madim, xdim, 1);
Xminusone = [Xminusone(:, 2:end) xt];
Xiterated = [Xiterated xt];
end
end
function Xiterated = iterate_predict_ar(Xstart, model, horizon, opts)
%% ITERATE_PREDICT_AR
% Use models A and B to predict the next point from Xstart
% If Phistart is empty, then compute it before proceeding
% The predicted phi will use a generative Laplace model
xdim = size(Xstart, 1);
Xminusone = Xstart(:, (end-opts.ardim+1):end);
xt = RarmaFcns.computeAR(model.A, Xminusone, opts.ardim, xdim, 1);
Xiterated = xt;
Xminusone = [Xminusone(:, 2:end) xt];
for i = 2:horizon
xt = RarmaFcns.computeAR(model.A, Xminusone, opts.ardim, xdim, 1);
Xminusone = [Xminusone(:, 2:end) xt];
Xiterated = [Xiterated xt];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%% RARMA LOSS FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [f,g] = euclidean_rarma(X, A, B, Epsilon, var)
% EUCLIDEAN_RARMA implements the euclidean loss
% See genericloss_rarma below for a description of the parameters
lossType = 'euclidean';
[f,g] = RarmaFcns.genericloss_rarma(X, A, B, Epsilon, var, lossType);
end
function [f,g] = robust_rarma(X, A, B, Epsilon, var)
% ROBUST_RARMA implements the l1 loss
% See genericloss_rarma below for a description of the parameters
sigma = 1;
lossType = 'robust';
[f,g] = RarmaFcns.genericloss_rarma(X, A, B, Epsilon, var, lossType, sigma);
end
function [f,g] = genericloss_rarma(X, A, B, Epsilon, var, lossType, param)
% If Epsilon empty, then B = Z
% Ignores the first ardim parts of X in the loss
% EITHER:
% L( sum_{i=1}^p A^(i) x_t-i + sum_{j=1}^q Z^(j)_{:, t-j}, x_t)
% OR
% L( sum_{i=1}^p A^(i) x_t-i + sum_{j=1}^q B^(j) Epsilon_{:, t-j}, x_t)
% Xhist = [ X_{:,p} X_{:,p+1} ... X_{:,t-1};
% X_{:,p-1} X_{:,p} ... X_{:,t-2}; ...
% X_{:,1} X_{:,2} ... X_{:,t-p}; ...
% ]
%
% A = [A^(1) A^(2) ... A^(p)]; n x n*p
% B = [B^(1); B^(2); ... ; B^(q)]; n*q x latent_dim
% Epsilon = [Epsilon_{:,1} Epsilon_{:, 2} ... Epsilon_{:, t}]; latent_dim x t
% Z = [Z^(1); Z^(2); ... ; Z^(q)] = [B^(1) Epsilon; B^(2) Epsilon; ... ; B^(q) Epsilon]; n*q x t
% When nargin == 5, var says for which variable to return gradient
% var = 1 means gradient in A
% var = 2 means gradient in Z
% var = 3 means gradient in B
% var = 4 means gradient in Epsilon
xdim = size(X, 1);
ardim = size(A, 2)/xdim;
madim = size(B, 1)/xdim;
r = max(ardim, madim-1); % this is because phi_t and x_t always in the same spot
numsamples = size(X,2);
switch lossType
case 'euclidean'
lossfcnar = @euclidean_loss_ar;
lossfcnma = @euclidean_loss_ma;
case 'robust'
lossfcnar = @robust_loss_ar;
lossfcnma = @robust_loss_ma;
sigma = param;
otherwise
error('Unknown RARMA loss type!');
end
% Now A is the variable; A = [A^(1) ... A^(p)]
% where A^(i) is n x n
if var == 1
maPart = RarmaFcns.computeMA(B, Epsilon, madim, xdim, numsamples);
X_modified = X - maPart;
X_modified(:,1:r) = 0; % don't care about the first r points
% F = sum_i A^(i) X_{t-i}, so [Xhat_p+1, ..., Xhat_t]
F = RarmaFcns.computeAR(A, X(:, 1:end-1), ardim, xdim, numsamples);
F(:,1:r) = 0;
diff = F - X_modified;
[f,g] = lossfcnar();
% Now Z is the variable
elseif var == 2
arPart = RarmaFcns.computeAR(A, X(:, 1:end-1), ardim, xdim, numsamples);
X_modified = X - arPart;
X_modified(:,1:r) = 0;
F = RarmaFcns.computeMA(B, [], madim, xdim, numsamples);
F(:,1:r) = 0;
diff = F - X_modified;
[f,g] = lossfcnma();
% Now B is the variable
elseif var == 3
arPart = RarmaFcns.computeAR(A, X(:, 1:end-1), ardim, xdim, numsamples);
X_modified = X - arPart;
X_modified(:,1:r) = 0;
F = RarmaFcns.computeMA(B, Epsilon, madim, xdim, numsamples);
F(:,1:r) = 0;
diff = F - X_modified;
[f,g] = lossfcnma();
g = g*Epsilon';
% Now Epsilon is the variable
elseif var == 4
arPart = RarmaFcns.computeAR(A, X(:, 1:end-1), ardim, xdim, numsamples);
X_modified = X - arPart;
X_modified(:,1:r) = 0;
F = RarmaFcns.computeMA(B, Epsilon, madim, xdim, numsamples);
F(:,1:r) = 0;
diff = F - X_modified;
[f,g] = lossfcnma();
g = B'*g;
else
error('modified_euclidean_loss -> var must be 1, 2, 3 or 4.');
end
f = f / numsamples;
g = g ./ numsamples;
function [f,g] = euclidean_loss_ar
f = (0.5) * sum(sum(diff.^2));
Xhist = [zeros(xdim*ardim, ardim) RarmaFcns.generate_history(X(:,1:end-1),ardim)];
g = diff * Xhist';
end
function [f,g] = euclidean_loss_ma
f = (0.5) * sum(sum(diff.^2));
T = size(diff, 2);
g = zeros(xdim*madim, T);
for j = 1:madim
idx = RarmaFcns.blockInd(j,xdim);
g(idx,1:T-j+1) = diff(:,j:T);
end
end
function [f,g] = robust_loss_ar
idx = abs(diff) < sigma;
Z = abs(diff) - (sigma/2);
Z(idx) = diff(idx).^2 * (0.5/sigma);
f = sum(sum(Z));
Xhist = [zeros(xdim*ardim, ardim) RarmaFcns.generate_history(X(:,1:end-1),ardim)];
g = sign(diff);
g(idx) = diff(idx)/sigma;
g = g * Xhist';
end
function [f,g] = robust_loss_ma
idx = abs(diff) < sigma;
Z = abs(diff) - (sigma/2);
Z(idx) = diff(idx).^2 * (0.5/sigma);
f = sum(sum(Z));
gg = sign(diff);
gg(idx) = diff(idx)/sigma;
T = size(gg, 2);
g = zeros(xdim*madim, T);
for j = 1:madim
idx = RarmaFcns.blockInd(j,xdim);
g(idx,1:T-j+1) = gg(:,j:T);
end
end
end
function [val, G] = trace_norm(A)
%% TRACE_NORM
% Computes the trace norm on mxn A
%
% ||A||_tr = sum_i^{min(m,n)} sigma_i
[U, S, V] = svd(A, 'econ');
val = sum(S(:)); % diag allows S to be rectangular
G = U*V'; % subgradient
end
function [f, g] = frob_norm_sq(A)
%% FROB_NORM_SQ Computes the Frobenius norm on mxn A
f = sum(sum(A.^2));
g = 2.*A;
end
function X_hist = generate_history(Xminusone, ardim)
% create a matrix where each column is vectorized last ardim samples
% X_hist = [[X_ardim; X_ardim-1;...;X_1 ] ... [X_t-1; X_t-2;...;X_t-ardim ]]
% X_hist is n*ardim x t-ardim
% X is n x t-1
tminusone = size(Xminusone, 2);
X_hist = [];
for i = 1:ardim
X_hist = [X_hist; Xminusone(:,(ardim-i+1):(tminusone-i+1))];
end
%for i = ardim:tminusone
% x_iplusone = Xminusone(:,i:-1:(i-ardim+1));
% X_hist = [X_hist x_iplusone(:)];
%end
end
% END OF METHODS
end
% END OF CLASSDEF
end