eulerpi.core.sparsegrid module

This module provides functions to handle create Sparse Grids (SGs) and work with them.

All pure SG functions are defined on the unit hypercube $[0,1]^d$.

Warning

The inference with this class is not tested and not recommended for use!

class SparseGrid(dim: int, max_level_sum: int)[source]

Bases: object

Each object of this class respresents a sparse grid. In this implementation, a sparse grid is a list of Smolnyak-subspaces. Each subspace is in principle a regular grid of a certain grid width but every second grid point is negelcted.

dim

The dimension of the sparse grid. This is the same as the dimension of the parameter space.

Type:

int

max_level_sum

The maximum sum of all levels of the subspaces. This is the same as the maximum level of the sparse grid.

Type:

int

subspace_list

A list of all subspaces that are part of the sparse grid.

Type:

list

levels2index

A dictionary that maps the level combination of a subspace to its index in the subspace_list.

Type:

dict

nSubspaces

The number of subspaces in the sparse grid.

Type:

int

n_points

The number of grid points in the sparse grid.

Type:

int

index_list4top_down_sparse_grid_traverse[

A list of indices that defines an ordering of subspaces where low-level subspaces come before high-level ones.

Type:

list

allPoints

A matrix of shape #Points x #Dims defining all grid points in the sparse grid.

Type:

np.ndarray

compute_all_points()[source]

Collect all SG points in one array by iterating over all subspaces.

compute_coefficients()[source]

When using sparse grids for function interpolation (and quadrature), this function computes the coefficients of all basis function of the whole sparse grid.

Args:

Returns:

compute_index_list4top_down_sparse_grid_traverse()[source]

Create an ordering of subspaces where low-level subspaces come before high-level ones.

compute_integral()[source]

Perform sparse grid integration over whole Sparse Grid using the computed coefficients (coeffs) and the volume of each basis function (basis_func_vol)

compute_n_points()[source]

Iterates over all subspaces of the sparse grid and accumulates the total number of gridpoints.

eval_function_sg(function: Callable)[source]

Evaluate the provided function for all subspaces of a sparse grid by using Subspace.eval_function

Parameters:

function (Callable) – The function that is to be evaluated. It must be possible to evaluate the function in a single sparse grid point.

refine_subspace(current_levels: ndarray, indexRefinedLevel: int) None[source]

Recursive function used to accumulate all subspaces up to a specified level sum in the form of a list It returns the list itself together with a dictionary that maps the level-combination of each subspace onto its index inside the list. This function only lists each subspace once.

Parameters:
  • current_levels (np.ndarray) – The level combination of the subspace that is currently being refined. Shape (dim,)

  • indexRefinedLevel (int) – The index of the level that was refined to form the current subspace.

class Subspace(levels: ndarray, SG: SparseGrid)[source]

Bases: object

Objects represent one Smolnyak-Subspace of a sparse grid and are only defined by a level for each dimension.

eval_function(function: Callable)[source]

Evaluate a function in all points of the respective subspace. This function is typically called by SparseGrid.eval_function_sg.

Parameters:

function (Callable) – The function that is to be evaluated. It must be possible to evaluate the function in a single sparse grid point.

pass_contributions2higher_levels()[source]

During sparse grid interpolation, this function passes contributions to all subspaces with higher level.

basis_1d(points1D: ndarray, centre1D: float64, level: int) ndarray[source]

Evaluate a 1D hat function in an array of doubles. The hat is centered around centre1D and the hat’s level defines its support. The support shrinks exponentially with growing level and a level of 0 is equivalent with full support on [0,1].

Parameters:
  • points1D (np.ndarray) – The points at which the hat function should be evaluated.

  • centre1D (np.double) – The centre of the hat function.

  • level (int) – The level of the hat function. The level defines the support of the hat function.

Returns:

The hat function evaluated at the given points.

Return type:

np.ndarray

basis_nd(points: ndarray, centre: ndarray, levels: ndarray) ndarray[source]

Use a tensor product to generalise the 1D basis function to arbitrarily high dimensions.

Parameters:
  • points (np.ndarray) – The points at which the basis function should be evaluated. Shape: (numPoints, numDims)

  • centre (np.ndarray) – The centre of the basis function. Shape: (numDims,)

  • levels (np.ndarray) – The levels of the basis function. Shape: (numDims,)

Returns:

The basis function evaluated at the given points. Shape: (numPoints,)

Return type:

np.ndarray

evaluate_on_sparse_grid(args: Tuple[ndarray, BaseModel, ndarray, DataTransformation, ndarray, ndarray]) ndarray[source]

This function is used to evaluate a function on a sparse grid in parallel. The input args is a tuple containing the function, the sparse grid and the number of processes to be used.

Parameters:
  • params (np.ndarray) – The parameters to be evaluated.

  • model (BaseModel) – The model used for the inference.

  • data (np.ndarray) – The data points used for the inference.

  • data_transformation (DataTransformation) – The data transformation used to normalize the data.

  • data_stdevs (np.ndarray) – The standard deviations of the data points. (Currently the kernel width, #TODO!)

  • slice (np.ndarray) – The slice defines for which dimensions of the grid points / paramater vectors the marginal density should be evaluated.

Returns:

The density values for the given params.

Return type:

np.ndarray

inference_sparse_grid(model: BaseModel, data: ndarray, data_transformation: DataTransformation, result_manager: ResultManager, slices: List[ndarray], num_processes: int, num_levels: int = 5) Tuple[Dict[str, ndarray], Dict[str, ndarray], Dict[str, ndarray], ResultManager][source]

Evaluates the transformed parameter density over a set of points resembling a sparse grid, thereby attempting parameter inference. If a data path is given, it is used to load the data for the model. Else, the default data path of the model is used.

Parameters:
  • model (BaseModel) – The model describing the mapping from parameters to data.

  • data (np.ndarray) – The data to be used for inference.

  • data_transformation (DataTransformation) – The data transformation used to normalize the data.

  • num_processes (int) – number of processes to use for parallel evaluation of the model.

  • num_levels (int, optional) – Maximum sparse grid level depth that mainly defines the number of points. Defaults to 5.

Returns:

The parameter samples, the corresponding simulation results, the corresponding density evaluations for each slice and the result manager used for the inference.

Return type:

Tuple[Dict[str, np.ndarray], Dict[str, np.ndarray], Dict[str, np.ndarray], ResultManager]

meshgrid2matrix(meshgrid: list) ndarray[source]

Convert a np.meshgrid into a np.2darray of grid points. The function is mainly used when assigning grid points to Smolnyak-Subspaces.

Parameters:

meshgrid (list) – A list of np.arrays returned by np.meshgrid

Returns:

A matrix of shape #Points x #Dims defining all grid points

Return type:

np.ndarray