tolerancing.monte_carlo

Monte Carlo Module

This module contains the Monte Carlo class for performing Monte Carlo analysis on a tolerancing system. The Monte Carlo class is a subclass of the SensitivityAnalysis class and provides methods for running Monte Carlo simulations, visualizing the results, and analyzing the correlation between operands.

Kramer Harrison, 2024

Classes

MonteCarlo(tolerancing)

Standalone Monte Carlo analysis for a tolerancing system.

class MonteCarlo(tolerancing: Tolerancing)[source]

Standalone Monte Carlo analysis for a tolerancing system.

Unlike SensitivityAnalysis (which sweeps each perturbation in sequence), MonteCarlo applies all perturbations simultaneously from random samples on each iteration.

Parameters:

tolerancing – The tolerancing system to analyse.

tolerancing

The tolerancing system.

operand_names

List of operand names in the tolerancing system.

_results

DataFrame storing the Monte Carlo results.

get_results() pandas.DataFrame[source]

Return the Monte Carlo analysis results.

Returns:

The results of the Monte Carlo simulation.

Return type:

pd.DataFrame

run(num_iterations: int)[source]

Executes the Monte Carlo simulation for a specified number of iterations.

Parameters:

num_iterations (int) – The number of iterations to run the simulation.

Returns:

The results are stored in the instance variable _results

as a pandas DataFrame.

Return type:

None

The method performs the following steps for each iteration:
  1. Resets the tolerancing system.

  2. Applies perturbations to the system.

  3. Applies compensators to the system and stores the results.

  4. Evaluates the operands and stores their values.

  5. Saves the perturbation types and values, operand values, and

    compensator values in a dictionary.

  6. Appends the dictionary to the results list.

The final results are converted to a pandas DataFrame and stored in

the _results attribute.

view_cdf() tuple[Figure, NDArray[np.object_]][source]

Generates and displays a cumulative distribution function (CDF) plot of the data.

view_heatmap(figsize: tuple[float, float] = (8, 6), vmin: float | None = None, vmax: float | None = None) tuple[Figure, Axes][source]

Generates and displays a heatmap of the correlation matrix of the results.

Parameters:
  • figsize (tuple, optional) – A tuple representing the size of the figure (width, height). Default is (8, 6).

  • vmin (float, optional) – Minimum value for the color scale. Default is None, which uses the minimum value in the data.

  • vmax (float, optional) – Maximum value for the color scale. Default is None, which uses the maximum value in the data.

Returns:

A tuple containing the figure and axes of the heatmap.

Return type:

tuple

view_histogram(kde: bool = True) tuple[Figure, NDArray[np.object_]][source]

Displays a histogram of the data.

Parameters:

kde (bool) – If True, a Kernel Density Estimate (KDE) is plotted. Otherwise, a histogram is plotted.