Tie-Aware Ranking Probabilities for Three and Four Tasters
Math & Bit ManipulationAuto-graded · 15 test casesLast updated
Derive conditional ranking probabilities for i.i.d. scores, with and without ties, and return them as a dictionary keyed by named quantities.
Ranking probabilities with and without ties
A tasting panel scores several independent samples. Write S_A, S_B, S_C and S_D for the scores of four samples. In every model below the scores are independent and identically distributed.
Implement a function that takes an integer num_levels (the number of possible discrete score levels) and returns a dictionary of probabilities and facts about them.
Models
Continuous model. Each score is drawn from the same continuous distribution, so any two scores tie with probability 0.
Discrete model. Each score is an integer chosen uniformly and independently from {1, 2, ..., num_levels}, so ties are possible. In this model all comparisons are strict: "S_A > S_B" does not include equality.
Required output
Return a dictionary with exactly these keys:
| Key | Meaning |
|---|---|
"continuous_conditional" | In the continuous model, P(S_A > S_C | S_A > S_B). |
"continuous_distribution_independent" | True if the value above is the same for every continuous distribution, otherwise False. |
"discrete_tie_probability" | In the discrete model, P(S_A = S_B). |
"discrete_strict_probability" | In the discrete model, P(S_A > S_B). |
"discrete_conditional" | In the discrete model, P(S_A > S_C | S_A > S_B), as a function of num_levels. |
"discrete_limit" | The limit of "discrete_conditional" as num_levels goes to infinity. |
"limit_matches_continuous" | True if that limit equals "continuous_conditional", otherwise False. |
"four_sample_conditional" | In the continuous model with all four samples, P(S_C > S_D | S_A > S_B and S_A > S_C). |
Probabilities are returned as Python floats. The two boolean entries are Python booleans.
Invalid input
If num_levels < 2, raise a ValueError.
Constraints
num_levelsis an integer withnum_levels >= 2for valid calls.- All scores are mutually independent and identically distributed.
- Discrete comparisons are strict, so a tie counts as neither "greater" nor "less".
- The result must contain exactly the eight keys listed, with the meanings above.
Examples
Example 1
Input: ranking_probability_summary(2)
Output: {'continuous_conditional': 0.6666666666666666, 'continuous_distribution_independent': True, 'discrete_tie_probability': 0.5, 'discrete_strict_probability': 0.25, 'discrete_conditional': 0.5, 'discrete_limit': 0.6666666666666666, 'limit_matches_continuous': True, 'four_sample_conditional': 0.375}
Example 2
Input: ranking_probability_summary(3)
Output: {'continuous_conditional': 0.6666666666666666, 'continuous_distribution_independent': True, 'discrete_tie_probability': 0.3333333333333333, 'discrete_strict_probability': 0.3333333333333333, 'discrete_conditional': 0.5555555555555556, 'discrete_limit': 0.6666666666666666, 'limit_matches_continuous': True, 'four_sample_conditional': 0.375}
Example 3
Input: ranking_probability_summary(10)
Output: {'continuous_conditional': 0.6666666666666666, 'continuous_distribution_independent': True, 'discrete_tie_probability': 0.1, 'discrete_strict_probability': 0.45, 'discrete_conditional': 0.6333333333333333, 'discrete_limit': 0.6666666666666666, 'limit_matches_continuous': True, 'four_sample_conditional': 0.375}
Hints
Hint 1
In the continuous model ties never happen, so all orderings of the scores are equally likely. Count the orderings that satisfy each event and take a ratio.
Hint 2
In the discrete model, condition on the value a of S_A. Then S_B and S_C must each be at most a - 1, independently. Sum over a, and use the sum of squares formula for the joint event.
Hint 3
For the four-sample part, A must be the maximum of {A, B, C}. Place D uniformly among the possible slots in each of the two equally likely orderings of B and C below A, or count the 3 favorable orderings out of the 8 allowed ones.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every entry has a closed form, so the function evaluates formulas after validating the input. The work is in deriving them.
Continuous model (no ties)
Scores are i.i.d. and continuous, so all 3! = 6 orderings of (S_A, S_B, S_C) are equally likely.
P(S_A > S_B)= 3/6 = 1/2.P(S_A > S_B and S_A > S_C)= P(A is the largest) = 1/3.
So P(S_A > S_C | S_A > S_B) = (1/3) / (1/2) = 2/3. Only exchangeability and the absence of ties were used, so the value is the same for every continuous distribution and continuous_distribution_independent is True.
Discrete model with k = num_levels
P(S_A = S_B) = k * (1/k)^2 = 1/k.- By symmetry the two strict directions share the remaining mass, so
P(S_A > S_B) = (1 - 1/k) / 2 = (k - 1) / (2k). - For the joint event, condition on
S_A = a. Then B and C must each be belowa, giving probability((a-1)/k)^2. HenceP(A > B, A > C) = (1/k^3) * sum_{j=0}^{k-1} j^2 = (k-1)(2k-1) / (6k^2). - Dividing:
[(k-1)(2k-1)/(6k^2)] / [(k-1)/(2k)] = (2k - 1) / (3k).
As a check, with k = 2, A > B forces A = 2 and B = 1, and A > C then needs C = 1, which has probability 1/2. The formula gives 3/6 = 1/2.
The expression equals 2/3 - 1/(3k), which tends to 2/3. This equals the continuous value, so limit_matches_continuous is True.
Four samples, continuous model
All 24 orderings are equally likely.
- The condition "A > B and A > C" means A is the maximum of
{A, B, C}. That is 8 of the 24 orderings. - Adding
C > DgivesA > C > D, so A is the overall maximum. The remaining three samples{B, C, D}must satisfyC > D, which holds in 3 of their 6 orderings.
The probability is 3/8. Conditioning on C having lost to A makes C's score tend to be lower, which is why the answer is below 1/2.
Complexity
Time and space are O(1), since the function only evaluates a fixed set of formulas.
Reference solution
from typing import Any, Dict
def ranking_probability_summary(num_levels: int) -> Dict[str, Any]:
k = num_levels
if k < 2:
raise ValueError("num_levels must be at least 2")
return {
"continuous_conditional": 2 / 3,
"continuous_distribution_independent": True,
"discrete_tie_probability": 1 / k,
"discrete_strict_probability": (k - 1) / (2 * k),
"discrete_conditional": (2 * k - 1) / (3 * k),
"discrete_limit": 2 / 3,
"limit_matches_continuous": True,
"four_sample_conditional": 3 / 8,
}