Order Trading Strategies by Chance of a Positive Day
Heaps, Sorting & SearchAuto-graded · 14 test casesLast 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 > 0andN ≥ 1for every recordmumay 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Tomorrow's return has two sources of randomness:
- ordinary day-to-day variation, with variance
sigma^2; - error in the estimated average, with variance
sigma^2 / N.
Adding them gives a predictive variance of sigma^2 * (1 + 1/N). The return is then normal with center mu and standard deviation sigma * sqrt(1 + 1/N).
The probability that it exceeds zero is Phi(z), where
z = mu / (sigma * sqrt(1 + 1/N))
and Phi is the standard normal CDF. Since Phi is strictly increasing, ranking by probability is the same as ranking by z. We compute z for each strategy, sort in descending order, and output the labels.
Why it is correct
- A larger
zmeans the center sits more predictive standard deviations above zero, which means a larger chance of a positive result. - A high
sigmashrinksz, so volatile strategies are pulled toward a coin flip even if their mean is high. - A small
Ninflates the factorsqrt(1 + 1/N), which shrinkszfurther. This is the penalty for relying on a short history. - For one-day outcomes the volatility effect dominates and the sample-size effect is smaller. That is why a low-volatility strategy with a modest mean can beat a high-mean, high-volatility one.
- Python's sort is stable, so strategies with identical scores stay in input order, as the statement requires.
Complexity
- Time: O(k log k) for k strategies, dominated by the sort.
- Space: O(k) for the sorted copy and the output list.
Reference solution
from typing import Dict, List
def order_by_positive_chance(data: List[Dict]) -> List[str]:
# Tomorrow's return ~ Normal(mu, sigma^2 * (1 + 1/N)).
# P(return > 0) = Phi(mu / (sigma * sqrt(1 + 1/N))), and Phi is increasing,
# so sorting by the z-score gives the same order as sorting by probability.
def z_score(row):
return row["mu"] / (row["sigma"] * (1 + 1 / row["N"]) ** 0.5)
# sorted() is stable, so equal scores keep their input order
# (reverse=True preserves the original order of equal elements too).
ranked = sorted(data, key=z_score, reverse=True)
return [row["strategy"] for row in ranked]