diff --git a/cov/covDiff/covDiff.m b/cov/covDerObs/covDerObs.m similarity index 97% rename from cov/covDiff/covDiff.m rename to cov/covDerObs/covDerObs.m index 6a7759d..2cded97 100644 --- a/cov/covDiff/covDiff.m +++ b/cov/covDerObs/covDerObs.m @@ -1,8 +1,8 @@ -function K = covDiff(f_handles, hyp, x, xd, z, i) +function K = covDerObs(f_handles, hyp, x, xd, z, i) %% function name convention % cov: covariance function -% Diff: differentiable w.r.t input coordinates or take derivative observations +% DerObs: differentiable w.r.t input coordinates or take derivative observations % Bwise: block maxtrix-wised version %% input/output arguments diff --git a/cov/covDiff/covMaterniso3Diff/covMaterniso3DD.m b/cov/covDerObs/covMaterniso3DerObs/covMaterniso3DD.m similarity index 100% rename from cov/covDiff/covMaterniso3Diff/covMaterniso3DD.m rename to cov/covDerObs/covMaterniso3DerObs/covMaterniso3DD.m diff --git a/cov/covDiff/covMaterniso3Diff/covMaterniso3Diff.m b/cov/covDerObs/covMaterniso3DerObs/covMaterniso3DerObs.m similarity index 53% rename from cov/covDiff/covMaterniso3Diff/covMaterniso3Diff.m rename to cov/covDerObs/covMaterniso3DerObs/covMaterniso3DerObs.m index 25a4dca..96e1636 100644 --- a/cov/covDiff/covMaterniso3Diff/covMaterniso3Diff.m +++ b/cov/covDerObs/covMaterniso3DerObs/covMaterniso3DerObs.m @@ -1,12 +1,21 @@ -function K = covMaterniso3Diff(hyp, x, z, i, xd) +function K = covMaterniso3DerObs(hyp, x, z, i) if nargin<1, K = '2'; return; end % report number of parameters if nargin<3, z = []; end % make sure, z exists if nargin<4, i = 0; end % derivative index -if nargin<5, xd = []; end % derivative inputs +dg = strcmp(z,'diag') && numel(z)>0; % determine mode + +% x, xd +if dg + xd = []; +else + der_mask = x(:, 1) ~= 0; + xd = x( der_mask, 2:end); + x = x(~der_mask, 2:end); +end % call FDI template f_handles.FF = {@covMaterniso, 3}; f_handles.FD = {@covMaterniso3FD}; f_handles.DD = {@covMaterniso3DD}; -K = covDiff(f_handles, hyp, x, xd, z, i); \ No newline at end of file +K = covDerObs(f_handles, hyp, x, xd, z, i); \ No newline at end of file diff --git a/cov/covDiff/covMaterniso3Diff/covMaterniso3FD.m b/cov/covDerObs/covMaterniso3DerObs/covMaterniso3FD.m similarity index 100% rename from cov/covDiff/covMaterniso3Diff/covMaterniso3FD.m rename to cov/covDerObs/covMaterniso3DerObs/covMaterniso3FD.m diff --git a/cov/covDiff/covSEisoDiff/covSEisoDD.m b/cov/covDerObs/covSEisoDerObs/covSEisoDD.m similarity index 100% rename from cov/covDiff/covSEisoDiff/covSEisoDD.m rename to cov/covDerObs/covSEisoDerObs/covSEisoDD.m diff --git a/cov/covDiff/covSEisoDiff/covSEisoDiff.m b/cov/covDerObs/covSEisoDerObs/covSEisoDerObs.m similarity index 52% rename from cov/covDiff/covSEisoDiff/covSEisoDiff.m rename to cov/covDerObs/covSEisoDerObs/covSEisoDerObs.m index 7c9ad42..71b337c 100644 --- a/cov/covDiff/covSEisoDiff/covSEisoDiff.m +++ b/cov/covDerObs/covSEisoDerObs/covSEisoDerObs.m @@ -1,12 +1,21 @@ -function K = covSEisoDiff(hyp, x, z, i, xd) +function K = covSEisoDerObs(hyp, x, z, i) if nargin<1, K = '2'; return; end % report number of parameters if nargin<3, z = []; end % make sure, z exists if nargin<4, i = 0; end % derivative index -if nargin<5, xd = []; end % derivative inputs +dg = strcmp(z,'diag') && numel(z)>0; % determine mode + +% x, xd +if dg + xd = []; +else + der_mask = x(:, 1) ~= 0; + xd = x( der_mask, 2:end); + x = x(~der_mask, 2:end); +end % call FDI template f_handles.FF = {@covSEiso}; f_handles.FD = {@covSEisoFD}; f_handles.DD = {@covSEisoDD}; -K = covDiff(f_handles, hyp, x, xd, z, i); \ No newline at end of file +K = covDerObs(f_handles, hyp, x, xd, z, i); \ No newline at end of file diff --git a/cov/covDiff/covSEisoDiff/covSEisoFD.backup b/cov/covDerObs/covSEisoDerObs/covSEisoFD.backup similarity index 100% rename from cov/covDiff/covSEisoDiff/covSEisoFD.backup rename to cov/covDerObs/covSEisoDerObs/covSEisoFD.backup diff --git a/cov/covDiff/covSEisoDiff/covSEisoFD.m b/cov/covDerObs/covSEisoDerObs/covSEisoFD.m similarity index 100% rename from cov/covDiff/covSEisoDiff/covSEisoFD.m rename to cov/covDerObs/covSEisoDerObs/covSEisoFD.m diff --git a/cov/covDiff/covSparseisoDiff/covSparseisoDD.m b/cov/covDerObs/covSparseisoDerObs/covSparseisoDD.m similarity index 100% rename from cov/covDiff/covSparseisoDiff/covSparseisoDD.m rename to cov/covDerObs/covSparseisoDerObs/covSparseisoDD.m diff --git a/cov/covDiff/covSparseisoDiff/covSparseisoDiff.m b/cov/covDerObs/covSparseisoDerObs/covSparseisoDerObs.m similarity index 53% rename from cov/covDiff/covSparseisoDiff/covSparseisoDiff.m rename to cov/covDerObs/covSparseisoDerObs/covSparseisoDerObs.m index 44efb9b..2a9fa6e 100644 --- a/cov/covDiff/covSparseisoDiff/covSparseisoDiff.m +++ b/cov/covDerObs/covSparseisoDerObs/covSparseisoDerObs.m @@ -1,12 +1,21 @@ -function K = covSparseisoDiff(hyp, x, z, i, xd) +function K = covSparseisoDerObs(hyp, x, z, i) if nargin<1, K = '2'; return; end % report number of parameters if nargin<3, z = []; end % make sure, z exists if nargin<4, i = 0; end % derivative index -if nargin<5, xd = []; end % derivative inputs +dg = strcmp(z,'diag') && numel(z)>0; % determine mode + +% x, xd +if dg + xd = []; +else + der_mask = x(:, 1) ~= 0; + xd = x( der_mask, 2:end); + x = x(~der_mask, 2:end); +end % call FDI template f_handles.FF = {@covSparseiso}; f_handles.FD = {@covSparseisoFD}; f_handles.DD = {@covSparseisoDD}; -K = covDiff(f_handles, hyp, x, xd, z, i); \ No newline at end of file +K = covDerObs(f_handles, hyp, x, xd, z, i); \ No newline at end of file diff --git a/cov/covDiff/covSparseisoDiff/covSparseisoFD.m b/cov/covDerObs/covSparseisoDerObs/covSparseisoFD.m similarity index 100% rename from cov/covDiff/covSparseisoDiff/covSparseisoFD.m rename to cov/covDerObs/covSparseisoDerObs/covSparseisoFD.m diff --git a/cov/covDiff/covDiff.backup b/cov/covDiff/covDiff.backup deleted file mode 100644 index 6dcda83..0000000 --- a/cov/covDiff/covDiff.backup +++ /dev/null @@ -1,106 +0,0 @@ -function K = covDiff(hyp, x, xd, z, ii, hCovFF, hCovFD, hCovDD) - -if nargin<4, z = []; end % make sure, z exists -xeqz = numel(z)==0; dg = strcmp(z,'diag') && numel(z)>0; % determine mode -if nargin<5, ii = 0; end % derivative index - -% number of data and dimensions -[n, d] = size(x); -n1 = n - n2; -x1 = x(1:n1, :); -x2 = x(n1+1:end, :); - -if dg - K = feval(hCovFF{:}, hyp, x, z); -else - if xeqz - % |F1(n1)|D1(n1)|D2(n1)|D3(n1)| F2(n2) - % K = ------------------------------------------ - % F1(n1) | F1F1, F1D1, F1D2, F1D3,| F1F2 - % D1(n1) | -, D1D1, D1D2, D1D3,| D1F2 - % D2(n1) | -, -, D2D2, D2D3,| D2F2 - % D3(n1) | -, -, -, D3D3,| D3F2 - % ------------------------------------------ - % F2(n2) | -, -, -, -, | F2F2 - - K = zeros(n1*(d+1)+n2, n1*(d+1)+n2); - for row = 0:d - idx_row = n1*row+1:n1*(row+1); - for col = row:d - idx_col = n1*col+1:n1*(col+1); - - % itself - if row == 0 - if col == 0 - % F1F1 - if ii == 0, K(idx_row, idx_col) = feval(hCovFF{:}, hyp, x1); - else K(idx_row, idx_col) = feval(hCovFF{:}, hyp, x1, [], ii); end - else - % F1D* - K(idx_row, idx_col) = feval(hCovFD{:}, hyp, x1, x1, col, ii); - end - else - % D*D* - K(idx_row, idx_col) = feval(hCovDD{:}, hyp, x1, row, x1, col, ii); - end - - % transpose - if row ~= col - K(idx_col, idx_row) = K(idx_row, idx_col)'; - end - - end - % last column - idx_col = n1*(d+1)+1:n1*(d+1)+n2; - - % F1F2 - if row == 0 - if ii == 0, K(idx_row, idx_col) = feval(hCovFF{:}, hyp, x1, x2); - else K(idx_row, idx_col) = feval(hCovFF{:}, hyp, x1, x2, ii); end - - % D*F2 - else - K(idx_row, idx_col) = feval(hCovFD{:}, hyp, x2, x1, row, ii)'; - end - - % transpose - K(idx_col, idx_row) = K(idx_row, idx_col)'; - end - % last row and last column - idx_row = n1*(d+1)+1:n1*(d+1)+n2; - idx_col = n1*(d+1)+1:n1*(d+1)+n2; - - % F2F2 - if ii == 0, K(idx_row, idx_col) = feval(hCovFF{:}, hyp, x2, []); - else K(idx_row, idx_col) = feval(hCovFF{:}, hyp, x2, [], ii); end - else - % |F(m)| - % K = ------------- - % F1(n1) | F1F - % D1(n1) | D1F - % D2(n1) | D2F - % D3(n1) | D3F - % ------------- - % F2(n2) | F2F - - [m, d] = size(z); - K = zeros(n1*(d+1)+n2, m); - for row = 0:d - idx_row = n1*row+1:n1*(row+1); - if row == 0 - % F1F - if ii == 0, K(idx_row, :) = feval(hCovFF{:}, hyp, x1, z); - else K(idx_row, :) = feval(hCovFF{:}, hyp, x1, z, ii); end - else - % D*F - K(idx_row, :) = feval(hCovFD{:}, hyp, z, x1, row, ii)'; - end - end - % last row - idx_row = n1*(d+1)+1:n1*(d+1)+n2; - - % F2F - if ii == 0, K(idx_row, :) = feval(hCovFF{:}, hyp, x2, z); - else K(idx_row, :) = feval(hCovFF{:}, hyp, x2, z, ii); end - end -end \ No newline at end of file diff --git a/cov/covisoUnstable/covMaterniso3DiffUnstable/covMaterniso3DiffUnstable.m b/cov/covisoDerObsUnstable/covMaterniso3DerObsUnstable/covMaterniso3DerObsUnstable.m similarity index 75% rename from cov/covisoUnstable/covMaterniso3DiffUnstable/covMaterniso3DiffUnstable.m rename to cov/covisoDerObsUnstable/covMaterniso3DerObsUnstable/covMaterniso3DerObsUnstable.m index f09eb56..b5f30ac 100644 --- a/cov/covisoUnstable/covMaterniso3DiffUnstable/covMaterniso3DiffUnstable.m +++ b/cov/covisoDerObsUnstable/covMaterniso3DerObsUnstable/covMaterniso3DerObsUnstable.m @@ -1,27 +1,35 @@ -function K = covMaterniso3DiffUnstable(hyp, x, z, i, xd, f_Bwise) +function K = covMaterniso3DerObsUnstable(hyp, x, z, i, f_Bwise) %% function name convention % cov: covariance function % Matern: Matern % iso: isotropic -% Diff: differentiable w.r.t input coordinates or take derivative observations +% DerObs: differentiable w.r.t input coordinates or take derivative observations % Unstable: cannot handle divide-by-zero %% input/output arguments -% hyp: [1x2] hyperparameters, hyp = [log(ell), log(sigma_f)] -% x: [nxd] first function input vectors -% z: [nsxd] second function input vectors, default: [] meaning z = x -% i: partial deriavtive coordiante w.r.t hyperparameters, default: 0 -% xd: [ndxd] first derivative input vectors -% f_Bwise: true: block matrix-wised version, false: coeffient-wised version -% K: [nnxns] covariance, nn = n + nd*d +% hyp: [1x2] hyperparameters, hyp = [log(ell), log(sigma_f)] +% x: [nx(d+1)] first function/derivative input vectors +% z: [nsxd] second function input vectors, default: [] meaning z = x +% i: partial deriavtive coordiante w.r.t hyperparameters, default: 0 +% f_Bwise: true: block matrix-wised version, false: coeffient-wised version +% K: [nnxns] covariance, nn = n + nd*d %% default parameters if nargin < 2, K = '2'; return; end % report number of parameters if nargin < 3, z = []; end % make sure, z exists if nargin < 4, i = 0; end -if nargin < 5, xd = []; end -if nargin < 6, f_Bwise = true; end +if nargin < 5, f_Bwise = true; end +dg = strcmp(z,'diag') && numel(z)>0; % determine mode + +% x, xd +if dg + xd = []; +else + der_mask = x(:, 1) ~= 0; + xd = x( der_mask, 2:end); + x = x(~der_mask, 2:end); +end %% component functions @@ -56,9 +64,9 @@ % call if f_Bwise - K = covisoDiffBwiseUnstable(f_handles, hyp, x, xd, z, i); + K = covisoDerObsBwiseUnstable(f_handles, hyp, x, xd, z, i); else - K = covisoDiffCwiseUnstable(f_handles, hyp, x, xd, z, i); + K = covisoDerObsCwiseUnstable(f_handles, hyp, x, xd, z, i); end %% sub component function diff --git a/cov/covisoUnstable/covSEisoDiffUnstable/covSEisoDiffUnstable.m b/cov/covisoDerObsUnstable/covSEisoDerObsUnstable/covSEisoDerObsUnstable.m similarity index 68% rename from cov/covisoUnstable/covSEisoDiffUnstable/covSEisoDiffUnstable.m rename to cov/covisoDerObsUnstable/covSEisoDerObsUnstable/covSEisoDerObsUnstable.m index 36bf228..0fb2e1c 100644 --- a/cov/covisoUnstable/covSEisoDiffUnstable/covSEisoDiffUnstable.m +++ b/cov/covisoDerObsUnstable/covSEisoDerObsUnstable/covSEisoDerObsUnstable.m @@ -1,27 +1,35 @@ -function K = covSEisoDiffUnstable(hyp, x, z, i, xd, f_Bwise) +function K = covSEisoDerObsUnstable(hyp, x, z, i, f_Bwise) %% function name convention % cov: covariance function % SE: squared exponential % iso: isotropic -% Diff: differentiable w.r.t input coordinates or take derivative observations +% DerObs: differentiable w.r.t input coordinates or take derivative observations % Unstable: cannot handle divide-by-zero %% input/output arguments -% hyp: [1x2] hyperparameters, hyp = [log(ell), log(sigma_f)] -% x: [nxd] first function input vectors -% z: [nsxd] second function input vectors, default: [] meaning z = x -% i: partial deriavtive coordiante w.r.t hyperparameters, default: 0 -% xd: [ndxd] first derivative input vectors -% f_Bwise: true: block matrix-wised version, false: coeffient-wised version -% K: [nnxns] covariance, nn = n + nd*d +% hyp: [1x2] hyperparameters, hyp = [log(ell), log(sigma_f)] +% x: [nx(d+1)] first function/derivative input vectors +% z: [nsxd] second function input vectors, default: [] meaning z = x +% i: partial deriavtive coordiante w.r.t hyperparameters, default: 0 +% f_Bwise: true: block matrix-wised version, false: coeffient-wised version +% K: [nnxns] covariance, nn = n + nd*d %% default parameters if nargin < 2, K = '2'; return; end % report number of parameters if nargin < 3, z = []; end % make sure, z exists if nargin < 4, i = 0; end -if nargin < 5, xd = []; end -if nargin < 6, f_Bwise = true; end +if nargin < 5, f_Bwise = true; end +dg = strcmp(z,'diag') && numel(z)>0; % determine mode + +% x, xd +if dg + xd = []; +else + der_mask = x(:, 1) ~= 0; + xd = x( der_mask, 2:end); + x = x(~der_mask, 2:end); +end %% component functions @@ -51,9 +59,9 @@ % call if f_Bwise - K = covisoDiffBwiseUnstable(f_handles, hyp, x, xd, z, i); + K = covisoDerObsBwiseUnstable(f_handles, hyp, x, xd, z, i); else - K = covisoDiffCwiseUnstable(f_handles, hyp, x, xd, z, i); + K = covisoDerObsCwiseUnstable(f_handles, hyp, x, xd, z, i); end %% sub component function diff --git a/cov/covisoUnstable/covSparseisoDiffUnstable/covSparseisoDiffUnstable.m b/cov/covisoDerObsUnstable/covSparseisoDerObsUnstable/covSparseisoDerObsUnstable.m similarity index 76% rename from cov/covisoUnstable/covSparseisoDiffUnstable/covSparseisoDiffUnstable.m rename to cov/covisoDerObsUnstable/covSparseisoDerObsUnstable/covSparseisoDerObsUnstable.m index 6658d34..1f4599c 100644 --- a/cov/covisoUnstable/covSparseisoDiffUnstable/covSparseisoDiffUnstable.m +++ b/cov/covisoDerObsUnstable/covSparseisoDerObsUnstable/covSparseisoDerObsUnstable.m @@ -1,27 +1,35 @@ -function K = covSparseisoDiffUnstable(hyp, x, z, i, xd, f_Bwise) +function K = covSparseisoDerObsUnstable(hyp, x, z, i, f_Bwise) %% function name convention % cov: covariance function % Sparse: sparse % iso: isotropic -% Diff: differentiable w.r.t input coordinates or take derivative observations +% DerObs: differentiable w.r.t input coordinates or take derivative observations % Unstable: cannot handle divide-by-zero %% input/output arguments -% hyp: [1x2] hyperparameters, hyp = [log(ell), log(sigma_f)] -% x: [nxd] first function input vectors -% z: [nsxd] second function input vectors, default: [] meaning z = x -% i: partial deriavtive coordiante w.r.t hyperparameters, default: 0 -% xd: [ndxd] first derivative input vectors -% f_Bwise: true: block matrix-wised version, false: coeffient-wised version -% K: [nnxns] covariance, nn = n + nd*d +% hyp: [1x2] hyperparameters, hyp = [log(ell), log(sigma_f)] +% x: [nx(d+1)] first function/derivative input vectors +% z: [nsxd] second function input vectors, default: [] meaning z = x +% i: partial deriavtive coordiante w.r.t hyperparameters, default: 0 +% f_Bwise: true: block matrix-wised version, false: coeffient-wised version +% K: [nnxns] covariance, nn = n + nd*d %% default parameters if nargin < 2, K = '2'; return; end % report number of parameters if nargin < 3, z = []; end % make sure, z exists if nargin < 4, i = 0; end -if nargin < 5, xd = []; end -if nargin < 6, f_Bwise = true; end +if nargin < 5, f_Bwise = true; end +dg = strcmp(z,'diag') && numel(z)>0; % determine mode + +% x, xd +if dg + xd = []; +else + der_mask = x(:, 1) ~= 0; + xd = x( der_mask, 2:end); + x = x(~der_mask, 2:end); +end %% component functions @@ -56,9 +64,9 @@ % call if f_Bwise - K = covisoDiffBwiseUnstable(f_handles, hyp, x, xd, z, i); + K = covisoDerObsBwiseUnstable(f_handles, hyp, x, xd, z, i); else - K = covisoDiffCwiseUnstable(f_handles, hyp, x, xd, z, i); + K = covisoDerObsCwiseUnstable(f_handles, hyp, x, xd, z, i); end %% sub component function diff --git a/cov/covisoUnstable/covisoDiffBwiseUnstable.m b/cov/covisoDerObsUnstable/covisoDerObsBwiseUnstable.m similarity index 90% rename from cov/covisoUnstable/covisoDiffBwiseUnstable.m rename to cov/covisoDerObsUnstable/covisoDerObsBwiseUnstable.m index daacb28..ae1232b 100644 --- a/cov/covisoUnstable/covisoDiffBwiseUnstable.m +++ b/cov/covisoDerObsUnstable/covisoDerObsBwiseUnstable.m @@ -1,4 +1,4 @@ -function K = covisoDiffBwiseUnstable(f_handles, hyp, x, xd, z, i) +function K = covisoDerObsBwiseUnstable(f_handles, hyp, x, xd, z, i) %% function name convention % cov: covariance function @@ -25,7 +25,8 @@ % prediction if dg % Kss - K = ones(n+nd, 1); + assert(i == 0); + K = exp(2*hyp(end))*ones(n+nd, 1); % sigma_f = exp(hyp(end)) % learning else @@ -83,7 +84,7 @@ idx_col = start_col:end_col; % calculation - K(idx_row, idx_col) = covisoDiffUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); + K(idx_row, idx_col) = covisoDerObsUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); end end @@ -97,6 +98,8 @@ % ... | % df(xd)/dx_d | + assert(i == 0); + nn = n + nd*d; K = zeros(nn, ns); @@ -131,7 +134,7 @@ idx_col = start_col:end_col; % calculation - K(idx_row, idx_col) = covisoDiffUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); + K(idx_row, idx_col) = covisoDerObsUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); end end end diff --git a/cov/covisoUnstable/covisoDiffCwiseUnstable.m b/cov/covisoDerObsUnstable/covisoDerObsCwiseUnstable.m similarity index 89% rename from cov/covisoUnstable/covisoDiffCwiseUnstable.m rename to cov/covisoDerObsUnstable/covisoDerObsCwiseUnstable.m index e906a14..27ae7a6 100644 --- a/cov/covisoUnstable/covisoDiffCwiseUnstable.m +++ b/cov/covisoDerObsUnstable/covisoDerObsCwiseUnstable.m @@ -1,4 +1,4 @@ -function K = covisoDiffCwiseUnstable(f_handles, hyp, x, xd, z, i) +function K = covisoDerObsCwiseUnstable(f_handles, hyp, x, xd, z, i) %% function name convention % cov: covariance function @@ -24,7 +24,8 @@ % prediction if dg % Kss - K = ones(n+nd, 1); + assert(i == 0); + K = exp(2*hyp(end))*ones(n+nd, 1); % sigma_f = exp(hyp(end)) % learning else @@ -73,7 +74,7 @@ end % calculation - K(row, col) = covisoDiffUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); + K(row, col) = covisoDerObsUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); end end @@ -87,6 +88,8 @@ % ... | % df(xd)/dx_d | + assert(i == 0); + nn = n + nd*d; K = zeros(nn, ns); @@ -111,7 +114,7 @@ zz = z(col, :); % calculation - K(row, col) = covisoDiffUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); + K(row, col) = covisoDerObsUnstable(f_handles, hyp, xx, zz, i, pdx, pdz); end end end diff --git a/cov/covisoUnstable/covisoDiffUnstable.m b/cov/covisoDerObsUnstable/covisoDerObsUnstable.m similarity index 98% rename from cov/covisoUnstable/covisoDiffUnstable.m rename to cov/covisoDerObsUnstable/covisoDerObsUnstable.m index 2a34d10..453c9e9 100644 --- a/cov/covisoUnstable/covisoDiffUnstable.m +++ b/cov/covisoDerObsUnstable/covisoDerObsUnstable.m @@ -1,4 +1,4 @@ -function K = covisoDiffUnstable(f_handles, hyp, x, z, i, pdx, pdz) +function K = covisoDerObsUnstable(f_handles, hyp, x, z, i, pdx, pdz) %% function name convention % cov: covariance function @@ -151,6 +151,6 @@ % [3.0] dk(x, dz/dz_j)/dlog(sigma_f) = 2 * k(x, dz/dz_j) % dk(dx/dx_i, z)/dlog(sigma_f) = 2 * k(dx/dx_i, z) % dk(dx/dx_j, dz/dz_j)/dlog(sigma_f) = 2 k(dx/dx_j, dz/dz_j) - K = 2 * covisoDiffUnstable(f_handles, hyp, x, z, 0, pdx, pdz); + K = 2 * covisoDerObsUnstable(f_handles, hyp, x, z, 0, pdx, pdz); end end \ No newline at end of file diff --git a/inf/infExactDerObs.m b/inf/infExactDerObs.m new file mode 100644 index 0000000..9c1a770 --- /dev/null +++ b/inf/infExactDerObs.m @@ -0,0 +1,81 @@ +function [post nlZ dnlZ] = infExactDerObs(hyp, mean, cov, lik, x, y) + +% Exact inference for a GP with Gaussian likelihood. Compute a parametrization +% of the posterior, the negative log marginal likelihood and its derivatives +% w.r.t. the hyperparameters. See also "help infMethods". +% +% Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2011-02-18 +% +% See also INFMETHODS.M. + +likstr = lik; if ~ischar(lik), likstr = func2str(lik); end +if ~strcmp(likstr,'likGaussDerObs') % NOTE: no explicit call to likGauss + error('Exact inference only possible with Gaussian likelihood dealing with derivative observations'); +end + +% number of function and derivative training data +nd = sum(x(:, 1) ~= 0); % derivative training data +n = size(x, 1) - nd; % function derivative training data +d = size(x, 2) - 1; % number of dimensions +nn = n + nd*d; + +K = feval(cov{:}, hyp.cov, x); % evaluate covariance matrix +m = feval(mean{:}, hyp.mean, x); % evaluate mean vector + +sn2 = exp(2*hyp.lik(1)); % noise variance of likGauss +snd2 = exp(2*hyp.lik(2)); % noise variance of likGauss +D = [ sn2*ones(n, 1); + snd2*ones(nd*d, 1)]; +L = chol(K+diag(D)); % Cholesky factor of covariance with noise +alpha = solve_chol(L,y-m); + +post.alpha = alpha; % return the posterior parameters +post.sW = ones(nn,1); % sqrt of noise precision vector +post.L = L; % L = chol(eye(n)+sW*sW'.*K) + +if nargout>1 % do we want the marginal likelihood? + % p(y) = N(m, Kn) = (2pi)^(-n/2) * |Kn|^(-1/2) * exp[(-1/2) * (y-m)' * inv(Kn) * (y-m)] + % nlZ = (1/2) * (y-m)' * inv(Kn) * (y-m) + (1/2) * log |Kn| + (n/2) * log(2pi) + % = (1/2) * (y-m)' * alpha + (1/2) * log |L*L'| + (n/2) * log(2pi) + % = (1/2) * (y-m)' * alpha + (1/2) * log |L|*|L'| + (n/2) * log(2pi) + % = (1/2) * (y-m)' * alpha + log |L|| + (n/2) * log(2pi) + % = (1/2) * (y-m)' * alpha + tr[log (L)] + (n/2) * log(2pi) + nlZ = (y-m)'*alpha/2 + sum(log(diag(L))) + n*log(2*pi)/2; % -log marg lik + + if nargout>2 % do we want derivatives? + dnlZ = hyp; % allocate space for derivatives + + % (1) w.r.t the mean parameters + % nlZ = (1/2) * (y-m)' * inv(Kn) * (y-m) + % = - m' * inv(Kn) * y + (1/2) m' * inv(Kn) * m + % nlZ_i = - m_i' * inv(Kn) * y + m_i' * inv(Kn) * m + % = - m_i' * inv(Kn) (y - m) + % = - m_i' * alpha + for i = 1:numel(hyp.mean), + dnlZ.mean(i) = -feval(mean{:}, hyp.mean, x, i)'*alpha; + end + + % (2) w.r.t the cov parameters + % nlZ = (1/2) * (y-m)' * inv(Kn) * (y-m) + (1/2) * log |Kn| + % nlZ_j = (-1/2) * (y-m)' * inv(Kn) * K_j * inv(Kn) * (y-m) + (1/2) * tr[inv(Kn) * K_j] + % = (-1/2) * alpha' * K_j * alpha + (1/2) * tr[inv(Kn) * K_j] + % = (-1/2) * tr[(alpha' * alpha) * K_j] + (1/2) * tr[inv(Kn) * K_j] + % = (1/2) tr[(inv(Kn) - alpha*alpha') * K_j] + % = (1/2) tr[Q * K_j] + % Q = inv(Kn) - alpha*alpha' + Q = solve_chol(L,eye(nn)) - alpha*alpha'; % precompute for convenience + for i = 1:numel(hyp.cov) + dnlZ.cov(i) = sum(sum(Q.*feval(cov{:}, hyp.cov, x, [], i)))/2; + end + + % (3) w.r.t the cov parameters + % nlZ = (1/2) * (y-m)' * inv(K + D) * (y-m) + (1/2) * log |K + D| + % nlZ_j = (-1/2) * (y-m)' * inv(Kn) * D_j * inv(Kn) * (y-m) + (1/2) * tr[inv(Kn) * D_j] + % = (-1/2) * alpha' * D_j * alpha + (1/2) * tr[inv(Kn) * D_j] + % = (-1/2) * tr[(alpha' * alpha) * D_j] + (1/2) * tr[inv(Kn) * D_j] + % = (1/2) tr[(inv(Kn) - alpha*alpha') * D_j] + % = (1/2) tr[Q * D_j] + dnlZ.lik(1) = sn2*trace(Q(1:n, 1:n)); + dnlZ.lik(2) = snd2*trace(Q(n+1:end, n+1:end)); + end +end diff --git a/lik/likGaussDerObs.m b/lik/likGaussDerObs.m new file mode 100644 index 0000000..149e253 --- /dev/null +++ b/lik/likGaussDerObs.m @@ -0,0 +1,59 @@ +function [varargout] = likGaussDerObs(hyp, y, mu, s2, inf, i) + +% likGauss - Gaussian likelihood function for regression. The expression for the +% likelihood is +% likGauss(t) = exp(-(t-y)^2/2*sn^2) / sqrt(2*pi*sn^2), +% where y is the mean and sn is the standard deviation. +% +% The hyperparameters are: +% +% hyp = [ log(sn), log(snd) ] +% +% Several modes are provided, for computing likelihoods, derivatives and moments +% respectively, see likelihoods.m for the details. In general, care is taken +% to avoid numerical issues when the arguments are extreme. +% +% See also likFunctions.m. + +if nargin<2, varargout = {'2'}; return; end % report number of hyperparameters + +sn2 = exp(2*hyp(1)); + +if nargin<5 % prediction mode if inf is not present + if numel(y)==0, y = zeros(size(mu)); end + s2zero = 1; if nargin>3, if norm(s2)>0, s2zero = 0; end, end % s2==0 ? + if s2zero % log probability + lp = -(y-mu).^2./sn2/2-log(2*pi*sn2)/2; s2 = 0; + else + lp = likGaussDerObs(hyp, y, mu, s2, 'infEP'); % prediction + end + ymu = {}; ys2 = {}; + if nargout>1 + ymu = mu; % first y moment + if nargout>2 + ys2 = s2 + sn2; % second y moment + end + end + varargout = {lp,ymu,ys2}; +else + switch inf + case 'infEP' + if nargin<6 % no derivative mode + lZ = -(y-mu).^2./(sn2+s2)/2 - log(2*pi*(sn2+s2))/2; % log part function + dlZ = {}; d2lZ = {}; + if nargout>1 + dlZ = (y-mu)./(sn2+s2); % 1st derivative w.r.t. mean + if nargout>2 + d2lZ = -1./(sn2+s2); % 2nd derivative w.r.t. mean + end + end + varargout = {lZ,dlZ,d2lZ}; + else % derivative mode + dlZhyp = ((y-mu).^2./(sn2+s2)-1) ./ (1+s2./sn2); % deriv. w.r.t. hyp.lik + varargout = {dlZhyp}; + end + + otherwise + error('Not Implimented'); + end +end diff --git a/main/main_covSEisoDiff.m b/main/main_covSEisoDiff.m deleted file mode 100644 index 9261ffa..0000000 --- a/main/main_covSEisoDiff.m +++ /dev/null @@ -1,34 +0,0 @@ -clc -clear all -close all - -addpath('../util/'); -addpath('../cov/'); -addpath('../cov/covSEisoDiff'); - -% hyperparameters -hyp = log([1, 1]); - -% data -d = 3; -n = 5; x = rand(n, d); -nd = 4; xd = rand(nd, d); -ns = 3; z = rand(ns, d); - -% We have function and derivative observation at derivative inputs. -% Or we have surface normal vectors at sampled hit points. -x = [x; xd]; - -% K -K = covSEisoDiffCwise(hyp, x, [], 0, xd); -print_matrix_for_reference(K); - -% dK/dlog(theta) -dK_dlogell = covSEisoDiffCwise(hyp, x, [], 1, xd); -dK_dlogsf = covSEisoDiffCwise(hyp, x, [], 2, xd); -print_matrix_for_reference(dK_dlogell); -print_matrix_for_reference(dK_dlogsf); - -% Ks -Ks = covSEisoDiffCwise(hyp, x, z, 0, xd); -print_matrix_for_reference(Ks); diff --git a/main/main_prediction.m b/main/main_prediction.m new file mode 100644 index 0000000..7f68baa --- /dev/null +++ b/main/main_prediction.m @@ -0,0 +1,117 @@ +clc +clear all +close all + +%% path +addpath('../util/'); + +% mean function +addpath('../mean'); + +% covariance function +addpath('../cov/covSparseiso'); + +addpath('../cov/covisoDerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covSEisoDerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covMaterniso3DerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covSparseisoDerObsUnstable'); + +addpath('../cov/covDerObs'); +addpath('../cov/covDerObs/covSEisoDerObs'); +addpath('../cov/covDerObs/covMaterniso3DerObs'); +addpath('../cov/covDerObs/covSparseisoDerObs'); + +% likelihood function +addpath('../lik'); + +% inference method +addpath('../inf'); + + +%% data +% function +f = @(x) 2*sin(3*pi*x) - 2*x.^3 + x.^2 -3*x + 1 - sqrt(x) - cos(10*pi*x); +df = @(x) 6*pi*cos(3*pi*x) - 6*x.^2 + 2*x - 3 - 1./(2*sqrt(x)) + 10*pi*sin(10*pi*x); + +% points +x = (0:0.01:1)'; +y = feval(f, x); + +xx = (0.04:0.1:0.95)'; +yy = feval(f, xx); +dy = feval(df, xx); + +% plot +figure('position', [0, 0, 1200, 400]); +subplot(1, 3, 1); +hold on +delta_x = 0.03; +plot(x, y, 'k-'); +plot(xx, yy, 'or'); +plot([xx' - delta_x; xx' + delta_x], [yy' - dy'*delta_x; yy' + dy'*delta_x], 'r-'); +axis([0, 1, min(y)-0.1, max(y)+0.1]); + +%% GP - Function Observations +% setting +mean_func = @meanZero; +cov_func = {@covSEiso}; +% cov_func = {@covMaterniso, 3}; +lik_func = @likGauss; +inf_method = @infExact; + +% hyperparameter +ell = 0.5; sf = 1.5; sn = 0.1; +hyp.cov = log([ell, sf]); +hyp.lik = log(sn); + +% training +hyp1 = minimize(hyp, @gp, -100, inf_method, mean_func, cov_func, lik_func, xx, yy); + +% prediction - regression +[dummy, dummy, fmu, fs2] = gp(hyp1, inf_method, mean_func, cov_func, lik_func, xx, yy, x); + +% plot +subplot(1, 3, 2); +hold on; +ff = [fmu+2*sqrt(fs2); flipdim(fmu-2*sqrt(fs2),1)]; +fill([x; flipdim(x,1)], ff, [7 7 7]/8) +plot(x, fmu, 'b-'); +plot(x, y, 'k-'); +plot(xx, yy, 'or'); +axis([0, 1, min(y)-0.1, max(y)+0.1]); + + +%% GP - Derivative and Function Observations +% setting +mean_func = @meanZeroDerObs; +cov_func = {@covSEisoDerObs}; +lik_func = @likGaussDerObs; +inf_method = @infExactDerObs; + +% data +xxd = [zeros(length(xx), 1), xx; + ones(length(xx), 1), xx]; +yyd = [yy; + dy]; +% xxd = [zeros(length(xx), 1), xx]; +% yyd = [yy]; + +% hyperparameter +hyp.cov = log([ell, sf]); +hyp.lik = log([sn, sn]); + +% training +hyp2 = minimize(hyp, @gp, -100, inf_method, mean_func, cov_func, lik_func, xxd, yyd); + +% prediction - regression +[dummy, dummy, fmu, fs2] = gp(hyp2, inf_method, mean_func, cov_func, lik_func, xxd, yyd, x); + +% plot +subplot(1, 3, 3); +hold on; +ff = [fmu+2*sqrt(fs2); flipdim(fmu-2*sqrt(fs2),1)]; +fill([x; flipdim(x,1)], ff, [7 7 7]/8) +plot(x, fmu, 'b-'); +plot(x, y, 'k-'); +plot(xx, yy, 'or'); +axis([0, 1, min(y)-0.1, max(y)+0.1]); \ No newline at end of file diff --git a/main/main_print_all_cov.m b/main/main_print_all_cov.m new file mode 100644 index 0000000..a8b5d9d --- /dev/null +++ b/main/main_print_all_cov.m @@ -0,0 +1,134 @@ +clc +clear all +close all + +%% path +addpath('../util/'); + +addpath('../cov/covSparseiso'); + +addpath('../cov/covisoDerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covSEisoDerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covMaterniso3DerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covSparseisoDerObsUnstable'); + +addpath('../cov/covDerObs'); +addpath('../cov/covDerObs/covSEisoDerObs'); +addpath('../cov/covDerObs/covMaterniso3DerObs'); +addpath('../cov/covDerObs/covSparseisoDerObs'); + +%% Setting +% hyperparameters +ell = 0.5; +sf = 1.5; +hyp = log([ell, sf]); + +% data +d = 3; +n = 5; +nd = 4; +ns = 3; + +% scale = 0.1; +scale = 1; +% scale = 10; + +% x = scale*rand(n, d); +% xd = scale*rand(nd, d); +% z = scale*rand(ns, d); +% +% x = scale*rand(n-1, d); x = [x; x(end, :)]; % duplication +% xd = scale*rand(nd-2, d); xd = [xd; x(end, :); xd(end, :)]; % duplication +% z = scale*rand(ns-2, d); z = [z; x(end, :); xd(end, :)]; % duplication + +x = [ +0.789963029944531, 0.111705744193203, 0.189710406017580; +0.318524245398992, 0.136292548938299, 0.495005824990221; +0.534064127370726, 0.678652304800188, 0.147608221976689; +0.089950678770581, 0.495177019089661, 0.054974146906188; +0.089950678770581, 0.495177019089661, 0.054974146906188]; + +xd = [ +0.850712674289007, 0.929608866756663, 0.582790965175840; +0.560559527354885, 0.696667200555228, 0.815397211477421; +0.089950678770581, 0.495177019089661, 0.054974146906188; +0.560559527354885, 0.696667200555228, 0.815397211477421]; + +z = [ +0.879013904597178, 0.988911616079589, 0.000522375356945; +0.089950678770581, 0.495177019089661, 0.054974146906188; +0.560559527354885, 0.696667200555228, 0.815397211477421]; + +% We have function and derivative observation at derivative inputs. +% Or we have surface normal vectors at sampled hit points. +% x = [x; xd]; + +% print_matrix_for_reference(x, 'x'); +% print_matrix_for_reference(xd, 'xd'); +% print_matrix_for_reference(z, 'z'); + + +%% Covariances +% print_matrix_for_reference(sq_dist(x'), 'sqDist(x)'); +% print_matrix_for_reference(sq_dist(x', z'), 'sqDist(x, z)'); +% print_matrix_for_reference(delta(x, x, 1), 'delta(x1, x1)'); +% print_matrix_for_reference(delta(x, z, 1), 'delta(x1, z1)'); + +% print_cov({@covSEiso}, 'covSEiso', hyp, x, z); +% print_covDerObs(@covSEisoDerObs, 'covSEiso', hyp, x, xd, z); + + +%% Gaussian Processes +% setting +mean_func = @meanZero; +cov_func = {@covSEiso}; +% cov_func = {@covMaterniso, 3}; +lik_func = @likGauss; +inf_method = @infExact; + +% hyperparameter +hyp.cov = log([ell, sf]); +sn = 0.1; +hyp.lik = log(sn); + +% y = scale*rand(n, 1); +y = [ +0.729513045504647; +0.224277070664514; +0.269054731773365; +0.673031165004119; +0.477492197726861]; + +% nn = n + nd*d; +% y = scale*rand(nn, 1); +% y = [ +% 0.346448761300360; +% 0.886543861760306; +% 0.454694864991908; +% 0.413427289020815; +% 0.217732068357300; +% 0.125654587362626; +% 0.308914593566815; +% 0.726104431664832; +% 0.782872072979123; +% 0.693787614986897; +% 0.009802252263062; +% 0.843213338010510; +% 0.922331997796276; +% 0.770954220673925; +% 0.042659855935049; +% 0.378186137050219; +% 0.704339624483368]; + +print_matrix_for_reference(y, 'y'); + +% training +[nlZ, dnlZ] = gp(hyp, inf_method, mean_func, cov_func, lik_func, x, y) + +% prediction - regression +[dummy, dummy, fmu, fs2] = gp(hyp, inf_method, mean_func, cov_func, lik_func, x, y, z); +print_matrix_for_reference(fmu, 'fmu'); +print_matrix_for_reference(fs2, 'fs2'); + +% prediction - classification +%[dummy, dummy, fmu, fs2, lp] = gp(hyp, inf_func, mean_func, cov_func, lik_func, x, y, xs, ys); \ No newline at end of file diff --git a/mean/meanZeroDerObs.m b/mean/meanZeroDerObs.m new file mode 100644 index 0000000..5f0f554 --- /dev/null +++ b/mean/meanZeroDerObs.m @@ -0,0 +1,16 @@ +function A = meanZeroDerObs(hyp, x, i) + +% Zero mean function. The mean function does not have any parameters. +% +% m(x) = 0 + +if nargin<2, A = '0'; return; end % report number of hyperparameters + +% number of function and derivative training data +nd = sum(x(:, 1) ~= 0); % derivative training data +n = size(x, 1) - nd; % function derivative training data +d = size(x, 2) - 1; % number of dimensions +nn = n + nd*d; + +% value +A = zeros(nn, 1); % derivative and mean diff --git a/test/test_all_cov.m b/test/test_all_cov.m index e5a59f2..61e263c 100644 --- a/test/test_all_cov.m +++ b/test/test_all_cov.m @@ -7,15 +7,15 @@ addpath('../cov/covSparseiso'); -addpath('../cov/covisoUnstable'); -addpath('../cov/covisoUnstable/covSEisoDiffUnstable'); -addpath('../cov/covisoUnstable/covMaterniso3DiffUnstable'); -addpath('../cov/covisoUnstable/covSparseisoDiffUnstable'); +addpath('../cov/covisoDerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covSEisoDerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covMaterniso3DerObsUnstable'); +addpath('../cov/covisoDerObsUnstable/covSparseisoDerObsUnstable'); -addpath('../cov/covDiff'); -addpath('../cov/covDiff/covSEisoDiff'); -addpath('../cov/covDiff/covMaterniso3Diff'); -addpath('../cov/covDiff/covSparseisoDiff'); +addpath('../cov/covDerObs'); +addpath('../cov/covDerObs/covSEisoDerObs'); +addpath('../cov/covDerObs/covMaterniso3DerObs'); +addpath('../cov/covDerObs/covSparseisoDerObs'); %% setting % hyperparameters @@ -34,23 +34,27 @@ % xd = scale*rand(nd, d); % z = scale*rand(ns, d); % -% x = [0.0423 0.6751 0.0839 -% 0.9730 0.3610 0.9748 -% 0.1892 0.6203 0.6513 -% 0.6671 0.8112 0.2312 -% 0.5864 0.0193 0.4035]; -% xd = [0.1220 0.1522 0.0943 -% 0.2684 0.3480 0.9300 -% 0.2578 0.1217 0.3990 -% 0.3317 0.8842 0.0474]; -% z = [0.3424 0.5449 0.0548 -% 0.7360 0.6862 0.3037 -% 0.7947 0.8936 0.0462]; +x = [0.0423 0.6751 0.0839 + 0.9730 0.3610 0.9748 + 0.1892 0.6203 0.6513 + 0.6671 0.8112 0.2312 + 0.5864 0.0193 0.4035]; +xd = [0.1220 0.1522 0.0943 + 0.2684 0.3480 0.9300 + 0.2578 0.1217 0.3990 + 0.3317 0.8842 0.0474]; +z = [0.3424 0.5449 0.0548 + 0.7360 0.6862 0.3037 + 0.7947 0.8936 0.0462]; -x = scale*rand(n-1, d); x = [x; x(end, :)]; % duplication -xd = scale*rand(nd-2, d); xd = [xd; x(end, :); xd(end, :)]; % duplication -z = scale*rand(ns-2, d); z = [z; x(end, :); xd(end, :)]; % duplication +% x = scale*rand(n-1, d); x = [x; x(end, :)]; % duplication +% xd = scale*rand(nd-2, d); xd = [xd; x(end, :); xd(end, :)]; % duplication +% z = scale*rand(ns-2, d); z = [z; x(end, :); xd(end, :)]; % duplication +xx = [zeros(n, 1), x]; +xxd = [zeros(n, 1), x + ones(nd, 1), xd]; + % sigma_n % sigma_n = 0; sigma_n = 0.0000001; @@ -61,10 +65,10 @@ % testcase_cov({@covMaterniso, 3}, 'covMaterniso3', hyp, x, sigma_n); % testcase_cov({@covSparseiso}, 'covSparseiso', hyp, x, sigma_n); -% testcase_covDiffUnstable(@covSEisoDiffUnstable, {@covSEiso}, 'covSEiso', hyp, x, xd, z, sigma_n); -% testcase_covDiffUnstable(@covMaterniso3DiffUnstable, {@covMaterniso, 3}, 'covMaterniso3', hyp, x, xd, z, sigma_n); -% testcase_covDiffUnstable(@covSparseisoDiffUnstable, {@covSparseiso}, 'covSparseiso', hyp, x, xd, z, sigma_n); +% testcase_covisoDerObsUnstable(@covSEisoDerObsUnstable, {@covSEiso}, 'covSEiso', hyp, x, z, xx, xxd, sigma_n); +% testcase_covisoDerObsUnstable(@covMaterniso3DerObsUnstable, {@covMaterniso, 3}, 'covMaterniso3', hyp, x, z, xx, xxd, sigma_n); +% testcase_covisoDerObsUnstable(@covSparseisoDerObsUnstable, {@covSparseiso}, 'covSparseiso', hyp, x, z, xx, xxd, sigma_n); -% testcase_covDiff(@covSEisoDiff, @covSEisoDiffUnstable, {@covSEiso}, 'covSEiso', hyp, x, xd, z, sigma_n); -% testcase_covDiff(@covMaterniso3Diff, @covMaterniso3DiffUnstable, {@covMaterniso, 3}, 'covMaterniso3', hyp, x, xd, z, sigma_n); -testcase_covDiff(@covSparseisoDiff, @covSparseisoDiffUnstable, {@covSparseiso}, 'covSparseiso', hyp, x, xd, z, sigma_n); +testcase_covDerObs(@covSEisoDerObs, @covSEisoDerObsUnstable, {@covSEiso}, 'covSEiso', hyp, x, z, xx, xxd, sigma_n); +testcase_covDerObs(@covMaterniso3DerObs, @covMaterniso3DerObsUnstable, {@covMaterniso, 3}, 'covMaterniso3', hyp, x, z, xx, xxd, sigma_n); +testcase_covDerObs(@covSparseisoDerObs, @covSparseisoDerObsUnstable, {@covSparseiso}, 'covSparseiso', hyp, x, z, xx, xxd, sigma_n); diff --git a/test/testcase_covDerObs.m b/test/testcase_covDerObs.m new file mode 100644 index 0000000..aa728b3 --- /dev/null +++ b/test/testcase_covDerObs.m @@ -0,0 +1,114 @@ +function testcase_covDerObs(f_covDerObs, f_covisoDerObsUnstable, f_cov, str_cov, hyp, x, z, xx, xxd, sigma_n) + +% f_covDerObs: function handle for the covariance function with derivative observations (stable version) +% f_covisoDerObsUnstable: function handle for the covariance function with derivative observations (unstable version) +% f_cov: function handle for the covariance function +% str_cov: covariance function name +% hyp: hyperparameters +% x: first function inputs +% xd: first derivative inputs +% z: second function inputs +% sigma_n: output noise variance + +%% Setting +testcase_name = ['[TEST_CASE: ', str_cov, 'DerObs]']; +disp(testcase_name); + + +%% Test: cov*DerObs vs. cov* +test_name = ['\t[TESE: ', str_cov, 'DerObs vs. ', str_cov, ']\n']; +fprintf(1, test_name); + +% K = K(x, x) +TEST_EQ(feval(f_covDerObs, hyp, xx, []), ... + feval(f_cov{:}, hyp, x), ... + 'K = K(x, x)'); + +% K_i = dK(x, x)/dtheta_i +for i = 1:length(hyp) + TEST_EQ(feval(f_covDerObs, hyp, xx, [], i), ... + feval(f_cov{:}, hyp, x, [], i), ... + ['K_', num2str(i), ' = dK(x, x)/dtheta_', num2str(i)]); +end + +% Ks = K(x, z) +TEST_EQ(feval(f_covDerObs, hyp, xx, z), ... + feval(f_cov{:}, hyp, x, z), ... + 'Ks = K(x, z)'); + +% Kss = K(z, z) +TEST_EQ(feval(f_covDerObs, hyp, z, 'diag'), ... + feval(f_cov{:}, hyp, z, 'diag'), ... + 'Kss = K(z, z)'); + + +%% Test: cov*DerObs vs. cov*DerObsBwiseUnstable +test_name = ['\t[TESE: ', str_cov, 'DerObs vs. ', str_cov, 'DerObsBwiseUnstable]\n']; +fprintf(1, test_name); + +f_Bwise = true; + +% K = K(x, x) +TEST_EQ(feval(f_covDerObs, hyp, xxd, [], 0), ... + feval(f_covisoDerObsUnstable, hyp, xxd, [], 0, f_Bwise), ... + 'K = K(x, x)'); + +% K_i = dK(x, x)/dtheta_i +for i = 1:length(hyp) + TEST_EQ(feval(f_covDerObs, hyp, xxd, [], i), ... + feval(f_covisoDerObsUnstable, hyp, xxd, [], i, f_Bwise), ... + ['K_', num2str(i), ' = dK(x, x)/dtheta_', num2str(i)]); +end + +% Ks = K(x, z) +TEST_EQ(feval(f_covDerObs, hyp, xxd, z, 0), ... + feval(f_covisoDerObsUnstable, hyp, xxd, z, 0, f_Bwise), ... + 'Ks = K(x, z)'); + +% Kss = K(z, z) +TEST_EQ(feval(f_covDerObs, hyp, z, 'diag', 0), ... + feval(f_covisoDerObsUnstable, hyp, z, 'diag', 0, f_Bwise), ... + 'Kss = K(z, z)'); + + +%% Test: cov*DerObs - symmetric +test_name = ['\t[TESE: ', str_cov, 'DerObs - symmetric]\n']; +fprintf(1, test_name); + +% K = K(x, x) +K = feval(f_covDerObs, hyp, xxd, [], 0); +TEST_EQ(K, ... + K', ... + 'K = K(x, x)'); + +% K_i = dK(x, x)/dtheta_i +for i = 1:length(hyp) + K = feval(f_covDerObs, hyp, xxd, [], i); + TEST_EQ(K, ... + K', ... + ['K_', num2str(i), ' = dK(x, x)/dtheta_', num2str(i)]); +end + +%% Test: cov*DerObs - positive definite +test_name = ['\t[TESE: ', str_cov, 'DerObs - positive definite]\n']; +fprintf(1, test_name); + +% K = K(x, x) +K = feval(f_covDerObs, hyp, xxd, [], 0); +K = K + sigma_n^2*eye(size(K)); +[R, p] = chol(K); +TEST_EQ(p, ... + 0', ... + 'K = K(x, x)'); + +% % K_i = dK(x, x)/dtheta_i +% for i = 1:length(hyp) +% K = feval(f_covDerObs, hyp, xxd, [], i); +% K = K + sigma_n^2*eye(size(K)); +% [R, p] = chol(K); +% TEST_EQ(p, ... +% 0', ... +% ['K_i = dK(x, x)/dtheta_', num2str(i)]); +% end + +disp(' '); \ No newline at end of file diff --git a/test/testcase_covDiff.m b/test/testcase_covDiff.m deleted file mode 100644 index e8a6cf9..0000000 --- a/test/testcase_covDiff.m +++ /dev/null @@ -1,114 +0,0 @@ -function testcase_covDiff(f_covDiff, f_covDiffUnstable, f_cov, str_cov, hyp, x, xd, z, sigma_n) - -% f_covDiff: function handle for the covariance function with derivative observations (stable version) -% f_covDiffUnstable: function handle for the covariance function with derivative observations (unstable version) -% f_cov: function handle for the covariance function -% str_cov: covariance function name -% hyp: hyperparameters -% x: first function inputs -% xd: first derivative inputs -% z: second function inputs -% sigma_n: output noise variance - -%% Setting -testcase_name = ['[TEST_CASE: ', str_cov, 'Diff]']; -disp(testcase_name); - - -%% Test: cov*Diff vs. cov* -test_name = ['\t[TESE: ', str_cov, 'Diff vs. ', str_cov, ']\n']; -fprintf(1, test_name); - -% K = K(x, x) -TEST_EQ(feval(f_covDiff, hyp, x, [], 0, []), ... - feval(f_cov{:}, hyp, x), ... - 'K = K(x, x)'); - -% K_i = dK(x, x)/dtheta_i -for i = 1:length(hyp) - TEST_EQ(feval(f_covDiff, hyp, x, [], i, []), ... - feval(f_cov{:}, hyp, x, [], i), ... - ['K_i = dK(x, x)/dtheta_', num2str(i)]); -end - -% Ks = K(x, z) -TEST_EQ(feval(f_covDiff, hyp, x, z, 0, []), ... - feval(f_cov{:}, hyp, x, z), ... - 'Ks = K(x, z)'); - -% Kss = K(z, z) -TEST_EQ(feval(f_covDiff, hyp, z, 'diag', 0, []), ... - feval(f_cov{:}, hyp, z, 'diag'), ... - 'Kss = K(z, z)'); - - -%% Test: cov*Diff vs. cov*DiffBwiseUnstable -test_name = ['\t[TESE: ', str_cov, 'Diff vs. ', str_cov, 'DiffBwiseUnstable]\n']; -fprintf(1, test_name); - -f_Bwise = true; - -% K = K(x, x) -TEST_EQ(feval(f_covDiff, hyp, x, [], 0, xd), ... - feval(f_covDiffUnstable, hyp, x, [], 0, xd, f_Bwise), ... - 'K = K(x, x)'); - -% K_i = dK(x, x)/dtheta_i -for i = 1:length(hyp) - TEST_EQ(feval(f_covDiff, hyp, x, [], i, xd), ... - feval(f_covDiffUnstable, hyp, x, [], i, xd, f_Bwise), ... - ['K_i = dK(x, x)/dtheta_', num2str(i)]); -end - -% Ks = K(x, z) -TEST_EQ(feval(f_covDiff, hyp, x, z, 0, xd), ... - feval(f_covDiffUnstable, hyp, x, z, 0, xd, f_Bwise), ... - 'Ks = K(x, z)'); - -% Kss = K(z, z) -TEST_EQ(feval(f_covDiff, hyp, z, 'diag', 0, []), ... - feval(f_covDiffUnstable, hyp, z, 'diag', 0, [], f_Bwise), ... - 'Kss = K(z, z)'); - - -%% Test: cov*Diff - symmetric -test_name = ['\t[TESE: ', str_cov, 'Diff - symmetric]\n']; -fprintf(1, test_name); - -% K = K(x, x) -K = feval(f_covDiff, hyp, x, [], 0, xd); -TEST_EQ(K, ... - K', ... - 'K = K(x, x)'); - -% K_i = dK(x, x)/dtheta_i -for i = 1:length(hyp) - K = feval(f_covDiff, hyp, x, [], i, xd); - TEST_EQ(K, ... - K', ... - ['K_i = dK(x, x)/dtheta_', num2str(i)]); -end - -%% Test: cov*Diff - positive definite -test_name = ['\t[TESE: ', str_cov, 'Diff - positive definite]\n']; -fprintf(1, test_name); - -% K = K(x, x) -K = feval(f_covDiff, hyp, x, [], 0, xd); -K = K + sigma_n^2*eye(size(K)); -[R, p] = chol(K); -TEST_EQ(p, ... - 0', ... - 'K = K(x, x)'); - -% % K_i = dK(x, x)/dtheta_i -% for i = 1:length(hyp) -% K = feval(f_covDiff, hyp, x, [], i, xd); -% K = K + sigma_n^2*eye(size(K)); -% [R, p] = chol(K); -% TEST_EQ(p, ... -% 0', ... -% ['K_i = dK(x, x)/dtheta_', num2str(i)]); -% end - -disp(' '); \ No newline at end of file diff --git a/test/testcase_covDiffUnstable.m b/test/testcase_covDiffUnstable.m deleted file mode 100644 index 738d23d..0000000 --- a/test/testcase_covDiffUnstable.m +++ /dev/null @@ -1,141 +0,0 @@ -function testcase_covDiffUnstable(f_covDiffUnstable, f_cov, str_cov, hyp, x, xd, z, sigma_n) - -% f_covDiffUnstable: function handle for the covariance function with derivative observations (unstable version) -% f_cov: function handle for the covariance function -% str_cov: covariance function name -% hyp: hyperparameters -% x: first function inputs -% xd: first derivative inputs -% z: second function inputs -% sigma_n: output noise variance - -%% Setting -testcase_name = ['[TEST_CASE: ', str_cov, 'DiffUnstable]']; -disp(testcase_name); - -f_Cwise = false; -f_Bwise = true; - - -%% Test: cov*DiffCwiseUnstable vs. cov* -test_name = ['\t[TESE: ', str_cov, 'DiffCwiseUnstable vs. ', str_cov, ']\n']; -fprintf(1, test_name); - -% K = K(x, x) -TEST_EQ(feval(f_covDiffUnstable, hyp, x, [], 0, [], f_Cwise), ... - feval(f_cov{:}, hyp, x), ... - 'K = K(x, x)'); - -% K_i = dK(x, x)/dtheta_i -for i = 1:length(hyp) - TEST_EQ(feval(f_covDiffUnstable, hyp, x, [], i, [], f_Cwise), ... - feval(f_cov{:}, hyp, x, [], i), ... - ['K_i = dK(x, x)/dtheta_', num2str(i)]); -end - -% Ks = K(x, z) -TEST_EQ(feval(f_covDiffUnstable, hyp, x, z, 0, [], f_Cwise), ... - feval(f_cov{:}, hyp, x, z), ... - 'Ks = K(x, z)'); - -% Kss = K(z, z) -TEST_EQ(feval(f_covDiffUnstable, hyp, z, 'diag', 0, [], f_Cwise), ... - feval(f_cov{:}, hyp, z, 'diag'), ... - 'Kss = K(z, z)'); - - -%% Test: cov*DiffBwiseUnstable vs. cov* -test_name = ['\t[TESE: ', str_cov, 'DiffBwiseUnstable vs. ', str_cov, ']\n']; -fprintf(1, test_name); - -% K = K(x, x) -TEST_EQ(feval(f_covDiffUnstable, hyp, x, [], 0, [], f_Bwise), ... - feval(f_cov{:}, hyp, x), ... - 'K = K(x, x)'); - -% K_i = dK(x, x)/dtheta_i -for i = 1:length(hyp) - TEST_EQ(feval(f_covDiffUnstable, hyp, x, [], i, [], f_Bwise), ... - feval(f_cov{:}, hyp, x, [], i), ... - ['K_i = dK(x, x)/dtheta_', num2str(i)]); -end - -% Ks = K(x, z) -TEST_EQ(feval(f_covDiffUnstable, hyp, x, z, 0, [], f_Bwise), ... - feval(f_cov{:}, hyp, x, z), ... - 'Ks = K(x, z)'); - -% Kss = K(z, z) -TEST_EQ(feval(f_covDiffUnstable, hyp, z, 'diag', 0, [], f_Bwise), ... - feval(f_cov{:}, hyp, z, 'diag'), ... - 'Kss = K(z, z)'); - - -%% Test: cov*DiffCwiseUnstable vs. cov*DiffBwiseUnstable -test_name = ['\t[TESE: ', str_cov, 'DiffCwiseUnstable vs. ', str_cov, 'DiffBwiseUnstable]\n']; -fprintf(1, test_name); - -% K = K(x, x) -TEST_EQ(feval(f_covDiffUnstable, hyp, x, [], 0, xd, f_Cwise), ... - feval(f_covDiffUnstable, hyp, x, [], 0, xd, f_Bwise), ... - 'K = K(x, x)'); - -% K_i = dK(x, x)/dtheta_i -for i = 1:length(hyp) - TEST_EQ(feval(f_covDiffUnstable, hyp, x, [], i, xd, f_Cwise), ... - feval(f_covDiffUnstable, hyp, x, [], i, xd, f_Bwise), ... - ['K_i = dK(x, x)/dtheta_', num2str(i)]); -end - -% Ks = K(x, z) -TEST_EQ(feval(f_covDiffUnstable, hyp, x, z, 0, xd, f_Cwise), ... - feval(f_covDiffUnstable, hyp, x, z, 0, xd, f_Bwise), ... - 'Ks = K(x, z)'); - -% Kss = K(z, z) -TEST_EQ(feval(f_covDiffUnstable, hyp, z, 'diag', 0, [], f_Cwise), ... - feval(f_covDiffUnstable, hyp, z, 'diag', 0, [], f_Bwise), ... - 'Kss = K(z, z)'); - - -%% Test: cov*DiffBwiseUnstable - symmetric -test_name = ['\t[TESE: ', str_cov, 'DiffBwiseUnstable - symmetric]\n']; -fprintf(1, test_name); - -% K = K(x, x) -K = feval(f_covDiffUnstable, hyp, x, [], 0, xd, f_Bwise); -TEST_EQ(K, ... - K', ... - 'K = K(x, x)'); - -% K_i = dK(x, x)/dtheta_i -for i = 1:length(hyp) - K = feval(f_covDiffUnstable, hyp, x, [], i, xd, f_Bwise); - TEST_EQ(K, ... - K', ... - ['K_i = dK(x, x)/dtheta_', num2str(i)]); -end - -%% Test: cov*DiffBwiseUnstable - positive definite -test_name = ['\t[TESE: ', str_cov, 'DiffBwiseUnstable - positive definite]\n']; -fprintf(1, test_name); - -% K = K(x, x) -K = feval(f_covDiffUnstable, hyp, x, [], 0, xd, f_Bwise); -K = K + sigma_n^2*eye(size(K)); -[R, p] = chol(K); -TEST_EQ(p, ... - 0', ... - 'K = K(x, x)'); - -% % K_i = dK(x, x)/dtheta_i -% for i = 1:length(hyp) -% K = feval(f_covDiffUnstable, hyp, x, [], i, xd, f_Bwise); -% K = K + sigma_n^2*eye(size(K)); -% [R, p] = chol(K); -% TEST_EQ(p, ... -% 0', ... -% ['K_i = dK(x, x)/dtheta_', num2str(i)]); -% end - -disp(' '); \ No newline at end of file diff --git a/test/testcase_covisoDerObsUnstable.m b/test/testcase_covisoDerObsUnstable.m new file mode 100644 index 0000000..f5677c6 --- /dev/null +++ b/test/testcase_covisoDerObsUnstable.m @@ -0,0 +1,141 @@ +function testcase_covisoDerObsUnstable(f_covisoDerObsUnstable, f_cov, str_cov, hyp, x, z, xx, xxd, sigma_n) + +% f_covisoDerObsUnstable: function handle for the covariance function with derivative observations (unstable version) +% f_cov: function handle for the covariance function +% str_cov: covariance function name +% hyp: hyperparameters +% x: first function inputs +% xd: first derivative inputs +% z: second function inputs +% sigma_n: output noise variance + +%% Setting +testcase_name = ['[TEST_CASE: ', str_cov, 'DerObsUnstable]']; +disp(testcase_name); + +f_Cwise = false; +f_Bwise = true; + + +%% Test: cov*DerObsCwiseUnstable vs. cov* +test_name = ['\t[TESE: ', str_cov, 'DerObsCwiseUnstable vs. ', str_cov, ']\n']; +fprintf(1, test_name); + +% K = K(x, x) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xx, [], 0, f_Cwise), ... + feval(f_cov{:}, hyp, x), ... + 'K = K(x, x)'); + +% K_i = dK(x, x)/dtheta_i +for i = 1:length(hyp) + TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xx, [], i, f_Cwise), ... + feval(f_cov{:}, hyp, x, [], i), ... + ['K_', num2str(i), ' = dK(x, x)/dtheta_', num2str(i)]); +end + +% Ks = K(x, z) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xx, z, 0, f_Cwise), ... + feval(f_cov{:}, hyp, x, z), ... + 'Ks = K(x, z)'); + +% Kss = K(z, z) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, z, 'diag', 0, f_Cwise), ... + feval(f_cov{:}, hyp, z, 'diag'), ... + 'Kss = K(z, z)'); + + +%% Test: cov*DerObsBwiseUnstable vs. cov* +test_name = ['\t[TESE: ', str_cov, 'DerObsBwiseUnstable vs. ', str_cov, ']\n']; +fprintf(1, test_name); + +% K = K(x, x) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xx, [], 0, f_Bwise), ... + feval(f_cov{:}, hyp, x), ... + 'K = K(x, x)'); + +% K_i = dK(x, x)/dtheta_i +for i = 1:length(hyp) + TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xx, [], i, f_Bwise), ... + feval(f_cov{:}, hyp, x, [], i), ... + ['K_', num2str(i), ' = dK(x, x)/dtheta_', num2str(i)]); +end + +% Ks = K(x, z) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xx, z, 0, f_Bwise), ... + feval(f_cov{:}, hyp, x, z), ... + 'Ks = K(x, z)'); + +% Kss = K(z, z) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, z, 'diag', 0, f_Bwise), ... + feval(f_cov{:}, hyp, z, 'diag'), ... + 'Kss = K(z, z)'); + + +%% Test: cov*DerObsCwiseUnstable vs. cov*DerObsBwiseUnstable +test_name = ['\t[TESE: ', str_cov, 'DerObsCwiseUnstable vs. ', str_cov, 'DerObsBwiseUnstable]\n']; +fprintf(1, test_name); + +% K = K(x, x) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xxd, [], 0, f_Cwise), ... + feval(f_covisoDerObsUnstable, hyp, xxd, [], 0, f_Bwise), ... + 'K = K(x, x)'); + +% K_i = dK(x, x)/dtheta_i +for i = 1:length(hyp) + TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xxd, [], i, f_Cwise), ... + feval(f_covisoDerObsUnstable, hyp, xxd, [], i, f_Bwise), ... + ['K_', num2str(i), ' = dK(x, x)/dtheta_', num2str(i)]); +end + +% Ks = K(x, z) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, xxd, z, 0, f_Cwise), ... + feval(f_covisoDerObsUnstable, hyp, xxd, z, 0, f_Bwise), ... + 'Ks = K(x, z)'); + +% Kss = K(z, z) +TEST_EQ(feval(f_covisoDerObsUnstable, hyp, z, 'diag', 0, f_Cwise), ... + feval(f_covisoDerObsUnstable, hyp, z, 'diag', 0, f_Bwise), ... + 'Kss = K(z, z)'); + + +%% Test: cov*DerObsBwiseUnstable - symmetric +test_name = ['\t[TESE: ', str_cov, 'DerObsBwiseUnstable - symmetric]\n']; +fprintf(1, test_name); + +% K = K(x, x) +K = feval(f_covisoDerObsUnstable, hyp, xxd, [], 0, f_Bwise); +TEST_EQ(K, ... + K', ... + 'K = K(x, x)'); + +% K_i = dK(x, x)/dtheta_i +for i = 1:length(hyp) + K = feval(f_covisoDerObsUnstable, hyp, xxd, [], i, f_Bwise); + TEST_EQ(K, ... + K', ... + ['K_', num2str(i), ' = dK(x, x)/dtheta_', num2str(i)]); +end + +%% Test: cov*DerObsBwiseUnstable - positive definite +test_name = ['\t[TESE: ', str_cov, 'DerObsBwiseUnstable - positive definite]\n']; +fprintf(1, test_name); + +% K = K(x, x) +K = feval(f_covisoDerObsUnstable, hyp, xxd, [], 0, f_Bwise); +K = K + sigma_n^2*eye(size(K)); +[R, p] = chol(K); +TEST_EQ(p, ... + 0', ... + 'K = K(x, x)'); + +% % K_i = dK(x, x)/dtheta_i +% for i = 1:length(hyp) +% K = feval(f_covisoDerObsUnstable, hyp, xxd, [], i, f_Bwise); +% K = K + sigma_n^2*eye(size(K)); +% [R, p] = chol(K); +% TEST_EQ(p, ... +% 0', ... +% ['K_i = dK(x, x)/dtheta_', num2str(i)]); +% end + +disp(' '); \ No newline at end of file