Fit Beta Parameters by Maximum Likelihood
Problem statement
For this practice exercise, fit a Beta(alpha,beta) distribution to the individual observations in samples. Its support is fixed to [0,1]: location is 0 and scale is 1. Both shape parameters must be positive.
Use the actual data to implement an iterative maximum-likelihood fit. Return [alpha,beta] at the unique finite likelihood maximum, to the numerical precision below. Repeated observations count repeatedly; sample order does not affect the mathematical answer.
The mean log-likelihood is
L(a,b) = logGamma(a+b) - logGamma(a) - logGamma(b)
+ (a-1) * mean(log(x)) + (b-1) * mean(log(1-x)).Maximize this expression jointly over a > 0 and b > 0. Implement the numerical iteration directly with standard-library arithmetic and elementary math; no statistics-fitting library is needed. Method-of-moments estimates may initialize the iteration, but they are not the required final answer. An arbitrary fixed number of updates is insufficient unless it actually achieves the requested fit precision.
Each coordinate is accepted when its error is at most 1e-6 * max(1, abs(expected)). Do not round to a fixed number of decimal places.
Interview context
The original final ML-fundamentals report asks how observed data estimates Beta parameters by MLE and explicitly asks for an iterative implementation. It does not supply the data, numerical solver, initialization, stopping rule or returned representation. The fixed support, input bounds, likelihood interface and output precision here are disclosed practice choices. The earlier phone simulation and other reports' plotting or KS-test exercises are separate tasks.
Function
fitBetaParameters(samples: double[]) → double[]Examples
Example 1
samples = [0.25,0.75]return = [1.9538947266061,1.9538947266061]Two distinct symmetric observations force equal positive shape estimates; moments alone is not the likelihood optimum. The displayed values are approximate likelihood estimates, independently computed from these observations.
Example 2
samples = [0.01,0.02,0.05,0.1,0.2,0.3]return = [0.89734372204648,7.0711783149515]Uses the actual log sufficient statistics and distinguishes an iterative likelihood fit from moments. The displayed values are approximate likelihood estimates, independently computed from these observations.
Example 3
samples = [0.01,0.99]return = [0.24418732507043,0.24418732507043]Both inclusive input-value endpoints are valid and produce shapes below 1; positivity is essential. The displayed values are approximate likelihood estimates, independently computed from these observations.
Constraints
- For this exercise, assume
2 <= samples.length <= 2000. - Every observation is finite and lies in
[0.01,0.99], including both endpoints. - For this exercise, assume the population variance
sum((x-mean)^2)/nis at least0.001. These bounds exclude constant and almost-constant samples, as well as support endpoints 0 and 1. - The fit estimates only positive shape parameters; location and scale are fixed as stated. Use double-precision arithmetic.