KatInterview

Gaussian Fit Summary: Mean and Two Variance Estimates

Math & Bit ManipulationAuto-graded · 19 test cases
EasyAuto-gradedMath & Bit Manipulationstatisticsmatharrays

Last 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:

  1. Mean: the arithmetic average of the readings. This is the maximum likelihood estimate of the mean.
  2. 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.
  3. 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.fsum is recommended for accuracy.

Constraints

  • 2 <= n <= 10^5, where n is 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.