otaf.sampling package

Module contents

class otaf.sampling.LagrangeConstraintSolver(dim, g, bounds, starting_points)[source]

Bases: object

Class to solve the problem of finding the subspace in Lambda where g(X) = 0, subject to the bounds for lambda_i.

dim

Dimensionality of the space (number of lambda_i variables).

Type:

int

g

Function g(X) that represents the constraint g(X) = 0.

Type:

callable

bounds

List of (min, max) tuples for each lambda_i.

Type:

list of tuples

starting_points

Initial points for the optimization, one per dimension.

Type:

np.ndarray

objective_function(X)[source]

The objective function to minimize. In this case, we’re simply trying to minimize the function g(X) to reach g(X) = 0.

Parameters:

X (np.ndarray) – Array of lambda_i values.

Returns:

The value of g(X).

Return type:

float

solve()[source]

Solves the optimization problem using SLSQP method to find the subspace where g(X) = 0.

Returns:

The result of the optimization, including

the point X where g(X) = 0.

Return type:

res (OptimizeResult)

class otaf.sampling.UniformSurfaceSampler(dim, g, bounds, starting_points, step_size=0.1)[source]

Bases: object

objective_function(X)[source]

Objective function to minimize, ensuring g(X) = 0.

sample_surface(num_samples)[source]

Generate a uniform grid of points on the surface where g(X) = 0.

Parameters:

num_samples (int) – Number of samples to generate.

Returns:

Array of sampled points that satisfy g(X) = 0.

Return type:

np.ndarray

solve(x0)[source]

Solves the optimization problem to find a new point close to x0 that still satisfies g(X) = 0.

Parameters:

x0 (np.ndarray) – Initial guess for optimization.

Returns:

Resulting point on the surface.

Return type:

res (OptimizeResult)

otaf.sampling.compose_defects_with_lambdas(lds, rdv)[source]

Multiplies each defect sample by the corresponding lambda sample.

For each point in the lambda sample, this function generates a new defect sample by multiplying it with the point from the lambda sample. The dimensions of the lambda sample and the defect sample must match.

Parameters:

ldsot.Sample

Lambda sample of size Nl.

rdvot.Sample

Random defects sample of size Nd.

Returns:

: list[ot.Sample]

List of size Nl, where each element is a sample of defects, each with size Nd.

otaf.sampling.condition_lambda_sample(sample, squared_sum=False)[source]

Condition lambda parameters to sum to 1 for each feature.

Parameters:
  • sample (ot.Sample) – Lambda parameters with each feature ending in an integer sequence.

  • squared_sum (bool, optional) – Whether to square the resulting values. Defaults to False.

Returns:

Conditioned lambda parameters.

Return type:

ot.Sample

Note

THis function can only be used if all the features are plane./ If thezy are cylindrical the constraints are aboluetly different. To use both u need to make a comlpex function that checks the feature type for each variable and uses the right constraint.

otaf.sampling.condition_sample_array(sample_array, feature_indices, squared_sum)[source]

Condition the sample array by normalizing the features.

Parameters:
  • sample_array (np.ndarray) – The input sample array.

  • feature_indices (list[int]) – Indices of the features.

  • squared_sum (bool) – Whether to square the resulting values.

Returns:

Conditioned sample array.

Return type:

np.ndarray

otaf.sampling.find_best_worst_quantile(parameters, results, quantile)[source]

Find the best and worst performing observations based on a given quantile.

This function calculates the best and worst performing observations from a given set of parameters and corresponding results, based on the specified quantile.

Parameters:
  • parameters (numpy.ndarray) – Array containing parameter values.

  • results (numpy.ndarray) – Array containing corresponding result values.

  • quantile (float) – Desired quantile value, must be in the range [0, 1].

Returns:

A tuple containing two tuples:
  • The first tuple contains the parameters and results of the best performing observations.

  • The second tuple contains the parameters and results of the worst performing observations.

