Skip to content

Commit

Permalink
creating a test for SGMK integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Tamellini committed Nov 30, 2023
1 parent e2bac6e commit f3d83f3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
48 changes: 48 additions & 0 deletions clients/matlab/check_sgmk.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function check_sgmk()

% An aux function to check and download the sparse grids matlab kit

if exist('create_sparse_grid', 'file')==0
% the sgmk is not found in the current path, so either we don't have it or we are not in the right subfolder

if exist('sparse-grids-matlab-kit', 'dir')==0
% there is no subfolder -> we don't have sgmk. Need to download it (if we haven't yet) and unzip

if exist('sgmk.zip', 'file')==0
% zip not found, let's download it

try
disp('Sparse Grids Matlab Kit not found, downloading from github (https://github.com/lorenzo-tamellini/sparse-grids-matlab-kit/)...');
opts = weboptions;
opts.CertificateFilename=(''); % No idea why this is needed
opts.Timeout = 100; % a larger timeout (in ms, default 5), just in case internet is slow today
websave('sgmk.zip', 'https://github.com/lorenzo-tamellini/sparse-grids-matlab-kit/archive/refs/heads/main.zip', opts);
disp('... done');
catch ME
fprintf('\nAutomatic download from github failed. Try manual download, from either github or project website https://sites.google.com/view/sparse-grids-kit. Download the latest version in .zip and name it sgmk.zip \n\n')
error(ME.identifier,ME.message);
end
end

% now we have the zip, unzip
try
disp('unzipping ...');
unzip('sgmk.zip','sparse-grids-matlab-kit');
disp('done');
catch ME
error('%s. Automatic unzipping failed. Please extract sgmk.zip here', ME.message);
end
end

% ready to add to path. Mind the double folder
cd('sparse-grids-matlab-kit/sparse-grids-matlab-kit-main');
addpath(genpath(pwd))
disp('added Sparse Grids Matlab Kit to path')
cd('../..');

else
% the sgmk is found in the current path, we're good to go

disp('Sparse Grids Matlab Kit is already in Matlab''s path')
end

42 changes: 42 additions & 0 deletions clients/matlab/sgmkClient.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
clear


% Analytic-Banana benchmark. Use the sparse grids matlab kit as a high-dimensional quadrature tool to compute the
% integral of the posterior density defined in the benchmark. The problem is a bit challenging so even a poor result is
% ok, this is just for testing the client. The pdfs in the benchmark are not normalized so the integral should be
% around 3

% add the Sparse Grids Matlab Kit and umbridge to your path
check_sgmk()

% uri of the service running the server
uri = 'http://localhost:4243';

% HTTPModel is an object provided by the UM-Bridge Matlab client.
% model = HTTPModel(uri, 'posterior');
model = HTTPModel(uri, 'posterior','webwrite');

% model.evaluate(y) sends a request to the server to evaluate the model at y. Wrap it in an @-function:
f = @(y) model.evaluate(y);

% define the sparse grid. Here we create it a basic one, but it can be as sophisticated as the user wants
N=2;
w=7;
% it is convenient to define the quadrature domain by a matrix D. IF the domain is [a b] x [c d], take each interval
% as a column, so that
% D = [a c ;
% b d ];
domain = [-5 -5;
5 5];
knots={@(n) knots_CC(n,domain(1,1),domain(2,1),'nonprob'), @(n) knots_CC(n,domain(1,2),domain(2,2),'nonprob')};
S=create_sparse_grid(N,w,knots,@lev2knots_doubling);
Sr=reduce_sparse_grid(S);
f_evals=evaluate_on_sparse_grid(f,Sr);

% from here on, do whatever UQ analysis you want with the values contained in f_evals
figure
plot_sparse_grids_interpolant(S,Sr,domain,exp(f_evals),'nb_plot_pts',80)
figure
plot_sparse_grids_interpolant(S,Sr,domain,f_evals,'with_f_values')

Ev = quadrature_on_sparse_grid(exp(f_evals),Sr)

0 comments on commit f3d83f3

Please sign in to comment.