পাঠ ১৮ · ৩০-এর মধ্যে · মডিউল ২
Home / AI Courses / AI Foundations / প্রত্যাশা ও ভেরিয়েন্স

প্রত্যাশা ও ভেরিয়েন্স

Expectation and variance
৭ মিনিট পড়া শুরু · Beginner NumPy কোডসহ

এই পাঠে যা শিখবেন

  • Expectation ($E[X]$) — গড়ের সাধারণ রূপ
  • Variance ($\text{Var}[X]$) — বিস্তৃতি কতটা
  • Standard deviation ($\sigma$) — variance-এর বর্গমূল, বাস্তব এককে
  • Covariance — দু'টি চলক একসাথে কীভাবে ওঠানামা করে

১ · প্রত্যাশা — ExpectationExpectation E[X]একটি random variable-এর গড় মান, weighted by probability. দীর্ঘকালীন গড় — অনেক বার পরীক্ষা করলে যা আসবে।

সংজ্ঞা

একটি random variable $X$-এর expectation মানে — অসংখ্য বার পরীক্ষা চালালে গড়ে যা আসবে। চিহ্ন: $E[X]$ বা $\mu$।

Discrete বিতরণে

$$E[X] = \sum_i x_i \cdot P(X = x_i)$$

উদাহরণ — একটি ভারসাম্যপূর্ণ ছক্কা

প্রতিটি পাশের সম্ভাবনা $\tfrac{1}{6}$।

$$E[X] = 1 \cdot \tfrac{1}{6} + 2 \cdot \tfrac{1}{6} + 3 \cdot \tfrac{1}{6} + 4 \cdot \tfrac{1}{6} + 5 \cdot \tfrac{1}{6} + 6 \cdot \tfrac{1}{6} = \tfrac{21}{6} = 3.5$$

ছক্কায় ৩.৫ কখনো পড়ে না — তবু গড় ৩.৫। অনেকবার ছুড়লে গড় ৩.৫-এর কাছাকাছি থাকবে।

একটি লটারি টিকিট ১০ টাকার। প্রাইজ ১০,০০০ টাকা — সম্ভাবনা $\tfrac{1}{১০০০}$।
$E[\text{প্রাইজ}] = 10000 \cdot \tfrac{1}{1000} = 10$ টাকা।
প্রত্যাশিত লাভ = $10 - 10 = 0$। অর্থাৎ গড়ে আপনি লাভ-ক্ষতি কিছু না (যদি ভাগ্য নিরপেক্ষ হয়)। বাস্তব লটারিতে প্রত্যাশিত মান ঋণাত্মক — সবসময়।

২ · Expectation-এর কয়েকটি দরকারি ধর্ম

  • রৈখিকতাLinearity of Expectation$E[X+Y] = E[X] + E[Y]$ — সবসময় সত্য, $X, Y$ স্বাধীন না হলেও। Probability theory-র সবচেয়ে শক্তিশালী & underrated tool. অনেক counterintuitive problem এই দিয়ে সরল হয়ে যায় (যেমন coupon collector, hat-check)। AI-তে — batch loss = sum of individual losses-এর প্রত্যাশা।: $E[X + Y] = E[X] + E[Y]$  (সর্বদা সত্য, এমনকি $X, Y$ স্বাধীন না হলেও)
  • ধ্রুবক-গুণ: $E[cX] = c \cdot E[X]$
  • ধ্রুবকের প্রত্যাশা: $E[c] = c$
  • স্বাধীনতা থাকলে: $E[XY] = E[X] \cdot E[Y]$

৩ · VarianceVariance Var[X]মানগুলো গড় থেকে কতটা ছড়িয়ে — তার পরিমাপ। গড় থেকে বর্গ-দূরত্বের প্রত্যাশা। বড় variance = unpredictable. — বিস্তৃতির পরিমাপ

দু'টি বিতরণের গড় একই হলেও — বিস্তৃতি ভিন্ন হতে পারে। যেমন —
বিতরণ ক: সবাই ৫০ স্কোর। গড় ৫০, বিস্তৃতি ০।
বিতরণ খ: অর্ধেক ০, অর্ধেক ১০০। গড়ও ৫০, কিন্তু বিস্তৃতি বিশাল।

সংজ্ঞা

