CSC317 Simulation and Modeling

Simulation and ModelingUnit 310 min read

Random Number Generation & Statistical Tests in Simulation

Unit 3 of Simulation and Modeling covers true vs. pseudo-random number generation (LCG, MCG), statistical tests (KS, Chi-square), and variate generation techniques (inverse transform, acceptance-rejection) with real-world applications in Nepalese systems like eSewa and Ncell.

TAKEAWAYS:

  • Pseudo-random numbers (LCG/MCG) are deterministic but appear random for simulations, while true randomness comes from physical entropy (e.g., atmospheric noise).
  • Statistical tests (KS, Chi-square) verify if generated numbers are uniform and independent—critical for valid simulation outputs.
  • Inverse transform converts uniform random numbers to any distribution (e.g., exponential for Ncell call arrival times), while acceptance-rejection samples from complex distributions.
  • Monte Carlo uses random sampling to estimate solutions (e.g., Daraz’s inventory risk analysis) and relies on the Law of Large Numbers for accuracy.
  • Output analysis methods (point estimation, confidence intervals) quantify simulation results’ reliability (e.g., predicting eSewa transaction delays).

1. True vs. Pseudo-Random Numbers

Definitions and Properties

Random numbers are essential for simulations to model uncertainty. There are two types:

Property True Random Numbers Pseudo-Random Numbers (PRN)
Source Physical entropy (e.g., hardware noise) Algorithmic (deterministic)
Repeatability Non-repeatable (unpredictable) Repeatable (same seed → same sequence)
Speed Slow (requires hardware) Fast (software-generated)
Use Case Cryptography, lotteries Simulations, games, Monte Carlo methods

How PRNs Work: Linear Congruential Generator (LCG)

The most common PRN algorithm:

  • Parameters:
    • : Seed (starting value)
    • : Multiplier
    • : Increment
    • : Modulus (controls range)

Example: Generate 10 random integers using LCG with , , , .

130255186124873343494057386250765681409
LCG sequence with m=1000, a=19, c=6, X₀=13 (X₀ to X₉)

Output: 13, 255, 861, 487, 343, 940, 738, 250, 656, 140

Real-World Tie-In: Nepal’s Ncell uses PRNs to simulate call arrival times in its network traffic models. A poorly seeded LCG could create artificial patterns, overloading servers during peak hours (e.g., 6–9 PM).


2. Statistical Tests for Randomness

Simulations require random numbers that pass uniformity and independence tests.

A. Kolmogorov-Smirnov (KS) Test for Uniformity

Purpose: Checks if a sequence is uniformly distributed between [0,1]. Steps:

  1. Sort the sequence and plot the empirical distribution function (ECDF).
  2. Compute , where:
    • = ECDF (step function).
    • = Uniform CDF ( for ).
  3. Compare to critical values (e.g., ).

Example: Test the sequence 0.35, 0.77, 0.12, 0.33, 0.88, 0.45, 0.19 for uniformity at .

0.10.20.30.40.50.60.70.80.910.20.40.60.81xyUniform CDF (F(x) = x)
KS Test: ECDF vs Uniform CDF (n=7, D=0.285 < 0.41 → Accept H₀)

Critical Value Table:

7 0.41
10 0.35

B. Chi-Square Test for Uniformity

Purpose: Divides [0,1] into bins and checks if counts match expectations. Formula: Example: Test 0.54, 0.73, 0.98, 0.11, 0.68, 0.45 for uniformity with bins.

00.751.52.253[0, 0.33)1[0.33, 0.66)2[0.66, 1.0]3Observed Counts (Oᵢ)
Chi-Square Test: Bins (k=3), O=[1,2,3], E=[2,2,2], χ²=0.5 < 5.99 → Accept H₀

C. Poker Test for Independence

Purpose: Checks if digits in a sequence are independent (e.g., no repeated pairs). Steps:

  1. Split numbers into groups of 4 digits (e.g., 1234, 5678).
  2. Count how often all 4 digits are unique (no repeats).
  3. Compare to expected frequency under independence.

Example: In 1,000 four-digit numbers, 525 had all unique digits.

  • Expected: per number → 504 expected.
  • Observed: 525 → Not significantly different (passes independence).

Real-World Tie-In: eSewa uses random number tests to detect fraud in transaction IDs. If the Poker test fails, IDs may be algorithmically generated (e.g., 1111, 2222), flagging potential hacking.


3. Generating Non-Uniform Random Variates

Simulations often need numbers from specific distributions (e.g., exponential for call times).

A. Inverse Transform Method