Return type:

tuple

otaf.sampling.generateLHSExperiment(composed_distribution, N, SEED=999, T0=10, c=0.95, iMax=2000)[source]
otaf.sampling.generate_and_transform_sequence(dim, samplesize, target_distribution, sequence_type='halton')[source]

Generate a low-discrepancy sequence in the unit hypercube and map it to the target distribution space using the iso-probabilistic transformation.

Parameters: - dim: int, number of dimensions. - samplesize: int, number of samples. - target_distribution: openturns.Distribution, the target distribution to transform the sequence. - sequence_type: str, one of [‘sobol’, ‘halton’, ‘reverse_halton’, ‘faure’, ‘haselgrove’].

Returns: - transformed_sample: np.array, the transformed sample points in the target distribution space.

otaf.sampling.generate_imprecise_probabilistic_samples(subgroup_sizes, num_samples=None, seed=None, discretization=4)[source]

Generate a random subset of valid permutations representing the imprecise probabilistic contributions of random variables in a constrained space.

Each subgroup in subgroup_sizes represents a set of variables that contribute to a probabilistic model. The sum of the elements in each subgroup is constrained to 1, and the function samples random permutations from the imprecise space of possible contributions for these subgroups.

Parameters:

subgroup_sizeslist of int

A list where each entry corresponds to the size of a subgroup (the number of variables in that group). For each subgroup, the sum of its elements is constrained to 1.

num_samplesint, optional

The number of random samples to generate. If num_samples is -1, the function will compute all valid permutations. Otherwise, it will randomly sample from the space of possible contributions.

seedint, optional

A seed for the random number generator to ensure reproducibility of results.

discretizationint, optional

The number of steps to discretize the space between [0, 1]. Higher values allow for finer granularity in the space of possible contributions.

Returns:

: Generator

A generator yielding valid samples of probabilistic contributions for the subgroups, representing the imprecise space of random variables. Each sample is a 1D array where each element belongs to a specific subgroup, and the sum of the elements in each subgroup is 1.

Example:

>>> subgroup_sizes = [3, 4]
>>> num_samples = 5
>>> generate_imprecise_probabilistic_samples(subgroup_sizes, num_samples=num_samples, seed=42)
<generator object generate_imprecise_probabilistic_samples at 0x...>

Example usage with printing: >>> for sample in generate_imprecise_probabilistic_samples(subgroup_sizes, num_samples=num_samples, seed=42): >>> print(sample)

Notes:

  • This function is designed to explore the space of imprecise probabilistic contributions of random variables.

  • The generator produces samples lazily to reduce memory consumption, allowing it to handle larger models efficiently.

  • If num_samples is -1, the function will generate all possible permutations lazily, so it won’t store all permutations in memory.

  • The discretization parameter controls how finely the space between [0, 1] is divided for the contribution values in each subgroup. Higher values give finer precision but may increase computation time.

otaf.sampling.generate_random_permutations_with_sampling(subgroup_sizes, num_samples=None, seed=None)[source]

Generate a random subset of permutations with both positive and negative sign flips, based on the specified number of samples (num_samples), to mitigate the curse of dimensionality.

Each subgroup in subgroup_sizes represents a set of one-hot encoded vectors (e.g., for size 3: [1, 0, 0], [0, 1, 0], [0, 0, 1]). The function generates a random subset of valid permutations where each subgroup has exactly one non-zero element. Additionally, each non-zero element is randomly assigned either a +1 or -1 value.

Parameters:

subgroup_sizeslist of int

A list where each entry corresponds to the size of a subgroup (the number of possible one-hot vectors for that group). For example, [3, 4] would mean one group of size 3 and another group of size 4.

num_samplesint, optional

The number of random permutations to generate. If num_samples is not provided or exceeds the total number of possible permutations, the function will generate all possible permutations. Each permutation accounts for both structural configurations and sign flips (+1 or -1).

