From f3d83f3ee15f11271c35495d318bcb7daf2fd142 Mon Sep 17 00:00:00 2001 From: Lorenzo Tamellini Date: Thu, 30 Nov 2023 15:02:31 +0100 Subject: [PATCH] creating a test for SGMK integration --- clients/matlab/check_sgmk.m | 48 +++++++++++++++++++++++++++++++++++++ clients/matlab/sgmkClient.m | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 clients/matlab/check_sgmk.m create mode 100644 clients/matlab/sgmkClient.m diff --git a/clients/matlab/check_sgmk.m b/clients/matlab/check_sgmk.m new file mode 100644 index 0000000..627425a --- /dev/null +++ b/clients/matlab/check_sgmk.m @@ -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 + diff --git a/clients/matlab/sgmkClient.m b/clients/matlab/sgmkClient.m new file mode 100644 index 0000000..301e89f --- /dev/null +++ b/clients/matlab/sgmkClient.m @@ -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) \ No newline at end of file