KatInterview

Tie-Aware Ranking Probabilities for Three and Four Tasters

Math & Bit ManipulationAuto-graded · 15 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilitycombinatoricsconditional-probability

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

KeyMeaning
"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_levels is an integer with num_levels >= 2 for 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.