pythia package

pythia.basis module

Assemble sparse univariate and multivariate basis polynomials.

Build univariate or multivariate normalized basis polynomials depending on the domain and distribution (and other degrees of freedom) of the parameter(s).

Currently supported are the following distribution types:
  • uniform

  • normal

  • Gamma

  • Beta

pythia.basis.multivariate_basis(univariate_bases, indices, partial=None)[source]

Assemble multivariate polynomial basis.

Set the (partial derivative of the) multivariate (product) polynomial basis functions.

Parameters:
  • univariate_bases (list of list of callable) – Univariate basis functions for parameters. Is called by univariate_bases[paramIdx][deg]().

  • indices (array_like) – Array of multiindices for multivariate basis functions.

  • partial (list of int) – Number of partial derivatives for each dimension. Length is same as univariate_bases.

Returns:

List of multivariate product polynomials with univariate degrees as specified in indices.

Return type:

List[Callable]

pythia.basis.normalize_polynomial(weight, basis, param)[source]

Normalize orthogonal polynomials.

Normalize a polynomial of an orthogonal system with respect to the scalar product

\[a(u,v)_\mathrm{pdf} = \int u(p) v(p) \mathrm{pdf}(p) \mathrm{d}p.\]

The normalized polynomial \(\phi_j\) for any given polynomial \(P_j\) is given by \(\phi_j = P_j / \sqrt{c_j}\) for the constant \(c_j = \int \mathrm{pdf}(p) * P_j(p)^2 \mathrm{d}p\).

Parameters:
  • weight (callable) – Probability density function.

  • basis (list of numpy.polynomial.Polynomial) – Polynomials to normalize w.r.t. weight.

  • param (pythia.parameter.Parameter) – Parameter used for distribution and domain information.

Returns:

List of normalized univariate polynomials.

Return type:

List[Callable]

pythia.basis.set_hermite_basis(param, deg)[source]

Generate list of probabilists Hermite polynomials.

Generate the Hermite Polynomials up to certain degree according to the mean and variance of the specified parameter.

Parameters:
  • param (pythia.parameters.Parameter) – Parameter for basis function. Needs to be normal distributed.

  • deg (int) – Maximum degree for polynomials.

Returns:

List of probabilists Hermite polynomials up to (including) degree specified in deg.

Return type:

List[Callable]

pythia.basis.set_jacobi_basis(param, deg)[source]

Generate list of Jacobi polynomials.

Generate the Jacobi Polynomials up to certain degree on the interval and DoFs specified by the parameter.

Note

The Jacobi polynomials have leading coefficient 1.

Parameters:
  • param (pythia.parameters.Parameter) – Parameter for basis function. Needs to be Beta-distributed.

  • deg (int) – Maximum degree for polynomials.

Returns:

List of Jacobi polynomials up to (including) degree specified in deg.

Return type:

List[Callable]

pythia.basis.set_laguerre_basis(param, deg)[source]

Generate list of Leguerre polynomials.

Generate the generalized Laguerre polynomials up to certain degree on the interval and DoFs specified by the parameter.

Parameters:
  • param (pythia.parameters.Parameter) – Parameter for basis function. Needs to be Gamma-distributed.

  • deg (int) – Maximum degree for polynomials.

Returns:

List of Laguerre polynomials up to (including) degree specified in deg.

Return type:

List[Callable]

pythia.basis.set_legendre_basis(param, deg)[source]

Generate list of the Legendre Polynomials.

Generate the Legendre Polynomials up to certain degree on the interval specified by the parameter.

Parameters:
  • param (pythia.parameters.Parameter) – Parameter for basis function. Needs to be uniformly distributed.

  • deg (int) – Maximum degree for polynomials.

Returns:

List of Legendre polynomials up to (including) degree specified in deg.

Return type:

List[Callable]

pythia.basis.univariate_basis(params, degs)[source]

Assemble a univariate polynomial basis.

Set polynomial basis up to deg for each parameter in params according to the parameter distribution and area of definition.

Parameters:
  • params (list of pythia.parameter.Parameter) – Parameters to compute univariate basis function for.

  • degs (array_like) – Max. degrees of univariate polynomials for each parameter.

