Class MonteCarloSimulator

java.lang.Object
neqsim.process.util.optimizer.MonteCarloSimulator
All Implemented Interfaces:
Serializable

public class MonteCarloSimulator extends Object implements 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:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serialization version.
      See Also:
    • logger

      private static final org.apache.logging.log4j.Logger logger
      Logger object for class.
    • baseProcess

      private final ProcessSystem baseProcess
      Base process system to clone for each iteration.
    • iterations

      private int iterations
      Number of Monte Carlo iterations.
    • random

      private Random random
      Random number generator.
    • parameters

      private final List<MonteCarloSimulator.UncertainParameter> parameters
      Uncertain input parameters.
    • outputName

      private String outputName
      Output extractor name.
    • outputExtractor

      private transient Function<ProcessSystem, Double> outputExtractor
      Output value extractor function.
  • Constructor Details

    • MonteCarloSimulator

      public MonteCarloSimulator(ProcessSystem baseProcess, int iterations)
      Creates a Monte Carlo simulator.
      Parameters:
      baseProcess - the base process system to clone for each iteration
      iterations - number of Monte Carlo iterations (recommended 200+ for NeqSim)
  • Method Details

    • setSeed

      public MonteCarloSimulator setSeed(long seed)
      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 value
      mode - most likely value
      high - maximum value
      applier - 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 name
      low - minimum value
      high - maximum value
      applier - 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 name
      extractor - 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

      private double sampleValue(MonteCarloSimulator.UncertainParameter param)
      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 value
      mode - most likely value
      high - maximum value
      Returns:
      sampled value
    • computeTornado

      private List<MonteCarloSimulator.TornadoEntry> 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

      private double runWithSingleParamOverride(int paramIndex, double overrideValue) throws Exception
      Runs with one parameter overridden and all others at their mode values.
      Parameters:
      paramIndex - index of the parameter to override
      overrideValue - value to use for the overridden parameter
      Returns:
      output value
      Throws:
      Exception - if simulation fails