Class MonteCarloSimulator
java.lang.Object
neqsim.process.util.optimizer.MonteCarloSimulator
- All Implemented Interfaces:
Serializable
Monte Carlo uncertainty analysis for process simulations.
Runs N iterations of a process simulation with randomly sampled input parameters drawn from triangular distributions. Computes P10/P50/P90 percentiles, mean, standard deviation, and probability of exceeding thresholds. Also generates tornado sensitivity data by varying each parameter individually between its low and high bounds.
Usage:
ProcessSystem process = ...;
process.run();
MonteCarloSimulator mc = new MonteCarloSimulator(process, 200);
mc.addTriangularParameter("Gas Price", 0.8, 1.5, 2.5,
(p, v) -> { /* apply gas price to p */ });
mc.addTriangularParameter("CAPEX multiplier", 0.85, 1.0, 1.4,
(p, v) -> { /* apply capex multiplier to p */ });
mc.setOutputExtractor("NPV (MNOK)",
p -> calculateNPV(p));
MonteCarloResult result = mc.run();
System.out.println("P10=" + result.getP10());
System.out.println("P50=" + result.getP50());
System.out.println("P90=" + result.getP90());
System.out.println(result.toJson());
- Version:
- 1.0
- Author:
- Even Solbraa
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classResults of a Monte Carlo simulation.static interfaceFunctional interface for applying a parameter value to a process system.static classA tornado sensitivity entry.private static classAn uncertain input parameter. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ProcessSystemBase process system to clone for each iteration.private intNumber of Monte Carlo iterations.private static final org.apache.logging.log4j.LoggerLogger object for class.private Function<ProcessSystem, Double> Output value extractor function.private StringOutput extractor name.private final List<MonteCarloSimulator.UncertainParameter> Uncertain input parameters.private RandomRandom number generator.private static final longSerialization version. -
Constructor Summary
ConstructorsConstructorDescriptionMonteCarloSimulator(ProcessSystem baseProcess, int iterations) Creates a Monte Carlo simulator. -
Method Summary
Modifier and TypeMethodDescriptionaddTriangularParameter(String name, double low, double mode, double high, MonteCarloSimulator.ParameterApplier applier) Adds an uncertain parameter with a triangular distribution.addUniformParameter(String name, double low, double high, MonteCarloSimulator.ParameterApplier applier) Adds an uncertain parameter with a uniform distribution.private List<MonteCarloSimulator.TornadoEntry> Computes tornado sensitivity by varying each parameter low/high while holding others at mode.run()Runs the Monte Carlo simulation.private doublerunWithSingleParamOverride(int paramIndex, double overrideValue) Runs with one parameter overridden and all others at their mode values.private doublesampleTriangular(double low, double mode, double high) Samples from a triangular distribution.private doubleSamples a value from the parameter's distribution.setOutputExtractor(String name, Function<ProcessSystem, Double> extractor) Sets the output variable to track.setSeed(long seed) Sets the random seed for reproducibility.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version.- See Also:
-
logger
private static final org.apache.logging.log4j.Logger loggerLogger object for class. -
baseProcess
Base process system to clone for each iteration. -
iterations
private int iterationsNumber of Monte Carlo iterations. -
random
Random number generator. -
parameters
Uncertain input parameters. -
outputName
Output extractor name. -
outputExtractor
Output value extractor function.
-
-
Constructor Details
-
MonteCarloSimulator
Creates a Monte Carlo simulator.- Parameters:
baseProcess- the base process system to clone for each iterationiterations- number of Monte Carlo iterations (recommended 200+ for NeqSim)
-
-
Method Details
-
setSeed
Sets the random seed for reproducibility.- Parameters:
seed- random seed- Returns:
- this for chaining
-
addTriangularParameter
public MonteCarloSimulator addTriangularParameter(String name, double low, double mode, double high, MonteCarloSimulator.ParameterApplier applier) Adds an uncertain parameter with a triangular distribution.- Parameters:
name- descriptive name (e.g., "Gas Price (NOK/Sm3)")low- minimum valuemode- most likely valuehigh- maximum valueapplier- function to apply the sampled value to a process system- Returns:
- this for chaining
-
addUniformParameter
public MonteCarloSimulator addUniformParameter(String name, double low, double high, MonteCarloSimulator.ParameterApplier applier) Adds an uncertain parameter with a uniform distribution.- Parameters:
name- descriptive namelow- minimum valuehigh- maximum valueapplier- function to apply the sampled value- Returns:
- this for chaining
-
setOutputExtractor
public MonteCarloSimulator setOutputExtractor(String name, Function<ProcessSystem, Double> extractor) Sets the output variable to track.- Parameters:
name- output variable nameextractor- function that extracts the output value from a run process system- Returns:
- this for chaining
-
run
Runs the Monte Carlo simulation.- Returns:
- results with P10/P50/P90, tornado data, and all sampled values
-
sampleValue
Samples a value from the parameter's distribution.- Parameters:
param- the parameter to sample- Returns:
- sampled value
-
sampleTriangular
private double sampleTriangular(double low, double mode, double high) Samples from a triangular distribution.- Parameters:
low- minimum valuemode- most likely valuehigh- maximum value- Returns:
- sampled value
-
computeTornado
Computes tornado sensitivity by varying each parameter low/high while holding others at mode.- Returns:
- list of tornado entries sorted by swing (descending)
-
runWithSingleParamOverride
Runs with one parameter overridden and all others at their mode values.- Parameters:
paramIndex- index of the parameter to overrideoverrideValue- value to use for the overridden parameter- Returns:
- output value
- Throws:
Exception- if simulation fails
-