mcframework.core.MonteCarloFramework#
- class mcframework.core.MonteCarloFramework[source]#
Bases:
objectRegistry for named simulations that runs and compares results.
Orchestrates multiple
MonteCarloSimulationinstances.Examples
>>> from mcframework.core import MonteCarloFramework, MonteCarloSimulation >>> class MySim(MonteCarloSimulation): ... def single_simulation(self, _rng=None): ... rng = self._rng(_rng, self.rng) ... return float(rng.normal()) ... >>> sim1 = MySim(name="NormalSim") >>> sim2 = MySim(name="AnotherSim") >>> framework = MonteCarloFramework() >>> framework.register_simulation(sim1) >>> framework.register_simulation(sim2) >>> res1 = framework.run_simulation("NormalSim", 10000, backend="auto") >>> res2 = framework.run_simulation("AnotherSim", 10000) >>> comparison = framework.compare_results(["NormalSim", "AnotherSim"], metric="mean") >>> print(comparison) {'NormalSim': 0.01234, 'AnotherSim': -0.05678}
Methods
Compare a metric across previously run simulations.
Register a simulation instance under a name.
Run a registered simulation by name.
- compare_results(names: list[str], metric: str = 'mean') dict[str, float][source]#
Compare a metric across previously run simulations.
- Parameters:
- Returns:
dict{name: value}pairs.
- Raises:
ValueErrorIf a requested percentile was not part of the user’s requested set at run time (enforced via
result.metadata["requested_percentiles"]), or if the metric name is unknown.
- register_simulation(simulation: MonteCarloSimulation, name: str | None = None)[source]#
Register a simulation instance under a name.
- Parameters:
- simulation
MonteCarloSimulation The simulation instance to register.
- name
str, optional If omitted,
simulation.nameis used.
- simulation
- run_simulation(name: str, n_simulations: int, **kwargs) SimulationResult[source]#
Run a registered simulation by name.
- Parameters:
- name
str Key used in
register_simulation().- n_simulations
int Number of draws.
- **kwargs
Any Forwarded to
MonteCarloSimulation.run().
- name
- Returns:
- classmethod __new__(*args, **kwargs)#