KatInterview

Order Trading Strategies by Chance of a Positive Day

Heaps, Sorting & SearchAuto-graded · 14 test cases
MediumAuto-gradedHeaps, Sorting & Searchsortingstatisticscustom-key

Last updated

Rank strategies by the chance of a positive next-day return, using a predictive spread that includes both daily volatility and uncertainty in the estimated mean.

Which strategy is most likely to finish tomorrow in the green?

You have summary statistics for several trading strategies, each measured over a past window of trading days. Every strategy is described by:

  • strategy: a string label,
  • mu: its average daily return (in percent),
  • sigma: the standard deviation of its daily returns (in percent),
  • N: the number of daily observations behind these two estimates.

Order the strategies from the one most likely to have a strictly positive return tomorrow to the one least likely.

Probability model

Assume daily returns are normally distributed. The true mean is not known exactly. It is only estimated from N observations, so the estimate carries a standard error of sigma / sqrt(N).

Treat tomorrow's return as normal with:

  • center mu,
  • variance sigma^2 + sigma^2 / N, which combines ordinary day-to-day noise with the uncertainty in the estimated mean.

Under this model, the probability of a positive return is the standard normal CDF evaluated at mu / (sigma * sqrt(1 + 1/N)). Strategies are ranked by this probability, highest first.

Task

Implement a function that takes the list of strategy records and returns the list of labels in ranked order.

  • If two strategies have exactly the same computed score, keep them in the order they appeared in the input.
  • Because only the ordering matters, you do not need to compute the actual probabilities.

Constraints

  • 1 ≤ number of strategies ≤ 1000
  • sigma > 0 and N ≥ 1 for every record
  • mu may be positive, zero, or negative
  • Labels are unique strings
  • Each record is a dict with the keys "strategy", "mu", "sigma", "N"
  • The returned list contains every label exactly once

Examples

Example 1

Input: order_by_positive_chance([{'strategy': 'A', 'mu': 0.15, 'sigma': 0.8, 'N': 100}, {'strategy': 'B', 'mu': 0.2, 'sigma': 1.2, 'N': 80}, {'strategy': 'C', 'mu': 0.1, 'sigma': 0.5, 'N': 50}, {'strategy': 'D', 'mu': 0.3, 'sigma': 2.5, 'N': 20}, {'strategy': 'E', 'mu': 0.4, 'sigma': 4, 'N': 10}])

Output: ['C', 'A', 'B', 'D', 'E']

Hints

Hint 1

Probability of a positive outcome under a normal model depends only on how many standard deviations the center sits above zero.

Hint 2

Tomorrow's spread is larger than sigma because the mean itself is uncertain. Add the variance of the mean estimate, sigma^2 / N, to sigma^2 before taking the square root.

Hint 3

The normal CDF is increasing, so you can skip computing probabilities and sort by mu / (sigma * sqrt(1 + 1/N)) in descending order. Python's sort is stable, which handles ties.