5.4.14 · HinglishScientific Computing (Python)

scipy.stats — distributions, hypothesis tests

1,795 words8 min readRead in English

5.4.14 · Coding › Scientific Computing (Python)


YEH module exist kyun karta hai?

KYA tum actually har baar compute karte ho:

  • "Exactly is value ki kitni probability hai?" → density (PDF).
  • "Is value tak ki kitni probability hai?" → cumulative (CDF).
  • "95th percentile par kaun sa value baithta hai?" → quantile (PPF).
  • "Kya mera observed effect surprising hai?" → p-value.

Char functions, ek idea se derive kiye

from scipy import stats
n = stats.norm(loc=0, scale=1)      # mean 0, sd 1
n.pdf(0)        # 0.3989  height of the bell at center
n.cdf(1.96)     # 0.9750  area to the left of 1.96
n.sf(1.96)      # 0.0250  area to the right (= 1 - cdf)
n.ppf(0.975)    # 1.96    inverse of cdf
n.rvs(size=5)   # 5 random samples

Standardizing: z-score, derive kiya


Hypothesis testing, scratch se

data = [5.1, 4.9, 5.3, 5.0, 4.8]
stats.ttest_1samp(data, popmean=5.0)
# TtestResult(statistic=-0.0, pvalue=1.0)  -> no evidence mean ≠ 5

Worked examples


Common mistakes


Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho ek bag of marbles jahan kuch sizes common hain aur kuch rare. Ek distribution woh rulebook hai jo batata hai har size kitna common hai. PDF hai "exactly is size ki kitni popularity hai," CDF hai "is size ya usse chhote kitne marbles hain." Ek hypothesis test aisa hai jaise koi dost claim kare ki bag mein "sab medium marbles hain." Tum ek mutthi bharke uthao: agar baar baar giants nikle, tum kaho "yeh sirf luck nahi ho sakta — teri claim galat hai!" p-value hai ki pure luck kitni baar tumhe itni weird mutthi de sakta tha. Tiny p = "luck explain nahi kar sakta" = tumhara dost galat tha.


Flashcards

stats.norm.cdf(x) kya return karta hai?
, ke left mein PDF ke neeche ka area.
.sf(x) kya compute karta hai aur 1-cdf se zyaada prefer kyun karte hain?
Survival function ; yeh far right tail mein numerically zyaada accurate hai.
PPF (.ppf(p)) kya hai?
Inverse CDF: woh value jahan — yaani -quantile.
P-value precisely define karo.
.
Standard error mein se divide kyun karte hain?
Sample mean ka variance hota hai, toh uska sd hai; values average karne se noise shrink hoti hai.
One-sample test ke liye t-statistic derive karo.
par center karo, estimated standard error se scale karo; follow karta hai.
ki jagah kyun?
Kyunki ko se estimate kiya jaata hai, jo uncertainty badhata hai; df wala heavier-tailed Student-t iske liye account karta hai.
stats.chisquare kya measure karta hai?
, observed aur expected counts ke beech total scaled squared mismatch.
Common error: kya chhota p ko false prove karta hai?
Nahi — yeh hai, nahi; sirf yeh kehta hai ki ke under data surprising hai.
stats.expon mein scale ka matlab kya hai?
scale = 1/λ (mean), rate λ nahi.
stats.norm.fit(data) kya return karta hai?
Maximum-likelihood estimates jo data ko best fit karte hain.
T value se two-sided p-value kaise nikalte hain?
stats.t.sf(abs(t), df).

Connections

  • Normal Distribution workhorse jo z-score se standardize hota hai.
  • Central Limit Theoremkyun sample means ~normal hote hain, t/z tests ko justify karta hai.
  • p-values and Significance aur rejection ki decision logic.
  • Maximum Likelihood Estimation.fit under the hood kya karta hai.
  • numpy.random.rvs ke comparison mein lower-level sampling.
  • Chi-square Distribution — squared normals ka sum; goodness-of-fit ka basis.

Concept Map

differentiate

1 minus F

invert

bundles

provides

maps any normal to

gives 1.96 cutoff

assumes

computes

extremeness under H0

measured via

CDF F of x = P X leq x

PDF density

Survival S of x

PPF quantile

Distribution object stats.norm

Z-score transform

Standard normal N 0,1

Hypothesis test

Test statistic

p-value

Null hypothesis H0