The Story That Explains Varianceवह कहानी जो Variance समझाती है一个讲清方差的故事
Imagine two pizza delivery services in your city — QuickSlice and PizzaPal. Both say their average delivery time is 30 minutes. You order from both for ten days and write down the times.
अपने शहर की दो pizza delivery services की कल्पना करें — QuickSlice और PizzaPal. दोनों कहती हैं कि उनका औसत delivery time 30 minutes है। आप दस दिन दोनों से order करते हैं और समय लिखते हैं।
想象你所在城市有两家披萨外卖 —— QuickSlice 和 PizzaPal。两家都说平均送达时间是 30 分钟。 你连续十天从两家各点一次,并记下送达时间。
| Dayदिन天 | QuickSlice (mins)QuickSlice (मिनट)QuickSlice(分钟) | PizzaPal (mins)PizzaPal (मिनट)PizzaPal(分钟) |
|---|---|---|
| 1 | 29 | 10 |
| 2 | 31 | 55 |
| 3 | 30 | 8 |
| 4 | 28 | 62 |
| 5 | 32 | 15 |
| 6 | 30 | 48 |
| 7 | 29 | 20 |
| 8 | 31 | 65 |
| 9 | 30 | 12 |
| 10 | 30 | 5 |
Both have a mean of exactly 30 minutes. But QuickSlice always arrives within 28–32 minutes, while PizzaPal arrives anywhere from 5 to 65 minutes.
The mean cannot show this difference. Variance can.
दोनों का mean ठीक 30 minutes है। पर QuickSlice हमेशा 28–32 मिनट में आता है, जबकि PizzaPal 5 से 65 मिनट में कभी भी आ सकता है।
Mean यह अंतर नहीं दिखा सकता। Variance दिखा सकता है।
两家的均值都正好是 30 分钟。但 QuickSlice 总是在 28–32 分钟送到,而 PizzaPal 可能在 5 到 65 分钟之间的任何时间送到。
均值看不出这个差别。方差可以。
Variance measures how far the values are from the mean, on average (using squared distances). A low variance means values stay close to the mean (like QuickSlice). A high variance means values are spread out (like PizzaPal). Same mean — completely different story.
Variance मापता है कि values औसतन mean से कितनी दूर हैं (squared दूरियों से)। कम variance मतलब values mean के पास रहती हैं (जैसे QuickSlice)। ज़्यादा variance मतलब values फैली हुई हैं (जैसे PizzaPal)। वही mean — पूरी तरह अलग कहानी।
方差衡量各个值平均离均值有多远(用距离的平方来算)。方差小,说明数值紧贴均值(像 QuickSlice);方差大,说明数值很分散(像 PizzaPal)。均值相同 —— 故事却完全不同。
Variance in Pictures — Understand It Before the Formulaतस्वीरों में Variance — Formula से पहले समझें图解方差 — 先看懂,再看公式
Darts: both players aim at the centre and both are "right on average", but one is far more consistent. Variance measures that consistency.
Squares: draw a square from each point to the mean. Its side is the distance, its area is the squared distance. Variance is (roughly) the average area of these squares — big squares mean big spread.
Darts: दोनों खिलाड़ी केंद्र पर निशाना लगाते हैं और दोनों "औसतन सही" हैं, पर एक कहीं ज़्यादा consistent है। Variance इसी consistency को मापता है।
Squares: हर point से mean तक एक square बनाएँ। उसकी भुजा दूरी है, उसका क्षेत्रफल squared दूरी है। Variance (लगभग) इन squares का औसत क्षेत्रफल है — बड़े squares मतलब ज़्यादा फैलाव।
飞镖:两位选手都瞄准中心,平均来看都“打中了”,但其中一位稳定得多。方差衡量的正是这种稳定性。
正方形:从每个点到均值画一个正方形,边长是距离,面积就是距离的平方。方差(大致)就是这些正方形的平均面积 —— 正方形越大,越分散。
Each delivery time has a square to the green mean line. Switch between QuickSlice and PizzaPal, drag any dot, and switch the divisor between n − 1 (sample) and n (population).
हर delivery time से हरी mean line तक एक square है। QuickSlice और PizzaPal के बीच बदलें, कोई भी dot खींचें, और भाजक को n − 1 (sample) और n (population) के बीच बदलें।
每个送达时间都有一个连到绿色均值线的正方形。在 QuickSlice 和 PizzaPal 之间切换,拖动任意一个点,并在 n − 1(样本)和 n(总体)之间切换除数。
The FormulaFormula公式
This is Bessel's correction. In a sample we measure distances from the sample mean, and the sample mean is always as close as possible to its own data. So the squared distances come out a little too small, and dividing by n underestimates the population variance. Dividing by (n − 1) fixes this on average, which is why s² is called an unbiased estimator. The simulator below proves it with thousands of random samples.
इसे Bessel's correction कहते हैं। Sample में हम दूरियाँ sample mean से मापते हैं, और sample mean हमेशा अपने data के जितना हो सके पास होता है। इसलिए squared दूरियाँ थोड़ी छोटी आती हैं, और n से भाग देने पर population variance कम आँका जाता है। (n − 1) से भाग देने पर यह औसतन ठीक हो जाता है, इसीलिए s² को unbiased estimator कहते हैं। नीचे का simulator हज़ारों random samples से इसे साबित करता है।
这叫作 Bessel 校正。在样本中,我们是从样本均值出发量距离的,而样本均值总是尽可能贴近它自己的数据。所以算出来的平方距离会偏小一点,除以 n 会低估总体方差。 除以 (n − 1) 在平均意义上修正了这个偏差,所以 s² 被称为无偏估计量。下面的模拟器用成千上万个随机样本来证明这一点。
A population with true variance 100. Draw samples of size n and compute the variance two ways. Watch the running averages: dividing by n stays too low, dividing by n − 1 lands on 100. Try n = 2 and n = 20.
असली variance 100 वाली एक population. Size n के samples निकालें और variance दो तरीकों से निकालें। चलते औसत देखें: n से भाग देने वाला बहुत नीचे रहता है, n − 1 वाला 100 पर पहुँचता है। n = 2 और n = 20 आज़माएँ।
一个真实方差为 100 的总体。抽取大小为 n 的样本,用两种方法计算方差。观察累计平均:除以 n 的一直偏低,除以 n − 1 的正好落在 100。试试 n = 2 和 n = 20。
import numpy as np
# Why divide by n - 1? Draw many small samples from a population with a known variance.
rng = np.random.default_rng(42)
population = rng.normal(30, 10, 1_000_000) # true variance ≈ 100
print(f"True population variance: {population.var():.1f}")
samples = rng.choice(population, size=(100_000, 5)) # 100,000 samples of size n = 5
print(f"Average of variances dividing by n : {samples.var(axis=1, ddof=0).mean():.1f}")
print(f"Average of variances dividing by n - 1: {samples.var(axis=1, ddof=1).mean():.1f}")
With n = 5, dividing by n gives on average 80 instead of 100 — exactly (n − 1)/n = 4/5 of the truth. Dividing by n − 1 gives 100. The bigger the sample, the smaller this gap.
n = 5 पर, n से भाग देने पर औसतन 100 की जगह 80 मिलता है — ठीक (n − 1)/n = 4/5 हिस्सा। n − 1 से भाग देने पर 100 मिलता है। Sample जितना बड़ा, यह gap उतना छोटा।
n = 5 时,除以 n 平均得到 80 而不是 100 —— 正好是真值的 (n − 1)/n = 4/5。除以 n − 1 得到 100。样本越大,这个差距越小。
Step-by-Step CalculationStep-by-Step गणना分步计算
Let us calculate the variance for QuickSlice: [29, 31, 30, 28, 32, 30, 29, 31, 30, 30] — mean = 30
चलिए QuickSlice का variance निकालें: [29, 31, 30, 28, 32, 30, 29, 31, 30, 30] — mean = 30
我们来计算 QuickSlice 的方差:[29, 31, 30, 28, 32, 30, 29, 31, 30, 30] —— 均值 = 30
(29−30)=−1 (31−30)=1 (30−30)=0 (28−30)=−2 (32−30)=2 (30−30)=0 (29−30)=−1 (31−30)=1 (30−30)=0 (30−30)=0
Check: these always add up to 0.
(29−30)=−1 (31−30)=1 (30−30)=0 (28−30)=−2 (32−30)=2 (30−30)=0 (29−30)=−1 (31−30)=1 (30−30)=0 (30−30)=0
जाँच: इनका जोड़ हमेशा 0 होता है।
(29−30)=−1 (31−30)=1 (30−30)=0 (28−30)=−2 (32−30)=2 (30−30)=0 (29−30)=−1 (31−30)=1 (30−30)=0 (30−30)=0
检查:它们的和永远是 0。
1 + 1 + 0 + 4 + 4 + 0 + 1 + 1 + 0 + 0 = 12
1 + 1 + 0 + 4 + 4 + 0 + 1 + 1 + 0 + 0 = 12
1 + 1 + 0 + 4 + 4 + 0 + 1 + 1 + 0 + 0 = 12
12 / (10 − 1) = 12 / 9 = 1.33 min²
12 / (10 − 1) = 12 / 9 = 1.33 min²
12 / (10 − 1) = 12 / 9 = 1.33 分钟²
Now PizzaPal: [10, 55, 8, 62, 15, 48, 20, 65, 12, 5] — mean = 30
अब PizzaPal: [10, 55, 8, 62, 15, 48, 20, 65, 12, 5] — mean = 30
再看 PizzaPal:[10, 55, 8, 62, 15, 48, 20, 65, 12, 5] —— 均值 = 30
(10−30)=−20 (55−30)=25 (8−30)=−22 (62−30)=32 (15−30)=−15 (48−30)=18 (20−30)=−10 (65−30)=35 (12−30)=−18 (5−30)=−25
(10−30)=−20 (55−30)=25 (8−30)=−22 (62−30)=32 (15−30)=−15 (48−30)=18 (20−30)=−10 (65−30)=35 (12−30)=−18 (5−30)=−25
(10−30)=−20 (55−30)=25 (8−30)=−22 (62−30)=32 (15−30)=−15 (48−30)=18 (20−30)=−10 (65−30)=35 (12−30)=−18 (5−30)=−25
400 + 625 + 484 + 1024 + 225 + 324 + 100 + 1225 + 324 + 625 = 5356
400 + 625 + 484 + 1024 + 225 + 324 + 100 + 1225 + 324 + 625 = 5356
400 + 625 + 484 + 1024 + 225 + 324 + 100 + 1225 + 324 + 625 = 5356
5356 / 9 = 595.11 min²
5356 / 9 = 595.11 min²
5356 / 9 = 595.11 分钟²
QuickSlice variance = 1.33 min² | PizzaPal variance = 595.11 min²
Same mean, but PizzaPal's variance is about 446 times larger. Now you have proof — not just a feeling — that PizzaPal is very unpredictable.
QuickSlice variance = 1.33 min² | PizzaPal variance = 595.11 min²
वही mean, पर PizzaPal का variance लगभग 446 गुना बड़ा है। अब आपके पास सबूत है — सिर्फ़ एहसास नहीं — कि PizzaPal बहुत अनिश्चित है।
QuickSlice 方差 = 1.33 分钟² | PizzaPal 方差 = 595.11 分钟²
均值相同,但 PizzaPal 的方差约是前者的 446 倍。现在你有了证据 —— 而不只是感觉 —— 证明 PizzaPal 非常不靠谱。
Python ImplementationPython ImplementationPython 实现
Manual calculationहाथ से गणना手动计算
quickslice = [29, 31, 30, 28, 32, 30, 29, 31, 30, 30]
pizzapal = [10, 55, 8, 62, 15, 48, 20, 65, 12, 5]
def sample_variance(data):
n = len(data)
mean = sum(data) / n
return sum((x - mean) ** 2 for x in data) / (n - 1)
print(f"QuickSlice variance: {sample_variance(quickslice):.2f}")
print(f"PizzaPal variance: {sample_variance(pizzapal):.2f}")
print(f"PizzaPal is {sample_variance(pizzapal) / sample_variance(quickslice):.0f}x more variable")
Using the statistics modulestatistics module से使用 statistics 模块
import statistics
quickslice = [29, 31, 30, 28, 32, 30, 29, 31, 30, 30]
pizzapal = [10, 55, 8, 62, 15, 48, 20, 65, 12, 5]
# Sample variance (divides by n - 1)
print(statistics.variance(quickslice))
print(statistics.variance(pizzapal))
# Population variance (divides by n)
print(statistics.pvariance(quickslice))
print(statistics.pvariance(pizzapal))
Using NumPy and pandasNumPy और pandas से使用 NumPy 和 pandas
import numpy as np
import pandas as pd
pizzapal = [10, 55, 8, 62, 15, 48, 20, 65, 12, 5]
# NumPy: ddof=0 (population) is the DEFAULT
print(np.var(pizzapal)) # divides by n
print(np.var(pizzapal, ddof=1)) # divides by n - 1
# pandas: ddof=1 (sample) is the DEFAULT — the opposite of NumPy!
s = pd.Series(pizzapal)
print(s.var()) # divides by n - 1
print(s.var(ddof=0)) # divides by n
np.var() uses ddof=0 by default (population variance), but pandas .var() uses ddof=1 (sample variance). The same data gives 535.6 in NumPy and 595.11 in pandas.
Always write ddof explicitly — this small detail causes many real bugs.
np.var() default में ddof=0 (population variance) इस्तेमाल करता है, पर pandas .var() ddof=1 (sample variance)। वही data NumPy में 535.6 और pandas में 595.11 देता है।
हमेशा ddof साफ़-साफ़ लिखें — यह छोटी बात बहुत सारे असली bugs की वजह बनती है।
np.var() 默认 ddof=0(总体方差),而 pandas 的 .var() 默认 ddof=1(样本方差)。同样的数据,NumPy 给出 535.6,pandas 给出 595.11。
一定要显式写出 ddof —— 这个小细节导致了很多真实的 bug。
Two Things to Watch: Squared Units and Outliersदो बातों का ध्यान रखें: Squared Units और Outliers需要注意的两件事:平方单位与离群点
Variance is powerful, but its unit is squared. If your data is in minutes, variance is in minutes squared, which is hard to picture.
Variance ताक़तवर है, पर इसकी unit squared होती है। अगर data minutes में है, तो variance minutes squared में होगा, जिसकी कल्पना करना मुश्किल है।
方差很有用,但它的单位是平方。数据以分钟为单位,方差的单位就是平方分钟,很难想象。
| DataData数据 | UnitUnit单位 | Variance unitVariance unit方差单位 | Easy to read?पढ़ना आसान?容易理解吗? |
|---|---|---|---|
| Delivery timesDelivery times送达时间 | minutesminutes分钟 | minutes²minutes²分钟² | Hard to interpretसमझना मुश्किल难以理解 |
| Heightsऊँचाई身高 | cm | cm² | Hard to interpretसमझना मुश्किल难以理解 |
| Pricesदाम价格 | ₹ | ₹² | Hard to interpretसमझना मुश्किल难以理解 |
| Temperaturesतापमान温度 | °C | °C² | Hard to interpretसमझना मुश्किल难以理解 |
Multiply every value by k (for example, minutes → seconds is k = 60) and add a constant c. The shaded band is mean ± one standard deviation.
हर value को k से गुणा करें (जैसे minutes → seconds में k = 60) और एक constant c जोड़ें। छायांकित पट्टी mean ± एक standard deviation है।
把每个值乘以 k(例如分钟 → 秒就是 k = 60),再加上常数 c。阴影带表示均值 ± 一个标准差。
Taking the square root of the variance gives the standard deviation, which is in the same unit as the data. QuickSlice: √1.33 ≈ 1.15 minutes; PizzaPal: √595.11 ≈ 24.4 minutes — easy to understand. That is why the standard deviation is used to explain spread, while variance is used inside formulas and models. The next tutorial covers standard deviation in full.
Variance का square root लेने से standard deviation मिलता है, जो data की ही unit में होता है। QuickSlice: √1.33 ≈ 1.15 मिनट; PizzaPal: √595.11 ≈ 24.4 मिनट — समझना आसान। इसीलिए फैलाव समझाने के लिए standard deviation इस्तेमाल होता है, जबकि variance formulas और models के अंदर। अगला tutorial standard deviation को पूरा cover करता है।
对方差开平方就得到标准差,单位和数据相同。QuickSlice:√1.33 ≈ 1.15 分钟;PizzaPal:√595.11 ≈ 24.4 分钟 —— 一目了然。 所以解释离散程度时用标准差,而在公式和模型内部用方差。下一篇教程会完整讲解标准差。
Outliers: one value can take overOutliers: एक value हावी हो सकती है离群点:一个值就能主导一切
Nine QuickSlice deliveries stay fixed (grey). Move the last one with the slider. The curve shows the variance for every position of that one value — it grows with the square of the distance.
QuickSlice की नौ deliveries अपनी जगह रहती हैं (स्लेटी)। आख़िरी को slider से खिसकाएँ। Curve उस एक value की हर जगह के लिए variance दिखाता है — यह दूरी के square के साथ बढ़ता है।
QuickSlice 的九次配送固定不动(灰色),用滑块移动最后一次。曲线显示这一个值处于每个位置时的方差 —— 它随距离的平方增长。
import numpy as np
data = [29, 31, 30, 28, 32, 30, 29, 31, 30, 30] # QuickSlice
print(f"Original : var = {np.var(data, ddof=1):8.2f} sd = {np.std(data, ddof=1):6.2f}")
# One late delivery: 30 minutes becomes 90 minutes
late = data[:-1] + [90]
print(f"One late delivery : var = {np.var(late, ddof=1):8.2f} sd = {np.std(late, ddof=1):6.2f}")
# Units: minutes → seconds multiplies every value by 60
secs = [x * 60 for x in data]
print(f"In seconds : var = {np.var(secs, ddof=1):8.2f} sd = {np.std(secs, ddof=1):6.2f}")
# Adding a constant (+5 minutes to every order) does not change the spread
plus5 = [x + 5 for x in data]
print(f"Every order +5 min: var = {np.var(plus5, ddof=1):8.2f} sd = {np.std(plus5, ddof=1):6.2f}")
One late delivery (90 minutes instead of 30) raises the variance from 1.33 to 361 — about 270 times. Converting minutes to seconds multiplies the variance by 60² = 3,600 (1.33 → 4,800), while adding 5 minutes to every order changes nothing.
एक देर वाली delivery (30 की जगह 90 मिनट) variance को 1.33 से 361 कर देती है — लगभग 270 गुना। Minutes को seconds में बदलने से variance 60² = 3,600 से गुणा हो जाता है (1.33 → 4,800), जबकि हर order में 5 मिनट जोड़ने से कुछ नहीं बदलता।
一次迟到的配送(90 分钟而不是 30 分钟)把方差从 1.33 推到 361 —— 约 270 倍。把分钟换成秒,方差乘以 60² = 3,600(1.33 → 4,800);而每单都加 5 分钟,方差完全不变。
Where Variance Is Used in Data ScienceData Science में Variance कहाँ इस्तेमाल होता है方差在数据科学中的应用
- Finds the directions of maximum varianceसबसे ज़्यादा variance वाली दिशाएँ खोजता है找出方差最大的方向
- Reduces the number of dimensionsDimensions की संख्या घटाता है减少维度
- Keeps the components that explain most varianceसबसे ज़्यादा variance समझाने वाले components रखता है保留解释方差最多的成分
- Compares the variance between groups with the variance within groupsGroups के बीच के variance की तुलना groups के अंदर के variance से करता है比较组间方差与组内方差
- Tests whether group means really differजाँचता है कि groups के means सच में अलग हैं या नहीं检验各组均值是否真有差异
- Used for experiments with 3+ versions3+ versions वाले experiments में इस्तेमाल用于 3 个及以上版本的实验
- Variance of returns = investment riskReturns का variance = निवेश का जोखिम收益率的方差 = 投资风险
- Portfolio optimisation (mean–variance)Portfolio optimisation (mean–variance)投资组合优化(均值—方差)
- Volatility analysisVolatility analysis波动率分析
₹1,00,000 invested in two funds. Both earn 1% per month on average. Fund A has a monthly standard deviation of 2%; set Fund B with the slider. Five random futures for each fund.
दो funds में ₹1,00,000 लगाए गए। दोनों औसतन हर महीने 1% कमाते हैं। Fund A का monthly standard deviation 2% है; Fund B का slider से set करें। हर fund के पाँच random भविष्य।
₹1,00,000 分别投入两只基金。两只基金平均每月都赚 1%。基金 A 的月标准差是 2%,基金 B 用滑块设置。每只基金模拟五种随机的未来。
Golden RulesGolden Rules黄金法则
ddof in code: NumPy defaults to 0, pandas to 1.ddof लिखें: NumPy का default 0 है, pandas का 1.ddof:NumPy 默认 0,pandas 默认 1。