System Modeling & Simulation
All Chapters
Chapter 4Exam Weight: HIGH (Midterm 2024 Q1b = 6 pts, Midterm 2025 Q2 = 13 pts)Lec 4 Slides 1–37Lec 4 Transcript (Dr. Ayman)Lab 3

Monte Carlo Simulation & Confidence Intervals

محاكاة مونت كارلو وفترة الثقة (Confidence Intervals)

Hit-or-Miss vs. Expectation Monte Carlo integration, Law of Large Numbers, Variance, Standard Error eₙ, 95% Confidence Intervals & their true interpretation, sampling bias, and PortaCom risk analysis.

حساب التكامل بطريقة Hit-or-Miss وبطريقة التوقع، قانون الأعداد الكبيرة، الـ Variance والـ Standard Error، فترة الثقة 95% وتفسيرها الصح، تحيز العينات، ومثال PortaCom.

Chapter Mastery0% (0/3 sections · 0/4 checks)
x
Scope: Assoc. Prof. Ayman Ghoneim (FCAI — Cairo University)
What you'll lock down in this chapter
  • Estimate 1D integrals on [0, 1] and [a, b] using both Hit-or-Miss and General Expectation Monte Carlo.
  • Compute Var[g(X)], Standard Error eₙ = √(Var[g(X)] / n), and 95% CI Î ± 1.96·eₙ.
  • Calculate required sample size n for a desired error tolerance ε (1/√n scaling).
  • State the correct frequentist interpretation of a 95% CI and distinguish Probabilistic vs. Non-probabilistic sampling.
  • Compare deterministic What-If analysis against Monte Carlo Risk Analysis (Possible vs. Probable).
Section 4.1Exam #1 Priority

Monte Carlo Integration: Hit-or-Miss vs. Expectation

التكامل بمونت كارلو: Hit-or-Miss vs التوقع

Core Concept
Two Ways to Estimate an Integral by Sampling

طريقتين لحساب التكامل بالعشوائية

Hit-or-Miss throws random darts (u_x, u_y) inside a rectangle [a, b] × [0, f_max] and estimates I ≈ FRACT · (b - a) · f_max—just like estimating π ≈ 4·(inCircle/n) or measuring lake area from satellite pixels. Its big flaw: finding f_max is itself an optimization problem!

  • General Expectation MC (Midterm Favorite!): Sample xᵢ ~ U(a, b) via xᵢ = a + rᵢ·(b - a), evaluate g(xᵢ), and estimate I ≈ (b - a) · (1/n) ∑ g(xᵢ).
  • When integrating on [0, 1], xᵢ = rᵢ directly and (b - a) = 1, so Î = (1/n) ∑ g(rᵢ).
شرح عربي — كلام الدكتور

طريقة Hit-or-Miss بترمي نقط عشوائية جوه مستطيل عرضه (b-a) وارتفاعه f_max وتضرب نسبة النقط اللي تحت المنحنى في مساحة المستطيل (زي حساب مساحة بحيرة من بيكسلات صورة قمر صناعي). عيبها إنك ساعات مش بتبقى عارف أعلى قيمة f_max كام!

  • طريقة التوقع (اللي جت في ميدترم 2024!): تاخد أرقام عشوائية rᵢ وتجيب xᵢ = a + rᵢ(b-a)، وتحسب متوسط الدالة وتضربه في (b-a). لو التكامل من 0 لـ 1 يبقى xᵢ = rᵢ على طول!
Expectation Monte Carlo Integral
Formula

صيغة تكامل مونت كارلو بالتوقع

ExpectationIntegralEstimator::I^=b−an∑i=1ng(xi),xi=a+ri(b−a),    ri∼U(0,1)Expectation Integral Estimator :: \hat{I} = \frac{b - a}{n}\sum_{i=1}^{n} g(x_i), \qquad x_i = a + r_i(b - a), \;\; r_i \sim U(0, 1)

When to use: Always preferred over Hit-or-Miss because you do NOT need f_max and variance is smaller.

أفضل من Hit-or-Miss ومش محتاجة f_max.

Section 4.2Exam Trap Alert

Variance, Standard Error, 95% CI & Sampling Bias

التباين، الخطأ المعياري، فترة الثقة 95%، والتحيز

Core Concept
Standard Error, 95% CI & 4× Samples Rule

الخطأ المعياري وقاعدة تضاعف العينة 4 مرات

By the Law of Large Numbers, Î converges to I as n → ∞, with error shrinking at 1/√n. On [0, 1], Dr. Ayman & Lab 3 compute Var[g(X)] = (1/n) ∑ g(xᵢ)² - β, Standard Error eₙ = √(Var[g(X)] / n), and 95% CI = Î ± 1.96·eₙ.

  • Sample size needed for error ≤ ε: n ≥ Var[g(X)] / ε² (general interval [a, b]: scale by (b - a)²).
  • Central Limit Theorem (CLT): for n ≥ 30 (or Normal populations), the sampling distribution of X̄ is approximately Normal with SE = σ/√n.
