{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"estimators-and-why-n-minus-one","title":"An estimator is a recipe, and why the variance divides by n − 1","url":"https://addaly.com/learn/maths-you-need/estimators-and-why-n-minus-one","position":49,"of":76,"minutes":9,"answer":"An estimator is a rule from sample to number with a bias and a variance of its own.","keyIdea":"An estimator is a rule from sample to number with a bias and a variance of its own, and the sample variance divides by n − 1 because distances measured from the sample's own mean are systematically too small by exactly one degree of freedom."},"body":{"format":"markdown","text":"## A number computed from a sample\n\nAn estimator is a rule: give it a sample, it gives back a number that stands in for something about the whole population. The sample mean estimates the population mean. The fraction of test items a model gets right estimates its accuracy on everything it will ever see. The rule is the estimator; the number it returns on one particular sample is the estimate.\n\nBecause the sample is random, the estimate is too. Draw a different sample and you get a different number. So an estimator has properties of its own, separate from any single result. Two matter most.\n\n- **Bias**: if you repeated the sampling forever and averaged all the estimates, would you land on the truth? If yes, the estimator is unbiased.\n- **Variance**: how far do the estimates scatter from one sample to the next?\n\nThe sample mean is unbiased for the population mean. The most common recipe for the variance is not, and the reason is worth seeing once with numbers.\n\n## Why dividing by n undercounts\n\nTake a population of two values, 0 and 2. Its mean is 1 and its variance, the average squared distance from the mean, is 1. Now draw samples of size two with replacement. There are four equally likely samples:\n\n```\nsample   sample mean   Σ(x − mean)²   ÷ n   ÷ (n−1)\n(0, 0)       0              0          0       0\n(0, 2)       1              2          1       2\n(2, 0)       1              2          1       2\n(2, 2)       2              0          0       0\n                          average:    0.5      1.0\n```\n\nDivide by `n` and the estimates average to 0.5, half the true variance. Divide by `n − 1` and they average to exactly 1. The `n − 1` is not a fudge; it is the correction that makes the average of the estimates equal the truth.\n\nThe mechanism: the formula measures distances from the *sample* mean, and the sample mean was chosen to be as close to the sample points as possible. Module 2 showed that the mean is the point minimising the sum of squared distances. So distances from it are systematically smaller than distances from the true mean, which the sample does not know. One degree of freedom was spent estimating the mean, and `n − 1` is what is left.\n\nAt `n = 1000` the two recipes differ by a tenth of a per cent and nobody cares. At `n = 5` they differ by 25 per cent, and small-sample statistics is full of places where they matter.\n\n## The trap in the libraries\n\nThe same column gives two different variances depending on which library you ask:\n\n```python\nimport numpy as np, pandas as pd, torch\nx = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nnp.var(x)                      # 8.25   divides by n\npd.Series(x).var()             # 9.17   divides by n − 1\ntorch.tensor(x, dtype=torch.float).var()   # 9.17   n − 1 by default\nnp.var(x, ddof=1)              # 9.17   ddof = 'delta degrees of freedom'\n```\n\nThe ratio is `n / (n − 1)`, here `10 / 9`. The standard deviation differs by the square root of that. If a number you computed disagrees with a colleague's by a few per cent, check `ddof` before anything else.\n\n## Unbiased is not the same as best\n\nBias is one property. An estimator's overall error combines both:\n\n```\nmean squared error = bias² + variance\n```\n\nA slightly biased estimator with much lower variance can be more accurate on any single sample. Dividing by `n` is biased but has lower variance than dividing by `n − 1`, and for some purposes it wins. The whole of regularisation, later in this module, is accepting a little bias to buy a lot less variance.\n\nTwo more surprises of the same kind. The sample standard deviation, the square root of the `n − 1` variance, is *still* biased, low. The square root is concave, so the average of square roots is less than the square root of the average (Jensen's inequality). The `n − 1` variance averages out at σ² exactly, so its square root averages out below σ. And the sample maximum is always a biased estimate of the population maximum: the largest of ten draws is never larger than the largest thing there is, and is usually smaller.\n\n## A useful estimator you were not taught\n\nThat last fact has a famous fix. Suppose items are numbered 1 to `N`, you see `k` of them at random, and the largest number you saw was `m`. The best estimate of `N` is not `m` but\n\n```\nN ≈ m + m/k − 1\n```\n\nSeeing five items with a maximum of 60 suggests `N ≈ 60 + 12 − 1 = 71`. Allied statisticians used this on the serial numbers of captured German tanks and estimated monthly production at 246 against a true figure of 245, while intelligence reports said 1,400. The same recipe estimates how many distinct records a system holds from a few sampled ids, or how many bugs a codebase has from the ones found so far. An estimator is a recipe, and some recipes are much better than the obvious one."},"check":{"question":"You compute the variance of a 10-row column with NumPy's default and with pandas' default and get 4.00 and 4.44. Which is which, and why do they differ?","options":[{"text":"NumPy gives 4.00 by dividing by n; pandas gives 4.44 by dividing by n − 1.","correct":true},{"text":"NumPy gives 4.44 because it applies the n − 1 correction; pandas gives 4.00 because it does not.","correct":false,"tempting":"Reverses the defaults: NumPy's ddof is 0 and pandas' is 1, which is exactly why the same column yields two numbers."},{"text":"The difference means one of the two libraries has a rounding bug in its variance routine.","correct":false,"tempting":"A ratio of exactly 10/9 is not rounding; it is the n versus n − 1 denominator, and both libraries are correct by their own definition."},{"text":"Pandas silently drops a missing value, so it divided by 9 rows instead of 10.","correct":false,"tempting":"Pandas does skip NaNs, but a column with no missing values still gives the n − 1 result; the denominator differs by design, not by data."}]},"prev":{"slug":"power-laws-and-log-log-plots","title":"Power laws, and the straight line on a log-log plot","url":"https://addaly.com/learn/maths-you-need/power-laws-and-log-log-plots"},"next":{"slug":"law-of-large-numbers-and-its-speed","title":"The law of large numbers, and how fast it works","url":"https://addaly.com/learn/maths-you-need/law-of-large-numbers-and-its-speed"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'An estimator is a recipe, and why the variance divides by n − 1', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/estimators-and-why-n-minus-one (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"An estimator is a recipe, and why the variance divides by n − 1","courseTitle":"The Maths You Actually Need","module":{"index":6,"slug":"estimation-from-samples","title":"Estimation: from a sample to a number you can trust"},"answer":null,"keyIdea":"An estimator is a rule from sample to number with a bias and a variance of its own, and the sample variance divides by n − 1 because distances measured from the sample’s own mean are systematically too small by exactly one degree of freedom.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"a-number-computed-from-a-sample","runs":[{"text":"A number computed from a sample"}]},{"t":"p","runs":[{"text":"An estimator is a rule: give it a sample, it gives back a number that stands in for something about the whole population. The sample mean estimates the population mean. The fraction of test items a model gets right estimates its accuracy on everything it will ever see. The rule is the estimator; the number it returns on one particular sample is the estimate."}]},{"t":"p","runs":[{"text":"Because the sample is random, the estimate is too. Draw a different sample and you get a different number. So an estimator has properties of its own, separate from any single result. Two matter most."}]},{"t":"ul","items":[[{"text":"Bias","b":true},{"text":": if you repeated the sampling forever and averaged all the estimates, would you land on the truth? If yes, the estimator is unbiased."}],[{"text":"Variance","b":true},{"text":": how far do the estimates scatter from one sample to the next?"}]]},{"t":"p","runs":[{"text":"The sample mean is unbiased for the population mean. The most common recipe for the variance is not, and the reason is worth seeing once with numbers."}]},{"t":"h2","id":"why-dividing-by-n-undercounts","runs":[{"text":"Why dividing by n undercounts"}]},{"t":"p","runs":[{"text":"Take a population of two values, 0 and 2. Its mean is 1 and its variance, the average squared distance from the mean, is 1. Now draw samples of size two with replacement. There are four equally likely samples:"}]},{"t":"code","lang":"","label":"Text","text":"sample   sample mean   Σ(x − mean)²   ÷ n   ÷ (n−1)\n(0, 0)       0              0          0       0\n(0, 2)       1              2          1       2\n(2, 0)       1              2          1       2\n(2, 2)       2              0          0       0\n                          average:    0.5      1.0"},{"t":"p","runs":[{"text":"Divide by "},{"text":"n","code":true},{"text":" and the estimates average to 0.5, half the true variance. Divide by "},{"text":"n − 1","code":true},{"text":" and they average to exactly 1. The "},{"text":"n − 1","code":true},{"text":" is not a fudge; it is the correction that makes the average of the estimates equal the truth."}]},{"t":"p","runs":[{"text":"The mechanism: the formula measures distances from the "},{"text":"sample","i":true},{"text":" mean, and the sample mean was chosen to be as close to the sample points as possible. Module 2 showed that the mean is the point minimising the sum of squared distances. So distances from it are systematically smaller than distances from the true mean, which the sample does not know. One degree of freedom was spent estimating the mean, and "},{"text":"n − 1","code":true},{"text":" is what is left."}]},{"t":"p","runs":[{"text":"At "},{"text":"n = 1000","code":true},{"text":" the two recipes differ by a tenth of a per cent and nobody cares. At "},{"text":"n = 5","code":true},{"text":" they differ by 25 per cent, and small-sample statistics is full of places where they matter."}]},{"t":"h2","id":"the-trap-in-the-libraries","runs":[{"text":"The trap in the libraries"}]},{"t":"p","runs":[{"text":"The same column gives two different variances depending on which library you ask:"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np, pandas as pd, torch\nx = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nnp.var(x)                      # 8.25   divides by n\npd.Series(x).var()             # 9.17   divides by n − 1\ntorch.tensor(x, dtype=torch.float).var()   # 9.17   n − 1 by default\nnp.var(x, ddof=1)              # 9.17   ddof = 'delta degrees of freedom'"},{"t":"p","runs":[{"text":"The ratio is "},{"text":"n / (n − 1)","code":true},{"text":", here "},{"text":"10 / 9","code":true},{"text":". The standard deviation differs by the square root of that. If a number you computed disagrees with a colleague’s by a few per cent, check "},{"text":"ddof","code":true},{"text":" before anything else."}]},{"t":"h2","id":"unbiased-is-not-the-same-as-best","runs":[{"text":"Unbiased is not the same as best"}]},{"t":"p","runs":[{"text":"Bias is one property. An estimator’s overall error combines both:"}]},{"t":"code","lang":"","label":"Text","text":"mean squared error = bias² + variance"},{"t":"p","runs":[{"text":"A slightly biased estimator with much lower variance can be more accurate on any single sample. Dividing by "},{"text":"n","code":true},{"text":" is biased but has lower variance than dividing by "},{"text":"n − 1","code":true},{"text":", and for some purposes it wins. The whole of regularisation, later in this module, is accepting a little bias to buy a lot less variance."}]},{"t":"p","runs":[{"text":"Two more surprises of the same kind. The sample standard deviation, the square root of the "},{"text":"n − 1","code":true},{"text":" variance, is "},{"text":"still","i":true},{"text":" biased, low. The square root is concave, so the average of square roots is less than the square root of the average (Jensen’s inequality). The "},{"text":"n − 1","code":true},{"text":" variance averages out at σ² exactly, so its square root averages out below σ. And the sample maximum is always a biased estimate of the population maximum: the largest of ten draws is never larger than the largest thing there is, and is usually smaller."}]},{"t":"h2","id":"a-useful-estimator-you-were-not-taught","runs":[{"text":"A useful estimator you were not taught"}]},{"t":"p","runs":[{"text":"That last fact has a famous fix. Suppose items are numbered 1 to "},{"text":"N","code":true},{"text":", you see "},{"text":"k","code":true},{"text":" of them at random, and the largest number you saw was "},{"text":"m","code":true},{"text":". The best estimate of "},{"text":"N","code":true},{"text":" is not "},{"text":"m","code":true},{"text":" but"}]},{"t":"code","lang":"","label":"Text","text":"N ≈ m + m/k − 1"},{"t":"p","runs":[{"text":"Seeing five items with a maximum of 60 suggests "},{"text":"N ≈ 60 + 12 − 1 = 71","code":true},{"text":". Allied statisticians used this on the serial numbers of captured German tanks and estimated monthly production at 246 against a true figure of 245, while intelligence reports said 1,400. The same recipe estimates how many distinct records a system holds from a few sampled ids, or how many bugs a codebase has from the ones found so far. An estimator is a recipe, and some recipes are much better than the obvious one."}]}],"check":{"question":[{"text":"You compute the variance of a 10-row column with NumPy’s default and with pandas’ default and get 4.00 and 4.44. Which is which, and why do they differ?"}],"options":[{"text":[{"text":"NumPy gives 4.00 by dividing by n; pandas gives 4.44 by dividing by n − 1."}],"correct":true,"tempting":null},{"text":[{"text":"NumPy gives 4.44 because it applies the n − 1 correction; pandas gives 4.00 because it does not."}],"correct":false,"tempting":[{"text":"Reverses the defaults: NumPy’s ddof is 0 and pandas’ is 1, which is exactly why the same column yields two numbers."}]},{"text":[{"text":"The difference means one of the two libraries has a rounding bug in its variance routine."}],"correct":false,"tempting":[{"text":"A ratio of exactly 10/9 is not rounding; it is the n versus n − 1 denominator, and both libraries are correct by their own definition."}]},{"text":[{"text":"Pandas silently drops a missing value, so it divided by 9 rows instead of 10."}],"correct":false,"tempting":[{"text":"Pandas does skip NaNs, but a column with no missing values still gives the n − 1 result; the denominator differs by design, not by data."}]}],"order":[3,2,0,1],"why":null},"prev":{"slug":"power-laws-and-log-log-plots","title":"Power laws, and the straight line on a log-log plot","minutes":9},"next":{"slug":"law-of-large-numbers-and-its-speed","title":"The law of large numbers, and how fast it works","minutes":9}}}