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)

Class for performing Monte Carlo analysis on a tolerancing system.

class MonteCarlo(tolerancing: Tolerancing)[source]

Class for performing Monte Carlo analysis on a tolerancing system.

Parameters:

tolerancing (Tolerancing) – The tolerancing system to perform Monte Carlo analysis on.

tolerancing

The tolerancing system to perform Monte Carlo analysis on.

Type:

Tolerancing

operand_names

List of operand names in the tolerancing system.

Type:

list

_results

DataFrame to store the Monte Carlo analysis results.

Type:

pd.DataFrame

run(num_iterations)[source]

Runs the Monte Carlo analysis for num_iterations.

get_results()

Returns the Monte Carlo analysis results.

view_histogram(kde=True)[source]

Visualizes the Monte Carlo analysis results as histograms.

view_cdf()[source]

Visualizes the Monte Carlo analysis results as cumulative distribution functions.

view_heatmap(figsize=(8, 6))[source]

Visualizes the correlation between operands in the Monte Carlo analysis results as a heatmap.

get_results()

Returns the results of the sensitivity analysis.

Returns:

The results of the sensitivity analysis.

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(figsize: tuple[float, float] = (2.5, 3.3), sharex: Literal['none', 'all', 'row', 'col'] | bool = 'col', sharey: Literal['none', 'all', 'row', 'col'] | bool = 'row') tuple[Figure, list[Axes]]

Visualizes the sensitivity analysis results.

Parameters:
  • figsize (tuple, optional) – The size of the figure in inches for each subplot. Default is (2.2, 3).

  • sharex (str, optional) – Specifies how the x-axis is shared among subplots. Default is ‘col’.

  • sharey (str, optional) – Specifies how the y-axis is shared among subplots. Default is ‘row’.

Returns:

A tuple containing the figure and axes of the plot.

Return type:

tuple

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.