src.pydasa.analysis.simulation#
Module for Monte Carlo Simulation execution and analysis in PyDASA.
This module provides the MonteCarlo class for performing Monte Carlo simulations on dimensionless coefficients derived from dimensional analysis.
Classes:
MonteCarlo: Performs Monte Carlo simulations on dimensionless coefficients.
IMPORTANT: Based on the theory from:
# H.Gorter, Dimensionalanalyse: Eine Theoririe der physikalischen Dimensionen mit Anwendungen
Classes#
MonteCarlo class for stochastic analysis in PyDASA. |
Module Contents#
- class src.pydasa.analysis.simulation.MonteCarlo#
Bases:
pydasa.core.basic.Foundation,pydasa.elements.specs.numerical.BoundsSpecsMonteCarlo class for stochastic analysis in PyDASA.
Performs Monte Carlo simulations on dimensionless coefficients to analyze the coefficient’s distribution and sensitivity to input parameter variations.
- Parameters:
Foundation – Foundation class for validation of symbols and frameworks.
BoundsSpecs – Value bounds for statistical properties (mean, median, dev, min, max).
- # Core Identification
- # Coefficient and Expression Management
- _coefficient#
Coefficient for the simulation.
- Type:
Optional[Coefficient]
- _sym_func#
Sympy function of the simulation.
- Type:
Callable
- _exe_func#
Executable function for numerical evaluation.
- Type:
Callable
- # Variable Management
- # Simulation Configuration
- # Results and Input data
- _results#
Raw simulation results.
- Type:
Optional[np.ndarray]
- # Statistics (inherited from BoundsSpecs
_mean, _median, _dev, _min, _max)
- _coefficient: pydasa.dimensional.buckingham.Coefficient#
Coefficient for the simulation.
- _sym_func: sympy.Expr | sympy.Basic | None = None#
Sympy expression object for the coefficient (Mul, Add, Pow, Symbol, etc.).
- _exe_func: Callable[Ellipsis, float | numpy.ndarray] | None = None#
Compiled executable function for evaluation of the coefficient.
- _variables: Dict[str, pydasa.elements.parameter.Variable]#
Dictionary of variables in the expression.
- _distributions: Dict[str, Dict[str, Any]]#
Variable sampling distributions and specifications that includes: - ‘dtype’: Distribution type name. - ‘params’: Distribution parameters (mean, std_dev, etc.). - ‘func’: Function for sampling, usually in Lambda format. - ‘depends’: List of variables this variable depends on.
- _simul_cache: Dict[str, numpy.typing.NDArray[numpy.float64]]#
Working sampled values during each simulation iteration. Memory cache.
- _data: Dict[str, numpy.typing.NDArray[numpy.float64]]#
Input data dictionary mapping variable names to their value arrays.
- _results: numpy.typing.NDArray[numpy.float64]#
Raw simulation results.
- _statistics: Dict[str, float | int | None] | None = None#
Statistical summary of the Monte Carlo simulation results.
- _validate_dist(value, field_name)#
_validate_dist() Custom validator to ensure all distributions have callable ‘func’.
- Parameters:
- Raises:
ValueError – If distributions don’t have callable ‘func’ functions.
- Return type:
None
- _validate_readiness()#
_validate_readiness() Checks if the simulation can be performed.
- Raises:
ValueError – If the simulation is not ready due to missing variables, executable function, or invalid number of iterations.
- Return type:
None
- set_coefficient(coef)#
set_coefficient() Configure analysis from a coefficient.
- Parameters:
coef (Coefficient) – Dimensionless coefficient to analyze.
- Raises:
ValueError – If the coefficient doesn’t have a valid expression.
- Return type:
None
- _collect_dataset(vars)#
_collect_dataset() Consolidate dataset from all variables for ‘DATA’ simulation mode.
- Parameters:
vars (Dict[str, Variable]) – Dictionary of variables to collect data from.
- Returns:
Dictionary mapping variable symbols to their data arrays.
- Return type:
Dict[str, NDArray[np.float64]]
- Raises:
ValueError – If variables have no data.
ValueError – If variables have inconsistent lengths.
- _generate_dataset(vars)#
_generate_dataset() Generate dataset by sampling from variable distributions for ‘DIST’ mode.
- Parameters:
vars (Dict[str, Variable]) – Dictionary of variables to generate samples for.
- Returns:
Dictionary mapping variable symbols to sampled arrays.
- Return type:
Dict[str, NDArray[np.float64]]
- Raises:
ValueError – If sampling fails or encounters errors.
- _parse_expression(expr)#
_parse_expression() Parse the LaTeX expression into a sympy function.
- Parameters:
expr (str) – LaTeX expression to parse.
- Raises:
ValueError – If the expression cannot be parsed.
- Return type:
None
- _generate_sample(var, memory)#
_generate_sample() Generate a sample for a given variable.
- run(iters=None, mode=SimulationMode.DIST)#
run() Execute the Monte Carlo simulation.
- Parameters:
iters (int, optional) – Number of iterations to run. If None, uses _experiments.
mode (str | SimulationMode, optional) – Simulation mode. Either ‘DIST’ to sample from distributions, or ‘DATA’ to use pre-existing Variable._data. Defaults to ‘DIST’.
- Raises:
ValueError – If simulation is not ready or encounters errors during execution.
- Return type:
None
- _reset_memory()#
_reset_memory() Reset results and inputs arrays.
- Return type:
None
- _reset_statistics()#
_reset_statistics() Reset all statistical attributes to default values.
- Return type:
None
- _calculate_statistics()#
_calculate_statistics() Calculate statistical properties of simulation results.
- Return type:
None
- get_confidence_interval(conf=0.95)#
get_confidence_interval() Calculate the confidence interval.
- Parameters:
conf (float, optional) – Confidence level for the interval. Defaults to 0.95.
- Raises:
ValueError – If no results are available or if the confidence level is invalid.
- Returns:
Lower and upper bounds of the confidence interval.
- Return type:
- _validate_cache_locations(var_syms, idx)#
_validate_cache_locations() Check if cache locations are valid for variable(s) at the iteration.
- _get_cached_value(var_sym, idx)#
_get_cached_value() Retrieve cached value for variable at the iteration.
- _set_cached_value(var_sym, idx, val)#
_set_cached_value() Store value in cache for variable at the iteration.
- Parameters:
- Raises:
ValueError – If cache location is invalid.
- Return type:
None
- extract_results()#
extract_results() Extract simulation results.
- Returns:
Dictionary containing simulation results.
- Return type:
Dict[str, NDArray[np.float64]]
- property variables: Dict[str, pydasa.elements.parameter.Variable]#
variables Get the variables involved in the simulation.
- property coefficient: pydasa.dimensional.buckingham.Coefficient | None#
coefficient Get the coefficient associated with the simulation.
- Returns:
The associated Coefficient object, or None.
- Return type:
Optional[Coefficient]
- property results: numpy.typing.NDArray[numpy.float64]#
results Raw simulation results.
- Returns:
Copy of the simulation results.
- Return type:
NDArray[np.float64]
- Raises:
ValueError – If no results are available.
- property data: Dict[str, numpy.typing.NDArray[numpy.float64]]#
data Get the input data dictionary.
- Returns:
Copy of input data mapping variable names to arrays.
- Return type:
Dict[str, NDArray[np.float64]]
- Raises:
ValueError – If no data is available.
- property statistics: Dict[str, float | int | None]#
statistics Get the statistical analysis of simulation results.
- Raises:
ValueError – If no results are available.
- Returns:
Dictionary containing statistical properties.
- Return type:
- property experiments: int#
experiments Number of simulation experiments.
- Returns:
Current number of experiments.
- Return type:
- property cat: str#
mode Get simulation execution category.
- Returns:
Current simulation category (DIST or DATA).
- Return type:
- property count: int#
count Number of valid simulation results.
- Returns:
Result count.
- Return type:
- Raises:
ValueError – If no results are available.
- property summary: Dict[str, float | int | None]#
summary Get the statistical analysis of simulation results.
- clear()#
clear() Reset all attributes to default values.
- Return type:
None