Class MonteCarloSimulator
java.lang.Object
com.quantfinlib.simulation.MonteCarloSimulator
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).
simulate(double, double, double, int, int)andsimulatePortfolio(double, double[], double[], double[][], int, int)return terminal values for risk analytics (SimulationResult). Both sample the exact lognormal terminal distribution, so there is no time-discretisation error.simulatePaths(double[], double[], double[], double[][], double, int, int, boolean, MonteCarloSimulator.PathPayoff)generates correlated multi-asset GBM paths on a uniform grid with exact log-Euler steps and hands each path to a caller payoff (barriers, Asians, autocalls, baskets), with optional antithetic variates (Glasserman, Monte Carlo Methods in Financial Engineering, 2003, §4.2).
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 ClassesModifier and TypeClassDescriptionstatic final recordMonte Carlo estimate: mean, its standard error, and the number of independent samples.static interfacePayoff of one simulated path. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprice(double[] spots, double[] drifts, double[] vols, double[][] correlation, double maturity, int steps, int paths, boolean antithetic, double discountRate, MonteCarloSimulator.PathPayoff payoff) Discounted Monte Carlo pricee^{−rT}·E[payoff]with its standard error.simulate(double initialValue, double annualReturn, double annualVol, int horizonDays, int simulations) Single-asset GBM terminal values afterhorizonDaystrading 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) Simulatespathscorrelated 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.
-
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 afterhorizonDaystrading days (T = horizonDays/252):V_T = V_0·exp((μ − σ²/2)T + σ√T·Z).- Parameters:
initialValue- starting value, > 0annualReturn- annualized drift μ (soE[V_T] = V_0·e^{μT})annualVol- annualized volatility σ, ≥ 0horizonDays- trading days to simulate, ≥ 1simulations- 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 meandailyMeanLogReturnsand covariancedailyCovariance, so the terminal log return of asset i overddays isN(d·m_i, d·Σ_ii)and the portfolio is worthV_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) Simulatespathscorrelated GBM paths and returns the payoff of each. Withantithetic, paths come in pairs driven byZand−Z; entry2pand2p+1are a pair (pathsmust then be even).- Parameters:
spots- S_i(0) > 0drifts- b_i per year (r − q_i for pricing)vols- σ_i ≥ 0 per yearcorrelation- PSD correlation matrix (unit diagonal)maturity- T in years, > 0steps- 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 pricee^{−rT}·E[payoff]with its standard error. Under antithetic sampling the error is computed on the pair averages, which are the independent samples.
-