diff --git a/clients/README.md b/clients/README.md index 74e40fd..62c2600 100644 --- a/clients/README.md +++ b/clients/README.md @@ -519,3 +519,46 @@ The remaining arguments are the stopping tolerance (`1e-5`) and a vectorization We can notice the total number of model evaluations as `cum#evals=449` in the printout of `greedy2_cross`, and the Tensor-Train rank `max_rank=3`. A neat reduction compared to 1089 points in the full 2D grid! [Full example sources here.](https://github.com/UM-Bridge/umbridge/blob/main/clients/matlab/ttClient.m) + + +## Sparse Grids Matlab Kit client + +The Sparse Grids Matlab Kit (SGMK) provides a Matlab implementation of sparse grids, and can be used for approximating high-dimensional functions and, in particular, for surrogate-model-based uncertainty quantification; for more info, see the [SGMK website](https://sites.google.com/view/sparse-grids-kit). + +The SGMK integrates in a very straightforward way with the Matlab UM-Bridge client (it literally takes 1 line!), see e.g. the script [sgmkClient.m](https://github.com/UM-Bridge/umbridge/blob/main/clients/matlab/sgmkClient.m) that can be found in the folder [clients/matlab](https://github.com/UM-Bridge/umbridge/blob/main/clients/matlab). + +The script begins by checking whether the SGMK is already in our path, and if not downloads it from the Github repo [here](https://github.com/lorenzo-tamellini/sparse-grids-matlab-kit) and adds it to the path: +```matlab +check_sgmk() +``` +The goal of this simple script is to use the SGMK as a high-dimensional quadrature tool to compute the integral of the posterior density function (pdf) defined in the benchmark **analytic-gaussian-mixture**. The pdf in the benchmark is actually not normalized so the integral should be around 3. + +To this end, create a model as before: +```matlab +uri = 'http://localhost:4243'; +model = HTTPModel(uri, 'posterior','webwrite'); +``` +then simply wrap `model.evaluate()` in an `@-function` and **you're done**! +```matlab +f = @(y) model.evaluate(y); +``` +The script then goes on creating a sparse grid and evaluating `f` over the sparse grid points: +```matlab +N=2; +w=7; +domain = [-5.5 -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); +``` +and finally, computing the integral given the values of `f` just obtained. Note that the values returned by the container and stored in `f_evals` are actually the log-posterior, so we need to take their exponent before computing the integral: +```matlab +Ev = quadrature_on_sparse_grid(exp(f_evals),Sr) +``` +which indeed returns `Ev = 2.9948`, i.e., close to 3 as expected. The script then ends by plotting the sparse grids interpolant of `f` and of `exp(f)`. + +[Full example sources here.](https://github.com/UM-Bridge/umbridge/blob/main/clients/matlab/sgmkClient.m) + + diff --git a/clients/matlab/sgmkClient.m b/clients/matlab/sgmkClient.m index 301e89f..6debb77 100644 --- a/clients/matlab/sgmkClient.m +++ b/clients/matlab/sgmkClient.m @@ -1,20 +1,27 @@ 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 +% Analytic-gaussian-mixture. Use the sparse grids matlab kit as a high-dimensional quadrature tool to compute the +% integral of the posterior density function (pdf) 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 is not normalized so the integral should be % around 3 -% add the Sparse Grids Matlab Kit and umbridge to your path +% add the Sparse Grids Matlab Kit to your path check_sgmk() +% run these commands too if you haven't umbridge in your path +% currdir = pwd; +% cd ../../matlab/ +% addpath(genpath(pwd)) +% cd(currdir) + +% also, start the Analytic-gaussian-mixture container +% sudo docker run -it -p 4243:4243 linusseelinger/benchmark-analytic-gaussian-mixture % 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 = HTTPModel(uri, 'posterior','webwrite'); % let's use webwrite, it's much faster % 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); @@ -26,17 +33,23 @@ % 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 +domain = [-5.5 -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. Here we just check that +% the pdf integrates to 3 (the benchmark is not normalized). Note that the values returned by the container +% and stored in f_evals are actually the log-posterior, so we need to take their exponent before computing the integral + +Ev = quadrature_on_sparse_grid(exp(f_evals),Sr) + + +% We also plot the sparse grids interpolant of the function in the benchmark 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