← Back to Financial Concepts
Quantitative Finance

Monte Carlo Portfolio Simulation

Monte Carlo simulation uses random sampling to model uncertain outcomes. In finance, it simulates thousands of possible price paths to estimate the probability distribution of portfolio returns. It's essential for risk management—VaR, stress testing, and retirement planning all rely on it.

Median Final
$0
10th Percentile
$0
90th Percentile
$0
Probability > 2x
0%

The Mathematics

Geometric Brownian Motion (GBM):
dS = μSdt + σSdW

Discrete form (monthly):
S(t+dt) = S(t) × exp[(μ - σ²/2)dt + σ√dt × Z]

Where Z ~ N(0,1) is a random shock each period.

How It Works

// Generate one price path
function simulatePath(initial, mu, sigma, years) {
    const steps = years * 12;
    const dt = 1 / 12;
    const path = [initial];
    let price = initial;
    
    for (let t = 0; t < steps; t++) {
        // GBM discrete formula
        const drift = (mu - 0.5 * sigma * sigma) * dt;
        const shock = sigma * Math.sqrt(dt) * randn();
        price = price * Math.exp(drift + shock);
        path.push(price);
    }
    return path;
}

// Run 1000 simulations
function runMonteCarlo(initial, mu, sigma, years) {
    const simulations = 1000;
    const paths = [];
    
    for (let i = 0; i < simulations; i++) {
        paths.push(simulatePath(initial, mu, sigma, years));
    }
    
    // Extract final values and sort
    const finals = paths.map(p => p[p.length-1]).sort(compare);
    
    // Return percentiles
    return {
        p10:  finals[100],
        p50:  finals[500],
        p90:  finals[900],
        prob2x: finals.filter(x => x > initial*2).length / simulations
    };
}