Maximum Likelihood Estimation for Uniform Distribution Bounds
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests understanding of MLE derivation for bounded distributions and bias correction in parameter estimation.
You are given a sample of observations drawn independently from a continuous uniform distribution over the interval [0, θ], where θ is an unknown positive parameter that you need to estimate.
Your task is to compute two estimators for θ:
-
Maximum Likelihood Estimator (MLE): Find the value of θ that maximizes the likelihood of observing the given sample.
-
Unbiased Estimator: Construct an estimator that corrects for the systematic bias present in the MLE.
Given a list of sample observations, return both estimates as a list of two floats: [mle_estimate, unbiased_estimate].
Background: For a uniform distribution U(0, θ), the likelihood function is positive only when θ is at least as large as the maximum observed value. Among all such valid values, smaller θ values yield higher likelihood. The MLE is therefore the smallest θ that makes all observations possible.
However, the MLE systematically underestimates the true θ because the sample maximum can never exceed θ. The expected value of the sample maximum from U(0, θ) with n observations is nθ/(n+1), which is less than θ. An unbiased estimator can be constructed by scaling the MLE appropriately.
Constraints
- 1 ≤ len(sample) ≤ 10^5
- All sample values are non-negative floats
- 0 ≤ sample[i] ≤ 10^6
- The sample is drawn from U(0, θ) for some unknown θ > 0 (or θ = 0 in degenerate cases)
- Return estimates rounded or exact to reasonable floating-point precision
Examples
Example 1
Input: estimate_uniform_parameter([0.2, 0.5, 0.8, 0.3])
Output: [0.8, 1]
Example 2
Input: estimate_uniform_parameter([1, 2.5, 1.7, 0.4, 2])
Output: [2.5, 3]
Example 3
Input: estimate_uniform_parameter([0, 0, 0])
Output: [0, 0]
Hints
Hint 1
The likelihood function for U(0, θ) is zero when θ is smaller than any observed value, and decreases as θ increases beyond that point
Hint 2
The MLE for θ must be the minimum value that keeps the likelihood positive, which relates directly to the sample's extreme values
Hint 3
To correct bias, consider that E[max(X₁,...,Xₙ)] = nθ/(n+1) for uniform samples, so scale the MLE by (n+1)/n
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires deriving estimators for the upper bound of a uniform distribution based on statistical theory.
Maximum Likelihood Estimator
For a uniform distribution U(0, θ), each observation has probability density 1/θ when 0 ≤ x ≤ θ, and zero otherwise. The joint likelihood for n independent observations is:
L(θ) = (1/θ)ⁿ if θ ≥ max(sample), and 0 otherwise
The likelihood is only positive when θ is at least as large as the largest observed value. For valid values of θ (those ≥ max(sample)), the likelihood decreases as θ increases, since (1/θ)ⁿ is a decreasing function.
Therefore, the likelihood is maximized at the boundary: θ̂_MLE = max(sample).
Unlike most MLE problems where you differentiate and set to zero, here the maximum occurs at a boundary point because the likelihood has a constraint (must be ≥ all observations) and is monotone decreasing in the feasible region.
Bias Correction
The MLE is biased downward. The sample maximum cannot exceed the true θ, so it systematically underestimates.
For n independent draws from U(0, θ), the distribution of the maximum M = max(X₁, ..., Xₙ) has CDF:
P(M ≤ x) = (x/θ)ⁿ for 0 ≤ x ≤ θ
Taking the derivative gives the PDF, and integrating x times the PDF yields:
E[M] = nθ/(n+1)
The bias is −θ/(n+1), meaning the MLE underestimates by roughly θ/n on average.
To construct an unbiased estimator, we scale up the MLE:
θ̂_unbiased = ((n+1)/n) × max(sample)
This ensures E[θ̂_unbiased] = (n+1)/n × nθ/(n+1) = θ.
Why This Matters
- The MLE is consistent (converges to θ as n→∞) and has minimum variance asymptotically, but for small samples, the bias can be substantial (10% underestimation when n=10).
- The unbiased estimator trades off: it has zero bias but slightly higher variance, particularly for small n.
- In practice, choosing between them depends on whether you prioritize bias or variance in your loss function.
Complexity
- Time: O(n) to find the maximum of n elements
- Space: O(1) auxiliary space
Both estimators require only a single pass through the data to compute the maximum.
Solution
from typing import List
def estimate_uniform_parameter(sample: List[float]) -> List[float]:
n = len(sample)
sample_max = max(sample)
mle_estimate = sample_max
unbiased_estimate = (n + 1) * sample_max / n
return [mle_estimate, unbiased_estimate]