$$\text{Var}[X] = E\big[(X - \mu)^2\big]$$ গড় থেকে প্রতিটি মানের বর্গ-দূরত্ব-এর প্রত্যাশা।

সমান অর্থ: $\text{Var}[X] = E[X^2] - (E[X])^2$   (অনেক সময় হিসাবে সহজ)

৪ · Standard DeviationStandard Deviation (σ)variance-এর বর্গমূল — মূল এককে interpretable. data কতটা ছড়ানো তার ব্যবহারিক পরিমাপ।

Variance-এর একটা সমস্যা — এর একক বর্গ-একক। যেমন উচ্চতা সেমিতে হলে variance "সেমি²" — যা স্বজ্ঞাত নয়। তাই আমরা ব্যবহার করি standard deviation:

$$\sigma = \sqrt{\text{Var}[X]}$$

একই এককে ফিরে আসে — সরাসরি ব্যাখ্যাযোগ্য। (পাঠ ১৭-এ Gaussian-এ আমরা $\sigma$ এই অর্থেই ব্যবহার করেছি।)

৫ · একটি ছোট হিসাব হাতে

একটি random variable $X$ মানগুলি $\{1, 2, 3\}$ — সম্ভাবনা যথাক্রমে $\{0.2, 0.5, 0.3\}$।

  • $E[X] = 1 \cdot 0.2 + 2 \cdot 0.5 + 3 \cdot 0.3 = 0.2 + 1.0 + 0.9 = 2.1$
  • $E[X^2] = 1 \cdot 0.2 + 4 \cdot 0.5 + 9 \cdot 0.3 = 0.2 + 2.0 + 2.7 = 4.9$
  • $\text{Var}[X] = 4.9 - (2.1)^2 = 4.9 - 4.41 = 0.49$
  • $\sigma = \sqrt{0.49} = 0.7$

৬ · Python-এ Expectation ও Variance

Python · NumPy
import numpy as np

# একটি ছক্কা ১,০০,০০০ বার ছুড়ে দেখি
np.random.seed(42)
rolls = np.random.randint(1, 7, size=100000)

print(f"E[X] (গড়)        ≈ {rolls.mean():.4f}   (তত্ত্ব: 3.5)")
print(f"Var[X] (ভেরিয়েন্স) ≈ {rolls.var():.4f}")
print(f"σ (std deviation) ≈ {rolls.std():.4f}")

    
ছক্কার তাত্ত্বিক variance $\approx 2.92$, $\sigma \approx 1.71$। এক লাখ ছোঁড়ায় অভিজ্ঞতাজাত মান কাছাকাছি আসে।

দু'টি বিতরণের তুলনা

Python — variance কেন গুরুত্বপূর্ণ
import numpy as np

np.random.seed(42)

# দুটি বিতরণ — গড় একই, বিস্তৃতি ভিন্ন
narrow = np.random.normal(loc=50, scale=2, size=10000)
wide   = np.random.normal(loc=50, scale=15, size=10000)

print(f"Narrow:  μ = {narrow.mean():.2f},  σ = {narrow.std():.2f}")
print(f"Wide:    μ = {wide.mean():.2f},  σ = {wide.std():.2f}")
print(f"\nNarrow-এ ৬০-এর বেশি কতজন: {(narrow > 60).mean():.4f}")
print(f"Wide-এ ৬০-এর বেশি কতজন:   {(wide > 60).mean():.4f}")

    
গড় একই (৫০), কিন্তু $\sigma$ ভিন্ন। চওড়া বিতরণে অনেক বেশি sample চরম মান (৬০+) হয়। variance শুধু সংখ্যা নয় — সিদ্ধান্তের জন্য গুরুত্বপূর্ণ।

৭ · CovarianceCovariance Cov(X,Y)দু'টি চলক একসাথে কীভাবে ওঠানামা করে তার পরিমাপ। ধনাত্মক = একসাথে বাড়ে; ঋণাত্মক = একটি বাড়লে অন্যটি কমে। ও CorrelationCorrelationcovariance-কে $\sigma_X \sigma_Y$ দিয়ে normalize — সবসময় $-1$ থেকে $+1$। স্কেল-নিরপেক্ষ সম্পর্কের পরিমাপ।

একটি একক চলকের variance. কিন্তু দু'টি চলক একসাথে কীভাবে চলে?

