CSC317 Simulation and Modeling

Simulation and ModelingUnit 712 min read

Monte Carlo Simulation: Random Sampling, Probability & Estimation

Unit 7 of Simulation and Modeling: Covers the core principles of Monte Carlo simulation—random sampling, probability distributions, convergence, and applications in estimation, optimization, and risk analysis—with worked examples, real-world ties to Nepalese tech (e.g., NEPSE stock modeling), and exam-focused visuals.

TAKEAWAYS:

  • Monte Carlo uses random sampling to estimate outcomes for complex systems where deterministic methods fail (e.g., NEPSE stock price simulations).
  • The Law of Large Numbers ensures convergence: more samples → more accurate results (e.g., Pathao’s delivery time estimates).
  • Probability distributions (uniform, normal, exponential) drive randomness in simulations (e.g., Khalti’s fraud detection via random transaction sampling).
  • Variance reduction techniques (e.g., stratified sampling) improve efficiency (e.g., Daraz’s inventory optimization).
  • Applications span finance (loan risk), logistics (traffic routing), and AI (neural network training).
  • Exam focus: Phases of Monte Carlo (modeling → sampling → analysis), advantages/disadvantages, and real-world traces (e.g., simulating Ncell’s call drop rates).

1. What is Monte Carlo Simulation?

Monte Carlo simulation is a statistical method that uses random sampling to model uncertainty in systems where exact solutions are impractical. It leverages probability distributions to generate possible outcomes and estimates results by averaging over many trials.

Key Idea: Randomness as a Tool

Unlike deterministic models (e.g., physics equations), Monte Carlo embraces stochasticity (randomness) to explore all possible scenarios. For example:

  • NEPSE stock prices: Instead of predicting a single future price, Monte Carlo simulates thousands of possible prices based on historical volatility.
  • Pathao delivery times: Randomly samples traffic delays, rider speeds, and order volumes to estimate average delivery times.

graph TD
    A["Start: Define Problem"] --> B["Step 1: Define Probability Distributions"]
    B --> C["Step 2: Generate Random Samples"]
    C --> D["Step 3: Run Simulation Trials"]
    D --> E["Step 4: Aggregate Results"]
    E --> F["Step 5: Analyze Output"]
    F --> G["End: Report Confidence Intervals"]

Visual: The 5-phase pipeline of Monte Carlo. Note how randomness (Step 2) drives the entire process.


2. How It Works: Step-by-Step

Step 1: Define Probability Distributions

Real-world variables rarely follow a single value. Monte Carlo uses distributions to model uncertainty:

  • Uniform: All outcomes equally likely (e.g., rolling a die → 1 to 6 with equal probability).
  • Normal (Gaussian): Symmetric around a mean (e.g., human heights, stock returns).
  • Exponential: Models time between events (e.g., customer arrivals at a coffee shop).

Example: Simulating a Khalti transaction fraud rate.

  • Assume fraud occurs 1 in 1000 transactions (exponential distribution with λ = 0.001).
  • Generate random numbers to simulate fraudulent vs. legitimate transactions.

graph LR
    A["Random Seed"] --> B["Uniform(0,1) Generator"]
    B --> C["Transform to Exponential: -ln(U)/λ"]
    C --> D["Compare to Threshold"]
    D -->|">0.001"| E["Fraud Detected"]
    D -->|"≤0.001"| F["Legitimate"]

Visual: Exponential sampling for fraud detection. U is a uniform random number; λ = 0.001.

Step 2: Generate Random Samples

Pseudorandom number generators (PRNGs) create sequences that appear random. Common algorithms:

  • Linear Congruential Generator (LCG): Simple but predictable for small seeds.
  • Mersenne Twister: High-quality, used in Python’s random module.

Worked Example: Simulate Ncell’s call drop rate.

  • Assume drops follow a Poisson process with λ = 0.05 drops/minute.
  • Generate 1000 random minutes and count drops:
    import random
    drops = [random.poisson(0.05) for _ in range(1000)]
    avg_drops_per_minute = sum(drops) / 1000  # ≈ 0.05
    

