Skip to content

Commit

Permalink
added LOO MM
Browse files Browse the repository at this point in the history
  • Loading branch information
noblec04 committed Aug 29, 2024
1 parent 1fb4229 commit 9504abd
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 74 deletions.
11 changes: 11 additions & 0 deletions MatlabGP/+utils/catunique.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [x,flag] = catunique(x,xn)

reps = ismembertol(xn,x,1e-6,'ByRows',true);

if ~reps
x = [x;xn];
end

flag = ~reps;

end
4 changes: 2 additions & 2 deletions MatlabGP/GP.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@

res = obj.Y - obj.mean.eval(obj.X);

kkp = pinv(obj.K,0);
kkp = pinv(obj.K);

sigp = sqrt(abs(res'*kkp*res./(size(obj.Y,1))));

Expand All @@ -151,7 +151,7 @@

obj.K = obj.K + diag(0*xx(:,1)+obj.kernel.signn);

obj.Kinv = pinv(obj.K,0);
obj.Kinv = pinv(obj.K);

obj.alpha = obj.Kinv*(res);

Expand Down
53 changes: 29 additions & 24 deletions MatlabGP/examples/TestAppliedAviationProblem.asv
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

clear all
clear
close all
clc

lb = [0.5 0.5 2.5*10^(-3)];
Expand All @@ -21,11 +22,11 @@ y{1} = y1;
y{2} = y2;

%%
% ma = means.linear([1 1 1 1]);
% mb = means.linear([1 1 1]);
ma = means.linear([1 1 1 1]);
mb = means.linear([1 1 1]);

ma = means.zero([1 1 1 1]);
mb = means.zero();
% ma = means.zero();
% mb = means.zero();

a = kernels.RQ(2,1,[0.1 0.2 0.1 0.3]);%.periodic(1,10);
b = kernels.RQ(2,1,[0.2 0.2 0.2]);
Expand Down Expand Up @@ -79,13 +80,13 @@ toc

%%

% mc = means.linear(ones(1,3));%*means.sine(1,10,0,1);
% c = kernels.RQ(2,1,ones(1,3));
% c.signn = eps;
%
% LOOZ = GP(mc,c);
% LOOZ = LOOZ.condition(x{1},MF.LOO,lb,ub);
% LOOZ = LOOZ.train();
mc = means.linear(ones(1,3));%*means.sine(1,10,0,1);
c = kernels.RQ(2,1,ones(1,3));
c.signn = 0.001;

LOOZ = GP(mc,c);
LOOZ = LOOZ.condition(x{1},log(abs(MF.LOO)),lb,ub);
LOOZ = LOOZ.train();


%%
Expand Down Expand Up @@ -115,23 +116,25 @@ C = [50 1];
%%
for jj = 1:60

[xn,Rn] = BO.argmax(@BO.MFSFDelta,MF);
%[xn,Rn] = BO.argmax(@BO.UCB,LOOZ);
%[xn,Rn] = BO.argmax(@BO.MFSFDelta,MF);
[xn,Rn] = BO.argmax(@BO.UCB,LOOZ);

sign(1) = Z{1}.eval_var(xn)/C(1);
sign(2) = Z{2}.eval_var(xn)/C(2);

[~,in] = max(sign);

if in==1
x{1} = [x{1}; xn];
[x{1},flag] = utils.catunique(x{1},xn);
if flag
y{1} = [y{1}; testFuncs.StressedPlate(xn,1)];
end
end
x{2} = [x{2}; xn];

if in==1
y{1} = [y{1}; testFuncs.StressedPlate(xn,1)];
[x{2},flag] = utils.catunique(x{2},xn);
if flag
y{2} = [y{2}; testFuncs.StressedPlate(xn,2)];
end
y{2} = [y{2}; testFuncs.StressedPlate(xn,2)];

for ii = 1:2
Z{ii} = Z{ii}.condition(x{ii},y{ii},lb,ub);
Expand All @@ -140,7 +143,7 @@ for jj = 1:60
MF.GPs = Z;
MF = MF.condition();

%LOOZ = LOOZ.condition(x{1},MF.LOO,lb,ub);
LOOZ = LOOZ.condition(x{1},log(abs(MF.LOO)),lb,ub);

pc(jj,1) = size(x{1},1);
pc(jj,2) = size(x{2},1);
Expand All @@ -151,17 +154,19 @@ for jj = 1:60
R2MF(jj) = 1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy);
RMAEMF(jj) = max(abs(yy - MF.eval_mu(xx)))./std(yy);

cost(jj) = C(1)*pc(jj,1)+pc(jj,2);

figure(3)
clf(3)
hold on
plot(pc(1:jj,1),R2z)
plot(pc(1:jj,1),R2MF)
plot(cost,R2z)
plot(cost,R2MF)

figure(4)
clf(4)
hold on
plot(pc(1:jj,1),RMAEz)
plot(pc(1:jj,1),RMAEMF)
plot(cost,RMAEz)
plot(cost,RMAEMF)

drawnow

Expand Down
36 changes: 21 additions & 15 deletions MatlabGP/examples/TestAppliedAviationProblem.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

clear all
clear
close all
clc

lb = [0.5 0.5 2.5*10^(-3)];
Expand Down Expand Up @@ -30,7 +31,7 @@
a = kernels.RQ(2,1,[0.1 0.2 0.1 0.3]);%.periodic(1,10);
b = kernels.RQ(2,1,[0.2 0.2 0.2]);
a.signn = eps;
b.signn = 0.1;
b.signn = 0.01;

%%
tic
Expand Down Expand Up @@ -81,10 +82,10 @@

% mc = means.linear(ones(1,3));%*means.sine(1,10,0,1);
% c = kernels.RQ(2,1,ones(1,3));
% c.signn = eps;
% c.signn = 1*10^(-7);
%
% LOOZ = GP(mc,c);
% LOOZ = LOOZ.condition(x{1},MF.LOO,lb,ub);
% LOOZ = LOOZ.condition(x{1},log(abs(MF.LOO)),lb,ub);
% LOOZ = LOOZ.train();


Expand Down Expand Up @@ -113,25 +114,28 @@
C = [50 1];

%%
for jj = 1:60
for jj = 1:100

[xn,Rn] = BO.argmax(@BO.MFSFDelta,MF);
%[xn,Rn] = BO.argmax(@BO.UCB,LOOZ);
%[xn,Rn] = BO.argmax(@BO.maxVAR,MF);

sign(1) = Z{1}.eval_var(xn)/C(1);
sign(2) = Z{2}.eval_var(xn)/C(2);

[~,in] = max(sign);

if in==1
x{1} = [x{1}; xn];
[x{1},flag] = utils.catunique(x{1},xn);
if flag
y{1} = [y{1}; testFuncs.StressedPlate(xn,1)];
end
end
x{2} = [x{2}; xn];

if in==1
y{1} = [y{1}; testFuncs.StressedPlate(xn,1)];
[x{2},flag] = utils.catunique(x{2},xn);
if flag
y{2} = [y{2}; testFuncs.StressedPlate(xn,2)];
end
y{2} = [y{2}; testFuncs.StressedPlate(xn,2)];

for ii = 1:2
Z{ii} = Z{ii}.condition(x{ii},y{ii},lb,ub);
Expand All @@ -140,7 +144,7 @@
MF.GPs = Z;
MF = MF.condition();

%LOOZ = LOOZ.condition(x{1},MF.LOO,lb,ub);
%LOOZ = LOOZ.condition(x{1},log(abs(MF.LOO)),lb,ub);

pc(jj,1) = size(x{1},1);
pc(jj,2) = size(x{2},1);
Expand All @@ -151,17 +155,19 @@
R2MF(jj) = 1 - mean((yy - MF.eval_mu(xx)).^2)./var(yy);
RMAEMF(jj) = max(abs(yy - MF.eval_mu(xx)))./std(yy);

cost(jj) = C(1)*pc(jj,1)+pc(jj,2);

figure(3)
clf(3)
hold on
plot(C(1)*pc(1:jj,1)+pc(1:jj,2),R2z)
plot(C(1)*pc(1:jj,1)+pc(1:jj,2),R2MF)
plot(cost,R2z)
plot(cost,R2MF)

figure(4)
clf(4)
hold on
plot(C(1)*pc(1:jj,1)+pc(1:jj,2),RMAEz)
plot(C(1)*pc(1:jj,1)+pc(1:jj,2),RMAEMF)
plot(cost,RMAEz)
plot(cost,RMAEMF)

drawnow

Expand Down
Loading

0 comments on commit 9504abd

Please sign in to comment.