Sample vs population variance calculator (n vs n − 1)
Sample variance divides the sum of squared deviations by n − 1; population variance divides by N. For 2, 4, 4, 4, 5, 5, 7, 9 the squared deviations add up to 32, so the sample variance is 32/7 = 4.571 and the population variance 32/8 = 4. numpy.var gives 4 by default, pandas .var() gives 4.571. Paste your numbers for every step, see why n − 1 is used, count samples, and work out the two-jacks and dice answers. Free, runs in your browser.
NIST e-Handbook 1.3.5.6 · Penn State STAT 414, 415 and 506 · NumPy, pandas and Python statistics docs · checked against an independent solution (2,159 checks at build) · Updated 24 Nov 2026
What are the mean, each deviation and squared deviation, the sample and population variance and SD, and what would numpy, pandas and statistics print for the same data?
- Sum of squared deviations 32; coefficient of variation (sample SD ÷ mean) 42.76 %.
| i | x | x − x̄ | (x − x̄)² |
|---|---|---|---|
| 1 | 2 | -3 | 9 |
| 2 | 4 | -1 | 1 |
| 3 | 4 | -1 | 1 |
| 4 | 4 | -1 | 1 |
| 5 | 5 | 0 | 0 |
| 6 | 5 | 0 | 0 |
| 7 | 7 | 2 | 4 |
| 8 | 9 | 4 | 16 |
| Σ | 40 | 0 | 32 |
| Python call | Returns | Formula |
|---|---|---|
| numpy.var(x) | 4 | population (ddof=0 default) |
| numpy.var(x, ddof=1) | 4.571428571 | sample |
| numpy.std(x) | 2 | population (ddof=0 default) |
| numpy.std(x, ddof=1) | 2.138089935 | sample |
| pandas.Series(x).var() | 4.571428571 | sample (ddof=1 default) |
| pandas.Series(x).std() | 2.138089935 | sample (ddof=1 default) |
| statistics.variance(x) | 4.571428571 | sample |
| statistics.pvariance(x) | 4 | population |
| statistics.stdev(x) | 2.138089935 | sample |
| statistics.pstdev(x) | 2 | population |
If we average each estimate over every possible sample, which one lands on the population variance?
| Exact, over every sample | Value |
|---|---|
| Samples enumerated | 36 (6², ordered, with replacement) |
| Population variance σ² (÷ N) | 2.9167 |
| Average of Σ(x − x̄)² ÷ n | 1.4583 = σ² × (n − 1)/n |
| Average of Σ(x − x̄)² ÷ (n − 1) | 2.9167 = σ² |
| Average of s (n − 1 SD) vs σ | 1.3749 vs 1.7078 |
- Even with n − 1, the SD is biased low: the square root of an unbiased variance is not an unbiased SD.
Simulation: 20,000 samples of each size, with replacement, mulberry32 seed 20261124. At n = 10 the ÷ n average is 2.6211 (exact 2.6250) and the ÷ (n − 1) average is 2.9123 (σ² = 2.9167).
How many different samples of size k can be drawn from a population of n, for each way of sampling?
- "n" is not the answer: with n = 5 there are 10 samples of size 2 and 31 non-empty samples of any size. C(n, k) equals n only when k = 1 or k = n − 1.
| Sampling | Formula | Count |
|---|---|---|
| Unordered, without replacement | C(n, k) | 10 |
| Ordered, without replacement | n!/(n − k)! | 20 |
| Ordered, with replacement | n^k | 25 |
| Unordered, with replacement | C(n + k − 1, k) | 15 |
| Any size 1 to n, without replacement | 2^n − 1 | 31 |
Exact integers (BigInt) up to n = 5,000. Above 15 digits the count is shown to 4 significant figures with its number of digits. A 5-card poker hand is C(52, 5) = 2,598,960.
Cards: what is the chance of exactly (or at least) j cards of one rank when k cards are drawn?
Dice: all the same, at least two the same, or a given sum, with m dice.
Stratified sample: how many units from each stratum (equal, proportional, Neyman), and how precise is each plan against a simple random sample of the same size?
Defaults: Penn State STAT 506's TV-hours example (three areas, σ = 5, 15, 10, n = 40). Rounding: each share is rounded down, then the units left go to the largest remainders (ties to the earlier stratum), so the plan adds up to n.
| Stratum | N_h | S_h | Equal | Proportional | Neyman |
|---|---|---|---|---|---|
| 1 | 155 | 5 | 14 | 20 (20.00) | 12 (11.7647) |
| 2 | 62 | 15 | 13 | 8 (8.00) | 14 (14.1176) |
| 3 | 93 | 10 | 13 | 12 (12.00) | 14 (14.1176) |
- Variance of the mean: equal 1.5488, proportional 1.9052, Neyman 1.5243, simple random sample 2.8317.
Stratified or cluster? (Penn State STAT 506, Lessons 6 to 8)
| Stratified | Cluster | |
|---|---|---|
| Groups | Strata: non-overlapping, as alike as possible inside | Clusters (primary units): each should look like the whole population |
| What is sampled | Units from every stratum | A few whole clusters; every unit in them is observed |
| Precision vs SRS | Usually better, most when strata are homogeneous | "No in most cases": usually worse |
| Why use it | Smaller error, lower cost per unit, estimates per subgroup | No list of units, only of clusters; travel cost |
| Example | Factory defects by production line | Households listed in 400 areas of 10, 4 areas surveyed |
Cluster sample vs SRS of the same number of units
- σ̂² = 3.033, s_u² = 195.7, Var(SRS)/Var(cluster) = 0.155: the cluster sample's variance is 6.5 times the SRS one.
Type your answers. Each one is checked, and a wrong one is matched to the slip that produces it.
- Type at least one answer, or fill the typical slips.
Why is the variance formula different for population and sample?
Both formulas add up squared deviations. The population formula divides by N and measures from the true mean μ. The sample formula divides by n − 1 and measures from the sample mean x̄ (NIST's e-Handbook writes the variance this way):
The sample mean is, by construction, the point closest to its own data in the squared sense, so deviations from x̄ are smaller on average than deviations from μ. Penn State's STAT 415 shows the size of the shortfall exactly. Dividing by n gives an average of σ²(n − 1)/n, "a biased estimator", while dividing by n − 1 gives σ², and it does so "for any model, not just the normal model":
Tab 2 proves it by brute force. Take a fair die as the population and list all 36 ordered samples of size 2. The divide-by-2 estimate averages exactly half the population variance, and the divide-by-1 estimate averages exactly σ²:
With samples of size 3 (216 of them) the divide-by-n average is 1.9444, two-thirds of 2.9167. The correction matters for small samples: at n = 30 it changes the answer by 3.4 %. NumPy's documentation calls it "Bessel's correction" because "it corrects for bias (toward lower values)". The square root does not stay unbiased. With n − 1, the average sample SD for the die and n = 2 is 1.3749, below σ = 1.7078. STAT 415: "S is not an unbiased estimator of σ".
One more twist from survey sampling. When you sample without replacement from a finite population, Penn State's STAT 506 defines the population variance with N − 1, and then s² averages exactly that. For the die, all 15 pairs without replacement give an average s² of 3.5: exactly the N − 1 variance (3.5), not the ÷ N value 2.9167.
Why do numpy and pandas give different variances for the same data?
Their defaults differ. numpy.var's signature reads "ddof=0", and "the divisor used in the calculation is N - ddof". pandas Series.var is "Normalized by N-1 by default". Python's statistics module has two functions: variance (n − 1) and pvariance (N). For the textbook data:
| Call | Returns | Divides by |
|---|---|---|
| numpy.var(x) | 4 | N |
| numpy.var(x, ddof=1) | 4.5714286 | n − 1 |
| numpy.std(x) | 2 | N |
| numpy.std(x, ddof=1) | 2.1380899 | n − 1 |
| pandas.Series(x).var() | 4.5714286 | n − 1 |
| pandas.Series(x).std() | 2.1380899 | n − 1 |
| statistics.variance(x) | 4.5714286 | n − 1 |
| statistics.pvariance(x) | 4 | N |
So a script that loads data with pandas and calls df.var() and another that calls np.var(df.values) will disagree by the factor n/(n − 1). pandas' own documentation says that to match numpy.std, use "ddof=0 (instead of the default ddof=1 )". For a sample, write numpy.var(x, ddof=1) explicitly so the reader knows which one you meant.
Why can the shortcut formula give a negative variance?
The shortcut subtracts two nearly equal large numbers:
On paper it equals the two-pass formula. In floating point, Σx² and (Σx)²/n can agree in most of their 16 digits, and the subtraction throws those digits away (catastrophic cancellation). For 10⁹ + 4, 10⁹ + 7, 10⁹ + 13 and 10⁹ + 16 the right sample variance is 30, but the shortcut in float64 gives -170.66666666666666, the value quoted on Wikipedia's algorithms page. The calculator subtracts the mean first (two passes), adds a small correction term, and also runs Welford's one-pass update (30 here). It warns when the shortcut would lose digits. Hand calculations do not have this problem; spreadsheets and scripts can.
How many different samples can be created from a population of size n?
The question needs a sample size k and a sampling scheme. Penn State's STAT 414 puts it this way: "always want to ask yourself whether or not the problem involves sampling with or without replacement". The five standard counts:
For n = 5 and k = 2: 10 unordered samples without replacement, 20 ordered, 25 ordered with replacement, 15 unordered with replacement, and 31 non-empty samples of any size. The answer "n" fits only k = 1 or k = n − 1, since C(n, 1) = C(n, n − 1) = n. "1" is right only when the sample is the whole population. The textbook reading of the course question, every possible sample of every size, is 2n − 1, because a sample with no members is not a sample.
What is the probability of drawing 2 jacks from a pack of cards?
A 52-card pack has 4 jacks. Drawing two cards together (or one after the other without putting the first back), the first is a jack with probability 4/52, and then the second with 3/51. Both must happen, so multiply:
If the first card goes back before the second draw, it is 4/52 × 4/52 = 1/169. "At least one jack in two cards" is a different question: 33/221 ≈ 0.1493. Common slips give 2/52, 4/52 + 3/51 or 6/52; tab 6 names each one.
What is the probability that both dice show the same number?
Two dice have 6 × 6 = 36 equally likely ordered outcomes. Six of them are doubles:
1/36 is one particular double, such as two sixes. Counting the 21 unordered outcomes as equally likely gives 6/21, which is wrong because (1, 2) can happen two ways and (1, 1) only one. With three dice, all the same is 1/36. Tab 4 also counts the outcomes one by one as a check on the formula.
What is the difference between stratified and cluster sampling?
Penn State's STAT 506: in stratified sampling "the population is partitioned into non-overlapping groups, called strata and a sample is selected by some design within each stratum". It "may produce a smaller error of estimation", most of all when units inside a stratum are alike. In a cluster sample "each sampling unit is a collection, or cluster, of elements", and clusters "should be formed so that one cluster is similar to another". Whether it is more precise is answered "no in most cases": cluster sampling is used because a list of clusters is easy to get or travel is costly.
Proportional allocation keeps the same sampling fraction in every stratum. Neyman (optimal) allocation also weights by the stratum SD, giving "a larger sample size to the larger and more variable stratum":
STAT 506's TV-hours example (Nh = 155, 62, 93; σh = 5, 15, 10; n = 40) gives Neyman shares 11.7647, 14.1176, 14.1176, chosen as 12, 14, 14. The tool reproduces the lesson's variance of the stratified mean, 1.97 (s = 5.95, 15.25, 9.36 with 20, 8, 12 sampled). For cluster samples, the lesson's household example has 400 clusters of 10 with sw² = 1.20 and sb² = 1.957:
That gives σ̂² = 3.03 and a ratio of 0.155. Treating that cluster sample as a simple random sample would understate the variance about 6.5 times. STAT 506 calls that "a BIG mistake".
Why use standard deviation when variance already measures dispersion?
Units. Squaring the deviations squares the unit, so the variance of lengths in mm is in mm². NIST: "The standard deviation restores the units of the spread to the original data units (the variance squares the units)". The SD can be read next to the data and the mean: "the diameters vary by about 0.02 mm" means something, "0.0004 mm²" does not.
Type a unit in tab 1 and the tool labels the variance in the squared unit and the SD in the data's unit. The coefficient of variation divides the SD by the mean, so it has no unit at all.
How 168 public reports answered the six questions (as of 23 Nov 2026)
We read the 168 public Skill-Lync reports titled "Basics of Probability and Statistics Week 1 Challenge" (texts fetched 23 Nov 2026 23:4x IST), n = 168. None is image-only (0 with under 150 words and three or more images), but 13 carry three or more screenshots whose content we could not read. Counts are of what the text shows. The links are examples.
- 102 / 168Q4 final answer 1/221 (or 0.0045, 0.45 %, 6/1326, 12/2652): without replacement, correct. report 1report 2
- 22 / 168Q4 answered 2/52 (= 1/26): treats it as 2 favourable of 52. report 1report 2
- 2 / 168Q4 with replacement only (1/169 or 0.0059). report 1report 2
- 19 / 168Q4 other wrong value or method. report 1report 2
- 146 / 168Q5 right: 1/6. report 1report 2
- 12 / 168Q5 wrong: 1/36. report 1report 2
- 56 / 168Q3 answer gives C(n,k) (alone or with n^k). report 1report 2
- 26 / 168Q3 answer reads the question as subsets: 2^n, 2^n - 1 or the sum form. report 1report 2
- 12 / 168Q3 answer: 1 (sample as large as population, one sample). report 1report 2
- 6 / 168Q3 answer: n samples. report 1report 2
- 17 / 168Q3 answered a different question (10% max sample size rule, Slovin, z-formula, SRS definitions). report 1report 2
- 155 / 168Q1 mentions n - 1 (or N - 1, one less). report 1report 2
- 68 / 168Q1 mentions bias / unbiased / biased. report 1report 2
- 20 / 168Q1 names Bessel (Bessel's correction). report 1report 2
- 21 / 168Q1 mentions degrees of freedom (or "free to vary"). report 1report 2
- 86 / 168Q1 gives none of: Bessel, unbiased/bias, degrees of freedom (n - 1 only or no reason). report 1report 2
- 107 / 168Q6 mentions units (squared units / same unit as the data). report 1report 2
- 6 / 168Hand-worked numeric variance example in the answer text. report 1report 2
- 2 / 6...of those, with an arithmetic slip that changes a reported number. report 1report 2
What stands out: 102 of 168 reach 1/221 for the two jacks, but 22 answer 2/52, as if "2 jacks" meant 2 favourable cards. For Q3, 56 give C(n, k) and 26 count subsets, while 17 answer a different question (a maximum sample size rule). 155 mention n − 1 for Q1, but 86 give no reason for it (no bias, Bessel or degrees of freedom). No report's text uses numpy or pandas, so the ddof default slip is not counted here. These are first-party counts, a starting point rather than a measure of ability.
How we checked the calculator
An independent Python script, scripts/verify/variance.py, rebuilds every number its own way. This page refuses to build if the browser code differs from it in any of 2,159 numbers, counts or verdicts (318 more assertions run inside the script, 258 cases). The script uses exact fractions and the statistics module, and matches the module's documented examples (variance 1.3720238095238095, stdev 1.0810874155219827). It reproduces STAT 506's beetle and TV numbers and the shortcut formula's −170.67. It enumerates every sample to prove E[s²] = σ² exactly, re-implements the mulberry32 generator bit for bit, and computes counts with Python's big integers. It brute-forces card and dice probabilities over every draw, and checks each verdict in tab 6. Two deliberate bugs (dividing the sample variance by n, and dropping Sh from the Neyman share) each make the build fail.
What this calculator does not cover
- Weighted data and frequency tables. Paste each value as many times as it occurs; weights are not supported.
- Confidence intervals and tests. The tool gives point estimates and their variance, not t intervals or degrees of freedom for a stratified design.
- Costs in allocation. Neyman allocation here assumes equal cost per unit. STAT 506 gives the cost-weighted version (nh ∝ NhSh/√ch).
- Strata smaller than their share. A Neyman share larger than Nh is flagged, not reallocated.
Variance, sampling and probability are the Week 1 challenge of the probability and statistics module in Skill-Lync's data analytics and data science program, before regression and machine learning on real datasets.
Frequently asked questions
Why do we divide by n − 1 for sample variance?
Because the deviations are measured from the sample mean, not the true mean. The sample mean sits closer to its own data than the population mean does, so squared deviations come out too small on average. Dividing by n gives an expected value of σ²(n − 1)/n; dividing by n − 1 (Bessel's correction) removes that bias exactly.
Why do numpy and pandas give different variances?
numpy.var and numpy.std divide by N by default (ddof=0), the population formula. pandas Series.var and std, and Python's statistics.variance and stdev, divide by n − 1 (ddof=1), the sample formula. For 2, 4, 4, 4, 5, 5, 7, 9 numpy gives 4 and pandas gives 4.571. Pass ddof=1 to numpy for a sample.
How many samples of size k can be drawn from a population of n?
It depends on the scheme. Without replacement, ignoring order, there are C(n, k); in order, n!/(n − k)!; with replacement in order, n^k; with replacement ignoring order, C(n + k − 1, k). Every non-empty sample of any size gives 2^n − 1. The answer n is right only for k = 1 or k = n − 1.
What is the probability of drawing 2 jacks from a pack of 52 cards?
For two cards drawn together, or one after another without putting the first back, it is 4/52 × 3/51 = 12/2652 = 1/221, about 0.45 %. That equals C(4, 2)/C(52, 2) = 6/1326. With replacement it is 4/52 × 4/52 = 1/169. The chance of at least one jack in two cards is 33/221.
What is the probability of getting the same number on two dice?
Two dice have 36 equally likely ordered outcomes, and six of them are doubles, (1, 1) to (6, 6). So the probability is 6/36 = 1/6, about 16.7 %. 1/36 is the chance of one particular double, such as two sixes. With three dice, all three the same is 6/216 = 1/36.
What is the difference between stratified and cluster sampling?
Stratified sampling splits the population into groups that are similar inside (strata) and samples from every group, which usually lowers the error. Cluster sampling splits it into groups such as villages or batches and surveys a few whole groups, which is cheaper but usually less precise. Clusters should each look like the whole population.
Related on Skill-Lync
Sources
- NIST/SEMATECH e-Handbook of Statistical Methods, 1.3.5.6 Measures of Scale: the variance and standard deviation with N − 1, the SD restores the units, average absolute deviation.
- Penn State STAT 415, Lesson 2 (unbiased estimation): E(σ̂²) = (n − 1)σ²/n, S² unbiased for any model, S not unbiased for σ. STAT 414, Lesson 3 (counting): sampling with and without replacement, permutations, combinations, 210 subsets.
- Penn State STAT 506, Lessons 1, 6, 7 and 8: SRS variance with the finite population correction and σ² over N − 1, stratified mean variance, proportional and optimal allocation (TV example), cluster sampling and its relative efficiency (cell phone example).
- NumPy v2.5 manual, numpy.var and numpy.std (ddof=0 default, Bessel's correction, the SD still biased); pandas 3.1.1, Series.var and DataFrame.std (ddof=1 default); Python 3.15 statistics module (variance, pvariance, stdev and their examples).
- Wikipedia, "Algorithms for calculating variance" (naive formula, catastrophic cancellation, −170.66666666666666, Welford's update) and "Multiset" (C(n + k − 1, k)).
- Skill-Lync, the 168 public probability and statistics reports, read 23 Nov 2026.