Class MonteCarloSimulator

java.lang.Object
com.quantfinlib.simulation.MonteCarloSimulator

public final class MonteCarloSimulator extends Object
Monte Carlo simulation of geometric Brownian motion, single- and multi-asset, in parallel; results are deterministic for a given seed regardless of thread scheduling (each scenario has its own SplittableRandom stream derived from the seed).

Model per asset i: dS_i/S_i = b_i dt + σ_i dW_i, d⟨W_i, W_j⟩ = ρ_ij dt. For pricing pass the risk-neutral drift b_i = r − q_i and discount the payoff yourself (or use price(double[], double[], double[], double[][], double, int, int, boolean, double, MonteCarloSimulator.PathPayoff)); for risk pass the real-world drift.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Monte Carlo estimate: mean, its standard error, and the number of independent samples.
    static interface 
    Payoff of one simulated path.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    price(double[] spots, double[] drifts, double[] vols, double[][] correlation, double maturity, int steps, int paths, boolean antithetic, double discountRate, MonteCarloSimulator.PathPayoff payoff)
    Discounted Monte Carlo price e^{−rT}·E[payoff] with its standard error.
    simulate(double initialValue, double annualReturn, double annualVol, int horizonDays, int simulations)
    Single-asset GBM terminal values after horizonDays trading days (T = horizonDays/252): V_T = V_0·exp((μ − σ²/2)T + σ√T·Z).
    double[]
    simulatePaths(double[] spots, double[] drifts, double[] vols, double[][] correlation, double maturity, int steps, int paths, boolean antithetic, MonteCarloSimulator.PathPayoff payoff)
    Simulates paths correlated GBM paths and returns the payoff of each.
    simulatePortfolio(double initialValue, double[] weights, double[] dailyMeanLogReturns, double[][] dailyCovariance, int horizonDays, int simulations)
    Buy-and-hold multi-asset portfolio.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MonteCarloSimulator

      public MonteCarloSimulator()
    • MonteCarloSimulator

      public MonteCarloSimulator(long seed)
  • Method Details

    • simulate

      public SimulationResult simulate(double initialValue, double annualReturn, double annualVol, int horizonDays, int simulations)
      Single-asset GBM terminal values after horizonDays trading days (T = horizonDays/252): V_T = V_0·exp((μ − σ²/2)T + σ√T·Z).
      Parameters:
      initialValue - starting value, > 0
      annualReturn - annualized drift μ (so E[V_T] = V_0·e^{μT})
      annualVol - annualized volatility σ, ≥ 0
      horizonDays - trading days to simulate, ≥ 1
      simulations - number of scenarios, ≥ 1
    • simulatePortfolio

      public SimulationResult simulatePortfolio(double initialValue, double[] weights, double[] dailyMeanLogReturns, double[][] dailyCovariance, int horizonDays, int simulations)
      Buy-and-hold multi-asset portfolio. Daily log returns are jointly normal with mean dailyMeanLogReturns and covariance dailyCovariance, so the terminal log return of asset i over d days is N(d·m_i, d·Σ_ii) and the portfolio is worth V_0·Σ w_i·exp(X_i). Long-only weights keep the value positive; short weights can drive it below zero, and it is not clamped (there is no absorbing bankruptcy state in GBM).
      Parameters:
      weights - initial value weights (need not sum to 1)
      dailyCovariance - PSD covariance of daily log returns
    • simulatePaths

      public double[] simulatePaths(double[] spots, double[] drifts, double[] vols, double[][] correlation, double maturity, int steps, int paths, boolean antithetic, MonteCarloSimulator.PathPayoff payoff)
      Simulates paths correlated GBM paths and returns the payoff of each. With antithetic, paths come in pairs driven by Z and −Z; entry 2p and 2p+1 are a pair (paths must then be even).
      Parameters:
      spots - S_i(0) > 0
      drifts - b_i per year (r − q_i for pricing)
      vols - σ_i ≥ 0 per year
      correlation - PSD correlation matrix (unit diagonal)
      maturity - T in years, > 0
      steps - grid points after spot, ≥ 1 (Δt = T/steps)
    • price

      public MonteCarloSimulator.Estimate price(double[] spots, double[] drifts, double[] vols, double[][] correlation, double maturity, int steps, int paths, boolean antithetic, double discountRate, MonteCarloSimulator.PathPayoff payoff)
      Discounted Monte Carlo price e^{−rT}·E[payoff] with its standard error. Under antithetic sampling the error is computed on the pair averages, which are the independent samples.