PyMCSL basic classes
SubSimulationEnv class
- class pymcsl.SubSimulationEnv(variables: List[Tuple[str, type, object]], begin_function: Callable[[subsimulation.ContextType], None], step_function: Callable[[subsimulation.ContextType, int], None])
The SubSimulationEnv class has a basic framework to simulate a stochastic process. The subsimulation environment has a set of variables (each with a name, a type, and a default value), a callback function to start the simulation, and a callback function to run the simulation steps. The history of variable states is stored in the environment after the simulation.
- Parameters
variables (List[Tuple[str, type, object]]) – List of simulation variables in the format [(variable_name, variable_type, default_value)].
begin_function (Callable[[ContextType], None]) – Function to prepare the simulation context before the first step.
step_function (Callable[[ContextType, int], None]) – Function to run the simulation steps.
- property auxiliary_objects: Dict[str, Any]
Returns a dictionary with all the auxiliary objects.
- Returns
Dictionary in the format {attribute_name: object}.
- Return type
Dict[str, Any]
- get_history() Dict[str, List]
Returns a copy of the historic dictionary.
- Returns
historic dictionary in the format {variable_name: variable_history}.
- Return type
Dict[str, List]
- get_history_dataframe() pandas.core.frame.DataFrame
Get variables history as a Pandas DataFrame.
- Returns
historic DataFrame.
- Return type
DataFrame
- get_numpy_history() Dict[str, numpy.ndarray]
Get variables history as a dictionary of NumPy arrays.
- Returns
variables history in the format {variable_name: variable_history}.
- Return type
Dict[str, np.ndarray]
- get_variable_history(var_name: str) List
Get a copy of the historic of a specific variable.
- Parameters
var_name (str) – variable name.
- Returns
variable state history.
- Return type
List
- get_variable_numpy_history(var_name: str) numpy.ndarray
Get the historic of a specific variable as a NumPy array.
- Parameters
var_name (str) – variable’s name.
- Returns
variable state history.
- Return type
np.ndarray
- get_variable_state(var_name: str) Any
Returns the state of a specific variable.
- Parameters
var_name (str) – Variable name.
- Returns
Variable state.
- Return type
Any
- get_variable_type(var_name: str) type
Returns the type of a specific variable.
- Parameters
var_name (str) – Variable name.
- Returns
Variable type.
- Return type
type
- run_steps(n: int)
Run ‘n’ steps of the simulation.
- Parameters
n (int) – Number of steps.
- property variables_names: List[str]
- Returns
List with all variables names.
- Return type
List[str]
- property variables_states: Dict[str, Any]
Returns a dictionary with all variables states.
- Returns
Dictionary in the format {variable_name: variable_state}.
- Return type
Dict[str, Any]
- property variables_types: Dict[str, type]
Returns a dictionary with all variables types.
- Returns
Dictionary in the format {variable_name: variable_type}.
- Return type
Dict[str, type]
MonteCarloSimulationEnv class
- class pymcsl.MonteCarloSimulationEnv(variables: List[Tuple[str, type, Union[str, int, float, bool]]], n_subsimulations: int, n_steps: int)
The MonteCarloSimulationEnv class provides a code base to facilitate the implementation of Monte Carlo simulations. The MonteCarloSimulationEnv class performs a series of independent subsimulations under the same conditions.
- Parameters
variables (List[Tuple[str, type, Union[str, int, float, bool]]]) – List of simulation variables in the format [(variable_name, variable_type, default_value)].
n_subsimulations (int) – Number of subsimulations.
n_steps (int) – Number of steps per subsimulation.
- get_subsim_env(subsim_index: int) subsimulation.SubSimulationEnv
Returns the SubSimulationEnv for a specific subsimulation.
- Parameters
subsim_index (int) – subsimulation index (starting at 0).
- Returns
SubSimulationEnv object.
- Return type
- get_variable_histogram(var_name: str, n_bins: int, density: bool = False, _range: Optional[Tuple[float, float]] = None) numpy.ndarray
Returns an array with a histogram of the variable for each step of the simulation. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
n_bins (int) – Number of bins per histogram.
density (bool, optional) – Set to true so histograms are density instead of counts. defaults to False
_range (Union[Tuple[float, float], None], optional) – defines manually the range (min, max) of the histogram. Default to None.
- Returns
Array of histograms.
- Return type
np.ndarray
- get_variable_histories(var_name: str) numpy.ndarray
Returns an array with all the outcomes that a variable had throughout the simulation. The 0-axis indices are the subsimulations and the 1-axis indices are the steps.
- Parameters
var_name (str) – Variable name.
- Returns
Array with the outcomes of the variable.
- Return type
np.ndarray
- get_variable_max(var_name: str, domain: str = 'step') Union[numpy.ndarray, float]
Calculates the maximum of a variable. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
domain (str, optional) – If domain=’step’, a maximum for each step is calculated; if domain=’subsim’, a maximum for each subsimulation is calculated, and if domain=None, the overall maximum is calculated, defaults to ‘step’
- Returns
An array with a maximum for each domain value (step or subsim), or an overall maximum.
- Return type
Union[np.ndarray, np.float]
- get_variable_mean(var_name: str, domain: str = 'step') Union[numpy.ndarray, float]
Calculates the mean of a variable. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
domain (str, optional) – If domain=’step’, an average for each step is calculated; if domain=’subsim’, an average for each subsimulation is calculated, and if domain=None, the overall average is calculated, defaults to ‘time’
- Returns
An array with an average for each domain value (step or subsim), or an overall average.
- Return type
Union[np.ndarray, np.float]
- get_variable_median(var_name: str, domain: str = 'step') Union[numpy.ndarray, float]
Calculates the median of a variable. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
domain (str, optional) – If domain=’step’, a median for each step is calculated; if domain=’subsim’, a median for each subsimulation is calculated, and if domain=None, the overall median is calculated, defaults to ‘time’
- Returns
An array with a median for each domain value (step or subsim), or an overall median.
- Return type
Union[np.ndarray, np.float]
- get_variable_min(var_name: str, domain: str = 'step') Union[numpy.ndarray, float]
Calculates the minimum of a variable. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
domain (str, optional) – If domain=’step’, a minimum for each step is calculated; if domain=’subsim’, a minimum for each subsimulation is calculated, and if domain=None, the overall minimum is calculated, defaults to ‘step’
- Returns
An array with a minimum for each domain value (step or subsim), or an overall minimum.
- Return type
Union[np.ndarray, np.float]
- get_variable_std(var_name: str, domain: str = 'step') Union[numpy.ndarray, float]
Calculates the standard deviation of a variable. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
domain (str, optional) – If domain=’step’, a standard deviation for each step is calculated; if domain=’subsim’, a standard deviation for each subsimulation is calculated, and if domain=None, the overall standard deviation is calculated, defaults to ‘step’
- Returns
An array with a standard deviation for each domain value (step or subsim), or an overall standard deviation.
- Return type
Union[np.ndarray, np.float]
- get_variable_sum(var_name: str, domain: str = 'step') Union[numpy.ndarray, float]
Calculates the sum of a variable. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
domain (str, optional) – If domain=’step’, a sum for each step is calculated; if domain=’subsim’, a sum for each subsimulation is calculated, and if domain=None, the overall sum is calculated, defaults to ‘step’
- Returns
An array with a sum for each domain value (step or subsim), or an overall sum.
- Return type
Union[np.ndarray, np.float]
- get_variable_var(var_name: str, domain: str = 'step') Union[numpy.ndarray, float]
Calculates the variance of a variable. The 0-axis indexes are the domain values (step indexes or subsim indexes).
- Parameters
var_name (str) – Variable name.
domain (str, optional) – If domain=’step’, a variance for each step is calculated; if domain=’subsim’, a variance for each subsimulation is calculated, and if domain=None, the overall variance is calculated, defaults to ‘step’
- Returns
An array with a variance for each domain value (step or subsim), or an overall variance.
- Return type
Union[np.ndarray, np.float]
- run(show_progress: bool = True)
Run all the independent subsimulations.
- Parameters
show_progress (bool, optional) – Enable progress bar, defaults to True
- set_subsim_begin_callback(f: Callable[[subsimulation.ContextType], None])
Subscribes a function as the begin-function of all subsimulations.
- Parameters
f (Callable[[ContextType], None]) – Callback function.
- set_subsim_step_callback(f: Callable[[subsimulation.ContextType], None])
Subscribes a function as the step-function of all subsimulations.
- Parameters
f (Callable[[ContextType], None]) – Callback function.
- property subsim_begin: Callable
Returns a decorator that subscribes a function as the beginning function of all subsimulations.
- Returns
Wrapped decorator.
- Return type
Callable
- property subsim_step: Callable
Returns a decorator that subscribes a function as the step-function of all subsimulations.
- Returns
Wrapped decorator.
- Return type
Callable