شرح عربي — كلام الدكتور

حسب Law of Large Numbers كل ما تزود التجارب n النتيجة بتقرب للحقيقة بمعدل 1/√n — يعني عشان تقلل الخطأ للنص لازم تزود التجارب **4 أضعاف** مش الضعف! بنحسب التباين ثم الـ Standard Error eₙ = √(Var/n) وبعدين فترة الثقة 95%: Î ± 1.96·eₙ.

  • حجم العينة المطلوب لدقة ε: n ≥ Var / ε².
  • نظرية النهاية المركزية (CLT): لما n ≥ 30 توزيع المتوسط X̄ بيقرب لـ Normal مهما كان شكل التوزيع الأصلي.
Exam Trap
True Meaning of a 95% Confidence Interval & Sampling Bias

المعنى الصح لفترة الثقة 95% وأنواع العينات

A single computed 95% CI either contains the fixed true parameter μ or it does not—probability does NOT sit inside one finished interval! Instead, '95% confidence' means if we repeated sampling and built intervals 100 times, about 95 of those intervals would capture μ.

  • Parameter (population truth, e.g., μ) vs. Estimator (formula X̄) vs. Estimate (computed number).
  • Probabilistic sampling (Simple Random, Stratified, Cluster, Systematic) gives unbiased representation; Convenience, Voluntary-response, and Snowball sampling introduce bias.
شرح عربي — كلام الدكتور

معلومة بيغلط فيها ناس كتير: القيمة الحقيقية μ رقم ثابت، فالفترة الواحدة اللي طلعتلك يا فيها μ يا مافيهاش (مش احتمال 95% إنها جواها!). الصح تقول: لو كررنا التجربة 100 مرة وعملنا 100 فترة، 95 فترة منهم هيحتووا الـ μ الحقيقية.

  • فرّق بين: Parameter (حقيقة المجتمع الثابتة)، Estimator (القانون زي X̄)، و Estimate (الرقم اللي حسبته).
  • العينات الاحتمالية (Random, Stratified, Cluster) عادلة؛ أما الـ Convenience و Voluntary فيها تحيز (Bias).
MC Variance, Standard Error & 95% CI (on [0,1])
Formula

تباين مونت كارلو والخطأ المعياري وفترة الثقة

Estimated Variance on [0,1][0, 1]
Var⁡^[g(X)]=1n∑i=1ng(xi)2−I^2\widehat{\operatorname{Var}}[g(X)] = \frac{1}{n}\sum_{i=1}^{n} g(x_i)^2 - \hat{I}^2
Standard Error
en=Var⁡^[g(X)]ne_n = \sqrt{\frac{\widehat{\operatorname{Var}}[g(X)]}{n}}
95% Confidence Interval
CI95%=I^±1.96 en\text{CI}_{95\%} = \hat{I} \pm 1.96\,e_n
Lec 4 Slide 25 & Lab 3 divide Var[g(X)] by n (e_n = √(Var/n)); some stat textbooks divide sample variance s² by n-1—show your formula clearly.
Say This, Not ThatLec 4 Audio
Don't Say / Trap

There is a 95% probability that μ lies inside this specific computed interval [1.24, 3.01].

Say This Instead

95% of intervals constructed by this repeated procedure will contain the true fixed parameter μ.

μ is fixed, not random; the random interval endpoints vary across samples.

الـ μ رقم ثابت مش بيتحرك! العشوائية في حدود الفترة نفسها لما بتاخد عينات مختلفة.

Say This, Not ThatLec 4 + Midterm 2025 Q2c
Don't Say / Trap

To cut Standard Error in half, double the number of trials (2n).

Say This Instead

To cut Standard Error in half, quadruple the trials (4n) because eₙ shrinks as 1/√n.

√(4n) = 2√n, halving eₙ.

الخطأ بيتناسب مع 1/√n — يبقى عشان تقسمه على 2 لازم تضرب n في 4!

Section 4.3

Monte Carlo Risk Analysis: PortaCom Case

تحليل المخاطر بمونت كارلو: مثال PortaCom

Midterm Exam Radar
Midterm 2024 · Q1(b)6 ptsView

Estimate ∫₀¹ 4/(1+x²) dx with 3 RNs [0.65, 0.21, 0.96] and compute Standard Error eₙ.

6 درجات: احسب تكامل π تقريبياً بـ 3 أرقام عشوائية واحسب الـ Standard Error.

