Skip to content

Commit d6d1368

Browse files
committed
Add code to compute beta vectors for MLBLUE
1 parent 329630b commit d6d1368

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

pyapprox/multifidelity/multilevelblue.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ def BLUE_Psi(Sigma, costs, reg_blue, nsamples_per_subset):
9898
return mat, submats
9999

100100

101+
def BLUE_betas(Sigma, asketch, reg_blue, nsamples_per_subset):
102+
nmodels = Sigma.shape[0]
103+
subsets = get_model_subsets(nmodels)
104+
Psi = BLUE_Psi(Sigma, None, reg_blue, nsamples_per_subset)[0]
105+
Psi_inv = np.linalg.inv(Psi)
106+
betas = np.empty((len(subsets), nmodels))
107+
for ii, subset in enumerate(subsets):
108+
Sigma_inv = np.linalg.inv(Sigma[np.ix_(subset, subset)])
109+
R = _restriction_matrix(nmodels, subset)
110+
betas[ii] = np.linalg.multi_dot(
111+
(R.T, Sigma_inv, R, Psi_inv, asketch))[:, 0]*nsamples_per_subset[ii]
112+
return betas
113+
114+
101115
def BLUE_RHS(Sigma, values):
102116
"""
103117
Parameters

pyapprox/multifidelity/tests/test_control_variate_monte_carlo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from pyapprox.benchmarks.multifidelity_benchmarks import (
4545
ShortColumnModelEnsemble, TunableModelEnsemble, PolynomialModelEnsemble
4646
)
47+
from pyapprox.multifidelity.multilevelblue import BLUE_betas, BLUE_variance
4748

4849

4950
skiptest = unittest.skipIf(
@@ -911,6 +912,20 @@ def test_MLBLUE(self):
911912
estimator.nsamples_per_subset).astype(int)
912913
# rounded_target_cost = np.sum(
913914
# estimator.nsamples_per_subset*subset_costs)
915+
916+
# test equivalent formulations for variance
917+
betas = BLUE_betas(cov, asketch, 1e-10, estimator.nsamples_per_subset)
918+
tmp = np.array(
919+
[np.linalg.multi_dot((betas[kk].T, cov, betas[kk]))
920+
for kk in range(betas.shape[0])])
921+
tmp[estimator.nsamples_per_subset > 0] /= (
922+
estimator.nsamples_per_subset[estimator.nsamples_per_subset > 0])
923+
variance = tmp.sum()
924+
assert np.allclose(
925+
variance,
926+
BLUE_variance(asketch, cov, None, 1e-10,
927+
estimator.nsamples_per_subset))
928+
914929
values = estimator.generate_data(model.functions, variable)
915930
for ii in range(model.nmodels):
916931
asketch = np.zeros((costs.shape[0], 1))

tutorials/multi_fidelity/plot_many_model_approximate_control_variate_monte_carlo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,15 @@
233233
#
234234
#where each model is the function of a single uniform random variable defined on the unit interval :math:`[0,1]`.
235235

236+
from pyapprox.util.configure_plots import mathrm_labels, mathrm_label
236237
plt.figure()
237238
benchmark = setup_benchmark("polynomial_ensemble")
238239
poly_model = benchmark.fun
239240
cov = poly_model.get_covariance_matrix()
240241
model_costs = np.asarray([10**-ii for ii in range(cov.shape[0])])
241242
nhf_samples = 10
242243
nsample_ratios_base = np.array([2, 4, 8, 16])
243-
cv_labels = [r'$\mathrm{OCV-1}$', r'$\mathrm{OCV-2}$', r'$\mathrm{OCV-4}$']
244+
cv_labels = mathrm_labels(["OCV-1", "OCV-2", "OCV-4"])
244245
cv_rsquared_funcs = [
245246
lambda cov: get_control_variate_rsquared(cov[:2, :2]),
246247
lambda cov: get_control_variate_rsquared(cov[:3, :3]),
@@ -253,7 +254,6 @@
253254
plt.axhline(y=1, linestyle='--', c='k')
254255
plt.text(xloc, 1, r'$\mathrm{MC}$', fontsize=16)
255256

256-
from pyapprox.util.configure_plots import mathrm_labels
257257
acv_labels = mathrm_labels(["MLMC", "MFMC", "ACVMF"])
258258
estimators = [
259259
multifidelity.get_estimator("mlmc", cov, model_costs, poly_model.variable),
@@ -272,7 +272,7 @@
272272
label=acv_labels[ii])
273273
plt.legend()
274274
plt.xlabel(r'$\log_2(r_i)-i$')
275-
_ = plt.ylabel(r'$\mathrm{Variance}$ $\mathrm{reduction}$ $\mathrm{ratio}$ $\gamma$')
275+
_ = plt.ylabel(mathrm_label("Variance reduction ratio ")+r"$\gamma$")
276276

277277
#%%
278278
#As the theory suggests MLMC and MFMC use multiple models to increase the speed to which we converge to the optimal 2 model CV estimator OCV-2. These two approaches reduce the variance of the estimator more quickly than the ACV estimator, but cannot obtain the optimal variance reduction.

tutorials/multi_fidelity/plot_monte_carlo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
The mean squared error (MSE) of this estimator can be expressed as
1515
1616
.. math::
17-
17+
1818
\mean{\left(Q_{\alpha,N}-\mean{Q}\right)^2}&=\mean{\left(Q_{\alpha,N}-\mean{Q_{\alpha,N}}+\mean{Q_{\alpha,N}}-\mean{Q}\right)^2}\\
1919
&=\mean{\left(Q_{\alpha,N}-\mean{Q_{\alpha,N}}\right)^2}+\mean{\left(\mean{Q_{\alpha,N}}-\mean{Q}\right)^2}\\
2020
&\qquad\qquad+\mean{2\left(Q_{\alpha,N}-\mean{Q_{\alpha,N}}\right)\left(\mean{Q_{\alpha,N}}-\mean{Q}\right)}\\
2121
&=\var{Q_{\alpha,N}}+\left(\mean{Q_{\alpha,N}}-\mean{Q}\right)^2
22-
22+
2323
Here we used that :math:`\mean{\left(Q_{\alpha,N}-\mean{Q_{\alpha,N}}\right)}=0` so the third term on the second line is zero. Now using
2424
2525
.. math::
2626
27-
\var{Q_{\alpha,N}}=\var{N^{-1}\sum_{n=1}^N f^{(n)}_\alpha}=N^{-1}\sum_{n=1}^N \var{f^{(n)}_\alpha}=N^{-1}\var{Q_\alpha}
27+
\var{Q_{\alpha,N}}=\var{N^{-1}\sum_{n=1}^N f^{(n)}_\alpha}=N^{-2}\sum_{n=1}^N \var{f^{(n)}_\alpha}=N^{-1}\var{Q_\alpha}
2828
2929
yields
3030
@@ -33,7 +33,7 @@
3333
\mean{\left(Q_{\alpha}-\mean{Q}\right)^2}=\underbrace{N^{-1}\var{Q_\alpha}}_{I}+\underbrace{\left(\mean{Q_{\alpha}}-\mean{Q}\right)^2}_{II}
3434
3535
From this expression we can see that the MSE can be decomposed into two terms;
36-
a so called stochastic error (I) and a deterministic bias (II). The first term is the variance of the Monte Carlo estimator which comes from using a finite number of samples. The second term is due to using an approximation of :math:`f`. These two errors should be balanced, however in the vast majority of all MC analyses a single model :math:`f_\alpha` is used and the choice of :math:`\alpha`, e.g. mesh resolution, is made a priori without much concern for the balancing bias and variance.
36+
a so called stochastic error (I) and a deterministic bias (II). The first term is the variance of the Monte Carlo estimator which comes from using a finite number of samples. The second term is due to using an approximation of :math:`f`. These two errors should be balanced, however in the vast majority of all MC analyses a single model :math:`f_\alpha` is used and the choice of :math:`\alpha`, e.g. mesh resolution, is made a priori without much concern for the balancing bias and variance.
3737
3838
Given a fixed :math:`\alpha` the modelers only recourse to reducing the MSE is to reduce the variance of the estimator. In the following we plot the variance of the MC estimate of a simple algebraic function :math:`f_1` which belongs to an ensemble of models
3939

0 commit comments

Comments
 (0)