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 , , , .
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:
- Sort the sequence and plot the empirical distribution function (ECDF).
- Compute , where:
- = ECDF (step function).
- = Uniform CDF ( for ).
- 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 .
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.
C. Poker Test for Independence
Purpose: Checks if digits in a sequence are independent (e.g., no repeated pairs). Steps:
- Split numbers into groups of 4 digits (e.g.,
1234,5678). - Count how often all 4 digits are unique (no repeats).
- 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:
- Generate .
- Compute , where is the target CDF.
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:
- Choose a proposal that dominates (target PDF).
- Generate and .
- Accept if , where is a constant.
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
- Problem: Estimate .
- Steps:
- Generate (proposal distribution).
- Compute .
Example: Estimate using Monte Carlo.
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:
- Sample 10,000 paths of daily returns from a normal distribution.
- Count paths where the cumulative return < -20%.
- 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
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).
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).
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
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.
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.
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.
Monte Carlo:
- Explain the Law of Large Numbers (more samples → better estimate).
- For estimation, sketch the unit circle and grid.
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…