Gaussian Fit Summary: Mean and Two Variance Estimates
Math & Bit ManipulationAuto-graded · 19 test casesLast updated
Compute the sample mean, the divide-by-n variance, and the divide-by-(n-1) variance from a list of numbers, tying MLE for a normal model to Bessel's correction.
Fitting a normal model to measurements
A monitoring script collects n independent numeric readings and models them as draws from a normal distribution with unknown mean and unknown variance. Maximizing the likelihood of this model gives closed-form estimates. The script also reports the usual bias-corrected variance.
Write a function that takes the readings and returns three numbers:
- Mean: the arithmetic average of the readings. This is the maximum likelihood estimate of the mean.
- MLE variance: the sum of squared differences between each reading and the mean, divided by
n. This is the maximum likelihood estimate of the variance. - Unbiased variance: the same sum of squared differences, divided by
n - 1(Bessel's correction).
Return them as a list in exactly this order: [mean, mle_variance, unbiased_variance].
Details
- Always use the sample mean computed from the given readings when forming the squared differences.
- If all readings are equal, both variances are
0. - Results are compared as floating-point numbers, so ordinary rounding differences are fine. Summing with
math.fsumis recommended for accuracy.
Constraints
2 <= n <= 10^5, wherenis the length of the input list.- Each reading is a finite real number (integer or float) with absolute value at most
10^6. - The return value is a list of exactly three floats.
Examples
Example 1
Input: gaussian_fit_summary([1, 2, 3, 4, 5])
Output: [3, 2, 2.5]
Example 2
Input: gaussian_fit_summary([10, 10, 10])
Output: [10, 0, 0]
Example 3
Input: gaussian_fit_summary([2, 4])
Output: [3, 1, 2]
Example 4
Input: gaussian_fit_summary([1.5, -2.5, 3, 0])
Output: [0.5, 4.125, 5.5]
Hints
Hint 1
Compute the mean first. Every other quantity depends on it.
Hint 2
Both variances share the same numerator: the sum of (x - mean)^2. Compute it once and divide it by two different denominators.
Hint 3
The MLE variance divides by n, and the unbiased variance divides by n - 1. Use math.fsum to reduce floating-point error when summing.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
For a normal model, setting the derivatives of the log-likelihood to zero gives two results. The best mean is the sample average. The best variance is the average squared distance from that sample average, so it divides by n. This estimate is biased low, because the distances are measured from the sample mean rather than the true mean. Dividing the same sum by n - 1 removes the bias.
So the algorithm has three steps:
- Compute the mean as the sum divided by
n. - Compute
S, the sum of squared deviations from that mean. - Return
[mean, S / n, S / (n - 1)].
Why it is correct
- The mean is exactly the closed-form MLE for the center of a normal distribution.
- Plugging that mean into the variance condition gives
S / nas the MLE variance. - Since
E[S] = (n - 1) * sigma^2, dividing byn - 1instead gives an unbiased estimate. This requiresn >= 2, which the constraints guarantee. - If all readings are equal, every deviation is zero, so both variances are zero.
Using math.fsum keeps the accumulated floating-point error small. The sum is computed once and reused for both variances.
Complexity
- Time:
O(n), with two passes over the data. - Space:
O(1)extra, apart from the generator used inside the sum.
Reference solution
import math
from typing import List
def gaussian_fit_summary(readings: List[float]) -> List[float]:
n = len(readings)
mean = math.fsum(readings) / n
sq_dev = math.fsum((x - mean) ** 2 for x in readings)
mle_variance = sq_dev / n
unbiased_variance = sq_dev / (n - 1)
return [mean, mle_variance, unbiased_variance]