Markets: A Rigorous Course

Exercise · m14_graduation.py

How many trades separate edge from noise; false-positive rate of 'edge' tests

Exercise stubs. See notes.md 14.10. Trades are measured in R-multiples (see M03).

Tests (6)

  • test_t_stat
  • test_trades_needed
  • test_false_positive_rate_matches_student_t
  • test_false_positive_breakout_distribution_is_small_but_nonzero
  • test_power_matches_normal_approximation
  • test_familywise

Theory: M14 notes. Run with Ctrl/⌘+Enter.

not run yet

Not saved: sign up to keep your code
"""M14 · How many trades separate edge from noise; false-positive rate of 'edge' tests.

Exercise stubs. See notes.md §14.10. Trades are measured in R-multiples (see M03).
"""

from __future__ import annotations


def t_stat(r) -> float:
    """One-sample t-statistic mean(r) / (s/sqrt(N)) with s the ddof=1 standard deviation."""
    raise NotImplementedError


def trades_needed(expectancy: float, sd: float, t_crit: float = 2.0) -> int:
    """Smallest integer N with expectancy*sqrt(N)/sd >= t_crit: ceil((t_crit*sd/expectancy)^2).
    Guard against float round-up (subtract 1e-12 before ceil). M03 Corollary 3.1.1."""
    raise NotImplementedError


def trades_needed_power(expectancy: float, sd: float, t_crit: float = 2.0, power: float = 0.8) -> int:
    """N giving probability `power` that the t-stat clears t_crit when the true mean is `expectancy`
    (normal approximation): ceil(((t_crit + z_power) * sd / expectancy)^2), z_power = Phi^-1(power).
    M03 Corollary 3.1.2."""
    raise NotImplementedError


def approx_power(expectancy: float, sd: float, n: int, t_crit: float = 2.0) -> float:
    """Normal approximation 1 - Phi(t_crit - expectancy*sqrt(n)/sd)."""
    raise NotImplementedError


def simulate_pass_rate(n_trades: int, n_sims: int, seed: int, true_mean: float = 0.0,
                       t_crit: float = 2.0, kind: str = "normal") -> float:
    """Fraction of n_sims simulated traders (each with n_trades i.i.d. R-multiples) whose
    t-stat >= t_crit. Use np.random.default_rng(seed) and vectorise over a (n_sims, n_trades) array.

    kind='normal':   R ~ N(true_mean, 1).
    kind='breakout': R = +0.65/0.35 with prob 0.35, else -1, then add true_mean
                     (zero-mean two-point distribution typical of breakout systems).
    Raise ValueError for any other kind.
    """
    raise NotImplementedError


def familywise_false_positive(alpha: float, k: int) -> float:
    """P(at least one of k independent null setups passes) = 1 - (1 - alpha)^k."""
    raise NotImplementedError

No runs yet

The stub's functions raise NotImplementedError until you write them. Fill in one, press Run tests, and watch its tests turn green.

Reference solution

Available after your first run, with an account approved for code runs.

Create a free account

Reading is open to everyone. An account keeps your progress, answers, notes and code drafts, and gives you the study plan, the journals and the simulator. Running code needs an account approved for it.