seedint, optional

A seed for the random number generator to ensure reproducibility of results.

Returns:

: numpy.ndarray

A 2D array where each row is a randomly generated permutation, and each column corresponds to an element in the concatenated vector of the one-hot encoded subgroups, including the applied sign flips (+1 or -1). The number of rows is either num_samples or the total number of possible permutations if num_samples exceeds the possible unique permutations.

Example:

>>> subgroup_sizes = [2, 2]
>>> num_samples = 4
>>> generate_random_permutations_with_sampling(subgroup_sizes, num_samples, seed=42)
array([[ 0,  1, -1,  0],
       [ 1,  0, -1,  0],
       [-1,  0,  0,  1],
       [ 0,  1,  0, -1]])

Notes:

  • The function addresses the curse of dimensionality by limiting the number of permutations to num_samples, if provided.

  • If num_samples exceeds the total number of possible permutations, the function generates all possible unique permutations.

  • The seed parameter ensures reproducibility for random sampling.

otaf.sampling.generate_scaled_permutations(subgroup_sizes, scaling_factors, num_samples=None, seed=None)[source]

Generate scaled random permutations with both positive and negative sign flips, based on the specified number of samples (num_samples). The resulting permutations are scaled by the given scaling factors.

Parameters:

subgroup_sizeslist of int

A list where each entry corresponds to the size of a subgroup (the number of possible one-hot vectors for that group). For example, [3, 4] would mean one group of size 3 and another group of size 4.

scaling_factorslist or numpy array of floats

A list or numpy array of the same length as the total number of elements in the permutation vector, where each entry corresponds to a scaling factor for the corresponding permutation element.

num_samplesint, optional

The number of random permutations to generate. If num_samples is not provided or exceeds the total number of possible permutations, the function will generate all possible permutations. Each permutation accounts for both structural configurations and sign flips (+1 or -1).

seedint, optional

A seed for the random number generator to ensure reproducibility of results.

Returns:

: numpy.ndarray

A 2D array where each row is a randomly generated permutation, and each column corresponds to an element in the concatenated vector of the one-hot encoded subgroups, scaled by the corresponding value in scaling_factors.

otaf.sampling.project_samples_to_hyperplane(a, b, samples, bounds=None)[source]

Project (N-1)-dimensional samples onto the N-dimensional hyperplane defined by the equation:

a_0 * x_0 + a_1 * x_1 + … + a_{N-1} * x_{N-1} = b

Parameters: a (list or np.ndarray): Coefficients of the hyperplane equation. b (float): Constant term in the hyperplane equation. samples (np.ndarray): Samples in (N-1)-dimensional space. bounds (list of tuples): Bounds for each dimension in the format [(min, max), …, (min, max)].

Returns: np.ndarray: Projected samples in N-dimensional space that satisfy the hyperplane equation.

otaf.sampling.scale_sample_with_params(sample, parameters)[source]

Scale a sample of shape (N, M) using mean and standard deviation parameters.

Each column of the sample corresponds to a component of a standard normal unit distribution. The input parameters define mean and standard deviation pairs for scaling each column.

Parameters:
  • sample (np.ndarray) – A 2D array of shape (N, M), where each column represents a random vector component from a standard normal distribution.

  • parameters (Union[np.ndarray, Sequence[float]]) – A 1D array or sequence of size 2 * M, where the elements alternate between mean and standard deviation for each column.

Returns:

A 2D array of shape (N, M), with each column scaled using the provided parameters.

Return type:

np.ndarray

Raises:
  • ValueError – If the sample is not a 2D array.

  • ValueError – If the parameters are not of size 2 * M.

otaf.sampling.validate_and_extract_indices(description)[source]

Validate the feature descriptions and extract feature indices.

Parameters:

description (list[str]) – List of feature descriptions.

Returns:

Extracted feature indices.

Return type:

list[int]

Raises:

ValueError – If the description is missing or invalid.