bet.sampling package¶
Submodules¶
bet.sampling.LpGeneralizedSamples module¶
This module provides methods to sample from Lp generalized normal, uniform, and beta distributions on the nD ball.
Adapted from natter.LpSphericallySymmetric.py https://github.com/fabiansinz/natter
- bet.sampling.LpGeneralizedSamples.Lp_generalized_beta(dim, num, p=2, d=2, scale=1.0, loc=None)¶
Generate samples from an Lp generalized beta distribution. When p=d then this is simly the Lp generalized uniform distribution.
- Parameters
p (float) – \(0 < p \leq \infty\), p for the lp norm where infinity is
numpy.infd (float) – shape parameter
dim (int) – Dimension of the space
num (int) – Number of samples to generate
scale (
float,int, ornumpy.ndarray) – Radius to scale the samples byloc (
numpy.ndarrayof shape (dim,)) – Location of the center of the samples
- bet.sampling.LpGeneralizedSamples.Lp_generalized_normal(dim, num, p=2, scale=1.0, loc=None)¶
Generate samples from an Lp generalized normal distribution.
- Parameters
p (float) – \(0 < p \leq \infty\), p for the lp norm where infinity is
numpy.infdim (int) – Dimension of the space
num (int) – Number of samples to generate
scale (
float,int, ornumpy.ndarray) – Radius to scale the samples byloc (
numpy.ndarrayof shape (dim,)) – Location of the center of the samples
- bet.sampling.LpGeneralizedSamples.Lp_generalized_uniform(dim, num, p=2, scale=1.0, loc=None)¶
Generate samples from an Lp generalized uniform distribution.
- Parameters
p (float) – \(0 < p \leq \infty\), p for the lp norm where infinity is
numpy.infdim (int) – Dimension of the space
num (int) – Number of samples to generate
scale (
float,int, ornumpy.ndarray) – Radius to scale the samples byloc (
numpy.ndarrayof shape (dim,)) – Location of the center of the samples
bet.sampling.basicSampling module¶
This module contains functions for sampling. We assume we are given access to a model, a parameter space, and a data space. The model is a map from the parameter space to the data space. We desire to build up a set of samples to solve an inverse problem thus giving information about the inverse mapping. Each sample consists for a parameter coordinate, data coordinate pairing. We assume the measure on both spaces is Lebesgue.
- exception bet.sampling.basicSampling.bad_object¶
Bases:
ExceptionException for when the wrong type of object is used.
- bet.sampling.basicSampling.lhs_sample_set(input_obj, num_samples, criterion, globalize=True)¶
Sampling algorithm for generating samples from a Latin hypercube in the domain present with
input_obj(a default unit hypercube is used if no domain has been specified)- Parameters
input_obj (
sample_setornumpy.ndarrayof shape (dim, 2) orint) –sample_setobject containing the dimension or domain to sample from, the domain to sample from, or the dimensionnum_samples (int) – number of samples
criterion (str) – latin hypercube criterion see PyDOE <http://pythonhosted.org/pyDOE/randomized.html>
globalize (bool) – Whether or not to globalize local variables.
- Return type
- Returns
- bet.sampling.basicSampling.random_sample_set(rv, input_obj, num_samples, globalize=True)¶
Create a sample set by sampling random variates from continuous distributions from
scipy.stats.rv_continuous. See https://docs.scipy.org/doc/scipy/reference/stats.html.rv can take multiple types of formats depending on type of distribution.
A string is used for the same distribution with default parameters in each dimension. ex. rv = ‘uniform’ or rv = ‘beta’
A list or tuple of length 2 is used for the same distribution with user-defined parameters in each dimension as a dictionary. ex. rv = [‘uniform’, {‘loc’:-2, ‘scale’:5}] or rv = [‘beta’, {‘a’: 2, ‘b’:5, ‘loc’:-2, ‘scale’:5}]
A list of length dim which entries of lists or tuples of length 2 is used for different distributions with user-defined parameters in each dimension as a dictionary. ex. rv = [[‘uniform’, {‘loc’:-2, ‘scale’:5}],
[‘beta’, {‘a’: 2, ‘b’:5, ‘loc’:-2, ‘scale’:5}]]
- Parameters
rv (str, list, or tuple) – Type and parameters for continuous random variables.
input_obj (
sample_setor int ornumpy.ndarray) –sample_setobject containing the dimension to sample from, or the dimension.num_samples (int) – Number of samples
globalize (bool) – Whether or not to globalize vectors.
- bet.sampling.basicSampling.regular_sample_set(input_obj, num_samples_per_dim=1)¶
Sampling algorithm for generating a regular grid of samples taken on the domain present with
input_obj(a default unit hypercube is used if no domain has been specified)- Parameters
input_obj (
sample_setornumpy.ndarrayof shape (dim, 2) orint) –sample_setobject containing the dimension or domain to sample from, the domain to sample from, or the dimensionnum_samples_per_dim (
ndarrayof dimension(input_sample_set._dim,)) – number of samples per dimension
- Return type
- Returns
sample_setobject which contains inputnum_samples
- bet.sampling.basicSampling.sample_from_updated(input_set, num_samples, globalize=True)¶
Create a new sample set from resampling from the updated probability measure of another sample set.
- Parameters
input_set (
sample_setordiscretization) – Sample set or discretization containing updated probability measure from which to sample.num_samples (int) – Number of new samples to create.
globalize – Whether or not to globalize objects.
:type bool :return: Sample set containing new samples :rtype:
sample_set
- class bet.sampling.basicSampling.sampler(lb_model, error_estimates=False, jacobians=False)¶
Bases:
objectThis class provides methods for sampling of parameter space to provide samples to be used by algorithms to solve inverse problems.
- compute_QoI_and_create_discretization(input_sample_set=None, savefile=None, globalize=True)¶
Dummy function for compute_qoi_and_create_discretization.
- compute_qoi_and_create_discretization(input_sample_set=None, savefile=None, globalize=True)¶
Samples the model at
input_sample_setand saves the results.Note: There are many ways to generate samples on a regular grid in Numpy and other Python packages. Instead of reimplementing them here we provide sampler that utilizes user specified samples.
- Parameters
input_sample_set (
sample_setwith num_samples) – samples to evaluate the model atsavefile (string) – filename to save samples and data
globalize (bool) – Makes local variables global.
- Return type
- Returns
discretizationobject which contains input and output of lengthnum_samples
- copy()¶
Returns a copy of the sampler object.
- create_random_discretization(rv, input_obj, savefile=None, num_samples=None, globalize=True)¶
Create a sample set by sampling random variates from continuous distributions from
scipy.stats.rv_continuous. See https://docs.scipy.org/doc/scipy/reference/stats.html, and evaluate the model to calculate quantities of interest and make a discretization.rv can take multiple types of formats depending on type of distribution.
A string is used for the same distribution with default parameters in each dimension. ex. rv = ‘uniform’ or rv = ‘beta’
A list or tuple of length 2 is used for the same distribution with user-defined parameters in each dimension as a dictionary. ex. rv = [‘uniform’, {‘loc’:-2, ‘scale’:5}] or rv = [‘beta’, {‘a’: 2, ‘b’:5, ‘loc’:-2, ‘scale’:5}]
A list of length dim which entries of lists or tuples of length 2 is used for different distributions with user-defined parameters in each dimension as a dictionary. ex. rv = [[‘uniform’, {‘loc’:-2, ‘scale’:5}],
[‘beta’, {‘a’: 2, ‘b’:5, ‘loc’:-2, ‘scale’:5}]]
- Parameters
rv (str, list, or tuple) – Type and parameters for continuous random variables.
input_obj (
sample_setor int) –sample_setobject containing the dimension to sample from, or the dimension.savefile (string) – filename to save discretization
num_samples (int) – Number of samples
globalize (bool) – Whether or not to globalize vectors.
- Return type
- Returns
discretizationobject which contains input and output sample sets withnum_samplestotal samples
- lhs_sample_set(input_obj, num_samples, criterion, globalize=True)¶
Sampling algorithm for generating samples from a Latin hypercube in the domain present with
input_obj(a default unit hypercube is used if no domain has been specified)- Parameters
input_obj (
sample_setornumpy.ndarrayof shape (dim, 2) orint) –sample_setobject containing the dimension or domain to sample from, the domain to sample from, or the dimensionnum_samples (int) – number of samples
criterion (str) – latin hypercube criterion see PyDOE <http://pythonhosted.org/pyDOE/randomized.html>
globalize (bool) – Whether or not to globalize local variables.
- Return type
- Returns
- local_to_global()¶
Globalize local variables.
- random_sample_set(rv, input_obj, num_samples, globalize=True)¶
Create a sample set by sampling random variates from continuous distributions from
scipy.stats.rv_continuous. See https://docs.scipy.org/doc/scipy/reference/stats.html.rv can take multiple types of formats depending on type of distribution.
A string is used for the same distribution with default parameters in each dimension. ex. rv = ‘uniform’ or rv = ‘beta’
A list or tuple of length 2 is used for the same distribution with user-defined parameters in each dimension as a dictionary. ex. rv = [‘uniform’, {‘loc’:-2, ‘scale’:5}] or rv = [‘beta’, {‘a’: 2, ‘b’:5, ‘loc’:-2, ‘scale’:5}]
A list of length dim which entries of lists or tuples of length 2 is used for different distributions with user-defined parameters in each dimension as a dictionary. ex. rv = [[‘uniform’, {‘loc’:-2, ‘scale’:5}],
[‘beta’, {‘a’: 2, ‘b’:5, ‘loc’:-2, ‘scale’:5}]]
- Parameters
rv (str, list, or tuple) – Type and parameters for continuous random variables.
input_obj (
sample_setor int) –sample_setobject containing the dimension to sample from, or the dimension.num_samples (int) – Number of samples
globalize (bool) – Whether or not to globalize vectors.
- Returns
- regular_sample_set(input_obj, num_samples_per_dim=1)¶
Sampling algorithm for generating a regular grid of samples taken on the domain present with
input_obj(a default unit hypercube is used if no domain has been specified)- Parameters
input_obj (
sample_setornumpy.ndarrayof shape (dim, 2) orint) –sample_setobject containing the dimension or domain to sample from, the domain to sample from, or the dimensionnum_samples_per_dim (
ndarrayof dimension(input_sample_set._dim,)) – number of samples per dimension
- Return type
- Returns
sample_setobject which contains inputnum_samples
bet.sampling.useLUQ module¶
The module contains a class for interfacing between BET and LUQ.
- exception bet.sampling.useLUQ.missing_module¶
Bases:
ExceptionException for when a module cannot be imported.
- bet.sampling.useLUQ.myModel(inputs, times)¶
Example for interfacing a time series model with LUQ. :param inputs: Parameter values at which to evaluate the model. :type inputs:
numpy.ndarrayof shape (num_inputs, num_params) :param times: Times at which to output results. :type times:numpy.ndarrayof shape (num_times, ) :return: Time series data :rtype:numpy.ndarrayof shape (num_inputs, num_times)
- class bet.sampling.useLUQ.useLUQ(predict_set, lb_model, times, obs_set=None)¶
Bases:
objectWrappers for interfacing BET with LUQ. Allows for the simple creation of bet.sample.discretization objects from LUQ output.
- clean_data(**kwargs)¶
Wrapper for luq.luq.LUQ.clean_data
- dynamics(**kwargs)¶
Wrapper for luq.luq.LUQ.dynamics
- get_obs()¶
Evaluate the model for the predicted time series.
- get_predictions()¶
Evaluate the model for the predicted time series.
- initialize(predicted_time_series=None, obs_time_series=None, times=None)¶
Initialize the LUQ object. This can be used manually if time series are pre-computed.
- Parameters
predicted_time_series (
numpy.ndarrayof shape (num_predict_samples, num_times)) – Time series solutions for predicted values.obs_time_series (
numpy.ndarrayof shape (num_obs_samples, num_times)) – Time series solutions for predicted values.times (
numpy.ndarraywith shape (num_times, )) – Times at which the series are output.
- learn_qois_and_transform(**kwargs)¶
Wrapper for luq.luq.LUQ.learn_qois_and_transform
- local_to_global()¶
Dummy function for saving.
- make_disc()¶
Construct bet.sample.discretization objects for predict and obs sets. :return: predict_disc, obs_disc :rtype: bet.sample.discretization, bet.sample.discretization or None if no observation set.
- save(savefile)¶
Save the object to a Pickle file. :param savefile: Name of file to save to. :type savefile: str
- set_observed_time_series(obs_time_series)¶
Set observed time series data manually. :param obs_time_series: time series data :type obs_time_series: :return:
numpy.ndarraywith shape (num_obs, num_times)
- setup()¶
Setup LUQ object all at once.
Module contents¶
This subpackage contains
basicSamplinga general class and associated set of methods that sample spaces and solve models through an interface.samplerrequests data (QoI) at a specified set of parameter samples.LpGeneralizedSamplesprovides methods for sampling on balls in Lp spaces.useLUQprovides methods for interfacing with the LUQ package.