Skip to content

Commit 582870d

Browse files
author
junfengwen
committed
Bug fixed
Fixed a bug in prediction. The solver does not seem to run well.
1 parent be53749 commit 582870d

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

RarmaFcns.m

+16-7
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@
4242
% Does not use Z^(j)_{:, T-madim+j+1:T}
4343
result = zeros(xdim, numsamples);
4444
T = size(B,2);
45+
% % JF: not working. This is a bug I never noticed, because at a
46+
% time, we decided that the prediction (in
47+
% convex_multi_view_model.m, line 300) should not use the "noise"
48+
% for future prediction.
49+
% for j = 1:madim
50+
% idx = RarmaFcns.blockInd(j,xdim);
51+
% Tidx = T:-1:max(T-numsamples+1,j);
52+
% result(:,Tidx) = result(:,Tidx) + B(idx, Tidx-j+1);
53+
% end
4554
for j = 1:madim
4655
idx = RarmaFcns.blockInd(j,xdim);
47-
Tidx = T:-1:max(T-numsamples+1,j);
48-
result(:,Tidx) = result(:,Tidx) + B(idx, Tidx-j+1);
56+
Tidx = numsamples:-1:max(1,numsamples-T+j);
57+
result(:,Tidx) = result(:,Tidx) + B(idx, Tidx-numsamples+T-j+1);
4958
end
5059
else
5160
result = zeros(xdim, numsamples);
@@ -94,7 +103,6 @@
94103
end
95104

96105
%%%%%%%%%%%%%%%%%%%%%%%%% Prediction Functions %%%%%%%%%%%%%%%%%%%%%%%%%%
97-
98106
% Model contains
99107
% model.Aall, model.B, model.z and model.zparam for the exponential scalar variable
100108
% Phistart = [Phi_1, ..., Phi_startnum]
@@ -107,7 +115,7 @@
107115
% Epsilonstart can either be Zstart or the epsilon; this is
108116
% determined by the size of the matrix
109117

110-
if isempty(Phistart)
118+
if isempty(Epsilonstart)
111119
Xiterated = iterate_predict_ar(Xstart, model, horizon, opts);
112120
return;
113121
end
@@ -120,12 +128,12 @@
120128
else
121129
Xminusone = Xstart(:, (end-r+1):end);
122130
end
123-
Zq = Phistart(:, (end-opts.madim+1):end);
131+
Zq = Epsilonstart(:, (end-opts.madim+1):end);
124132
if size(Zq, 1) ~= opts.madim*xdim
125133
if isempty(model.B)
126-
Zq = zeros(opts.madim*xdim, size(Xstart,2))
134+
Zq = zeros(opts.madim*xdim, size(Xstart,2));
127135
else
128-
Zq = model.B*Phistart(:, (end-opts.madim+1):end);
136+
Zq = model.B*Epsilonstart(:, (end-opts.madim+1):end);
129137
end
130138
end
131139

@@ -147,6 +155,7 @@
147155
% If Phistart is empty, then compute it before proceeding
148156
% The predicted phi will use a generative Laplace model
149157

158+
xdim = size(Xstart, 1);
150159
Xminusone = Xstart(:, (end-opts.ardim+1):end);
151160
xt = RarmaFcns.computeAR(model.A, Xminusone, opts.ardim, xdim, 1);
152161
Xiterated = xt;

RarmaUtilities.m

+21-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,27 @@
3131
end
3232
end
3333
end
34-
3534
end
35+
36+
function [isStable, eigs] = checkStable(A, threshold)
37+
% This function check whether a model is stable based on the
38+
% eigenvalues of A
39+
if nargin < 2
40+
threshold = 1.0;
41+
end
42+
[xdim, tmp] = size(A);
43+
ardim = round(tmp/xdim);
44+
bigA = A;
45+
if ardim < 1
46+
error('ardim < 1');
47+
elseif ardim ~= 1
48+
bigA = [bigA; eye((ardim-1)*xdim),...
49+
zeros((ardim-1)*xdim,xdim)];
50+
end
51+
eigs = abs(eig(bigA));
52+
maxEig = max(eigs); % should be <=1
53+
isStable = (maxEig<=threshold);
54+
end
55+
3656
end
3757
end

demo.m

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
clear
22
clc
33

4-
num_reps = 1;
4+
rng(100);
5+
num_reps = 2;
56
Models = cell(num_reps,1);
7+
Xpredictall = cell(num_reps,1);
8+
Err = zeros(num_reps,1);
69

710
%% Generate data
811
opts = [];
@@ -19,7 +22,12 @@
1922
opts.reg_wgt_ma = 1e-1;
2023
for ii = 1:num_reps
2124
Models{ii} = rarma(Xtrainall{ii},opts);
25+
[isStable, eigs] = RarmaUtilities.checkStable(Models{ii}.A)
2226
end
2327

2428
%% Prediction and Evaluation
25-
29+
for ii = 1:num_reps
30+
Xpredictall{ii} = Models{ii}.predict(Xtrainall{ii},...
31+
size(Xtestall{ii},2), opts);
32+
Err(ii) = sum(sum((Xpredictall{ii}-Xtestall{ii}).^2));
33+
end

rarma.m

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
if opts.recover == 1
127127
[model.B, model.Epsilon] = recoverModels(Z);
128128
end
129-
model.predict = @(Xstart, Phistart, horizon, opts)(RarmaFcns.iterate_predict(Xstart, model, horizon, opts));
129+
% model.predict = @(Xstart, Epsilonstart, horizon, opts)(RarmaFcns.iterate_predict(Xstart, Epsilonstart, model, horizon, opts));
130+
model.predict = @(Xstart, horizon, opts)(RarmaFcns.iterate_predict_ar(Xstart, model, horizon, opts));
130131
end
131132

132133
function [A, f] = iterateA(A, Z)

0 commit comments

Comments
 (0)