Idea: Use the inverse of the CDF to transform uniform random numbers. Steps:

  1. Generate .
  2. Compute , where is the target CDF.
0.10.20.30.40.50.60.70.80.910.20.40.60.81xyCDF(F(x))Inverse CDF (F⁻¹(u)) for U[0,1] → X
Inverse Transform: U[0,1] → X via F⁻¹(u)

Example: Generate exponential random variables with rate . Trace:

0.12 0.88 -0.1278 0.2556
0.45 0.55 -0.5978 1.1956

B. Acceptance-Rejection Method

Idea: Sample from a proposal distribution and accept/reject based on a bound. Steps:

  1. Choose a proposal that dominates (target PDF).
  2. Generate and .
  3. Accept if , where is a constant.
0.10.20.30.40.50.60.70.80.910.511.52xyProposal f(x) = 2 (uniform)Target g(x) = x (linear)M = 2 (constant)
Acceptance-Rejection: g(x) ≤ M·f(x) for x ∈ [0,1]

Example: Generate from on [0,1] using (uniform).

  • (since for all ).
  • Trace:
    • ,
    • Check: → Reject.
    • Next ,
    • Check: → Accept.

Real-World Tie-In: Pathao uses acceptance-rejection to model rider wait times. If arrival times follow a heavy-tailed distribution (e.g., ), uniform sampling would bias results. The method ensures realistic surge pricing during traffic jams.


4. Monte Carlo Simulation

Definition: Uses random sampling to estimate numerical results (e.g., integrals, probabilities).

How It Works

  1. Problem: Estimate .
  2. Steps:
    • Generate (proposal distribution).
    • Compute .

Example: Estimate using Monte Carlo.

y-axisx-axisy-axisx-axis[-1,-1][-1,1][1,-1][1,1]
Monte Carlo π Estimation: Unit Square [-1,1]×[-1,1] with Unit Circle

Trace (100 points):

  • Inside circle: 78
  • (close to 3.1416).

Real-World Tie-In: Nepal Stock Exchange (NEPSE) uses Monte Carlo to simulate portfolio risks. For example, estimating the probability that a stock’s value drops >20% in a year:

  1. Sample 10,000 paths of daily returns from a normal distribution.
  2. Count paths where the cumulative return < -20%.
  3. Probability ≈ count/10,000.

5. Output Analysis Methods

After running a simulation, analyze the results statistically.

Method Purpose Formula
Point Estimation Estimate mean of output
Confidence Interval Quantify uncertainty
Batch Means Reduce autocorrelation in time-series data Split runs into batches, compute means

Example: A bank simulates loan default rates over 100 runs, yielding defaults: [5, 7, 4, 6, 8].

  • Mean default rate: .
  • 95% CI: .

In the Real World

  1. eSewa Transaction IDs:

    • Idea: Poker test for independence.
    • How: eSewa generates 12-digit IDs. If the Poker test shows too many repeated digits (e.g., 112233), the system flags potential fraud (e.g., a hacker generating predictable IDs).
  2. Ncell Network Traffic Simulation:

    • Idea: Exponential variate generation (inverse transform).
    • How: Call arrivals follow a Poisson process (interarrival times are exponential). Ncell’s simulators use LCG to generate uniform numbers, then transform them to model peak-hour congestion (e.g., 7–8 PM).
  3. Daraz Inventory Management:

    • Idea: Monte Carlo for demand forecasting.
    • How: Daraz runs 10,000 simulations of daily orders for a product, sampling from historical demand distributions. The 90th percentile of the results determines safety stock levels to avoid stockouts during festivals (e.g., Dashain).

Exam Tip

  1. LCG/MCG Questions:

    • Always show the first 3–4 iterations in your answer.
    • Memorize the formula: .
    • Common Pitfall: Forgetting to take modulo in each step.
  2. Statistical Tests:

    • KS Test: Plot the ECDF vs. CDF and mark .
    • Chi-Square: Show the table of observed vs. expected counts.
    • Poker Test: Calculate the expected frequency of all-unique-digit numbers.
  3. Variate Generation:

    • For inverse transform, always write the CDF and its inverse.
    • For acceptance-rejection, draw the proposal and target PDFs and label the rejection region.
  4. Monte Carlo:

    • Explain the Law of Large Numbers (more samples → better estimate).
    • For estimation, sketch the unit circle and grid.
  5. Output Analysis:

    • If asked about confidence intervals, state the formula and plug in numbers.
    • For batch means, mention why it reduces autocorrelation.

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

Discussion

Loading…