Returns:

List of normalized univariate polynomials w.r.t. parameter domain and distribution up to specified degree for each parameter in params.

Return type:

List[List[Callable]]

pythia.chaos module

Sample-based computation of polynomial chaos expansion.

This module provides a class to compute the polynomial chaos approximation of an unknown function given via input/output training data pairs via linear least-squares regression.

class pythia.chaos.PolynomialChaos(params, index_set, x_train, w_train, y_train, coefficients=None)[source]

Bases: object

Computation of sparse polynomial chaos expansion.

Parameters:
  • params (list of pt.parameter.Parameter) – List of stochastic parameters.

  • index_set (pt.index.IndexSet) – Index set for sparse polynomial chaos expansion.

  • x_train (array_like) – Parameter realizations for training.

  • weights (array_like) – Regression weights for training.

  • fEval (array_like) – Function evaluation for training.

  • coefficients (array_like, default=None) – Polynomial expansion coefficients. If given, the coefficients are not computed during initiation. This can be used to load a chaos expansion.

eval(x, partial=None)[source]

Evaluate the (partial derivative of the) PC approximation.

Parameters:
  • x (array_like) – Parameter realizations in which the approximation is evaluated.

  • partial (list of int) – List that specifies the number of derivatives in each component. Length is the number of parameters.

Returns:

Evaluation of polynomial expansion in x values.

Return type:

ndarray

property mean: ndarray

Mean of the PC expansion.

Return type:

ndarray

property std: ndarray

Standard deviation of the PC expansion.

Return type:

ndarray

property var: ndarray

Variance of the PC expansion.

Return type:

ndarray

pythia.chaos.assemble_indices(enum_idx, sobol_tuples, max_terms)[source]

Compute automatic choice of multiindices.

Parameters:
  • enum_idx (np.ndarray) – Sorted enumeration indices according to magnitude of Sobol indices.

  • sobol_tuples (list of tuple) – List of Sobol subscript tuples.

  • max_terms (int) – Maximum number of expansion terms.

Returns:

indices – Array of (sparse) optimal multiindices.

Return type:

np.ndarray

Return type:

ndarray

pythia.chaos.find_optimal_indices(params, x_train, w_train, y_train, max_terms=0, threshold=0.001)[source]

Compute optimal multiindices of polynomial chaos expansion.

Compute the optimal multiindices for a polynomial chaos expansion based on an estimate of the Sobol coefficient values.

Parameters:
  • params (list of pythia.Parameters.Parameter) – Random parameters of the problem.

  • x_train (array_like) – Sample points for training

  • w_train (array_like) – Weights for training.

  • y_train (array_like) – Function evaluations for training.

  • max_terms (int, default=0) – Maximum number of expansion terms. Number of expansion terms is chosen automatically for max_terms=0.

  • threshold (float, default=1e-03) – Truncation threshold for Sobol indices. Sobol indices with smaller magnitude are ignored.

Returns:

  • indices – Array with multiindices.

  • sobol – Crude intermediate approximation of Sobol indices.

Return type:

Tuple[ndarray, ndarray]

pythia.chaos.get_gram_batchsize(dim, save_memory=538445312.5)[source]

Compute memory allocation batch sizes for information matrix.

Compute the maximal number of samples in each batch when assembling the information matrix to be maximally memory efficient and avoid OutOfMemory errors.

Parameters:
  • dim (int) – Number of rows/columns of information matrix.

  • save_memory (int, default=3*1025/2) – Memory (in bytes), that should be kept free. The default is equivalent to 512 MB.

Returns:

n – Batchsize for assembling of information matrix.

Return type:

int

pythia.index module

Create, manipulate and store information about multiindices.

class pythia.index.IndexSet(indices)[source]

Bases: object

Generate index set object for sparse PC expansion.

Parameters:

indices (np.ndarray) – Array of multiindices with shape (#indices, param dim).

get_index_number(indices)[source]

Get enumeration number of indices.

Get the row indices of the given multiindices such that self.indices[rows] = indices.

Parameters:

indices (np.ndarray) – Indices to get the number of.

Returns:

Array containing the enumeration numbers of the indices.

Return type:

ndarray

get_sobol_tuple_number(sobol_tuples)[source]

Get enumeration indices of Sobol tuples.

Parameters:

sobol_tuples (list of tuple) – List of Sobol tuples.

Returns:

Array containing the enumeration number of the Sobol tuples.

Return type:

ndarray

index_to_sobol_tuple(indices)[source]

Map array of indices to their respective Sobol tuples.

Parameters:

indices (np.ndarray) – Array of multiindices.

Returns:

List of Sobol tuples.

Return type:

List[Tuple]

sobol_tuple_to_indices(sobol_tuples)[source]

Map Sobol tuples to their respective indices.

Parameters:

sobol_tuples (tuple or list of tuple) – List of Sobol tuples.

Returns:

List of index arrays for each given Sobol tuple.

Return type:

List[ndarray]

pythia.index.add_indices(index_list)[source]

Add multiple arrays of multiindices.

Concatenate multiple arrays of multiindices, remove duplicates and sort them by sum of multiindices.

Parameters:

index_list (list of np.ndarray) – List of multiindex arrays.

Returns:

Array with all multiindices.

Return type:

ndarray

pythia.index.simplex(dimension, maximum)[source]

Create a simplex index set.

A simplex index set consists of all multiindices with sum less then or equal the maximum given.

Parameters:
  • dimension (int) – Dimension of the multiindices.

  • maximum (int) – Maximal sum value for the multiindices.

Returns:

Array with all possible multiindices in simplex set.

Examples

>>> pt.index.simplex(2, 2)
array([[0, 0],
       [0, 1],
       [1, 0],
       [0, 2],
       [1, 1],
       [2, 0]])
Return type:

ndarray

pythia.index.sort_index_array(indices)[source]

Sort multiindices and remove duplicates.

Sort rows of indices by sum of multiindex and remove duplicate multiindices.

Parameters:

indices (np.ndarray) – Index list before sorting.

Returns:

Sorted index array.

Return type:

ndarray

pythia.index.subtract_indices(indices, subtract)[source]

Set difference of two index arrays.

Parameters:
  • indices (np.ndarray) – Index array multiindices are taken out of.

  • subtract (np.ndarray) – Indices that are taken out of the original set.

Returns:

Set difference of both index arrays.

Return type:

ndarray

pythia.index.tensor(shape, lower=None)[source]

Create a tensor index set.

Parameters:
  • shape (array_like) – Shape of the tensor, enumeration starting from 0.

  • lower (array_like, default = None) – Starting values for each dimension of the tensor set. If None, all dimensions start with 0.

Returns:

Array with all possible multiindices in tensor set.

Examples

>>> pt.index.tensor([2, 2])
array([[0, 0],
       [0, 1],
       [1, 0],
       [1, 1]])

It is also possible to use the tensor functions to create n-variate subsets for the overall tensor set.

>>> pt.index.tensor([1, 5, 2], [0, 2, 0])
array([[0, 2, 0],
       [0, 2, 1],
       [0, 3, 0],
       [0, 3, 1],
       [0, 4, 0],
       [0, 4, 1]])
Return type:

ndarray

pythia.misc module

Miscellaneous functions to support PyThia core functionality.

pythia.misc.batch(iterable, n=1)[source]

Split iterable into different batches of batchsize n.

Parameters:
  • iterable (array_like) – Iterable to split.

  • n (int, default=1) – Batch size.

Yields:

Iterable for different batches.

Return type:

Iterator

pythia.misc.cartProd(array_list)[source]

Compute the outer product of two or more arrays.

Assemble an array containing all possible combinations of the elements of the input vectors \(v_1,\dots,v_n\).

Parameters:

array_list (list of array_like) – List of vectors \(v_1,\dots,v_n\).

Returns:

Cartesian product array.

Return type:

ndarray

pythia.misc.distributionDict()[source]

Set aliases for distribution descriptions.

Deprecated since version 2.0.0: distributionDict will be removed in PyThia 3.0.0.

Returns:

Dictionary with aliases for distribution descriptions.

Return type:

Dict

pythia.misc.doerfler_marking(values, idx=None, threshold=0.9)[source]

Dörfler marking for arbitrary values.

Parameters:
  • values (array_like) – Values for the Dörfler marking.

  • idx (list of int, optional) – List of indices associated with the entries of values. If None, this is set to range(len(values)).

  • threshold (float, default=0.9) – Threshold paramter for Dörfler marking.

Returns:

  • idx_reordered – Reordered indices given by idx. Ordered from largest to smallest value.

  • ordered_values – Reordered values. Ordered from largest to smallest.

  • marker – Threshold marker such that sum(values[:marker]) > threshold * sum(values).

Return type:

Tuple[ndarray, ndarray, int]

pythia.misc.formatTime(dt)[source]

Converts time (seconds) to time format string.

Parameters:

dt (float) – Time in seconds.

Returns:

Formatted time string.

Return type:

str

pythia.misc.gelman_rubin_condition(chains)[source]

Compute Gelman-Rubin criterion.

Implementation of the Gelman-Rubin convergence criterion for multiple parameters. A Markov chain is said to be in its convergence, if the final ration is close to one.

Parameters:

chains (array_like, ndim=3) – Array containing the Markov chains of each parameter. All chains are equal in length, the assumed shape is (#chains, chain length, #params).

Returns:

Values computed by Gelman-Rubin criterion for each parameter.

Return type:

ndarray

pythia.misc.get_confidence_interval(samples, rate=0.95, resolution=500)[source]

Compute confidence intervals of samples.

Compute the confidence intervals of the 1D marginals of the samples (slices). The confidence interval of a given rate is the interval around the median (not mean) of the samples containing roughly rate percent of the total mass. This is computed for the left and right side of the median independently.

Parameters:
  • samples (array_like, ndim < 3) – Array containing the (multidimensional) samples.

  • rate (float, default=0.95) – Fraction of the total mass the interval should contain.

  • resolution (int, default=500) – Number of bins used in histogramming the samples.

Returns:

Confidence intervals for each component.

Return type:

ndarray

pythia.misc.is_contained(val, domain)[source]

Check if a given value (vector) is contained in a domain.

Checks if each component of the vector lies in the one dimensional interval of the corresponding component of the domain.

Parameters:
  • val (array_like) – Vector to check containment in domain

  • domain (array_like) – Product domain of one dimensional intervals.

Returns:

Bool stating if value is contained in domain.

Return type:

bool

pythia.misc.line(indicator, message=None)[source]

Print a line of 80 characters by repeating indicator.

An additional message can be given.

Parameters:
  • indicator (string) – Indicator the line consists of, e.g. ‘-’, ‘+’ or ‘+-‘.

  • message (string, optional) – Message integrated in the line.

Returns:

String of 80 characters length.

Return type:

str

pythia.misc.load(filename)[source]

Alias for numpy.load().

Return type:

ndarray

pythia.misc.now()[source]

Get string of current machine date and time.

Returns:

Formatted date and time string.

Return type:

str

pythia.misc.save(filename, data, path='./')[source]

Wrapper for numpy save.

Assures path directory is created if necessary and backup old data if existent.

Parameters:
  • name (str) – Filename to save data to.

  • data (array_like) – Data to save as .npy file.

  • path (str, default='./') – Path under which the file should be created.

Return type:

None

pythia.misc.shiftCoord(x, T, I)[source]

Shift x in interval T to interval I.

Use an affine transformation to shift points \(x\) from the interval \(T = [t_0, t_1]\) to the interval \(I = [a, b]\).

Parameters:
  • x (array_like) – Points in interval \(T\).

  • T (array_like) – Original interval.

  • I (array_like) – Target interval.

Returns:

Shifted values for x.

Return type:

ndarray

pythia.misc.str2iter(string, iterType=<class 'list'>, dataType=<class 'int'>)[source]

Cast str(iterable) to iterType of dataType.

Cast a string of lists, tuples, etc to the specified iterable and data type, i.e., for iterType=tuple and dataType=float cast str([1,2,3]) -> (1.0, 2.0, 3.0).

Parameters:
  • string (str) – String representation of iterable.

  • iterType (iterable, default=list) – Iterable type the string is converted to.

  • dataType (type, default=int) – Data type of entries of iterable, e.g. int or float.

Return type:

Sequence

pythia.misc.wlsSamplingBound(m, c=4)[source]

Compute the weighted Least-Squares sampling bound.

The number of samples \(n\) is chosen such that

\[\frac{n}{\log(n)} \geq cm,\]

where \(m\) is the dimension of the Gramian matrix (number of PC expansion terms) and \(c\) is an arbitrary constant. In Cohen & Migliorati 2017 the authors observed that the coice \(c=4\) yields a well conditioned Gramian with high probability.

Parameters:
  • m (int) – Dimension of Gramian matrix.

  • c (float, default=4) – Scaling constant.

Returns:

Number of required wLS samples.

Return type:

int

pythia.parameter module

PyThia classes containing Parameter information.

class pythia.parameter.Parameter(index, name, domain, distribution, mean=None, var=None, alpha=None, beta=None)[source]

Bases: object

Class used for stochasic parameters.

Parameters:
  • index (int) – Enumeration index of the parameter.

  • name (str) – Parameter name.

  • domain (array_like) – Supported domain of the parameter distribution.

  • distribution (str) – Distribution identifier of the parameter.

  • mean (float, default=None) – Mean of parameter probability.

  • var (float, default=None) – Variance of parameter probability.

  • alpha (float, default=None) – Alpha value of Beta and Gamma distribution.

  • beta (float, default=None) – Beta value of Beta and Gamma distribution.

alpha: float | None = None
beta: float | None = None
distribution: str
domain: List | Tuple | ndarray
index: int
mean: float | None = None
name: str
var: float | None = None

pythia.sampler module

Sampler classes for generating in random samples and PDF evaluations.

class pythia.sampler.BetaSampler(domain, alpha, beta)[source]

Bases: Sampler

Sampler for univariate Beta distributed samples on given domain.

Parameters:
  • domain (array_like) – Supported domain of distribution.

  • alpha (float) – Parameter for Beta distribution.

  • beta (float) – Parameter for Beta distribution.

property cov: float

(Co)Variance of the distribution.

Return type:

float

property dimension

Dimension of the parameters.

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of log-PDF.

Note

Not yet implemented.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of log-PDF.

Note

Not yet implemented.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass

Mass of the PDF.

property maximum

Maximum value of the PDF.

The maximum of the Beta distribution is given by

\[\begin{split}\max_{x\in[a,b]} f(x) = \begin{cases} \infty & \mbox{if } 0 < \alpha < 1 \mbox{ or } 0 < \beta < 1,\\ \frac{1}{(b-a)B(\alpha,\beta)} & \mbox{if } \alpha = 1 \mbox{ or } \beta = 1,\\ \frac{(\alpha-1)^{\alpha-1}(\beta-1)^{\beta-1}}{(\alpha+\beta-2)^{\alpha+\beta-2}(b-a)B(\alpha,\beta)} & \mbox{if } \alpha > 1 \mbox{ and } \beta > 1, \\ \end{cases}\end{split}\]

where \(B(\alpha,\beta)\) denotes the Beta-function.

property mean: float

Mean value of the distribution.

Return type:

float

pdf(x)[source]

Evaluate PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

property std: float

Standard deviation of the distribution.

Return type:

float

property var: float

Variance of the distribution.

Return type:

float

class pythia.sampler.GammaSampler(domain, alpha, beta)[source]

Bases: Sampler

Sampler for univariate Gamma distributed samples on given domain.

Parameters:
  • domain (array_like) – Supported domain of distribution.

  • alpha (float) – Parameter for Gamma distribution.

  • beta (float) – Parameter for Gamma distribution.

property cov: float

(Co)Variance of the distribution.

Return type:

float

property dimension: float

Dimension of the parameters.

Return type:

float

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of log-PDF.

Note

Not yet implemented.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of log-PDF.

Note

Not yet implemented.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass: float

Mass of the PDF.

Return type:

float

property maximum: float

Maximum value of the PDF.

The maximum of the Gamma distribution is given by

\[\begin{split}\max_{x\in[a,\infty)} f(x) = \begin{cases} \infty & \mbox{if } 0 < \alpha < 1\\ \frac{\beta^\alpha}{\Gamma(\alpha)} & \mbox{if } \alpha = 1\\ \frac{\beta^\alpha}{\Gamma(\alpha)} \Bigl(\frac{\alpha-1}{\beta} \Bigr)^{\alpha-1} e^{1-\alpha} & \mbox{if } \alpha > 1\\ \end{cases}\end{split}\]
Return type:

float

property mean: float

Mean value of the distribution.

Return type:

float

pdf(x)[source]

Evaluate PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

property std: float

Standard deviation of the distribution.

Return type:

float

property var: float

Variance of the distribution.

Return type:

float

class pythia.sampler.NormalSampler(mean, var)[source]

Bases: Sampler

Sampler for univariate normally distributed samples.

Parameters:
  • mean (float) – Mean of the Gaussian distribution.

  • var (float) – Variance of the Gaussian distribution.

property cov: float

(Co)Variance of the distribution.

Return type:

float

property dimension: float

Dimension of the parameters.

Return type:

float

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass: float

Mass of the PDF.

Return type:

float

property maximum: float

Maximum value of the PDF.

Return type:

float

property mean: float

Mean value of the distribution.

Return type:

float

pdf(x)[source]

Evaluate PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

property std: float

Standard deviation.

Return type:

float

class pythia.sampler.ParameterSampler(params)[source]

Bases: Sampler

Product sampler of given parameters.

Parameters:

params (list of pythia.parameter.Parameter) – List containing information of parameters.

property cov: ndarray

Covariance of the PDF.

Return type:

ndarray

property dimension: int

Dimension of the parameters.

Return type:

int

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate log-PDF.

The log-PDF is given by the sum of the univariate log-PDFs.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass: float

Mass of the PDF.

Return type:

float

property maximum: float

Maximum value of the PDF.

Return type:

float

property mean: ndarray

Mean of the PDF.

Return type:

ndarray

pdf(x)[source]

Evaluate PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

weight(x)[source]

Weights of the parameter product PDF.

Parameters:

x (np.ndarray) – Evaluation points.

Returns:

Array of uniform weights for samples.

Return type:

ndarray

class pythia.sampler.ProductSampler(sampler_list)[source]

Bases: Sampler

Tensor sampler for independent parameters.

Sampler for cartesian product samples of a list of (independent) univariate samplers.

Parameters:

sampler_list (list of pythia.sampler.Sampler) – List of (univariate) Sampler objects.

property cov: ndarray

Covariance of the PDF.

Return type:

ndarray

property dimension: int

Dimension of the parameters.

Return type:

int

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate log-PDF.

The log-PDF is given by the sum of the univariate log-PDFs.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass: float

Mass of the PDF.

Return type:

float

property maximum: float

Maximum value of the PDF.

Return type:

float

property mean: ndarray

Mean of the PDF.

Return type:

ndarray

pdf(x)[source]

Evaluate PDF.

The PDF is given by the product of the univariate PDFs.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

weight(x)[source]

Weights of the product PDF.

Parameters:

x (np.ndarray) – Evaluation points.

Returns:

Array of uniform weights for samples.

Return type:

ndarray

class pythia.sampler.Sampler[source]

Bases: ABC

Base class for all continuous samplers.

abstract property cov: float | ndarray

(Co)Variance of the pdf.

Return type:

Union[float, ndarray]

abstract property dimension: int

Dimension of the ambient space.

Return type:

int

domain: ndarray
abstract grad_x_log_pdf(x)[source]

Gradient of log-density of the samplers distribution.

Computes the gradient of the log-density of the samplers underlying distribution at the given points x.

Parameters:

x (array_like of shape (..., D)) – List of points or single point. D is the objects dimension.

Returns:

Gradient values of the log-density at the points with shape (…, D).

Return type:

ndarray

abstract hess_x_log_pdf(x)[source]

Hessian of log-density of the samplers distribution.

Computes the Hessian of the log-density of the samplers underlying distribution at the given points x.

Parameters:

x (array_like of shape (..., D)) – List of points or single point. D is the objects dimension.

Returns:

Hessian values of the log-density at the points with shape (…, D, D).

Return type:

ndarray

abstract log_pdf(x)[source]

Log-density of the samplers distribution.

Computes the log-density of the samplers underlying distribution at the given points x.

Parameters:

x (array_like of shape (..., D)) – List of points or single point. D is the objects dimension.

Returns:

Log-density values at the points.

Return type:

ndarray

abstract property mass

Mass of the sampler distribution.

The integral of the sampler distribution over the domain of definition. If the density is normalised this value should be one.

abstract property maximum: float

Maximum of the pdf.

Return type:

float

abstract property mean: float | ndarray

Mean value of the pdf.

Return type:

Union[float, ndarray]

abstract pdf(x)[source]

Density of the samplers distribution.

Computes the density of the samplers underlying distribution at the given points x.

Parameters:

x (array_like of shape (..., D)) – List of points or single point. D is the objects dimension.

Returns:

Density values at the points.

Return type:

ndarray

abstract sample(shape)[source]

Random values in a given shape.

Create an array of the given shape and populate it with random samples from the samplers distribution.

Parameters:

shape (array_like, optional) – The dimensions of the returned array, should all be positive. If no argument is given a single Python float is returned.

Returns:

Random values of specified shape.

Return type:

ndarray

class pythia.sampler.UniformSampler(domain)[source]

Bases: Sampler

Sampler for univariate uniformly distributed samples on given domain.

Parameters:

domain (array_like) – Interval of support of distribution.

property cov: float

(Co)Variance of the distribution.

Return type:

float

property dimension: int

Parameter dimension.

Return type:

int

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of uniform log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of uniform log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate uniform log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass: float

Mass of the PDF.

Return type:

float

property maximum: float

Maximum value of the PDF.

Return type:

float

property mean: float

Mean value of the distribution.

Return type:

float

pdf(x)[source]

Evaluate uniform PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from uniform distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

property std: float

Standard deviation of the distribution.

Return type:

float

property var: float

Variance of the distribution.

Return type:

float

class pythia.sampler.WLSSampler(params, basis, tsa=True, trial_sampler=None, bulk=None)[source]

Bases: Sampler

Weighted Least-Squares sampler as in Cohen & Migliorati 2017.

Parameters:
  • params (list of pythia.parameter.Parameter) – List of parameters.

  • basis (list) – List of basis functions.

  • tsa (bool, default=False) – Trial sampler adaptation. If True, a trial sampler is chosen on the distributions of parameters, if false a uniform trial sampler is used.

  • trial_sampler (pythia.sampler.Sampler, default=None) – Trial sampler for rejection sampling. If tsa is true and either trial_sampler or bulk are None, the trial sampler is chosen automatically.

  • bulk (float, defaul=None) – Scaling for trial sampler. If tsa is true and either trial_sampler or bulk are None, the trial sampler is chosen automatically.

property cov: ndarray

Covariance of the PDF.

Return type:

ndarray

property dimension: int

Dimension of the parameters.

Return type:

int

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate log-PDF.

The log-PDF is given by the sum of the univariate log-PDFs.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass

Mass of the PDF.

property maximum: float

Maximum value of the PDF.

Return type:

float

property mean: ndarray

Mean of the PDF.

Return type:

ndarray

pdf(x)[source]

Evaluate PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

weight(x)[source]

Weights for the PDF.

Parameters:

x (array_like) – Points the weight function is evaluated in.

Returns:

weights of evaluation points x.

Return type:

ndarray

class pythia.sampler.WLSTensorSampler(params, deg, tsa=True)[source]

Bases: Sampler

WLS sampler for tensor multivariate basis.

Sampler for weighted Least-Squares sampling as described by Cohen and Migliorati. Only full tensor space can be sampled. This allows for univariate weighted Least-Squares sampling in each component.

Parameters:
  • params (list of pythia.parameter.Parameter) – Parameter list.

  • deg (list of int) – Polynomial degree of each component (same for all).

  • tsa (bool, default=True) – Trial sampler adaptation. If True, a trial sampler is chosen on the distributions of parameters, if false a uniform trial sampler is used.

property cov: ndarray

Covariance of the PDF.

Return type:

ndarray

property dimension: int

Dimension of the parameters.

Return type:

int

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate log-PDF.

The log-PDF is given by the sum of the univariate log-PDFs.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass: float

Mass of the PDF.

Return type:

float

property maximum: float

Maximum value of the PDF.

Return type:

float

property mean: ndarray

Mean of the PDF.

Return type:

ndarray

pdf(x)[source]

Evaluate PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

weight(x)[source]

Weights for the PDF.

Parameters:

x (array_like) – Points the weight function is evaluated in.

Returns:

Weights of evaluation points x.

Return type:

ndarray

class pythia.sampler.WLSUnivariateSampler(param, deg, tsa=True)[source]

Bases: Sampler

Sampler for univariate optimally distributed samples on given domain.

Parameters:

domain (array_like) – Interval of support of distribution.

property cov: float

(Co)Variance of the distribution.

Return type:

float

property dimension: int

Parameter dimension.

Return type:

int

domain: ndarray
grad_x_log_pdf(x)[source]

Evaluate gradient of uniform log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of gradient (vector valued) of log-PDF evaluated in x.

Return type:

ndarray

hess_x_log_pdf(x)[source]

Evaluate Hessian of uniform log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of Hessian (matrix valued) of log-PDF evaluated in x.

Return type:

ndarray

log_pdf(x)[source]

Evaluate uniform log-PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of log-PDF evaluated in x.

Return type:

ndarray

property mass: float

Mass of the PDF.

Return type:

float

property maximum: float

Maximum value of the PDF.

Return type:

float

property mean: float

Mean value of the distribution.

Return type:

float

pdf(x)[source]

Evaluate uniform PDF.

Parameters:

x (array_like) – Evaluation points.

Returns:

Values of PDF evaluated in x.

Return type:

ndarray

sample(shape)[source]

Draw samples from weighted least-squares parameter distribution.

Parameters:

shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

property std: float

Standard deviation of the distribution.

Return type:

float

property var: float

Variance of the distribution.

Return type:

float

weight(x)[source]

Weights for the pdf.

Parameters:

x (np.ndarray) – Points the weight function is evaluated in.

Returns:

w – Weights of evaluation points x.

Return type:

array_like

Return type:

ndarray

pythia.sampler.assign_sampler(param)[source]

Assign a univariate sampler to the given parameter.

Parameters:

param (pythia.parameter.Parameter) –

Returns:

Univariate sampler.

Return type:

Sampler

pythia.sampler.constraint_sampling(sampler, constraints, shape)[source]

Draw samples according to algebraic constraints.

Draw samples from target distribution and discard samples that do not satisfy the constraints.

Parameters:
  • sampler (Sampler) – Sampler to sample from.

  • constraints (list of callable) – List of functions that return True if sample point satisfies the constraint.

Returns:

Samples drawn from sampler satisfying the constraints.

Notes

The constaints may lead to a non-normalized density function.

Return type:

ndarray

pythia.sampler.get_maximum(f, domain, n=1000)[source]

Compute essential maximum of function by point evaluations.

Parameters:
  • f (callable) – Function to evaluate. Needs to map from n-dim space to 1-dim space.

  • domain (array_like) – Domain to evaluate function on.

  • n (int, default=1000) – Number of function evaluations. Evaluations are done on a uniform grid in domain. Actual number of points may thus be a little greater.

Returns:

Approximation of maximum of function f.

Return type:

float

pythia.sampler.rejection_sampling(pdf, trial_sampler, scale, dimension, shape)[source]

Draw samples from pdf by rejection sampling.

Parameters:
  • pdf (Callable) – Probability density the samples are generated from.

  • trial_sampler (Sampler) – Trial sampler proposal samples are drawn from.

  • scale (float) – Threshold parameter with pdf <= scale * trialSampler.pdf

  • dimension (int) – Dimension of the (input of the) pdf.

  • shape (array_like) – Shape of the samples.

Returns:

Random samples of specified shape.

Return type:

ndarray

Module contents