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?

Sample variance s² (÷ n − 1)4.571SD s = 2.1381
Population variance σ² (÷ N)4SD σ = 2
Mean and n5n = 8, n/(n − 1) = 1.1429
  • Sum of squared deviations 32; coefficient of variation (sample SD ÷ mean) 42.76 %.
ixx − x̄(x − x̄)²
12-39
24-11
34-11
44-11
5500
6500
7724
89416
Σ40032
Python callReturnsFormula
numpy.var(x)4population (ddof=0 default)
numpy.var(x, ddof=1)4.571428571sample
numpy.std(x)2population (ddof=0 default)
numpy.std(x, ddof=1)2.138089935sample
pandas.Series(x).var()4.571428571sample (ddof=1 default)
pandas.Series(x).std()2.138089935sample (ddof=1 default)
statistics.variance(x)4.571428571sample
statistics.pvariance(x)4population
statistics.stdev(x)2.138089935sample
statistics.pstdev(x)2population
246812345678mean 5value number ix

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:

CallReturnsDivides by
numpy.var(x)4N
numpy.var(x, ddof=1)4.5714286n − 1
numpy.std(x)2N
numpy.std(x, ddof=1)2.1380899n − 1
pandas.Series(x).var()4.5714286n − 1
pandas.Series(x).std()2.1380899n − 1
statistics.variance(x)4.5714286n − 1
statistics.pvariance(x)4N

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.

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.
Learn statistics for data science

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.
Free counselling

From formulas to data decisions you can defend.

Analyst and data science roles want people who know which formula a library is using and why. A Skill-Lync counsellor can map the statistics, Python or data science program that fits your background.

Talk to a course counsellor

Free. We call you back, usually the same day.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.