Step 3: Run Simulation Trials

For each trial:

  1. Sample from distributions (e.g., customer arrival time, service time).
  2. Run the system (e.g., simulate a queue at a Daraz warehouse).
  3. Record outcomes (e.g., average wait time, stockouts).

Example Trace: Coffee shop simulation (from past exams).

  • Arrival rate: 1 customer every 3 minutes (exponential, λ = 1/3).
  • Service time: 2.5 minutes (exponential, μ = 1/2.5).
  • Trial 1:
    • Arrival times: 3.0, 6.2, 9.1, ... minutes.
    • Service completes at: 5.5, 8.7, 11.6, ... minutes.
    • Wait time for customer 2: 6.2 – 5.5 = 0.7 minutes.

graph TD
    A["Time 0"] -->|"Customer 1 arrives"| B["3.0 min"]
    B -->|"Start service"| C["5.5 min (service ends)"]
    C -->|"Customer 2 arrives"| D["6.2 min"]
    D -->|"Start service"| E["8.7 min (service ends)"]

Visual: Gantt chart of customer arrivals and service. Customer 2 waits 0.7 minutes.

Step 4: Aggregate Results

After N trials (e.g., N = 10,000), compute statistics:

  • Mean: Average wait time = .
  • Confidence Interval: Use the Central Limit Theorem to estimate uncertainty. For 95% CI: .

Example: If average wait time = 1.2 minutes and σ = 0.5, then:

  • 95% CI = ≈ minutes.

graph TD
    A["Run 10,000 Trials"] --> B["Calculate Mean (μ)"]
    B --> C["Compute Standard Deviation (σ)"]
    C --> D["Apply CLT: μ ± 1.96*(σ/√N)"]
    D --> E["Report: 95% CI = [1.19, 1.21]"]

Visual: Statistical aggregation pipeline.

Step 5: Analyze Output

Key metrics:

  1. Point Estimate: Single value (e.g., average delivery time = 15 minutes).
  2. Confidence Interval: Range (e.g., 95% sure time is 14–16 minutes).
  3. Probability of Events: E.g., "80% chance of stockout if demand > 500 units."

3. Why Use Monte Carlo?

Advantage Disadvantage When to Use
Handles complex uncertainty (e.g., NEPSE volatility). Computationally intensive (needs many trials). Systems with randomness (queues, finance).
Works for high-dimensional problems (e.g., neural networks). Not exact (only probabilistic). Optimization (e.g., Daraz’s warehouse layout).
Flexible: Any distribution can be modeled. Requires valid input distributions. Risk analysis (e.g., bank loan defaults).

4. Real-World Applications in Nepal

Example 1: NEPSE Stock Price Simulation

  • Problem: Predicting future stock prices for NEPSE’s top 10 companies.
  • Monte Carlo Approach:
    1. Model daily returns as normal distribution (μ = historical avg, σ = volatility).
    2. Simulate 10,000 possible price paths over 1 year.
    3. Output: 95% CI for price in 1 year = [Rs. 2500, Rs. 3200].
  • Why? Helps investors assess risk before buying shares.

Example 2: Pathao’s Delivery Time Estimation

  • Problem: Estimating delivery times for Kathmandu traffic.
  • Monte Carlo Approach:
    1. Traffic delays: Exponential distribution (λ = 0.2 delays/km).
    2. Rider speed: Normal distribution (μ = 20 km/h, σ = 5 km/h).
    3. Order volume: Poisson (λ = 5 orders/hour).
  • Output: 90% of deliveries take 12–18 minutes (vs. advertised 15 minutes).

Example 3: Khalti’s Fraud Detection

  • Problem: Flagging fraudulent transactions in real time.
  • Monte Carlo Approach:
    1. Simulate 1 million transactions with fraud rate = 0.1%.
    2. Use random sampling to detect anomalies (e.g., sudden large transfers).
    3. Result: 98% of frauds caught within 1 hour.

5. Variance Reduction Techniques

Monte Carlo can be slow. These methods speed it up:

Technique How It Works Example
Stratified Sampling Divide population into strata, sample each. Simulate high/low traffic hours separately for Pathao.
Antithetic Variates Use negatively correlated samples. If one trial overestimates, the other underestimates.
Importance Sampling Focus on high-probability regions. Simulate only high-risk loans for banks.

6. Common Pitfalls

  1. Initial Bias: Early trials may skew results. Discard the first N/10 trials.
  2. Poor Distributions: Using wrong distributions (e.g., normal for counts) gives garbage in, garbage out.
  3. Ignoring Correlation: Real-world variables are often correlated (e.g., traffic and weather). Use copulas to model dependencies.

7. Monte Carlo vs. Other Methods

Method When to Use Example
Monte Carlo High uncertainty, complex systems. NEPSE stock prices, neural network training.
Deterministic Exact solutions possible. Physics equations (e.g., projectile motion).
Discrete Event (DES) Event-driven systems (e.g., queues). Daraz warehouse operations.
Agent-Based Individual interactions matter. Traffic simulation in Kathmandu.

8. Worked Example: Loan Approval Simulation

Scenario: A bank in Nepal wants to estimate the probability a loan applicant defaults.

  • Inputs:
    • Default rate: 5% (historical).
    • Loan amount: Rs. 500,000.
    • Interest rate: 12% per annum.
  • Monte Carlo Steps:
    1. Generate 10,000 applicants with random credit scores (normal, μ = 650, σ = 100).
    2. Default if score < 600 (30% chance) or income < Rs. 50,000 (20% chance).
    3. Simulate repayment over 5 years with random interest rate fluctuations (±2%).
  • Output:
    • Average default rate: 4.8% (close to 5%).
    • 95% CI: [4.5%, 5.2%].
    • Risk: Only approve loans where expected loss < 3% of principal.

graph TD
    A["Applicant 1"] --> B["Credit Score: 620"]
    B -->|"Score < 600? No"| C["Income: Rs. 45,000"]
    C -->|"Income < 50k? Yes"| D["Default"]
    D --> E["Loan Rejected"]

Visual: Loan approval decision tree for one applicant.


9. Exam Tip: How to Score Full Marks

  1. Phases of Monte Carlo: Always draw the 5-phase pipeline (model → sample → simulate → analyze → report).
  2. Real-World Tie: Link examples to Nepalese tech (e.g., "Monte Carlo can simulate Ncell’s call drops by modeling arrival times as Poisson").
  3. Distributions: Name the correct distribution for each scenario:
    • Arrivals → Poisson/Exponential.
    • Measurements → Normal.
    • Discrete choices → Binomial.
  4. Convergence: Explain the Law of Large Numbers and why more trials = better accuracy.
  5. Advantages/Disadvantages: Use the table format above to compare with DES or deterministic methods.
  6. Avoid: Vague statements like "it’s used in finance." Instead, say:

    "Monte Carlo simulates NEPSE stock prices by sampling daily returns from a normal distribution with μ = 0.2% and σ = 1.5%, then aggregating 10,000 trials to estimate a 95% confidence interval for year-end price."


10. Practice Questions (Exam Style)

  1. Describe the phases of a Monte Carlo simulation using a flowchart. (Answer: Use the 5-phase pipeline diagram above.)

  2. A coffee shop has arrivals every 3 minutes (exponential) and service time of 2.5 minutes (exponential). Simulate 3 customers and calculate average wait time. (Answer: Trace arrivals at 3.0, 6.2, 9.1; services at 5.5, 8.7, 11.6; wait times = 0.7, 0.0, 0.0; avg = 0.23 min.)

  3. How would you use Monte Carlo to estimate the probability of a stockout at a Daraz warehouse? (Answer: Model demand as Poisson(λ=100/day), lead time as normal(μ=5,σ=1), and simulate inventory levels over 30 days.)

  4. What is the advantage of stratified sampling in Monte Carlo? (Answer: Reduces variance by focusing on high-probability regions, e.g., simulating peak vs. off-peak hours for Pathao separately.)

Based on the TU BSc CSIT syllabus for Simulation and Modeling (CSC317), unit 7.

Discussion

Loading…