Fit Beta Parameters by Maximum Likelihood
Updated
Problem statement
Fit a Beta(alpha,beta) distribution to the observations in samples. The distribution lives on [0,1]: its location is 0 and its scale is 1. Only the two shape parameters are unknown, and both must be positive.
Find them by maximum likelihood, using an iterative method on the actual data. Return [alpha,beta] at the one finite point where the likelihood is highest, to the precision given below. A value that appears several times counts several times. The order of the samples does not change the 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 it over a > 0 and b > 0 together. Write the iteration yourself with basic arithmetic and the standard math functions. You do not need a statistics-fitting library. You may start from the method-of-moments estimates, but they are not the answer. Running a fixed number of updates is not enough unless it really reaches the required precision.
Each returned value is accepted if its error is at most 1e-6 * max(1, abs(expected)). Do not round to a fixed number of decimal places.
Function
fitBetaParameters(samples: double[]) → double[]Examples
Example 1
samples = [0.25,0.75]return = [1.9538947266061,1.9538947266061]The two observations sit evenly on both sides of the middle, so alpha and beta come out equal. The method-of-moments estimate is not the likelihood maximum here. The values shown are approximate likelihood estimates, computed independently from these observations.
Example 2
samples = [0.01,0.02,0.05,0.1,0.2,0.3]return = [0.89734372204648,7.0711783149515]The fit uses the two log averages from the formula above, taken over the actual samples. Here an iterative likelihood fit gives a different result from the method of moments. The values shown are approximate likelihood estimates, computed independently from these observations.
Example 3
samples = [0.01,0.99]return = [0.24418732507043,0.24418732507043]Both ends of the allowed range are valid inputs. Here both shape estimates are below 1, which is why the rule that they must be positive matters. The values shown are approximate likelihood estimates, computed independently from these observations.
Constraints
2 <= samples.length <= 2000- Every observation is a finite number in
[0.01,0.99], and both ends of that range are allowed. - The population variance
sum((x-mean)^2)/nis at least0.001, wherenis the number of samples. Together, these limits rule out samples that are all equal or almost equal, and values at the ends 0 and 1. - Location and scale stay fixed as stated above. You estimate only the two positive shape parameters. Use double-precision arithmetic.
More Scale AI Onsite Interview questions
- Task Scheduler with DependenciesONSITE INTERVIEW · Seen Jul 2026
- Best Poker Category with a JokerPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Format and Validate a Model ResponseONSITE INTERVIEW · Seen Apr 2026
- Stateful Neuron Matrix UpdateONSITE INTERVIEW · Seen Apr 2026
- Clock Hand Angle with Subsecond PrecisionONSITE INTERVIEW · Seen Dec 2025
- Four-Player Matching Card GameONSITE INTERVIEW · Seen Feb 2025
- Rotate an Array to the RightONSITE INTERVIEW · Seen Oct 2024
- Rich-Text Segment Normalization and Bold ToggleONSITE INTERVIEW · Seen Apr 2024
- Minimum Distance Matching in a ForestPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2024
- Shortest Distance Between Two NodesONSITE INTERVIEW · Seen Jan 2024
Similar questions at other companies
- Max Points on a LineLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
- Deterministic K-Means AssignmentsMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
- Fixed-Round K-Means AssignmentsNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Confusing Bib NumbersVerily · ONSITE INTERVIEW
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Plus OneDruva · ONSITE INTERVIEW