mud.examples package#
Submodules#
mud.examples.adcirc module#
mud.examples.comparison module#
MUD vs MAP Comparison Example
Functions for running 1-dimensional polynomial inversion problem.
- mud.examples.comparison.comparison_plot(d_prob: DensityProblem, b_prob: BayesProblem, space: str = 'param', ax: Axes | None = None)[source]#
Generate plot comparing MUD vs MAP solution
- Parameters:
d_prob (mud.base.DensityProblem) – DensityProblem object that has been solved already with d_prob.estimate() or another such method.
b_prob (mud.base.BayesProblem) – BayesProblem object that has been solved already with b_prob.estimate() or another such method.
space (str, default="param") – What space to plot. Default is “param” to plot the parameter, or input, space, and thus the updated parameter distributions and associated MUD/MAP solutions. Any other value will plot the observable space, which includes the predicted distribution for the DensityProblem and the data-likelihood distribution for the BayesProblem.
ax (matplotlib.pyplot.Axes, optional) – Existing matplotlib Axes object to plot onto. If none provided (default), then a figure is initialized.
- Returns:
ax – Axes object that was plotted onto or created.
- Return type:
matplotlib.pyplot.Axes
- mud.examples.comparison.run_comparison_example(p: int = 5, num_samples: int = 1000, mu: float = 0.25, sigma: float = 0.1, domain: List[int] = [-1, 1], N_vals: List[int] = [1, 5, 10, 20], latex_labels: bool = True, save_path: str | None = None, dpi: int = 500, close_fig: bool = False)[source]#
Run MUD vs MAP Comparison Example
Entry-point function for running simple polynomial example comparing Bayesian and Data-Consistent solutions. Inverse problem involves inverting the polynomial QoI map Q(lam) = lam^5.
- Parameters:
num_samples (int, default=100) – Number of \(\lambda\) samples to generate from a uniform distribution over
domain
for solving inverse problem.mu (float, default=0.25) – True mean value of observed data.
sigma (float, default=0.1) – Standard deviation of observed data.
domain (
numpy.typing.ArrayLike
, default=[[-1, 1]]) – Domain to draw lambda samples from.N_vals (List[int], default=[1, 5, 10, 20]) – Values for N, the number of data-points to use to solve inverse problems, to use. Each N value will produce two separate plots.
save_path (str, optional) – If provided, path to save the resulting figure to.
dpi (int, default=500) – Resolution of images saved
close_fig (bool , default=False) – Set to True to close figure and only save it. Useful when running in notebook environments.
- Returns:
res –
matplotlib.axes.Axes
]] List of Tuples of(d_prob, b_prob, ax)
containing the resulting Density and Bayesian problem objects ind_prob
andb_prob
, resp., and the matplotlib axis to which the results were plotted, for each N case run.- Return type:
List[Tuple[
mud.base.DensityProblem
,mud.base.BayesProblem
,
mud.examples.examples module#
mud.examples.exp_decay module#
Exponential Decay Example
mud.examples.fenics module#
mud.examples.linear module#
MUD Linear Examples
Functions for examples for linear problems.
- mud.examples.linear.call_comparison(lin_prob: LinearGaussianProblem, **kwargs)[source]#
- mud.examples.linear.call_consistent(lin_prob: LinearGaussianProblem, **kwargs)[source]#
- mud.examples.linear.call_map(lin_prob: LinearGaussianProblem, **kwargs)[source]#
- mud.examples.linear.call_mismatch(lin_prob: LinearGaussianProblem, **kwargs)[source]#
- mud.examples.linear.call_mud(lin_prob: LinearGaussianProblem, **kwargs)[source]#
- mud.examples.linear.call_tikhonov(lin_prob: LinearGaussianProblem, **kwargs)[source]#
- mud.examples.linear.noisy_linear_data(M, reference_point, std, num_data=None)[source]#
Creates data produced by model assumed to be of the form:
Q(lam) = M * lam + odj,i =Mj(λ†)+ξi, ξi ∼N(0,σj2), 1≤i≤Nj
- mud.examples.linear.random_linear_problem(dim_input: int = 10, dim_output: int = 10, mean_i: _SupportsArray[dtype] | _NestedSequence[_SupportsArray[dtype]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, cov_i: _SupportsArray[dtype] | _NestedSequence[_SupportsArray[dtype]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, seed: int | None = None)[source]#
Construct a random linear Gaussian Problem
- mud.examples.linear.random_linear_wme_problem(reference_point, std_dev, num_qoi=1, num_observations=10, dist='normal', repeated=False)[source]#
Create a random linear WME problem
- Parameters:
reference_point (np.ndarray) – Reference true parameter value.
dist (str, default='normal') – Distribution to draw random linear map from. ‘normal’ or ‘uniform’ supported at the moment.
num_qoi (int, default = 1) – Number of QoI
num_observations (int, default = 10) – Number of observation data points.
std_dev (np.ndarray, optional) – Standard deviation of normal distribution from where observed data points are drawn from. If none specified, noise-less data is created.
- mud.examples.linear.rotation_map(qnum=10, tol=0.1, b=None, ref_param=None, seed=None)[source]#
Generate test data linear rotation map
- mud.examples.linear.rotation_map_trials(numQoI=10, method='ordered', num_trials=100, model_eval_budget=100, ax=None, color='r', label='Ordered QoI $(10\\times 10D)$', seed=None)[source]#
Run a set of trials for linear rotation map problems
- mud.examples.linear.run_contours(plot_fig: List[str] | None = None, save_path: str | None = None, dpi: int = 500, close_fig: bool = False, **kwargs)[source]#
Run Contours
Produces contour plots of 2-D parameter space for 2-to-1 linear map found in Figures 3 and 5 of [ref]. These contour plots show the different regularization terms between Bayesian and Data-Consistent solutions that lead to a different optimization problem and therefore a different solution to the inverse problem.
- Parameters:
plot_fig (str, default='all') – Which figures to produce. Possible options are data_mismatch,
save_path (str, optional) – If provided, path to save the resulting figure to.
dpi (int, default=500) – Resolution of images saved
close_fig (bool, default=False) – Set to True to close figure and only save it.
kwargs (dict, optional) –
kwargs to overwrite default arguments used to build linear problem. Possible values include and their expected types, and default values:
A : 2D array, default=[[1, 1]]
b - 1D array, default = [0]
y - 1D array, default =[1]
mean_i - 1D array, default = [0.25, 0.25]
cov_i - 2D array, default = [[1, -0.25], [-0.25, 0.5]]
cov_o - 1D array, default = [1]
- Returns:
lin_prob – LinearGaussianProblem object with solved linear inverse problem and associated data within.
- Return type:
- mud.examples.linear.run_high_dim_linear(dim_input: int = 100, dim_output: int = 100, seed: int = 21, save_path: str | None = None, dpi: int = 500, close_fig: bool = True)[source]#
Run High Dimension Linear Example
Reproduces Figure 6 from [ref], showing the relative error between the true parameter value and the MUD, MAP and least squares solutions to linear gaussian inversion problems for increasing dimension and rank of a randomly generated linear map A.
- Parameters:
dim_input (int, default=20) – Input dimension of linear map (number of rows in A).
dim_output (int, default=5) – Output dimension of linear map (number of columns in A).
seed (int, default = 21) – To fix results for reproducibility. Set to None to randomize results.
save_path (str, optional) – If provided, path to save the resulting figure to.
dpi (int, default=500) – Resolution of images saved
close_fig (bool, default=False) – Set to True to close figure and only save it.
- Returns:
rank_errs, dim_errs – Tuple containing the error between the true solution and each of the (mud, map, least_squares) solutions for increasing dimension and rank from 1 to dim_output. These arrays are used to produce the plots given.
- Return type:
Tuple[np.array, np.array]
- mud.examples.linear.run_wme_covariance(dim_input: int = 20, dim_output: int = 5, sigma: float = 0.1, Ns: List[int] = [10, 100, 1000, 10000], seed: int | None = None, save_path: str | None = None, dpi: int = 500, close_fig: bool = False)[source]#
Weighted Mean Error Map Updated Covariance
Reproduces figure 4 from [ref], showing the spectral properties of the updated covriance for a the Weighted Mean Error map on a randomly generated linear operator as more data from repeated measurements is used to constructthe QoI map.
- Parameters:
dim_input (int, default=20) – Input dimension of linear map (number of rows in A).
dim_output (int, default=5) – Output dimension of linear map (number of columns in A).
sigma (float, default=1e-1) – N(0, sigma) error added to produce “measurements” from linear operator.
Ns (List[str]. default = [10, 100, 1000, 10000]) – List of number of data points to collect in constructing Q_WME map to view how the spectral properties of the updated covariance change as more data is included in the Q_WME map.
seed (int, default = 21) – To fix results for reproducibility. Set to None to randomize results.
save_path (str, optional) – If provided, path to save the resulting figure to.
dpi (int, default=500) – Resolution of images saved
close_fig (bool, default=False) – Set to True to close figure and only save it.
- Returns:
linear_wme_prob, ax – Tuple containing solved linear WME problems for each Ns value, and axes containing the plot of the first 20 eigenvalues of the updated covariances for each Q_WME map.
- Return type:
Tuple[mud.base.LinearWMEProblem, matplotlib.pyplot.Axes]
mud.examples.poisson module#
mud.examples.simple module#
Simple Example
- mud.examples.simple.identity_1D_bayes_prob(num_samples=1000, num_obs=50, mu=0.5, sigma=0.05, init_dist=<scipy.stats._distn_infrastructure.rv_continuous_frozen object>, domain=None)[source]#
Identity 1D Bayes Problem
Solving 1d identity map parameter estimation problem using the BayesProlem class and the map point estimate.
- mud.examples.simple.identity_1D_density_prob(num_samples=2000, num_obs=20, mu=0.5, sigma=0.05, weights=None, init_dist='uniform', normalize=False, domain=[0, 1], analytical_pred=True)[source]#
Identity 1D Density Problem
Solving 1d identity map parameter estimation problem using the DensityProblem class and the mud point estimate.
- mud.examples.simple.identity_1D_temporal_prob(num_samples=2000, num_obs=20, y_true=0.5, noise=0.05, weights=None, init_dist=<scipy.stats._distn_infrastructure.rv_continuous_frozen object>, normalize=False, domain=None, wme_map=True, analytical_pred=True)[source]#
Identity 1D Temporal Problem
Solving 1d identity map parameter estimation problem using the SpatioTemporalProblem class to construct DensityProblem class using the WME map to aggregate data.
- mud.examples.simple.polynomial_1D_data(p: int = 5, num_samples: int = 1000, domain: ~numpy._typing._array_like._SupportsArray[~numpy.dtype] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype]] | bool | int | float | complex | str | bytes | ~numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes] = [[-1, 1]], init_dist=<scipy.stats._distn_infrastructure.rv_continuous_frozen object>, mu: float = 0.25, sigma: float = 0.1, N: int = 1)[source]#
Polynomial 1D QoI Map
Generates test data for an inverse problem involving the polynomial QoI map
(1)#\[\begin{split}Q_p(\\lambda) = \\lambda^p\end{split}\]Where the uncertain parameter to be determined is \(\lambda\).
num_samples
samples from a uniform distribution overdomain
are generated usingnumpy.random.uniform()
and pushed through the forward model.N
observed data points are generated from a normal distribution centered atmu
with standard deviationsigma
usingscipy.stats.norm
.- Parameters:
num_samples (int, default=100) – Number of \(\lambda\) samples to generate from a uniform distribution over
domain
for solving inverse problem.domain (
numpy.typing.ArrayLike
, default=[[-1, 1]]) – Domain to draw lambda samples from.mu (float, default=0.25) – True mean value of observed data.
sigma (float, default=0.1) – Standard deviation of observed data.
N (int, default=1) – Number of data points to generate from observed distribution. Note if 1, the default value, then the singular drawn value will always be
mu
.
- Returns:
data – Tuple of
(lam, q_lam, data)
wherelam
is contains the \(\lambda\) samples,q_lam
the value of \(Q_p(\lambda)\), anddata
the observed data values from the \(\mathcal{N}(\mu, \sigma)\) distribution.- Return type:
Tuple[
numpy.ndarray
,]
Examples
Note when N=1, data point drawn is always equal to mean.
>>> import numpy as np >>> from mud.examples.comparison import polynomial_1D_data >>> lam, q_lam, data = polynomial_1D_data(num_samples=10, N=1) >>> data[0] 0.25 >>> len(lam) 10
For higher values of N, values are drawn from N(mu, sigma) distribution.
>>> lam, q_lam, data = polynomial_1D_data(N=10, mu=0.5, sigma=0.01) >>> len(data) 10 >>> np.mean(data) < 0.6 True