Covariance

$$\text{Cov}(X, Y) = E\big[(X - \mu_X)(Y - \mu_Y)\big]$$ ধনাত্মক হলে — একসাথে বাড়ে। ঋণাত্মক হলে — একটি বাড়লে অন্যটি কমে। শূন্যের কাছাকাছি — সম্পর্ক দুর্বল।

Covariance-এর একটা সমস্যা — একক ও স্কেলে নির্ভর করে। তাই আমরা প্রায়ই correlation ব্যবহার করি — যা সবসময় $-1$ থেকে $+1$-এ থাকে:

$$\text{Corr}(X, Y) = \dfrac{\text{Cov}(X, Y)}{\sigma_X \sigma_Y}$$

Python · Correlation
import numpy as np

np.random.seed(42)

# x = বাড়ির আকার (sq.ft), y = দাম (লক্ষ টাকা)
x = np.random.normal(1500, 300, 100)
y = 0.05 * x + np.random.normal(0, 5, 100)   # দাম প্রায় x-এর সমানুপাতিক

# Correlation matrix
corr = np.corrcoef(x, y)
print(f"আকার ও দামের correlation: {corr[0, 1]:.3f}")
# কাছাকাছি 1 — শক্তিশালী ধনাত্মক সম্পর্ক

    
গড় একই, ভেরিয়েন্স ভিন্ন Same μ, different σ — distribution shape σ = 2 (narrow) μ=50 দৃঢ়, predictable σ = 15 (wide) μ=50 unpredictable, risky Correlation corr ≈ +1 corr ≈ −1 AI-তে: "low-variance model = বেশি ভরসা; high correlation feature = redundant"
গড় একই হলেও variance ভিন্ন → সিদ্ধান্ত ভিন্ন। Correlation feature relationships বলে।

৮ · AI-তে এদের ব্যবহার

  • Loss function প্রায়ই Mean Squared ErrorMean Squared Error (MSE)predict ও actual-এর পার্থক্যের বর্গের গড়। regression-এ standard loss; ভুলের magnitude পরিমাপ করে। — যা মডেলের ভুলের variance।
  • Bias-Variance tradeoffBias-Variance Tradeoffমডেলের expected error decompose হয় তিনটি term-এ: Bias² (model assumption ভুল), Variance (ডেটার noise-এ sensitive), Irreducible noise. সরল model = high bias, low variance. জটিল model = low bias, high variance. DL এই tradeoff-এর traditional view-এর challenge করে — "double descent" phenomenon. — ML-এর সবচেয়ে গুরুত্বপূর্ণ ধারণাগুলোর একটি (পরে দেখা যাবে)।
  • Feature selection — কোন ফিচার লক্ষ্যের সাথে correlated, কোনটা না।
  • RegularizationRegularizationমডেলকে অতিরিক্ত complex হতে বাধা দেওয়ার technique — overfitting ঠেকায়। L1, L2, dropout জনপ্রিয় উদাহরণ। — মডেলের weights-এর variance কমিয়ে overfitting ঠেকানো।
  • Batch normalizationBatch Normalizationপ্রতিটি layer-এর activation-কে mini-batch-এ mean=0, variance=1 -এ স্বাভাবিক করা। training দ্রুত ও স্থিতিশীল। — প্রতিটি স্তরের আউটপুটকে নির্দিষ্ট mean ও variance-এ নিয়ে আসা।
Correlation ≠ Causation. পাঠ ০৯-এ আলোচনা করেছি। আইসক্রিম বিক্রি ও ডুবে মরায় শক্তিশালী ধনাত্মক correlation আছে — কিন্তু একটি অন্যটির কারণ নয়।

ভাবনার প্রশ্ন

প্রতিটি প্রশ্ন নিজে কিছুক্ষণ ভাবুন — তারপর "→ উত্তর" চাপুন।

প্র ০১ Bias-Variance tradeoff — ML-এর central concept. কিন্তু DL "double descent" দেখায় — যেখানে massively overparameterized model আবার generalize ভাল করে। এই paradox কী?

Classical statistics ও modern DL-এর philosophical conflict — এই প্রশ্নে।