Midterm 2025 · Q213 ptsView

3-day Bakery MC profit simulation (20 loaves/day, no lost opportunity cost), avg profit & SE, experimental vs. theoretical demand.

13 درجة: محاكاة ربح المخبز 3 أيام + متوسط الربح والـ SE + مقارنة المتوسط التجريبي بالنظري.

Study Tip
Possible vs. Probable (Why What-If Isn't Enough)

الفرق بين الممكن (Possible) والمُرجّح (Probable)

In PortaCom, Profit = (249 - c₁ - c₂)·x - 1,000,000 where labor c₁ is discrete, parts c₂ ~ U(80, 100), and demand x ~ N(15000, 4500²). Deterministic What-If shows Best ($2,591,000) and Worst (-$847,000) extremes, but tells you NOTHING about their probabilities.

  • 500 MC trials gave Mean Profit = $698,457, StDev = $444,321, and P(Loss) = 51/500 = 0.1020.
  • Other business/engineering applications (Lec 4): Trim loss when cutting glass/fabric rolls, and pricing low-probability catastrophic insurance claims.
شرح عربي — كلام الدكتور

تحليل الـ What-If التقليدي بيقولك أحسن سيناريو وأوحش سيناريو وخلاص (إيه اللي Possible). لكن لما عملنا 500 محاكاة Monte Carlo لمشروع PortaCom عرفنا متوسط الربح ($698,457) وكمان احتمال الخسارة P(loss) = 51/500 = 10.2% — يعني عرفنا إيه اللي Probable!

  • تطبيقات تانية في المحاضرة: تقليل الهالك في تقطيع القماش أو الزجاج (Trim loss)، وحساب أقساط التأمين للحوادث النادرة.
Interactive Simulation LabCh 3–4

Distributions & Monte Carlo Sandbox

التوزيعات الاحتمالية ومحاكاة مونت كارلو

Ch 3–4 Interactive Lab: Distributions & Monte Carlo Sandbox
Tune Binomial, Poisson, Normal, Uniform, and Exponential shapes, then run live Monte Carlo simulations for π, integrals, and profit risk.
Ch 3 & 4
Trials (n)12
Success Prob (p)0.45
Type
Discrete (PMF)
Mean E[X]
5.40
Variance Var(X)
2.97
Instant Micro-Checks0/4 passed

Test your grasp before moving on

Wrong answers are automatically logged to your Weak Topics on the Coverage & Progress dashboard until you pass them.

Check 1
What is the main limitation of Hit-or-Miss Monte Carlo integration compared to General Expectation Monte Carlo?

إيه أكبر عيب في طريقة Hit-or-Miss مقارنة بطريقة التوقع؟

Check 2
Your Monte Carlo estimate from n = 100 trials has Standard Error e₁₀₀ = 0.40. Approximately how many trials are needed to drop SE to 0.10?

لو الخطأ المعياري عند 100 تجربة هو 0.40، محتاج كام تجربة عشان ينزل لـ 0.10؟

Check 3
Which statement correctly interprets a 95% confidence interval [2.10, 2.50] for the true integral I?

أي جملة بتوصف معنى فترة الثقة 95% بشكل علمي سليم؟

Check 4
In PortaCom, why did Dr. Ayman run 500 Monte Carlo trials after already computing Best-case and Worst-case What-If profits?

ليه عملنا 500 محاكاة لـ PortaCom رغم إننا حسبنا بالفعل Best-case و Worst-case؟

30-Second Recap
Read these bullets right before entering the exam hall.
  • 1Expectation MC: Î = (b-a)·(1/n)∑g(xᵢ) with xᵢ = a + rᵢ(b-a). Beats Hit-or-Miss (no f_max needed).
  • 2Var[g(X)] = (1/n)∑g(xᵢ)² - β, Standard Error eₙ = √(Var[g(X)]/n), 95% CI = Î ± 1.96·eₙ.
  • 3Error scales as 1/√n → 4× trials needed to halve error.
  • 495% CI = 95% of repeated intervals cover μ; MC Risk Analysis quantifies P(Loss) ('Probable' vs. 'Possible').

ملخص ٣٠ ثانية بالعربي:

  • تكامل مونت كارلو: Î = (b-a) × متوسط g(xᵢ)؛ ومش محتاج f_max.
  • eₙ = √(Var/n) وفترة الثقة 95% هي Î ± 1.96·eₙ.
  • تقليل الخطأ للنص محتاج 4 أضعاف التجارب (1/√n).
  • فترة الثقة بتقيس نسبة نجاح الطريقة لو اتكررت، ومونت كارلو بيطلع احتمال الخسارة.
Ready to drill Chapter 4 questions?
10curated concept & written questions for this chapter.