To understand how to model risks with high variance, I developed a Python script using numpy and pandas to run a Monte Carlo simulation. Instead of relying on a single “average” prediction for future risk exposure (e.g., potential financial loss), the simulation generated 1,000 random iterations based on historical volatility.
- Key Concept Applied: The script used
norm.ppf(Percent Point Function) to generate random variables within a specified mean and standard deviation, effectively simulating “black swan” events and best/worst-case scenarios. - Outcome: The output provided a probability distribution rather than a single number. This allowed me to state with 95% confidence that the potential risk exposure would fall within a specific range, providing a far more defensible metric for stakeholders than a “High/Medium/Low” label.
Description: This script simulates 1,000 potential outcomes for a financial risk scenario (e.g., cost of a data breach) using historical volatility data. It calculates the 95% confidence interval (Value at Risk).
import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom scipy.stats import norm# --- CONFIGURATION ---# Scenario: Estimating potential financial loss from a supply chain disruption# Based on historical data, we assume a normal distribution of daily loss.simulations = 1000 # Number of iterationsdays_to_forecast = 30 # Duration of the risk eventavg_daily_loss = 5000 # Mean daily loss in GBPstd_dev_loss = 1500 # Volatility (Standard Deviation)# --- MONTE CARLO SIMULATION ---def run_simulation(): results = [] for i in range(simulations): # Generate random daily losses based on normal distribution # norm.ppf converts a random percentage (0-1) to a value on the distribution curve daily_losses = norm.ppf(np.random.rand(days_to_forecast), loc=avg_daily_loss, scale=std_dev_loss) # Cumulative sum of losses for this 30-day iteration total_event_cost = daily_losses.sum() results.append(total_event_cost) return np.array(results)# --- EXECUTION & ANALYSIS ---simulated_costs = run_simulation()# Calculate Key Metricsmean_cost = np.mean(simulated_costs)worst_case = np.percentile(simulated_costs, 95) # 95th percentile (Value at Risk)best_case = np.percentile(simulated_costs, 5) # 5th percentileprint(f"--- RISK FORECAST (30 DAYS) ---")print(f"Mean Expected Cost: £{mean_cost:,.2f}")print(f"95% Confidence Worst Case: £{worst_case:,.2f}")print(f"5% Confidence Best Case: £{best_case:,.2f}")# Optional: Visualization code would go here# plt.hist(simulated_costs, bins=50)
References:
Fizell, Z. (2022) How to Create a Monte Carlo Simulation using Python. Available at: https://towardsdatascience.com/how-to-create-a-monte-carlo-simulation-using-python-c24634a0978a/