Classical bias-variance:

  • $\text{Error} = \text{Bias}^2 + \text{Variance} + \text{Noise}$।
  • Simple model: high bias (assumption rigid), low variance.
  • Complex model: low bias, high variance (overfit)।
  • Sweet spot: medium complexity → minimum total error.
  • "U-shaped curve" — test error model complexity-এর সাথে।

"Modern" practice — DL contradicts:

  • GPT-4 = 1.7T parameters, training data ~10T tokens.
  • Massively overparameterized — variance অসম্ভব বেশি হওয়া উচিত।
  • তবু — generalize অসাধারণ।
  • Classical theory predicts disaster; reality smooth performance.

Double descent (২০১৯, Belkin et al.):

  • Complexity বাড়ানোর সাথে test error ফিরে নামে — দ্বিতীয়বার।
  • Classical regime: simple → moderate → over-parameterized (peak error)।
  • Modern regime: over-parameterized → super-over-parameterized (error আবার কমে)।
  • Double-descent curve — empirically প্রমাণিত।

কেন DL escape করে — possible explanations:

  • Implicit regularization of SGD: SGD flat minima বাছে, সেগুলো generalize ভাল।
  • Feature learning hypothesis: Big network = adaptive features. relevant features শিখে, irrelevant ignore.
  • Lottery ticket hypothesis: Random init-এ বহু "winning subnetworks" — training সঠিকটি বাছে।
  • Neural Tangent Kernel (NTK): অসীম-wide network — kernel regression-এর মতো behave. Tractable theory.
  • Benign overfitting: Memorization without overfitting — possible high-dim-এ।

Practical implications:

  • "Use fewer parameters than data" — old rule, now wrong.
  • Modern: bigger almost always better, given data + compute.
  • Scaling laws (Chinchilla) compute-optimal model size dictate.
  • Fine-tuning small model → বড় model থেকে worse.

Caveats:

  • Small data (e.g., medical) — classical regime এখনো applicable.
  • Distributional shift — large models also fail.
  • Theory-practice gap — open research.

মূল উপলব্ধি: "More parameters always overfits" — DL-এ ভুল। Bias-variance tradeoff মৃত নয়, কিন্তু DL-এ refined. আজও active research area.

প্র ০২ Batch Normalization (২০১৫) — প্রতিটি layer-এর output normalize করে (mean=0, var=1)। কেন কাজ করে? কেন তবু debate চলছে এর প্রকৃত mechanism নিয়ে?

BN — DL-এর একটি simple-অথচ-deep technique. ১০ বছর পেরিয়েও আজ মেকানিজম নিয়ে বিতর্ক।

মূল algorithm:

  • Mini-batch-এ প্রতিটি activation channel-এ — mean ($\mu_B$) ও variance ($\sigma_B^2$) compute.
  • $\hat{x} = (x - \mu_B) / \sqrt{\sigma_B^2 + \epsilon}$ — normalize.
  • $y = \gamma \hat{x} + \beta$ — learnable scale + shift.
  • Inference-এ — running mean/var ব্যবহার (training-এ updated)।

Original explanation (Ioffe & Szegedy, ২০১৫):

  • "Internal Covariate Shift" — layer activations-এর distribution shift হয় training-এ।
  • BN distribution stabilize করে।
  • Faster training, higher learning rate possible.
  • Implicit regularization (mini-batch noise)।

Modern challenges to original explanation:

  • Santurkar et al. (২০১৮) — "How Does BN Help Optimization?": দেখায় internal covariate shift বাস্তবে BN-এর সাথে বাড়তে পারে। অথচ training improves.
  • Real reason — loss landscape smoothing: BN gradient-এর Lipschitz constant কমায়, loss surface smoother হয়।
  • Better gradient flow: Vanishing/exploding gradient এর চেয়ে কম sensitivity.
  • Implicit regularizer: Mini-batch statistics noise → SGD-এর মতো effect.

Practical observations:

  • Training 5-10x faster এ।
  • Higher learning rates stable.
  • Less sensitive to initialization.
  • Mild regularization effect — dropout-এর সাথে redundant হতে পারে।
  • Test-train mismatch (running stats use)।

Limitations:

  • Small batch size (e.g., 1-4) → unreliable statistics.
  • Sequence models (RNN, Transformer) — sequence length-এ batch dimension mismatch.
  • Distributed training — batch synchronization overhead.

