Skip to content

Commit

Permalink
Moved GP class to linear solve over pseudoinv
Browse files Browse the repository at this point in the history
  • Loading branch information
noblec04 committed Nov 25, 2024
1 parent 3160b65 commit 1c47bfd
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 48 deletions.
50 changes: 42 additions & 8 deletions MatlabGP/GP.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@

if nargout>1

%v = dot(ksf',(obj.K\ksf'))';
%sig = (abs(obj.kernel.scale - v));
v = dot(ksf',(obj.K\ksf'))';
sig = (abs(obj.kernel.scale - v));

sig = abs(obj.kernel.scale + obj.kernel.signn - dot(ksf',obj.Kinv*ksf')');
%sig = abs(obj.kernel.scale + obj.kernel.signn - dot(ksf',obj.Kinv*ksf')');
end
end

Expand All @@ -79,10 +79,13 @@

ksf = obj.kernel.build(xs,xx);

%v = dot(ksf',(obj.K\ksf'));
%sig = abs(obj.kernel.scale - v);
% v = obj.L\ksf';
% sig = abs(obj.kernel.scale - sum(v.^2,1))';

sig = abs(obj.kernel.scale - dot(ksf',obj.Kinv*ksf')');
v = dot(ksf',(obj.K\ksf'));
sig = abs(obj.kernel.scale - v);

%sig = abs(obj.kernel.scale - dot(ksf',obj.Kinv*ksf')');

end

Expand Down Expand Up @@ -131,7 +134,7 @@

end

function [obj] = condition(obj,X,Y,lb,ub)
function [obj] = conditionOld(obj,X,Y,lb,ub)

obj.X = X;
obj.Y = Y;
Expand All @@ -147,7 +150,6 @@
xx = (X - obj.lb_x)./(obj.ub_x - obj.lb_x);

obj.kernel.scale = 1;

[obj.K] = obj.kernel.build(xx,xx);

obj.K = obj.K + diag(0*xx(:,1)+obj.kernel.signn);
Expand Down Expand Up @@ -177,6 +179,38 @@

end

function [obj] = condition(obj,X,Y,lb,ub)

obj.X = X;
obj.Y = Y;

if nargin<4
obj.lb_x = min(X);
obj.ub_x = max(X);
else
obj.lb_x = lb;
obj.ub_x = ub;
end

xx = (X - obj.lb_x)./(obj.ub_x - obj.lb_x);

obj.kernel.scale = 1;
[obj.K] = obj.kernel.build(xx,xx);
obj.K = obj.K + diag(0*xx(:,1)+obj.kernel.signn) + (1e-6)*eye(size(xx,1));

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

obj.alpha = obj.K\res;

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

obj.kernel.scale = sigp^2;

obj.K = obj.kernel.scale*obj.K;
obj.alpha = obj.alpha/sigp^2;

end

function [nll] = LL(obj,theta,regress,ntm)

if regress
Expand Down
2 changes: 2 additions & 0 deletions MatlabGP/MFGP.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

function obj = condition(obj,GPs)

obj.GPs = GPs;

nF = numel(obj.GPs);

for i = nF:-1:2
Expand Down
5 changes: 2 additions & 3 deletions MatlabGP/NLMFGP.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
%{
Multi-Fidelity Gaussian Process
Non-Linear Multi-Fidelity Gaussian Process
An auto-regressive (AR(1)) model based on Kennedy and O'Hagan using the
recursive formulation of Le Gratiet.
An auto-regressive (AR(N)) model based on Perdikaris
The model can be constructed using data from nF fidelities.
Expand Down
18 changes: 11 additions & 7 deletions MatlabGP/VGP.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@
y = obj.mean.eval(x) + ksu*obj.alpha;

if nargout>1

v1 = obj.Kuu\ksu';
v2 = obj.M\ksu';

sigs = -dot(ksu',(obj.Kuuinv)*ksu') + dot(ksu',obj.Minv*ksu');
sigs = dot(ksu',v2)-dot(ksu',v1);

sig = obj.kernel.scale + obj.kernel.signn + sigs';
end
Expand Down Expand Up @@ -102,7 +105,10 @@

ksu = obj.kernel.build(xs,xu);

sigs = -dot(ksu',(obj.Kuuinv)*ksu') + dot(ksu',obj.Minv*ksu');
v1 = obj.Kuu\ksu';
v2 = obj.M\ksu';

sigs = dot(ksu',v2)-dot(ksu',v1);

sig = obj.kernel.scale + obj.kernel.signn + sigs';

Expand Down Expand Up @@ -205,16 +211,15 @@

obj.kernel.scale = std(Y)/2;

obj.Kuu = obj.kernel.build(xu,xu);
obj.Kuuinv = pinv(obj.Kuu,1*10^(-7));
obj.Kuu = obj.kernel.build(xu,xu) + (1e-6)*eye(size(xu,1));
%obj.Kuuinv = pinv(obj.Kuu,1*10^(-7));

obj.Kuf = obj.kernel.build(xu,xf);

obj.B = obj.Kuf*obj.Kuf'/obj.kernel.signn;
obj.M = obj.Kuu + obj.B;
obj.Minv = pinv(obj.M,1*10^(-7));

obj.alpha = obj.Minv*obj.Kuf*(Y - obj.mean.eval(X))/obj.kernel.signn;
obj.alpha = obj.M\(obj.Kuf*(Y - obj.mean.eval(X))/obj.kernel.signn);

end

Expand Down Expand Up @@ -397,7 +402,6 @@

obj.B = obj.B + k2s*k2s'/obj.kernel.signn;
obj.M = obj.Kuu + obj.B;
obj.Minv = pinv(obj.M);

obj.alpha = obj.alpha + k2s*y/obj.kernel.signn;
end
Expand Down
Binary file modified MatlabGP/docs/TestGPClass.mlx
Binary file not shown.
Binary file modified MatlabGP/docs/TestGPClass_2D.mlx
Binary file not shown.
Binary file modified MatlabGP/docs/TestMFGPClass.mlx
Binary file not shown.
Binary file modified MatlabGP/docs/TestNLMFGPClass.mlx
Binary file not shown.
Binary file modified MatlabGP/docs/TestVGPClass.mlx
Binary file not shown.
Binary file modified MatlabGP/docs/TestVGPClass_2D.mlx
Binary file not shown.
Binary file modified MatlabGP/docs/TestVGPClass_periodic.mlx
Binary file not shown.
Binary file added MatlabGP/docs/TestWarpedGPClass.mlx
Binary file not shown.
30 changes: 0 additions & 30 deletions MatlabGP/examples/TestBlockPI.asv

This file was deleted.

0 comments on commit 1c47bfd

Please sign in to comment.