Alternatives evolved:

  • Layer Normalization: per-sample, per-layer. Transformer-এ standard.
  • Group Normalization: channel groups. small-batch-এ ভাল।
  • Instance Normalization: per-sample, per-channel. Style transfer.
  • Weight Normalization: weights-এর norm separate.
  • RMSNorm: mean center skip. Llama, modern LLMs.

মূল উপলব্ধি: BN — "engineering hack that works"। Theory চলছে; practice mature. DL-এ এমন একটি common phenomenon — empirical সফলতা theoretical understanding-এর আগে।

প্র ০৩ "Correlation does not imply causation" — classic warning. কিন্তু AI/ML মডেল correlation-ই শেখে। তাহলে কি AI causal reasoning কখনো করতে পারবে না?

এই প্রশ্ন AI-র সবচেয়ে গভীর research direction-গুলোর একটি ছোঁয়। Judea Pearl-এর "Causal Revolution" এর হৃদয়।

Standard ML — যা পারে:

  • Pattern recognition: "যখন X ঘটে, Y-ও সাধারণত ঘটে।"
  • Prediction: "X দেখে — Y predict."
  • Anomaly detection.
  • সবই correlation-based.

Causal reasoning — যা দরকার কিন্তু কঠিন:

  • "যদি আমি X করি — Y কী হবে?" (intervention)।
  • "X যদি না ঘটত — Y কী হতো?" (counterfactual)।
  • Standard ML এই প্রশ্ন উত্তর দিতে পারে না।

Pearl-এর Ladder of Causation:

  • Rung 1 — Association: "Roosters crow ⟹ sun rises"। Pure correlation. Standard ML/DL এই স্তরে।
  • Rung 2 — Intervention: "Rooster silence করলে sun কি rise করবে?"। Cause-effect পরীক্ষা।
  • Rung 3 — Counterfactual: "যদি আমি rooster না কিনতাম, my farm-এ আজ কী হতো?"। Most sophisticated.

Confounding variable example:

  • Ice cream sales ↔ drowning. Both depend on temperature.
  • Standard ML — strong correlation শেখে।
  • Causal: ice cream ban করলে drowning কমবে না।

Causal AI methods:

  • Causal graphs (DAGs): Nodes + arrows. Domain expert encode knowledge.
  • Do-calculus (Pearl): "$P(Y | do(X))$" — intervention-এর math.
  • Randomized control trials (RCT): Gold standard. Confounder eliminate.
  • Instrumental variables: Confounder-এর effect isolate.
  • Causal discovery algorithms: PC, FCI — data থেকে DAG infer.

Modern AI causal efforts:

  • Microsoft DoWhy: Python causal inference framework.
  • CausalML (Uber): uplift modeling.
  • RL agents: Naturally interventional — actions take.
  • LLM + reasoning: Chain-of-thought causal-like কিন্তু provably না।

Future directions:

  • Causal representation learning — features-এ causal structure encode.
  • Foundation models + causal — Yann LeCun, Bengio's vision.
  • World models — environment-এর causal mental model.

মূল উপলব্ধি: Pure DL — Rung 1-এ আটকা। AGI-র জন্য Rung 2-3 দরকার। Causal AI = একটি active frontier; সম্ভব, কিন্তু challenging. "AI-এর হৃদয়ে statistics, কিন্তু true intelligence-এ causality."

প্র ০৪ "AI মডেলের expected accuracy ৯০%" — কিন্তু variance অজানা। এই scenario কীভাবে dangerous? Confidence interval ও AI deployment-এ এর role কী?

এই প্রশ্ন — point estimate ও uncertainty quantification-এর সবচেয়ে practical clash.

"Expected 90%" — যা বলে না:

  • Variance: Test runs-এ কত fluctuation? σ = ১% কি ১০%?
  • Subgroup performance: Some demographic-এ ৭০%, অন্যদের ৯৭% — গড় ৯০%।
  • Distribution shift: Training data-র মতো না হলে — ৯০% guarantee নেই।
  • Confidence calibration: মডেল কি জানে যে কখন uncertain?

Variance-এর importance:

  • Model A: 90% accuracy, σ = 0.5% — predictable, deployable.
  • Model B: 90% accuracy, σ = 8% — sometimes 82%, sometimes 98%। Risky.
  • Same expected value — completely different deployment risk.

Real-world disasters:

  • Knight Capital (২০১২): Trading algorithm ৯৯.৯% time fine, এক bad scenario-এ $440M loss. Variance ignore.
  • IBM Watson Health: "Cancer diagnosis accurate" — কিন্তু certain hospitals-এ poorly perform. Distribution shift.
  • Tesla Autopilot edge cases: 99% safe, 1% catastrophic failure mode.
  • COMPAS (criminal justice AI): Average accuracy fine, demographic disparities huge.

Confidence interval — কী দেয়:

  • "95% CI: [88.5%, 91.5%]" — ভাল। Deployment-এ confident.
  • "95% CI: [78%, 98%]" — wide. Production-এ unpredictable.
  • Bootstrapping দিয়ে compute করা যায় — multiple test resamples.

Modern uncertainty quantification:

  • Predictive variance: Bayesian DL — multiple weight samples → output distribution.
  • MC Dropout (Gal, ২০১৬): Test time-ও dropout enabled — variance estimate.
  • Deep Ensembles: Multiple models train, predictions average + variance.
  • Conformal prediction: Distribution-free uncertainty bounds.
  • Calibration techniques: Platt scaling, temperature scaling.

Deployment best practices:

  • Multiple test sets: Variance বুঝতে — different distributions.
  • Subgroup analysis: Demographic, geographic, temporal — gaps খুঁজুন।
  • Worst-case bounds: 95% confidence-এর "minimum" performance.
  • Monitoring in production: drift detection, performance alerts.
  • Reject options: low-confidence prediction-এ "I don't know" বলা।

মূল উপলব্ধি: "Average accuracy ৯০%" — necessary but not sufficient. Production AI-তে variance + worst-case + calibration সব দরকার। Engineering wisdom: "trust the variance, not the mean."

অনুশীলন

  1. হিসাব করুন: $X$-এর মান $\{0, 1, 2, 3\}$ — সম্ভাবনা $\{0.1, 0.3, 0.4, 0.2\}$। $E[X]$, $\text{Var}[X]$ ও $\sigma$ বের করুন।
    • $E[X] = 0(0.1) + 1(0.3) + 2(0.4) + 3(0.2) = 0 + 0.3 + 0.8 + 0.6 = 1.7$।
    • $E[X^2] = 0 + 0.3 + 1.6 + 1.8 = 3.7$।
    • $\text{Var}[X] = 3.7 - 1.7^2 = 3.7 - 2.89 = 0.81$।
    • $\sigma = \sqrt{0.81} = 0.9$।
  2. NumPy-তে যাচাই: $X$-কে ১০,০০০ বার sample করে অভিজ্ঞতাজাত মান দিয়ে যাচাই করুন।
    import numpy as np
    np.random.seed(42)
    X = np.random.choice([0, 1, 2, 3], size=10000, p=[0.1, 0.3, 0.4, 0.2])
    print(f"E[X] ≈ {X.mean():.4f}  (theory: 1.7)")
    print(f"Var[X] ≈ {X.var():.4f}  (theory: 0.81)")
    print(f"σ ≈ {X.std():.4f}  (theory: 0.9)")
  3. চিন্তা করুন: দুটি AI মডেলের accuracy যথাক্রমে — মডেল A: গড় ৮৫%, $\sigma = 1\%$। মডেল B: গড় ৮৫%, $\sigma = 8\%$। কোনটা বেশি ভরসাযোগ্য? কেন?

    প্র ০৪-এ বিস্তারিত আছে। সংক্ষেপে:

    • মডেল A বেশি ভরসাযোগ্য।
    • কেন: $\sigma = 1\%$ মানে — প্রায় সবসময় ৮৩-৮৭% range-এ। Predictable.
    • মডেল B: $\sigma = 8\%$ — কখনো ৭৭%, কখনো ৯৩%। Production-এ unpredictable.
    • Same average — completely different risk profile.
    • Deployment-এ — variance always check করুন।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? ব্রাউজারে কাজ না করলে Google Colab ব্যবহার করুন — Google-এর ফ্রি অনলাইন Python পরিবেশ, শুধু Gmail অ্যাকাউন্ট লাগে।
পূর্ববর্তী পাঠ
পাঠ ১৭ · সম্ভাবনা বিতরণ