broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(kind, strike, final, cash_amount):
    if kind.split('-')[1] == 'call':
        state = 1 if final > strike else (0.5 if final == strike else 0)
    else:
        state = 1 if final > strike else (0.5 if final == strike else 0)
    unit = cash_amount if kind.startswith('cash') else final
    return round(unit * state, 6)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression put moneyness 1', ['cash-put', 105, 104.5, 10], 10], ['regression put moneyness 2', ['cash-put', 100, 100.5, 100], 0], ['partial repair probe 1', ['cash-put', 95, 95, 1000], 500.0], ['partial repair probe 2', ['cash-put', 95, 95, 10], 5.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-call', 100, 100.5, 10], 100.5], ['normal control 2', ['asset-call', 100, 99.5, 1000], 0.0], ['normal control 3', ['cash-call', 105, 105, 1000], 500.0]], [['regression put moneyness 1', ['asset-put', 95, 95.5, 10], 0.0], ['regression put moneyness 2', ['asset-put', 105, 104.5, 10], 104.5], ['partial repair probe 1', ['asset-put', 100, 100, 1000], 50.0], ['partial repair probe 2', ['asset-put', 100, 100, 100], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 95, 95.5, 1000], 1000], ['normal control 2', ['cash-call', 100, 88, 1000], 0], ['normal control 3', ['asset-call', 105, 105.5, 10], 105.5]], [['regression put moneyness 1', ['asset-put', 100, 100.5, 1000], 0.0], ['regression put moneyness 2', ['asset-put', 95, 94.5, 100], 94.5], ['partial repair probe 1', ['cash-put', 100, 100, 1000], 500.0], ['partial repair probe 2', ['cash-put', 95, 95, 100], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-call', 100, 100.5, 1000], 100.5], ['normal control 2', ['asset-call', 105, 104.5, 100], 0.0], ['normal control 3', ['asset-call', 100, 100, 10], 50.0]], [['regression put moneyness 1', ['asset-put', 100, 107, 1000], 0], ['regression put moneyness 2', ['asset-put', 95, 102, 10], 0], ['partial repair probe 1', ['cash-put', 105, 105, 1000], 500.0], ['partial repair probe 2', ['asset-put', 95, 95, 100], 47.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 105, 93, 100], 0], ['normal control 2', ['cash-call', 100, 88, 10], 0], ['normal control 3', ['cash-call', 100, 107, 1000], 1000]], [['regression put moneyness 1', ['asset-put', 100, 100.5, 100], 0.0], ['regression put moneyness 2', ['cash-put', 105, 112, 100], 0], ['partial repair probe 1', ['asset-put', 100, 100, 10], 50.0], ['partial repair probe 2', ['cash-put', 105, 105, 10], 5.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 105, 105, 1000], 500.0], ['normal control 2', ['cash-call', 100, 99.5, 10], 0], ['normal control 3', ['asset-call', 105, 93, 1000], 0]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(kind, strike, final, cash_amount):
    if kind.split('-')[1] == 'call':
        state = 1 if final > strike else (0.5 if final == strike else 0)
    else:
        state = 1 if final < strike else 0
    unit = cash_amount if kind.startswith('cash') else final
    return round(unit * state, 6)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression put moneyness 1', ['cash-put', 105, 104.5, 10], 10], ['regression put moneyness 2', ['cash-put', 100, 100.5, 100], 0], ['partial repair probe 1', ['cash-put', 95, 95, 1000], 500.0], ['partial repair probe 2', ['cash-put', 95, 95, 10], 5.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-call', 100, 100.5, 10], 100.5], ['normal control 2', ['asset-call', 100, 99.5, 1000], 0.0], ['normal control 3', ['cash-call', 105, 105, 1000], 500.0]], [['regression put moneyness 1', ['asset-put', 95, 95.5, 10], 0.0], ['regression put moneyness 2', ['asset-put', 105, 104.5, 10], 104.5], ['partial repair probe 1', ['asset-put', 100, 100, 1000], 50.0], ['partial repair probe 2', ['asset-put', 100, 100, 100], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 95, 95.5, 1000], 1000], ['normal control 2', ['cash-call', 100, 88, 1000], 0], ['normal control 3', ['asset-call', 105, 105.5, 10], 105.5]], [['regression put moneyness 1', ['asset-put', 100, 100.5, 1000], 0.0], ['regression put moneyness 2', ['asset-put', 95, 94.5, 100], 94.5], ['partial repair probe 1', ['cash-put', 100, 100, 1000], 500.0], ['partial repair probe 2', ['cash-put', 95, 95, 100], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-call', 100, 100.5, 1000], 100.5], ['normal control 2', ['asset-call', 105, 104.5, 100], 0.0], ['normal control 3', ['asset-call', 100, 100, 10], 50.0]], [['regression put moneyness 1', ['asset-put', 100, 107, 1000], 0], ['regression put moneyness 2', ['asset-put', 95, 102, 10], 0], ['partial repair probe 1', ['cash-put', 105, 105, 1000], 500.0], ['partial repair probe 2', ['asset-put', 95, 95, 100], 47.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 105, 93, 100], 0], ['normal control 2', ['cash-call', 100, 88, 10], 0], ['normal control 3', ['cash-call', 100, 107, 1000], 1000]], [['regression put moneyness 1', ['asset-put', 100, 100.5, 100], 0.0], ['regression put moneyness 2', ['cash-put', 105, 112, 100], 0], ['partial repair probe 1', ['asset-put', 100, 100, 10], 50.0], ['partial repair probe 2', ['cash-put', 105, 105, 10], 5.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 105, 105, 1000], 500.0], ['normal control 2', ['cash-call', 100, 99.5, 10], 0], ['normal control 3', ['asset-call', 105, 93, 1000], 0]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Digital puts pay when the underlying rises.

Inputs kind (cash-call, cash-put, asset-call, asset-put), strike, final price and cash amount. Calls are in the money when final > strike, puts when final < strike; at final == strike both pay half. Cash-or-nothing pays the cash amount, asset-or-nothing pays the final price, times the state (1, 0.5 or 0). Round to 6.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(kind, strike, final, cash_amount):
    if kind.split('-')[1] == 'call':
        state = 1 if final > strike else (0.5 if final == strike else 0)
    else:
        state = 1 if final < strike else (0.5 if final == strike else 0)
    unit = cash_amount if kind.startswith('cash') else strike
    return round(unit * state, 6)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression asset-or-nothing unit 1', ['asset-call', 100, 107, 100], 107], ['regression asset-or-nothing unit 2', ['asset-put', 100, 99.5, 1000], 99.5], ['partial repair probe 1', ['asset-put', 100, 100, 1000], 50.0], ['partial repair probe 2', ['asset-put', 100, 100, 100], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-put', 100, 100.5, 1000], 0.0], ['normal control 2', ['cash-call', 95, 95, 1000], 500.0], ['normal control 3', ['cash-call', 100, 99.5, 100], 0]], [['regression asset-or-nothing unit 1', ['asset-put', 105, 104.5, 1000], 104.5], ['regression asset-or-nothing unit 2', ['asset-put', 95, 94.5, 1000], 94.5], ['partial repair probe 1', ['asset-put', 95, 95, 100], 47.5], ['partial repair probe 2', ['asset-call', 95, 95, 1000], 47.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-put', 100, 88, 100], 100], ['normal control 2', ['cash-put', 100, 100.5, 100], 0], ['normal control 3', ['asset-put', 95, 102, 100], 0]], [['regression asset-or-nothing unit 1', ['asset-put', 105, 93, 100], 93], ['regression asset-or-nothing unit 2', ['asset-put', 105, 104.5, 1000], 104.5], ['partial repair probe 1', ['asset-put', 95, 95, 1000], 47.5], ['partial repair probe 2', ['asset-call', 95, 95, 1000], 47.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 95, 95.5, 100], 100], ['normal control 2', ['cash-put', 100, 88, 10], 10], ['normal control 3', ['cash-put', 100, 107, 100], 0]], [['regression asset-or-nothing unit 1', ['asset-call', 95, 95.5, 10], 95.5], ['regression asset-or-nothing unit 2', ['asset-put', 105, 93, 1000], 93], ['partial repair probe 1', ['asset-call', 105, 105, 10], 52.5], ['partial repair probe 2', ['asset-put', 100, 100, 1000], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-put', 105, 104.5, 1000], 1000], ['normal control 2', ['cash-put', 95, 102, 1000], 0], ['normal control 3', ['asset-call', 95, 83, 1000], 0]], [['regression asset-or-nothing unit 1', ['asset-call', 95, 95.5, 10], 95.5], ['regression asset-or-nothing unit 2', ['asset-call', 100, 107, 1000], 107], ['partial repair probe 1', ['asset-call', 105, 105, 100], 52.5], ['partial repair probe 2', ['asset-call', 105, 105, 1000], 52.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 95, 95.5, 10], 10], ['normal control 2', ['cash-put', 95, 83, 10], 10], ['normal control 3', ['asset-call', 105, 104.5, 10], 0.0]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(kind, strike, final, cash_amount):
    if kind.split('-')[1] == 'call':
        state = 1 if final > strike else (0.5 if final == strike else 0)
    else:
        state = 1 if final < strike else (0.5 if final == strike else 0)
    unit = cash_amount if kind.startswith('cash') else final - strike
    return round(unit * state, 6)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression asset-or-nothing unit 1', ['asset-call', 100, 107, 100], 107], ['regression asset-or-nothing unit 2', ['asset-put', 100, 99.5, 1000], 99.5], ['partial repair probe 1', ['asset-put', 100, 100, 1000], 50.0], ['partial repair probe 2', ['asset-put', 100, 100, 100], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-put', 100, 100.5, 1000], 0.0], ['normal control 2', ['cash-call', 95, 95, 1000], 500.0], ['normal control 3', ['cash-call', 100, 99.5, 100], 0]], [['regression asset-or-nothing unit 1', ['asset-put', 105, 104.5, 1000], 104.5], ['regression asset-or-nothing unit 2', ['asset-put', 95, 94.5, 1000], 94.5], ['partial repair probe 1', ['asset-put', 95, 95, 100], 47.5], ['partial repair probe 2', ['asset-call', 95, 95, 1000], 47.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-put', 100, 88, 100], 100], ['normal control 2', ['cash-put', 100, 100.5, 100], 0], ['normal control 3', ['asset-put', 95, 102, 100], 0]], [['regression asset-or-nothing unit 1', ['asset-put', 105, 93, 100], 93], ['regression asset-or-nothing unit 2', ['asset-put', 105, 104.5, 1000], 104.5], ['partial repair probe 1', ['asset-put', 95, 95, 1000], 47.5], ['partial repair probe 2', ['asset-call', 95, 95, 1000], 47.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 95, 95.5, 100], 100], ['normal control 2', ['cash-put', 100, 88, 10], 10], ['normal control 3', ['cash-put', 100, 107, 100], 0]], [['regression asset-or-nothing unit 1', ['asset-call', 95, 95.5, 10], 95.5], ['regression asset-or-nothing unit 2', ['asset-put', 105, 93, 1000], 93], ['partial repair probe 1', ['asset-call', 105, 105, 10], 52.5], ['partial repair probe 2', ['asset-put', 100, 100, 1000], 50.0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-put', 105, 104.5, 1000], 1000], ['normal control 2', ['cash-put', 95, 102, 1000], 0], ['normal control 3', ['asset-call', 95, 83, 1000], 0]], [['regression asset-or-nothing unit 1', ['asset-call', 95, 95.5, 10], 95.5], ['regression asset-or-nothing unit 2', ['asset-call', 100, 107, 1000], 107], ['partial repair probe 1', ['asset-call', 105, 105, 100], 52.5], ['partial repair probe 2', ['asset-call', 105, 105, 1000], 52.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-call', 95, 95.5, 10], 10], ['normal control 2', ['cash-put', 95, 83, 10], 10], ['normal control 3', ['asset-call', 105, 104.5, 10], 0.0]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Asset-or-nothing payouts equal the strike instead of the underlying value.

Inputs kind (cash-call, cash-put, asset-call, asset-put), strike, final price and cash amount. Calls are in the money when final > strike, puts when final < strike; at final == strike both pay half. Cash-or-nothing pays the cash amount, asset-or-nothing pays the final price, times the state (1, 0.5 or 0). Round to 6.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(kind, strike, final, cash_amount):
    if kind.split('-')[0] == 'call':
        state = 1 if final > strike else (0.5 if final == strike else 0)
    else:
        state = 1 if final < strike else (0.5 if final == strike else 0)
    unit = cash_amount if kind.startswith('cash') else final
    return round(unit * state, 6)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression kind dispatch 1', ['cash-call', 105, 93, 10], 0], ['regression kind dispatch 2', ['asset-call', 105, 104.5, 10], 0.0], ['partial repair probe 1', ['asset-call', 95, 83, 100], 0], ['partial repair probe 2', ['asset-call', 105, 93, 1000], 0], ['boundary control 1', ['asset-put', 100, 90, 10], 90], ['boundary control 2', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-call', 100, 100, 10], 50.0], ['normal control 2', ['cash-put', 100, 100, 100], 50.0]], [['regression kind dispatch 1', ['cash-call', 105, 112, 1000], 1000], ['regression kind dispatch 2', ['cash-call', 95, 95.5, 100], 100], ['partial repair probe 1', ['asset-call', 100, 88, 1000], 0], ['partial repair probe 2', ['asset-call', 105, 93, 100], 0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['boundary control 2', ['asset-put', 100, 90, 10], 90], ['normal control 1', ['cash-put', 105, 105, 10], 5.0], ['normal control 2', ['cash-put', 100, 99.5, 1000], 1000]], [['regression kind dispatch 1', ['cash-call', 95, 94.5, 100], 0], ['regression kind dispatch 2', ['asset-call', 95, 95.5, 1000], 95.5], ['partial repair probe 1', ['cash-call', 100, 100.5, 1000], 1000], ['partial repair probe 2', ['asset-call', 95, 102, 100], 102], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['boundary control 2', ['asset-put', 100, 90, 10], 90], ['normal control 1', ['cash-put', 95, 95.5, 100], 0], ['normal control 2', ['asset-call', 100, 100, 1000], 50.0]], [['regression kind dispatch 1', ['cash-call', 95, 95.5, 10], 10], ['regression kind dispatch 2', ['cash-call', 95, 95.5, 1000], 1000], ['partial repair probe 1', ['cash-call', 100, 107, 100], 100], ['partial repair probe 2', ['asset-call', 105, 93, 100], 0], ['boundary control 1', ['asset-put', 100, 90, 10], 90], ['boundary control 2', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-put', 100, 107, 100], 0], ['normal control 2', ['asset-put', 105, 93, 10], 93]], [['regression kind dispatch 1', ['asset-call', 105, 104.5, 100], 0.0], ['regression kind dispatch 2', ['asset-call', 100, 88, 1000], 0], ['partial repair probe 1', ['cash-call', 105, 104.5, 100], 0], ['partial repair probe 2', ['asset-call', 100, 100.5, 100], 100.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['boundary control 2', ['asset-put', 100, 90, 10], 90], ['normal control 1', ['asset-put', 100, 100, 100], 50.0], ['normal control 2', ['asset-call', 105, 105, 100], 52.5]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(kind, strike, final, cash_amount):
    if 'call' in kind[:4]:
        state = 1 if final > strike else (0.5 if final == strike else 0)
    else:
        state = 1 if final < strike else (0.5 if final == strike else 0)
    unit = cash_amount if kind.startswith('cash') else final
    return round(unit * state, 6)
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression kind dispatch 1', ['cash-call', 105, 93, 10], 0], ['regression kind dispatch 2', ['asset-call', 105, 104.5, 10], 0.0], ['partial repair probe 1', ['asset-call', 95, 83, 100], 0], ['partial repair probe 2', ['asset-call', 105, 93, 1000], 0], ['boundary control 1', ['asset-put', 100, 90, 10], 90], ['boundary control 2', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['asset-call', 100, 100, 10], 50.0], ['normal control 2', ['cash-put', 100, 100, 100], 50.0]], [['regression kind dispatch 1', ['cash-call', 105, 112, 1000], 1000], ['regression kind dispatch 2', ['cash-call', 95, 95.5, 100], 100], ['partial repair probe 1', ['asset-call', 100, 88, 1000], 0], ['partial repair probe 2', ['asset-call', 105, 93, 100], 0], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['boundary control 2', ['asset-put', 100, 90, 10], 90], ['normal control 1', ['cash-put', 105, 105, 10], 5.0], ['normal control 2', ['cash-put', 100, 99.5, 1000], 1000]], [['regression kind dispatch 1', ['cash-call', 95, 94.5, 100], 0], ['regression kind dispatch 2', ['asset-call', 95, 95.5, 1000], 95.5], ['partial repair probe 1', ['cash-call', 100, 100.5, 1000], 1000], ['partial repair probe 2', ['asset-call', 95, 102, 100], 102], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['boundary control 2', ['asset-put', 100, 90, 10], 90], ['normal control 1', ['cash-put', 95, 95.5, 100], 0], ['normal control 2', ['asset-call', 100, 100, 1000], 50.0]], [['regression kind dispatch 1', ['cash-call', 95, 95.5, 10], 10], ['regression kind dispatch 2', ['cash-call', 95, 95.5, 1000], 1000], ['partial repair probe 1', ['cash-call', 100, 107, 100], 100], ['partial repair probe 2', ['asset-call', 105, 93, 100], 0], ['boundary control 1', ['asset-put', 100, 90, 10], 90], ['boundary control 2', ['cash-call', 100, 100, 10], 5.0], ['normal control 1', ['cash-put', 100, 107, 100], 0], ['normal control 2', ['asset-put', 105, 93, 10], 93]], [['regression kind dispatch 1', ['asset-call', 105, 104.5, 100], 0.0], ['regression kind dispatch 2', ['asset-call', 100, 88, 1000], 0], ['partial repair probe 1', ['cash-call', 105, 104.5, 100], 0], ['partial repair probe 2', ['asset-call', 100, 100.5, 100], 100.5], ['boundary control 1', ['cash-call', 100, 100, 10], 5.0], ['boundary control 2', ['asset-put', 100, 90, 10], 90], ['normal control 1', ['asset-put', 100, 100, 100], 50.0], ['normal control 2', ['asset-call', 105, 105, 100], 52.5]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Every digital is treated as a put.

Inputs kind (cash-call, cash-put, asset-call, asset-put), strike, final price and cash amount. Calls are in the money when final > strike, puts when final < strike; at final == strike both pay half. Cash-or-nothing pays the cash amount, asset-or-nothing pays the final price, times the state (1, 0.5 or 0). Round to 6.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression unbounded gain detection 1', [[['C', 80, 1, 0.5]], 1], [None, -0.5, [80.5]]], ['regression unbounded gain detection 2', [[['C', 100, 2, 7.75]], 100], [None, -1550.0, [107.75]]], ['partial repair probe 1', [[['S', 85, 2, 0.0], ['C', 100, -2, 0.5], ['C', 100, 1, 0.5], ['P', 85, -2, 7.75]], 1], [None, -324.0, [81.0]]], ['partial repair probe 2', [[['C', 120, 2, 5.1], ['C', 80, 1, 5.1], ['S', 105, -1, 0.0]], 1], [None, 9.7, []]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 110, -1, 5.1], ['P', 120, -2, 3.4], ['P', 120, -1, 1.25], ['C', 120, -1, 1.25]], 100], [440.0, None, [117.8, 122.2]]], ['normal control 2', [[['P', 110, 2, 0.5]], 100], [21900.0, -100.0, [109.5]]]], [['regression unbounded gain detection 1', [[['C', 110, 1, 5.1]], 100], [None, -510.0, [115.1]]], ['regression unbounded gain detection 2', [[['S', 110, -1, 0.0], ['C', 95, 1, 5.1], ['C', 110, -1, 5.1], ['S', 90, 2, 0.0]], 100], [None, -7000.0, [70.0]]], ['partial repair probe 1', [[['P', 100, 1, 2.0], ['C', 110, 2, 7.75], ['P', 100, -1, 2.0]], 100], [None, -1550.0, [117.75]]], ['partial repair probe 2', [[['P', 85, -1, 3.4], ['C', 105, 2, 3.4], ['P', 95, 1, 7.75], ['C', 100, -1, 5.1]], 100], [None, -1105.0, [88.95, 116.05]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 110, -1, 7.75]], 1], [7.75, None, [117.75]]], ['normal control 2', [[['C', 105, 2, 5.1], ['P', 80, 2, 2.0], ['P', 100, -2, 2.0], ['C', 90, -2, 1.25]], 1], [-27.7, -47.7, []]]], [['regression unbounded gain detection 1', [[['C', 120, 1, 5.1], ['C', 110, 2, 7.75], ['S', 85, -1, 0.0]], 1], [None, -45.6, [64.4, 137.8]]], ['regression unbounded gain detection 2', [[['C', 115, 2, 5.1]], 1], [None, -10.2, [120.1]]], ['partial repair probe 1', [[['S', 115, -1, 0.0], ['C', 120, 2, 2.0], ['P', 115, 2, 0.5], ['P', 120, -2, 7.75]], 100], [None, 50.0, []]], ['partial repair probe 2', [[['S', 120, 1, 0.0], ['C', 120, -2, 1.25], ['S', 110, 2, 0.0], ['P', 115, -2, 5.1]], 1], [None, -557.3, [111.46]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['S', 85, 2, 0.0], ['C', 120, -2, 5.1], ['C', 80, -1, 0.5]], 100], [4070.0, None, [79.65, 160.7]]], ['normal control 2', [[['C', 100, -1, 1.25]], 1], [1.25, None, [101.25]]]], [['regression unbounded gain detection 1', [[['C', 80, 2, 3.4]], 100], [None, -680.0, [83.4]]], ['regression unbounded gain detection 2', [[['C', 105, 1, 2.0], ['S', 85, -1, 0.0], ['P', 100, -1, 3.4], ['C', 105, 1, 2.0]], 100], [None, -2060.0, [125.6]]], ['partial repair probe 1', [[['C', 90, 2, 2.0], ['P', 105, 2, 7.75], ['P', 85, 1, 7.75], ['P', 115, -1, 0.5]], 1], [None, -21.75, [76.625, 107.25]]], ['partial repair probe 2', [[['S', 115, 1, 0.0], ['C', 105, 1, 3.4], ['S', 100, -1, 0.0]], 100], [None, -1840.0, [123.4]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 105, 1, 1.25], ['C', 95, -1, 3.4]], 100], [215.0, -785.0, [97.15]]], ['normal control 2', [[['P', 85, 2, 5.1], ['C', 85, -1, 1.25]], 100], [16105.0, None, [80.525]]]], [['regression unbounded gain detection 1', [[['S', 115, 2, 0.0]], 1], [None, -230.0, [115.0]]], ['regression unbounded gain detection 2', [[['C', 80, 1, 0.5], ['P', 115, 2, 0.5]], 100], [None, 3350.0, []]], ['partial repair probe 1', [[['C', 85, 2, 7.75], ['C', 105, 1, 0.5], ['P', 105, -2, 5.1]], 100], [None, -21580.0, [96.45]]], ['partial repair probe 2', [[['C', 110, 1, 0.5], ['P', 115, -1, 3.4]], 100], [None, -11210.0, [111.05]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['P', 100, 1, 0.5], ['C', 105, -1, 7.75], ['C', 100, 1, 5.1]], 1], [102.15, 2.15, []]], ['normal control 2', [[['C', 110, -1, 2.0], ['C', 115, -1, 2.0], ['P', 120, 1, 0.5], ['P', 95, 2, 7.75]], 1], [298.0, None, [108.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 and legs[-1][2] > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression unbounded gain detection 1', [[['C', 80, 1, 0.5]], 1], [None, -0.5, [80.5]]], ['regression unbounded gain detection 2', [[['C', 100, 2, 7.75]], 100], [None, -1550.0, [107.75]]], ['partial repair probe 1', [[['S', 85, 2, 0.0], ['C', 100, -2, 0.5], ['C', 100, 1, 0.5], ['P', 85, -2, 7.75]], 1], [None, -324.0, [81.0]]], ['partial repair probe 2', [[['C', 120, 2, 5.1], ['C', 80, 1, 5.1], ['S', 105, -1, 0.0]], 1], [None, 9.7, []]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 110, -1, 5.1], ['P', 120, -2, 3.4], ['P', 120, -1, 1.25], ['C', 120, -1, 1.25]], 100], [440.0, None, [117.8, 122.2]]], ['normal control 2', [[['P', 110, 2, 0.5]], 100], [21900.0, -100.0, [109.5]]]], [['regression unbounded gain detection 1', [[['C', 110, 1, 5.1]], 100], [None, -510.0, [115.1]]], ['regression unbounded gain detection 2', [[['S', 110, -1, 0.0], ['C', 95, 1, 5.1], ['C', 110, -1, 5.1], ['S', 90, 2, 0.0]], 100], [None, -7000.0, [70.0]]], ['partial repair probe 1', [[['P', 100, 1, 2.0], ['C', 110, 2, 7.75], ['P', 100, -1, 2.0]], 100], [None, -1550.0, [117.75]]], ['partial repair probe 2', [[['P', 85, -1, 3.4], ['C', 105, 2, 3.4], ['P', 95, 1, 7.75], ['C', 100, -1, 5.1]], 100], [None, -1105.0, [88.95, 116.05]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 110, -1, 7.75]], 1], [7.75, None, [117.75]]], ['normal control 2', [[['C', 105, 2, 5.1], ['P', 80, 2, 2.0], ['P', 100, -2, 2.0], ['C', 90, -2, 1.25]], 1], [-27.7, -47.7, []]]], [['regression unbounded gain detection 1', [[['C', 120, 1, 5.1], ['C', 110, 2, 7.75], ['S', 85, -1, 0.0]], 1], [None, -45.6, [64.4, 137.8]]], ['regression unbounded gain detection 2', [[['C', 115, 2, 5.1]], 1], [None, -10.2, [120.1]]], ['partial repair probe 1', [[['S', 115, -1, 0.0], ['C', 120, 2, 2.0], ['P', 115, 2, 0.5], ['P', 120, -2, 7.75]], 100], [None, 50.0, []]], ['partial repair probe 2', [[['S', 120, 1, 0.0], ['C', 120, -2, 1.25], ['S', 110, 2, 0.0], ['P', 115, -2, 5.1]], 1], [None, -557.3, [111.46]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['S', 85, 2, 0.0], ['C', 120, -2, 5.1], ['C', 80, -1, 0.5]], 100], [4070.0, None, [79.65, 160.7]]], ['normal control 2', [[['C', 100, -1, 1.25]], 1], [1.25, None, [101.25]]]], [['regression unbounded gain detection 1', [[['C', 80, 2, 3.4]], 100], [None, -680.0, [83.4]]], ['regression unbounded gain detection 2', [[['C', 105, 1, 2.0], ['S', 85, -1, 0.0], ['P', 100, -1, 3.4], ['C', 105, 1, 2.0]], 100], [None, -2060.0, [125.6]]], ['partial repair probe 1', [[['C', 90, 2, 2.0], ['P', 105, 2, 7.75], ['P', 85, 1, 7.75], ['P', 115, -1, 0.5]], 1], [None, -21.75, [76.625, 107.25]]], ['partial repair probe 2', [[['S', 115, 1, 0.0], ['C', 105, 1, 3.4], ['S', 100, -1, 0.0]], 100], [None, -1840.0, [123.4]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 105, 1, 1.25], ['C', 95, -1, 3.4]], 100], [215.0, -785.0, [97.15]]], ['normal control 2', [[['P', 85, 2, 5.1], ['C', 85, -1, 1.25]], 100], [16105.0, None, [80.525]]]], [['regression unbounded gain detection 1', [[['S', 115, 2, 0.0]], 1], [None, -230.0, [115.0]]], ['regression unbounded gain detection 2', [[['C', 80, 1, 0.5], ['P', 115, 2, 0.5]], 100], [None, 3350.0, []]], ['partial repair probe 1', [[['C', 85, 2, 7.75], ['C', 105, 1, 0.5], ['P', 105, -2, 5.1]], 100], [None, -21580.0, [96.45]]], ['partial repair probe 2', [[['C', 110, 1, 0.5], ['P', 115, -1, 3.4]], 100], [None, -11210.0, [111.05]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['P', 100, 1, 0.5], ['C', 105, -1, 7.75], ['C', 100, 1, 5.1]], 1], [102.15, 2.15, []]], ['normal control 2', [[['C', 110, -1, 2.0], ['C', 115, -1, 2.0], ['P', 120, 1, 0.5], ['P', 95, 2, 7.75]], 1], [298.0, None, [108.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Long calls report a finite maximum gain equal to the best breakpoint value.

Inputs legs [kind C/P/S, strike (purchase price for stock S), signed qty, premium] and a multiplier. Expiry value V(S) = multiplier * sum(q*(intrinsic - premium)) for options and q*(S - price) for stock, in exact fractions. Breakpoints are 0 and every strike. Tail slope = V(top+1)-V(top). Max gain is None if the slope > 0 else the max over breakpoints; max loss is None if slope < 0 else the min. Breakevens are zeros at breakpoints and linear-interpolated sign changes between them and in the tail. Return [gain, loss, breakevens] rounded to 4.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression unbounded loss detection 1', [[['C', 90, -1, 3.4], ['C', 100, 1, 0.5], ['C', 115, -1, 5.1]], 100], [800.0, None, [98.0]]], ['regression unbounded loss detection 2', [[['S', 105, -1, 0.0]], 100], [10500.0, None, [105.0]]], ['partial repair probe 1', [[['P', 105, 1, 7.75], ['S', 90, 2, 0.0], ['C', 100, -2, 2.0]], 1], [21.25, -78.75, [78.75]]], ['partial repair probe 2', [[['C', 90, -1, 7.75], ['P', 120, 2, 1.25], ['P', 105, 1, 3.4], ['S', 95, 1, 0.0]], 100], [25185.0, -315.0, [118.425]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['boundary control 2', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 100, -1, 0.5]], 100], [50.0, -9950.0, [99.5]]], ['normal control 2', [[['C', 100, 2, 0.5], ['P', 85, 2, 2.0]], 1], [None, -5.0, [82.5, 102.5]]]], [['regression unbounded loss detection 1', [[['S', 110, 1, 0.0], ['C', 110, -1, 7.75], ['C', 80, -1, 3.4]], 1], [-18.85, None, []]], ['regression unbounded loss detection 2', [[['P', 120, -1, 2.0], ['C', 95, -2, 7.75], ['C', 105, 1, 2.0], ['P', 80, -2, 3.4]], 1], [-2.7, None, []]], ['partial repair probe 1', [[['C', 120, 1, 7.75], ['P', 110, -1, 7.75], ['P', 85, -2, 3.4], ['C', 95, -1, 3.4]], 100], [-480.0, -26980.0, []]], ['partial repair probe 2', [[['S', 85, 1, 0.0], ['C', 110, -2, 3.4], ['S', 90, 1, 0.0]], 1], [51.8, -168.2, [84.1]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['boundary control 2', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 115, 1, 7.75], ['P', 105, 1, 0.5], ['P', 85, -2, 5.1], ['S', 100, 1, 0.0]], 100], [None, -4805.0, [48.05]]], ['normal control 2', [[['P', 85, -1, 5.1], ['C', 110, 2, 5.1], ['P', 105, 1, 2.0]], 1], [None, -7.1, [97.9, 113.55]]]], [['regression unbounded loss detection 1', [[['C', 100, 1, 1.25], ['S', 80, -1, 0.0], ['C', 120, -1, 0.5]], 100], [7925.0, None, [79.25]]], ['regression unbounded loss detection 2', [[['S', 95, -2, 0.0], ['C', 95, -1, 2.0], ['C', 90, 1, 1.25]], 1], [190.75, None, [97.875]]], ['partial repair probe 1', [[['P', 105, -1, 5.1], ['P', 95, 1, 1.25], ['C', 100, -1, 1.25], ['C', 90, 2, 3.4]], 100], [None, -1170.0, [95.5667]]], ['partial repair probe 2', [[['P', 85, 1, 5.1], ['S', 110, 1, 0.0], ['C', 110, -1, 0.5]], 1], [-4.6, -29.6, []]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 105, 1, 1.25], ['C', 80, 2, 3.4]], 100], [None, 1695.0, []]], ['normal control 2', [[['P', 115, 1, 1.25], ['P', 90, -1, 1.25]], 1], [25.0, 0.0, [115.0]]]], [['regression unbounded loss detection 1', [[['C', 85, -2, 7.75], ['P', 100, 2, 1.25], ['S', 100, -2, 0.0], ['C', 95, 1, 3.4]], 100], [40960.0, None, [96.92]]], ['regression unbounded loss detection 2', [[['C', 100, -1, 7.75], ['C', 115, -2, 5.1], ['C', 110, 1, 7.75], ['P', 115, -2, 7.75]], 100], [1570.0, None, [104.3, 122.85]]], ['partial repair probe 1', [[['C', 105, -2, 1.25], ['P', 115, 1, 3.4], ['S', 115, 1, 0.0], ['C', 95, 1, 2.0]], 100], [710.0, -290.0, [97.9, 112.1]]], ['partial repair probe 2', [[['C', 115, -1, 2.0], ['P', 115, -2, 5.1], ['S', 120, 1, 0.0], ['C', 100, 1, 0.5]], 1], [None, -338.3, [109.575]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 95, 1, 1.25], ['S', 85, -2, 0.0], ['C', 110, 1, 2.0], ['C', 80, 2, 2.0]], 100], [None, 275.0, []]], ['normal control 2', [[['P', 95, 2, 0.5], ['P', 100, 1, 0.5], ['P', 115, -2, 7.75], ['S', 110, 1, 0.0]], 1], [None, -36.0, [108.6667]]]], [['regression unbounded loss detection 1', [[['C', 100, -2, 0.5], ['C', 100, -1, 0.5], ['P', 80, 1, 3.4], ['P', 110, 2, 0.5]], 100], [29710.0, None, [103.42]]], ['regression unbounded loss detection 2', [[['P', 100, -1, 0.5], ['C', 110, -1, 7.75], ['P', 95, 1, 2.0]], 1], [6.25, None, [116.25]]], ['partial repair probe 1', [[['C', 80, -1, 1.25], ['S', 115, 2, 0.0], ['P', 105, -1, 0.5], ['C', 100, 1, 2.0]], 1], [None, -335.25, [125.125]]], ['partial repair probe 2', [[['C', 80, -1, 5.1], ['S', 115, 1, 0.0], ['P', 100, -1, 5.1], ['P', 120, 2, 0.5]], 1], [34.2, -25.8, [107.1]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 85, 1, 7.75], ['C', 100, 1, 3.4], ['P', 110, -2, 3.4]], 1], [None, -139.35, [108.1167]]], ['normal control 2', [[['P', 90, -2, 2.0]], 1], [4.0, -176.0, [88.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if any(l[0] == 'C' and l[2] < 0 for l in legs) else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression unbounded loss detection 1', [[['C', 90, -1, 3.4], ['C', 100, 1, 0.5], ['C', 115, -1, 5.1]], 100], [800.0, None, [98.0]]], ['regression unbounded loss detection 2', [[['S', 105, -1, 0.0]], 100], [10500.0, None, [105.0]]], ['partial repair probe 1', [[['P', 105, 1, 7.75], ['S', 90, 2, 0.0], ['C', 100, -2, 2.0]], 1], [21.25, -78.75, [78.75]]], ['partial repair probe 2', [[['C', 90, -1, 7.75], ['P', 120, 2, 1.25], ['P', 105, 1, 3.4], ['S', 95, 1, 0.0]], 100], [25185.0, -315.0, [118.425]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['boundary control 2', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 100, -1, 0.5]], 100], [50.0, -9950.0, [99.5]]], ['normal control 2', [[['C', 100, 2, 0.5], ['P', 85, 2, 2.0]], 1], [None, -5.0, [82.5, 102.5]]]], [['regression unbounded loss detection 1', [[['S', 110, 1, 0.0], ['C', 110, -1, 7.75], ['C', 80, -1, 3.4]], 1], [-18.85, None, []]], ['regression unbounded loss detection 2', [[['P', 120, -1, 2.0], ['C', 95, -2, 7.75], ['C', 105, 1, 2.0], ['P', 80, -2, 3.4]], 1], [-2.7, None, []]], ['partial repair probe 1', [[['C', 120, 1, 7.75], ['P', 110, -1, 7.75], ['P', 85, -2, 3.4], ['C', 95, -1, 3.4]], 100], [-480.0, -26980.0, []]], ['partial repair probe 2', [[['S', 85, 1, 0.0], ['C', 110, -2, 3.4], ['S', 90, 1, 0.0]], 1], [51.8, -168.2, [84.1]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['boundary control 2', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 115, 1, 7.75], ['P', 105, 1, 0.5], ['P', 85, -2, 5.1], ['S', 100, 1, 0.0]], 100], [None, -4805.0, [48.05]]], ['normal control 2', [[['P', 85, -1, 5.1], ['C', 110, 2, 5.1], ['P', 105, 1, 2.0]], 1], [None, -7.1, [97.9, 113.55]]]], [['regression unbounded loss detection 1', [[['C', 100, 1, 1.25], ['S', 80, -1, 0.0], ['C', 120, -1, 0.5]], 100], [7925.0, None, [79.25]]], ['regression unbounded loss detection 2', [[['S', 95, -2, 0.0], ['C', 95, -1, 2.0], ['C', 90, 1, 1.25]], 1], [190.75, None, [97.875]]], ['partial repair probe 1', [[['P', 105, -1, 5.1], ['P', 95, 1, 1.25], ['C', 100, -1, 1.25], ['C', 90, 2, 3.4]], 100], [None, -1170.0, [95.5667]]], ['partial repair probe 2', [[['P', 85, 1, 5.1], ['S', 110, 1, 0.0], ['C', 110, -1, 0.5]], 1], [-4.6, -29.6, []]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 105, 1, 1.25], ['C', 80, 2, 3.4]], 100], [None, 1695.0, []]], ['normal control 2', [[['P', 115, 1, 1.25], ['P', 90, -1, 1.25]], 1], [25.0, 0.0, [115.0]]]], [['regression unbounded loss detection 1', [[['C', 85, -2, 7.75], ['P', 100, 2, 1.25], ['S', 100, -2, 0.0], ['C', 95, 1, 3.4]], 100], [40960.0, None, [96.92]]], ['regression unbounded loss detection 2', [[['C', 100, -1, 7.75], ['C', 115, -2, 5.1], ['C', 110, 1, 7.75], ['P', 115, -2, 7.75]], 100], [1570.0, None, [104.3, 122.85]]], ['partial repair probe 1', [[['C', 105, -2, 1.25], ['P', 115, 1, 3.4], ['S', 115, 1, 0.0], ['C', 95, 1, 2.0]], 100], [710.0, -290.0, [97.9, 112.1]]], ['partial repair probe 2', [[['C', 115, -1, 2.0], ['P', 115, -2, 5.1], ['S', 120, 1, 0.0], ['C', 100, 1, 0.5]], 1], [None, -338.3, [109.575]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 95, 1, 1.25], ['S', 85, -2, 0.0], ['C', 110, 1, 2.0], ['C', 80, 2, 2.0]], 100], [None, 275.0, []]], ['normal control 2', [[['P', 95, 2, 0.5], ['P', 100, 1, 0.5], ['P', 115, -2, 7.75], ['S', 110, 1, 0.0]], 1], [None, -36.0, [108.6667]]]], [['regression unbounded loss detection 1', [[['C', 100, -2, 0.5], ['C', 100, -1, 0.5], ['P', 80, 1, 3.4], ['P', 110, 2, 0.5]], 100], [29710.0, None, [103.42]]], ['regression unbounded loss detection 2', [[['P', 100, -1, 0.5], ['C', 110, -1, 7.75], ['P', 95, 1, 2.0]], 1], [6.25, None, [116.25]]], ['partial repair probe 1', [[['C', 80, -1, 1.25], ['S', 115, 2, 0.0], ['P', 105, -1, 0.5], ['C', 100, 1, 2.0]], 1], [None, -335.25, [125.125]]], ['partial repair probe 2', [[['C', 80, -1, 5.1], ['S', 115, 1, 0.0], ['P', 100, -1, 5.1], ['P', 120, 2, 0.5]], 1], [34.2, -25.8, [107.1]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 85, 1, 7.75], ['C', 100, 1, 3.4], ['P', 110, -2, 3.4]], 1], [None, -139.35, [108.1167]]], ['normal control 2', [[['P', 90, -2, 2.0]], 1], [4.0, -176.0, [88.0]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Covered calls report unbounded loss while naked short stock reports a finite loss.

Inputs legs [kind C/P/S, strike (purchase price for stock S), signed qty, premium] and a multiplier. Expiry value V(S) = multiplier * sum(q*(intrinsic - premium)) for options and q*(S - price) for stock, in exact fractions. Breakpoints are 0 and every strike. Tail slope = V(top+1)-V(top). Max gain is None if the slope > 0 else the max over breakpoints; max loss is None if slope < 0 else the min. Breakevens are zeros at breakpoints and linear-interpolated sign changes between them and in the tail. Return [gain, loss, breakevens] rounded to 4.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) + p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression call premium sign 1', [[['S', 85, 1, 0.0], ['C', 100, -2, 2.0], ['C', 90, -1, 0.5], ['C', 110, -2, 3.4]], 1], [16.3, None, [73.7, 108.15]]], ['regression call premium sign 2', [[['P', 95, -2, 3.4], ['C', 80, 1, 5.1]], 1], [None, -188.3, [89.4333]]], ['partial repair probe 1', [[['C', 120, -2, 7.75], ['P', 100, -2, 3.4], ['C', 105, 2, 7.75], ['P', 120, -2, 5.1]], 1], [47.0, -423.0, [108.25]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['S', 105, -1, 0.0], ['P', 80, 1, 3.4]], 100], [18160.0, None, [101.6]]], ['normal control 2', [[['P', 80, -1, 1.25], ['S', 85, 2, 0.0]], 100], [None, -24875.0, [84.375]]], ['normal control 3', [[['P', 95, 1, 1.25], ['S', 90, 2, 0.0], ['P', 80, -2, 5.1]], 1], [None, -236.05, [78.6833]]], ['normal control 4', [[['P', 95, 2, 5.1], ['P', 90, 1, 0.5], ['P', 105, 2, 2.0]], 100], [47530.0, -1470.0, [97.65]]]], [['regression call premium sign 1', [[['S', 80, -2, 0.0], ['C', 85, -1, 1.25], ['P', 100, 2, 0.5], ['C', 90, 1, 5.1]], 100], [35515.0, None, [88.03]]], ['regression call premium sign 2', [[['P', 85, -1, 0.5], ['C', 85, 2, 0.5]], 1], [None, -85.5, [85.25]]], ['partial repair probe 1', [[['P', 115, -2, 1.25], ['P', 80, -2, 3.4], ['C', 105, -2, 5.1], ['C', 95, 2, 5.1]], 1], [29.3, -380.7, [102.675]]], ['partial repair probe 2', [[['C', 105, -1, 5.1], ['P', 95, 1, 5.1], ['C', 85, 1, 5.1], ['P', 100, 1, 1.25]], 100], [18865.0, 865.0, []]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 110, -1, 5.1], ['S', 85, 2, 0.0], ['S', 110, 2, 0.0]], 100], [None, -49490.0, [98.98]]], ['normal control 2', [[['P', 80, -1, 7.75]], 100], [775.0, -7225.0, [72.25]]], ['normal control 3', [[['P', 90, 2, 3.4]], 1], [173.2, -6.8, [86.6]]]], [['regression call premium sign 1', [[['C', 80, 2, 2.0], ['P', 105, 1, 2.0], ['P', 100, -1, 1.25], ['P', 110, 1, 0.5]], 1], [None, 29.75, []]], ['regression call premium sign 2', [[['C', 85, 1, 0.5], ['C', 105, -1, 1.25], ['S', 100, -2, 0.0], ['P', 105, 2, 0.5]], 1], [409.75, None, [109.875]]], ['partial repair probe 1', [[['P', 95, -2, 5.1], ['P', 90, 1, 2.0], ['C', 100, 1, 5.1], ['C', 120, -1, 5.1]], 100], [2820.0, -9180.0, [90.9]]], ['partial repair probe 2', [[['P', 80, 1, 7.75], ['P', 115, 1, 5.1], ['C', 80, -1, 0.5], ['C', 120, 1, 0.5]], 100], [18215.0, -5285.0, [91.075]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['S', 85, -1, 0.0], ['P', 120, -2, 2.0], ['S', 95, -2, 0.0], ['P', 80, 1, 2.0]], 1], [117.0, None, [58.5]]], ['normal control 2', [[['S', 115, -2, 0.0]], 100], [23000.0, None, [115.0]]], ['normal control 3', [[['P', 85, -2, 1.25]], 1], [2.5, -167.5, [83.75]]]], [['regression call premium sign 1', [[['C', 80, 1, 3.4]], 100], [None, -340.0, [83.4]]], ['regression call premium sign 2', [[['C', 115, -1, 1.25], ['P', 105, -1, 0.5], ['P', 100, -1, 5.1]], 100], [685.0, None, [99.075, 121.85]]], ['partial repair probe 1', [[['C', 115, -1, 3.4], ['C', 80, 1, 3.4], ['S', 115, 1, 0.0]], 100], [None, -11500.0, [97.5]]], ['partial repair probe 2', [[['C', 105, 1, 0.5], ['S', 85, 1, 0.0], ['C', 90, -1, 0.5], ['S', 120, 1, 0.0]], 1], [None, -205.0, [110.0]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 105, -2, 1.25]], 100], [250.0, -20750.0, [103.75]]], ['normal control 2', [[['P', 110, -1, 1.25], ['P', 95, -1, 0.5], ['S', 110, 2, 0.0], ['P', 100, 2, 0.5]], 100], [None, -22425.0, [109.75]]], ['normal control 3', [[['S', 90, 1, 0.0]], 100], [None, -9000.0, [90.0]]]], [['regression call premium sign 1', [[['S', 100, 1, 0.0], ['C', 105, -2, 1.25], ['P', 110, 2, 0.5]], 1], [121.5, None, [111.5]]], ['regression call premium sign 2', [[['P', 100, -2, 0.5], ['C', 120, -2, 3.4], ['C', 90, 2, 5.1], ['S', 85, 1, 0.0]], 1], [None, -287.4, [93.48]]], ['partial repair probe 1', [[['C', 120, -1, 5.1], ['P', 110, 2, 0.5], ['C', 110, 1, 5.1]], 1], [219.0, -1.0, [109.5, 111.0]]], ['partial repair probe 2', [[['S', 120, -1, 0.0], ['C', 110, 1, 7.75], ['P', 110, 1, 5.1], ['C', 90, -1, 7.75]], 1], [224.9, None, [104.9667]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 120, -2, 5.1]], 1], [10.2, -229.8, [114.9]]], ['normal control 2', [[['P', 105, 1, 2.0]], 100], [10300.0, -200.0, [103.0]]], ['normal control 3', [[['P', 100, -1, 0.5], ['S', 110, 1, 0.0], ['P', 115, -2, 5.1]], 100], [None, -42930.0, [109.7667]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * max(S - k - p, 0)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression call premium sign 1', [[['S', 85, 1, 0.0], ['C', 100, -2, 2.0], ['C', 90, -1, 0.5], ['C', 110, -2, 3.4]], 1], [16.3, None, [73.7, 108.15]]], ['regression call premium sign 2', [[['P', 95, -2, 3.4], ['C', 80, 1, 5.1]], 1], [None, -188.3, [89.4333]]], ['partial repair probe 1', [[['C', 120, -2, 7.75], ['P', 100, -2, 3.4], ['C', 105, 2, 7.75], ['P', 120, -2, 5.1]], 1], [47.0, -423.0, [108.25]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['S', 105, -1, 0.0], ['P', 80, 1, 3.4]], 100], [18160.0, None, [101.6]]], ['normal control 2', [[['P', 80, -1, 1.25], ['S', 85, 2, 0.0]], 100], [None, -24875.0, [84.375]]], ['normal control 3', [[['P', 95, 1, 1.25], ['S', 90, 2, 0.0], ['P', 80, -2, 5.1]], 1], [None, -236.05, [78.6833]]], ['normal control 4', [[['P', 95, 2, 5.1], ['P', 90, 1, 0.5], ['P', 105, 2, 2.0]], 100], [47530.0, -1470.0, [97.65]]]], [['regression call premium sign 1', [[['S', 80, -2, 0.0], ['C', 85, -1, 1.25], ['P', 100, 2, 0.5], ['C', 90, 1, 5.1]], 100], [35515.0, None, [88.03]]], ['regression call premium sign 2', [[['P', 85, -1, 0.5], ['C', 85, 2, 0.5]], 1], [None, -85.5, [85.25]]], ['partial repair probe 1', [[['P', 115, -2, 1.25], ['P', 80, -2, 3.4], ['C', 105, -2, 5.1], ['C', 95, 2, 5.1]], 1], [29.3, -380.7, [102.675]]], ['partial repair probe 2', [[['C', 105, -1, 5.1], ['P', 95, 1, 5.1], ['C', 85, 1, 5.1], ['P', 100, 1, 1.25]], 100], [18865.0, 865.0, []]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 110, -1, 5.1], ['S', 85, 2, 0.0], ['S', 110, 2, 0.0]], 100], [None, -49490.0, [98.98]]], ['normal control 2', [[['P', 80, -1, 7.75]], 100], [775.0, -7225.0, [72.25]]], ['normal control 3', [[['P', 90, 2, 3.4]], 1], [173.2, -6.8, [86.6]]]], [['regression call premium sign 1', [[['C', 80, 2, 2.0], ['P', 105, 1, 2.0], ['P', 100, -1, 1.25], ['P', 110, 1, 0.5]], 1], [None, 29.75, []]], ['regression call premium sign 2', [[['C', 85, 1, 0.5], ['C', 105, -1, 1.25], ['S', 100, -2, 0.0], ['P', 105, 2, 0.5]], 1], [409.75, None, [109.875]]], ['partial repair probe 1', [[['P', 95, -2, 5.1], ['P', 90, 1, 2.0], ['C', 100, 1, 5.1], ['C', 120, -1, 5.1]], 100], [2820.0, -9180.0, [90.9]]], ['partial repair probe 2', [[['P', 80, 1, 7.75], ['P', 115, 1, 5.1], ['C', 80, -1, 0.5], ['C', 120, 1, 0.5]], 100], [18215.0, -5285.0, [91.075]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['S', 85, -1, 0.0], ['P', 120, -2, 2.0], ['S', 95, -2, 0.0], ['P', 80, 1, 2.0]], 1], [117.0, None, [58.5]]], ['normal control 2', [[['S', 115, -2, 0.0]], 100], [23000.0, None, [115.0]]], ['normal control 3', [[['P', 85, -2, 1.25]], 1], [2.5, -167.5, [83.75]]]], [['regression call premium sign 1', [[['C', 80, 1, 3.4]], 100], [None, -340.0, [83.4]]], ['regression call premium sign 2', [[['C', 115, -1, 1.25], ['P', 105, -1, 0.5], ['P', 100, -1, 5.1]], 100], [685.0, None, [99.075, 121.85]]], ['partial repair probe 1', [[['C', 115, -1, 3.4], ['C', 80, 1, 3.4], ['S', 115, 1, 0.0]], 100], [None, -11500.0, [97.5]]], ['partial repair probe 2', [[['C', 105, 1, 0.5], ['S', 85, 1, 0.0], ['C', 90, -1, 0.5], ['S', 120, 1, 0.0]], 1], [None, -205.0, [110.0]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 105, -2, 1.25]], 100], [250.0, -20750.0, [103.75]]], ['normal control 2', [[['P', 110, -1, 1.25], ['P', 95, -1, 0.5], ['S', 110, 2, 0.0], ['P', 100, 2, 0.5]], 100], [None, -22425.0, [109.75]]], ['normal control 3', [[['S', 90, 1, 0.0]], 100], [None, -9000.0, [90.0]]]], [['regression call premium sign 1', [[['S', 100, 1, 0.0], ['C', 105, -2, 1.25], ['P', 110, 2, 0.5]], 1], [121.5, None, [111.5]]], ['regression call premium sign 2', [[['P', 100, -2, 0.5], ['C', 120, -2, 3.4], ['C', 90, 2, 5.1], ['S', 85, 1, 0.0]], 1], [None, -287.4, [93.48]]], ['partial repair probe 1', [[['C', 120, -1, 5.1], ['P', 110, 2, 0.5], ['C', 110, 1, 5.1]], 1], [219.0, -1.0, [109.5, 111.0]]], ['partial repair probe 2', [[['S', 120, -1, 0.0], ['C', 110, 1, 7.75], ['P', 110, 1, 5.1], ['C', 90, -1, 7.75]], 1], [224.9, None, [104.9667]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['P', 120, -2, 5.1]], 1], [10.2, -229.8, [114.9]]], ['normal control 2', [[['P', 105, 1, 2.0]], 100], [10300.0, -200.0, [103.0]]], ['normal control 3', [[['P', 100, -1, 0.5], ['S', 110, 1, 0.0], ['P', 115, -2, 5.1]], 100], [None, -42930.0, [109.7667]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Long calls look profitable at every price.

Inputs legs [kind C/P/S, strike (purchase price for stock S), signed qty, premium] and a multiplier. Expiry value V(S) = multiplier * sum(q*(intrinsic - premium)) for options and q*(S - price) for stock, in exact fractions. Breakpoints are 0 and every strike. Tail slope = V(top+1)-V(top). Max gain is None if the slope > 0 else the max over breakpoints; max loss is None if slope < 0 else the min. Breakevens are zeros at breakpoints and linear-interpolated sign changes between them and in the tail. Return [gain, loss, breakevens] rounded to 4.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a + va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression breakeven interpolation 1', [[['C', 100, -1, 2.0], ['C', 80, 1, 1.25], ['C', 95, 2, 5.1], ['C', 115, 1, 1.25]], 100], [None, -1070.0, [90.7]]], ['regression breakeven interpolation 2', [[['P', 95, -1, 7.75]], 100], [775.0, -8725.0, [87.25]]], ['partial repair probe 1', [[['C', 115, -1, 3.4], ['C', 95, 2, 3.4], ['C', 105, -1, 1.25]], 100], [2785.0, -215.0, [96.075]]], ['partial repair probe 2', [[['P', 85, -1, 2.0]], 100], [200.0, -8300.0, [83.0]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['S', 105, -1, 0.0], ['C', 110, 1, 0.5], ['C', 80, 2, 3.4]], 1], [None, 17.7, []]], ['normal control 2', [[['P', 100, 2, 0.5], ['C', 105, 2, 0.5], ['S', 90, 1, 0.0]], 1], [None, 8.0, []]], ['normal control 3', [[['C', 85, -1, 1.25]], 1], [1.25, None, [86.25]]]], [['regression breakeven interpolation 1', [[['S', 120, -1, 0.0], ['S', 90, 2, 0.0]], 100], [None, -6000.0, [60.0]]], ['regression breakeven interpolation 2', [[['C', 85, -1, 3.4], ['C', 80, -1, 1.25]], 1], [4.65, None, [84.65]]], ['partial repair probe 1', [[['S', 100, 2, 0.0], ['S', 120, 1, 0.0], ['P', 100, -2, 5.1]], 100], [None, -50980.0, [103.2667]]], ['partial repair probe 2', [[['C', 115, -2, 7.75], ['P', 105, -1, 2.0]], 1], [17.5, None, [87.5, 123.75]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 100, -2, 3.4], ['C', 105, 2, 3.4]], 1], [0.0, -10.0, [0.0, 100.0]]], ['normal control 2', [[['S', 95, -1, 0.0], ['C', 105, 2, 2.0], ['S', 110, 1, 0.0]], 1], [None, -19.0, [114.5]]], ['normal control 3', [[['C', 95, 1, 7.75], ['P', 120, 1, 5.1], ['C', 110, 1, 7.75]], 1], [None, 4.4, []]]], [['regression breakeven interpolation 1', [[['P', 110, -1, 5.1], ['P', 95, 1, 1.25]], 100], [385.0, -1115.0, [106.15]]], ['regression breakeven interpolation 2', [[['S', 115, 1, 0.0], ['P', 120, 1, 0.5], ['C', 110, 2, 3.4]], 1], [None, -2.3, [111.15]]], ['partial repair probe 1', [[['C', 110, -2, 5.1], ['P', 80, -1, 3.4]], 1], [13.6, None, [66.4, 116.8]]], ['partial repair probe 2', [[['C', 90, -1, 0.5], ['P', 90, -2, 5.1]], 1], [10.7, None, [84.65, 100.7]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 90, -2, 3.4]], 1], [6.8, None, [93.4]]], ['normal control 2', [[['C', 105, -1, 0.5]], 100], [50.0, None, [105.5]]], ['normal control 3', [[['P', 115, -1, 3.4], ['C', 110, 2, 5.1], ['S', 115, -1, 0.0], ['C', 95, -2, 2.0]], 100], [-280.0, None, []]]], [['regression breakeven interpolation 1', [[['C', 120, -1, 7.75], ['S', 80, -2, 0.0], ['C', 95, -1, 1.25]], 1], [169.0, None, [84.5]]], ['regression breakeven interpolation 2', [[['P', 110, -2, 2.0]], 100], [400.0, -21600.0, [108.0]]], ['partial repair probe 1', [[['P', 110, 1, 1.25]], 100], [10875.0, -125.0, [108.75]]], ['partial repair probe 2', [[['P', 110, -1, 3.4]], 100], [340.0, -10660.0, [106.6]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 105, 1, 7.75]], 100], [None, -775.0, [112.75]]], ['normal control 2', [[['C', 80, 1, 5.1], ['S', 85, 1, 0.0]], 1], [None, -90.1, [85.05]]], ['normal control 3', [[['C', 85, 2, 0.5]], 100], [None, -100.0, [85.5]]]], [['regression breakeven interpolation 1', [[['P', 80, -1, 7.75]], 1], [7.75, -72.25, [72.25]]], ['regression breakeven interpolation 2', [[['C', 90, 1, 7.75], ['P', 105, -2, 1.25]], 1], [None, -215.25, [101.75]]], ['partial repair probe 1', [[['P', 120, -2, 7.75], ['P', 105, 1, 1.25], ['P', 100, 2, 1.25], ['C', 115, 2, 0.5]], 1], [None, -24.25, [75.75, 114.625]]], ['partial repair probe 2', [[['C', 85, 2, 1.25], ['P', 100, -2, 5.1], ['P', 100, 1, 2.0]], 1], [None, -94.3, [88.1]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['S', 100, 1, 0.0], ['C', 105, -2, 2.0], ['P', 105, 1, 2.0], ['C', 95, 2, 1.25]], 1], [None, 4.5, []]], ['normal control 2', [[['S', 90, 1, 0.0], ['P', 115, 1, 7.75]], 1], [None, 17.25, []]], ['normal control 3', [[['C', 105, 1, 3.4]], 100], [None, -340.0, [108.4]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a + Fraction(b - a, 2))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression breakeven interpolation 1', [[['C', 100, -1, 2.0], ['C', 80, 1, 1.25], ['C', 95, 2, 5.1], ['C', 115, 1, 1.25]], 100], [None, -1070.0, [90.7]]], ['regression breakeven interpolation 2', [[['P', 95, -1, 7.75]], 100], [775.0, -8725.0, [87.25]]], ['partial repair probe 1', [[['C', 115, -1, 3.4], ['C', 95, 2, 3.4], ['C', 105, -1, 1.25]], 100], [2785.0, -215.0, [96.075]]], ['partial repair probe 2', [[['P', 85, -1, 2.0]], 100], [200.0, -8300.0, [83.0]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['S', 105, -1, 0.0], ['C', 110, 1, 0.5], ['C', 80, 2, 3.4]], 1], [None, 17.7, []]], ['normal control 2', [[['P', 100, 2, 0.5], ['C', 105, 2, 0.5], ['S', 90, 1, 0.0]], 1], [None, 8.0, []]], ['normal control 3', [[['C', 85, -1, 1.25]], 1], [1.25, None, [86.25]]]], [['regression breakeven interpolation 1', [[['S', 120, -1, 0.0], ['S', 90, 2, 0.0]], 100], [None, -6000.0, [60.0]]], ['regression breakeven interpolation 2', [[['C', 85, -1, 3.4], ['C', 80, -1, 1.25]], 1], [4.65, None, [84.65]]], ['partial repair probe 1', [[['S', 100, 2, 0.0], ['S', 120, 1, 0.0], ['P', 100, -2, 5.1]], 100], [None, -50980.0, [103.2667]]], ['partial repair probe 2', [[['C', 115, -2, 7.75], ['P', 105, -1, 2.0]], 1], [17.5, None, [87.5, 123.75]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 100, -2, 3.4], ['C', 105, 2, 3.4]], 1], [0.0, -10.0, [0.0, 100.0]]], ['normal control 2', [[['S', 95, -1, 0.0], ['C', 105, 2, 2.0], ['S', 110, 1, 0.0]], 1], [None, -19.0, [114.5]]], ['normal control 3', [[['C', 95, 1, 7.75], ['P', 120, 1, 5.1], ['C', 110, 1, 7.75]], 1], [None, 4.4, []]]], [['regression breakeven interpolation 1', [[['P', 110, -1, 5.1], ['P', 95, 1, 1.25]], 100], [385.0, -1115.0, [106.15]]], ['regression breakeven interpolation 2', [[['S', 115, 1, 0.0], ['P', 120, 1, 0.5], ['C', 110, 2, 3.4]], 1], [None, -2.3, [111.15]]], ['partial repair probe 1', [[['C', 110, -2, 5.1], ['P', 80, -1, 3.4]], 1], [13.6, None, [66.4, 116.8]]], ['partial repair probe 2', [[['C', 90, -1, 0.5], ['P', 90, -2, 5.1]], 1], [10.7, None, [84.65, 100.7]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 90, -2, 3.4]], 1], [6.8, None, [93.4]]], ['normal control 2', [[['C', 105, -1, 0.5]], 100], [50.0, None, [105.5]]], ['normal control 3', [[['P', 115, -1, 3.4], ['C', 110, 2, 5.1], ['S', 115, -1, 0.0], ['C', 95, -2, 2.0]], 100], [-280.0, None, []]]], [['regression breakeven interpolation 1', [[['C', 120, -1, 7.75], ['S', 80, -2, 0.0], ['C', 95, -1, 1.25]], 1], [169.0, None, [84.5]]], ['regression breakeven interpolation 2', [[['P', 110, -2, 2.0]], 100], [400.0, -21600.0, [108.0]]], ['partial repair probe 1', [[['P', 110, 1, 1.25]], 100], [10875.0, -125.0, [108.75]]], ['partial repair probe 2', [[['P', 110, -1, 3.4]], 100], [340.0, -10660.0, [106.6]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 105, 1, 7.75]], 100], [None, -775.0, [112.75]]], ['normal control 2', [[['C', 80, 1, 5.1], ['S', 85, 1, 0.0]], 1], [None, -90.1, [85.05]]], ['normal control 3', [[['C', 85, 2, 0.5]], 100], [None, -100.0, [85.5]]]], [['regression breakeven interpolation 1', [[['P', 80, -1, 7.75]], 1], [7.75, -72.25, [72.25]]], ['regression breakeven interpolation 2', [[['C', 90, 1, 7.75], ['P', 105, -2, 1.25]], 1], [None, -215.25, [101.75]]], ['partial repair probe 1', [[['P', 120, -2, 7.75], ['P', 105, 1, 1.25], ['P', 100, 2, 1.25], ['C', 115, 2, 0.5]], 1], [None, -24.25, [75.75, 114.625]]], ['partial repair probe 2', [[['C', 85, 2, 1.25], ['P', 100, -2, 5.1], ['P', 100, 1, 2.0]], 1], [None, -94.3, [88.1]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['S', 100, 1, 0.0], ['C', 105, -2, 2.0], ['P', 105, 1, 2.0], ['C', 95, 2, 1.25]], 1], [None, 4.5, []]], ['normal control 2', [[['S', 90, 1, 0.0], ['P', 115, 1, 7.75]], 1], [None, 17.25, []]], ['normal control 3', [[['C', 105, 1, 3.4]], 100], [None, -340.0, [108.4]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Breakevens are reported outside the segment where value changes sign.

Inputs legs [kind C/P/S, strike (purchase price for stock S), signed qty, premium] and a multiplier. Expiry value V(S) = multiplier * sum(q*(intrinsic - premium)) for options and q*(S - price) for stock, in exact fractions. Breakpoints are 0 and every strike. Tail slope = V(top+1)-V(top). Max gain is None if the slope > 0 else the max over breakpoints; max loss is None if slope < 0 else the min. Breakevens are zeros at breakpoints and linear-interpolated sign changes between them and in the tail. Return [gain, loss, breakevens] rounded to 4.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top + vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression tail breakeven 1', [[['C', 85, 2, 7.75]], 100], [None, -1550.0, [92.75]]], ['regression tail breakeven 2', [[['P', 90, -2, 0.5], ['C', 115, -1, 5.1]], 100], [610.0, None, [86.95, 121.1]]], ['partial repair probe 1', [[['C', 110, 1, 1.25], ['C', 115, -1, 3.4], ['C', 120, -1, 2.0]], 1], [9.15, None, [129.15]]], ['partial repair probe 2', [[['P', 105, -2, 2.0], ['P', 105, -1, 0.5], ['C', 100, -1, 3.4]], 1], [2.9, None, [103.55, 107.9]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['P', 105, 1, 2.0], ['P', 90, -2, 2.0]], 1], [17.0, -73.0, [73.0]]], ['normal control 2', [[['P', 90, 1, 3.4], ['S', 100, -1, 0.0]], 1], [186.6, None, [96.6]]]], [['regression tail breakeven 1', [[['P', 115, 1, 1.25], ['C', 110, 2, 7.75], ['P', 80, -2, 0.5]], 1], [None, -60.75, [60.75, 99.25, 117.875]]], ['regression tail breakeven 2', [[['P', 100, 2, 7.75], ['S', 115, 2, 0.0]], 1], [None, -45.5, [122.75]]], ['partial repair probe 1', [[['C', 80, 1, 7.75]], 1], [None, -7.75, [87.75]]], ['partial repair probe 2', [[['P', 115, -1, 2.0], ['S', 115, 2, 0.0], ['C', 110, -1, 0.5]], 1], [None, -342.5, [117.5]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['S', 80, -2, 0.0], ['P', 95, -2, 2.0], ['S', 110, -1, 0.0], ['S', 100, 1, 0.0]], 1], [-16.0, None, []]], ['normal control 2', [[['P', 105, 1, 2.0], ['P', 100, -1, 7.75], ['S', 105, 1, 0.0], ['P', 80, -1, 3.4]], 100], [None, -17085.0, [90.85]]]], [['regression tail breakeven 1', [[['C', 85, -1, 7.75]], 1], [7.75, None, [92.75]]], ['regression tail breakeven 2', [[['C', 105, -2, 3.4], ['S', 100, 1, 0.0]], 100], [1180.0, None, [93.2, 116.8]]], ['partial repair probe 1', [[['P', 100, -1, 3.4], ['S', 100, -1, 0.0], ['P', 90, -2, 7.75]], 100], [1890.0, None, [80.55, 118.9]]], ['partial repair probe 2', [[['C', 115, -1, 2.0], ['C', 85, 1, 7.75], ['S', 105, -1, 0.0], ['C', 110, -1, 2.0]], 1], [101.25, None, [120.625]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['S', 120, 1, 0.0], ['P', 95, -1, 1.25], ['P', 95, -2, 0.5]], 100], [None, -40275.0, [117.75]]], ['normal control 2', [[['P', 100, -1, 1.25]], 100], [125.0, -9875.0, [98.75]]]], [['regression tail breakeven 1', [[['C', 80, -1, 5.1]], 100], [510.0, None, [85.1]]], ['regression tail breakeven 2', [[['C', 120, -2, 5.1]], 1], [10.2, None, [125.1]]], ['partial repair probe 1', [[['P', 115, -1, 1.25], ['P', 95, -2, 5.1], ['C', 120, -1, 2.0]], 1], [13.45, None, [101.55, 133.45]]], ['partial repair probe 2', [[['C', 110, 2, 1.25]], 100], [None, -250.0, [111.25]]], ['boundary control 1', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['boundary control 2', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['S', 90, -2, 0.0], ['P', 105, 1, 7.75], ['C', 95, -1, 5.1], ['C', 120, -1, 0.5]], 1], [282.85, None, [94.2833]]], ['normal control 2', [[['P', 85, 1, 2.0], ['P', 115, -1, 3.4]], 1], [1.4, -28.6, [113.6]]]], [['regression tail breakeven 1', [[['C', 85, -1, 5.1]], 1], [5.1, None, [90.1]]], ['regression tail breakeven 2', [[['C', 115, -1, 0.5], ['C', 115, -2, 1.25], ['P', 90, 2, 2.0], ['C', 90, 2, 1.25]], 1], [176.5, None, [88.25, 91.75, 161.5]]], ['partial repair probe 1', [[['C', 105, 2, 5.1]], 100], [None, -1020.0, [110.1]]], ['partial repair probe 2', [[['C', 90, -1, 5.1], ['P', 95, -1, 3.4]], 1], [3.5, None, [86.5, 98.5]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 100, 1, 3.4], ['C', 120, -2, 5.1], ['P', 100, -2, 2.0], ['C', 100, 2, 0.5]], 1], [None, -190.2, [95.1]]], ['normal control 2', [[['C', 95, 1, 1.25], ['C', 120, -1, 5.1], ['C', 85, -1, 3.4], ['P', 120, 1, 2.0]], 1], [125.25, None, [115.25]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set([0] + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        pass
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression tail breakeven 1', [[['C', 85, 2, 7.75]], 100], [None, -1550.0, [92.75]]], ['regression tail breakeven 2', [[['P', 90, -2, 0.5], ['C', 115, -1, 5.1]], 100], [610.0, None, [86.95, 121.1]]], ['partial repair probe 1', [[['C', 110, 1, 1.25], ['C', 115, -1, 3.4], ['C', 120, -1, 2.0]], 1], [9.15, None, [129.15]]], ['partial repair probe 2', [[['P', 105, -2, 2.0], ['P', 105, -1, 0.5], ['C', 100, -1, 3.4]], 1], [2.9, None, [103.55, 107.9]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['P', 105, 1, 2.0], ['P', 90, -2, 2.0]], 1], [17.0, -73.0, [73.0]]], ['normal control 2', [[['P', 90, 1, 3.4], ['S', 100, -1, 0.0]], 1], [186.6, None, [96.6]]]], [['regression tail breakeven 1', [[['P', 115, 1, 1.25], ['C', 110, 2, 7.75], ['P', 80, -2, 0.5]], 1], [None, -60.75, [60.75, 99.25, 117.875]]], ['regression tail breakeven 2', [[['P', 100, 2, 7.75], ['S', 115, 2, 0.0]], 1], [None, -45.5, [122.75]]], ['partial repair probe 1', [[['C', 80, 1, 7.75]], 1], [None, -7.75, [87.75]]], ['partial repair probe 2', [[['P', 115, -1, 2.0], ['S', 115, 2, 0.0], ['C', 110, -1, 0.5]], 1], [None, -342.5, [117.5]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['S', 80, -2, 0.0], ['P', 95, -2, 2.0], ['S', 110, -1, 0.0], ['S', 100, 1, 0.0]], 1], [-16.0, None, []]], ['normal control 2', [[['P', 105, 1, 2.0], ['P', 100, -1, 7.75], ['S', 105, 1, 0.0], ['P', 80, -1, 3.4]], 100], [None, -17085.0, [90.85]]]], [['regression tail breakeven 1', [[['C', 85, -1, 7.75]], 1], [7.75, None, [92.75]]], ['regression tail breakeven 2', [[['C', 105, -2, 3.4], ['S', 100, 1, 0.0]], 100], [1180.0, None, [93.2, 116.8]]], ['partial repair probe 1', [[['P', 100, -1, 3.4], ['S', 100, -1, 0.0], ['P', 90, -2, 7.75]], 100], [1890.0, None, [80.55, 118.9]]], ['partial repair probe 2', [[['C', 115, -1, 2.0], ['C', 85, 1, 7.75], ['S', 105, -1, 0.0], ['C', 110, -1, 2.0]], 1], [101.25, None, [120.625]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['S', 120, 1, 0.0], ['P', 95, -1, 1.25], ['P', 95, -2, 0.5]], 100], [None, -40275.0, [117.75]]], ['normal control 2', [[['P', 100, -1, 1.25]], 100], [125.0, -9875.0, [98.75]]]], [['regression tail breakeven 1', [[['C', 80, -1, 5.1]], 100], [510.0, None, [85.1]]], ['regression tail breakeven 2', [[['C', 120, -2, 5.1]], 1], [10.2, None, [125.1]]], ['partial repair probe 1', [[['P', 115, -1, 1.25], ['P', 95, -2, 5.1], ['C', 120, -1, 2.0]], 1], [13.45, None, [101.55, 133.45]]], ['partial repair probe 2', [[['C', 110, 2, 1.25]], 100], [None, -250.0, [111.25]]], ['boundary control 1', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['boundary control 2', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['normal control 1', [[['S', 90, -2, 0.0], ['P', 105, 1, 7.75], ['C', 95, -1, 5.1], ['C', 120, -1, 0.5]], 1], [282.85, None, [94.2833]]], ['normal control 2', [[['P', 85, 1, 2.0], ['P', 115, -1, 3.4]], 1], [1.4, -28.6, [113.6]]]], [['regression tail breakeven 1', [[['C', 85, -1, 5.1]], 1], [5.1, None, [90.1]]], ['regression tail breakeven 2', [[['C', 115, -1, 0.5], ['C', 115, -2, 1.25], ['P', 90, 2, 2.0], ['C', 90, 2, 1.25]], 1], [176.5, None, [88.25, 91.75, 161.5]]], ['partial repair probe 1', [[['C', 105, 2, 5.1]], 100], [None, -1020.0, [110.1]]], ['partial repair probe 2', [[['C', 90, -1, 5.1], ['P', 95, -1, 3.4]], 1], [3.5, None, [86.5, 98.5]]], ['boundary control 1', [[['P', 100, 1, 2.0]], 1], [98.0, -2.0, [98.0]]], ['boundary control 2', [[['S', 100, 1, 0.0], ['C', 110, -1, 1.25]], 1], [11.25, -98.75, [98.75]]], ['normal control 1', [[['C', 100, 1, 3.4], ['C', 120, -2, 5.1], ['P', 100, -2, 2.0], ['C', 100, 2, 0.5]], 1], [None, -190.2, [95.1]]], ['normal control 2', [[['C', 95, 1, 1.25], ['C', 120, -1, 5.1], ['C', 85, -1, 3.4], ['P', 120, 1, 2.0]], 1], [125.25, None, [115.25]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Long call breakevens are reported below the strike.

Inputs legs [kind C/P/S, strike (purchase price for stock S), signed qty, premium] and a multiplier. Expiry value V(S) = multiplier * sum(q*(intrinsic - premium)) for options and q*(S - price) for stock, in exact fractions. Breakpoints are 0 and every strike. Tail slope = V(top+1)-V(top). Max gain is None if the slope > 0 else the max over breakpoints; max loss is None if slope < 0 else the min. Breakevens are zeros at breakpoints and linear-interpolated sign changes between them and in the tail. Return [gain, loss, breakevens] rounded to 4.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set(leg[1] for leg in legs))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression zero price breakpoint 1', [[['S', 90, -1, 0.0], ['C', 95, -1, 1.25], ['P', 90, 2, 2.0], ['P', 115, -2, 1.25]], 1], [39.75, None, [39.75]]], ['regression zero price breakpoint 2', [[['P', 105, -2, 0.5]], 1], [1.0, -209.0, [104.5]]], ['partial repair probe 1', [[['S', 95, -1, 0.0]], 1], [95.0, None, [95.0]]], ['partial repair probe 2', [[['C', 105, 1, 2.0], ['C', 95, 2, 3.4], ['S', 105, 1, 0.0], ['C', 115, -2, 2.0]], 100], [None, -10980.0, [99.9333]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 95, -2, 0.5], ['S', 90, -1, 0.0]], 100], [-400.0, None, []]], ['normal control 2', [[['S', 115, 2, 0.0], ['C', 95, -2, 2.0], ['C', 85, -1, 1.25], ['C', 90, -1, 0.5]], 1], [-49.25, None, []]], ['normal control 3', [[['C', 90, -1, 2.0]], 100], [200.0, None, [92.0]]]], [['regression zero price breakpoint 1', [[['S', 115, -2, 0.0], ['P', 80, 1, 3.4]], 1], [306.6, None, [113.3]]], ['regression zero price breakpoint 2', [[['P', 100, -1, 1.25], ['C', 85, 1, 7.75], ['C', 100, 1, 0.5], ['C', 120, -1, 1.25]], 100], [None, -10575.0, [95.375]]], ['partial repair probe 1', [[['S', 120, -1, 0.0]], 1], [120.0, None, [120.0]]], ['partial repair probe 2', [[['S', 90, -1, 0.0]], 100], [9000.0, None, [90.0]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 85, -1, 3.4]], 100], [340.0, None, [88.4]]], ['normal control 2', [[['C', 85, -1, 5.1]], 1], [5.1, None, [90.1]]], ['normal control 3', [[['P', 95, 1, 2.0], ['C', 105, 2, 1.25], ['P', 105, -1, 5.1]], 100], [None, -940.0, [104.4]]]], [['regression zero price breakpoint 1', [[['P', 100, -2, 3.4], ['S', 115, 1, 0.0]], 1], [None, -308.2, [108.2]]], ['regression zero price breakpoint 2', [[['P', 100, -1, 2.0]], 100], [200.0, -9800.0, [98.0]]], ['partial repair probe 1', [[['S', 85, -2, 0.0], ['S', 85, 2, 0.0]], 1], [0.0, 0.0, [0.0, 85.0]]], ['partial repair probe 2', [[['C', 105, -1, 0.5], ['S', 115, -1, 0.0]], 100], [11550.0, None, [110.25]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 115, -1, 7.75]], 1], [7.75, None, [122.75]]], ['normal control 2', [[['S', 85, -1, 0.0], ['C', 80, -1, 2.0], ['P', 85, -1, 7.75]], 1], [9.75, None, [87.375]]], ['normal control 3', [[['C', 85, 1, 7.75], ['C', 95, -1, 5.1]], 1], [7.35, -2.65, [87.65]]]], [['regression zero price breakpoint 1', [[['C', 110, 1, 5.1], ['P', 115, -1, 5.1]], 1], [None, -115.0, [112.5]]], ['regression zero price breakpoint 2', [[['P', 80, 1, 0.5], ['C', 105, 1, 1.25], ['C', 105, 2, 5.1]], 1], [None, -11.95, [68.05, 108.9833]]], ['partial repair probe 1', [[['S', 105, 1, 0.0]], 100], [None, -10500.0, [105.0]]], ['partial repair probe 2', [[['C', 80, 1, 2.0], ['S', 90, 2, 0.0]], 1], [None, -182.0, [87.3333]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 80, 1, 2.0], ['C', 80, 2, 5.1], ['C', 110, -2, 0.5]], 100], [None, -1120.0, [83.7333]]], ['normal control 2', [[['P', 115, 1, 7.75], ['C', 85, 2, 7.75]], 100], [None, 675.0, []]], ['normal control 3', [[['C', 80, 1, 5.1], ['P', 90, 2, 1.25], ['P', 90, -1, 2.0]], 1], [None, 4.4, []]]], [['regression zero price breakpoint 1', [[['S', 80, 1, 0.0], ['C', 105, -1, 1.25]], 1], [26.25, -78.75, [78.75]]], ['regression zero price breakpoint 2', [[['P', 110, -1, 5.1], ['P', 100, -1, 0.5], ['P', 85, -2, 5.1]], 100], [1580.0, -36420.0, [97.1]]], ['partial repair probe 1', [[['C', 120, 1, 2.0], ['S', 90, 1, 0.0], ['S', 120, 2, 0.0], ['S', 100, 1, 0.0]], 100], [None, -43200.0, [108.0]]], ['partial repair probe 2', [[['S', 95, 1, 0.0], ['S', 115, 2, 0.0]], 1], [None, -325.0, [108.3333]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 100, 2, 2.0], ['C', 85, 1, 3.4]], 1], [None, -7.4, [92.4]]], ['normal control 2', [[['C', 110, -1, 7.75]], 1], [7.75, None, [117.75]]], ['normal control 3', [[['C', 110, 1, 1.25]], 100], [None, -125.0, [111.25]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
N = 1
observations = []
def solve(legs, multiplier):
    def value(S):
        t = Fraction(0)
        for kind, k, q, prem in legs:
            p = Fraction(str(prem))
            if kind == 'C':
                t += q * (max(S - k, 0) - p)
            elif kind == 'P':
                t += q * (max(k - S, 0) - p)
            else:
                t += q * (S - k)
        return t * multiplier
    pts = sorted(set(([0] if any(l[0] == 'P' for l in legs) else []) + [leg[1] for leg in legs]))
    top = pts[-1]
    slope = value(top + 1) - value(top)
    vals = [value(p) for p in pts]
    gain = None if slope > 0 else max(vals)
    loss = None if slope < 0 else min(vals)
    bes = []
    for a, b in zip(pts, pts[1:]):
        va, vb = value(a), value(b)
        if va == 0:
            bes.append(Fraction(a))
        elif va * vb < 0:
            bes.append(a - va * (b - a) / (vb - va))
    vt = value(top)
    if vt == 0:
        bes.append(Fraction(top))
    elif vt * slope < 0:
        bes.append(top - vt / slope)
    def r4(x):
        return None if x is None else float(round(x, 4))
    return [r4(gain), r4(loss), [r4(x) for x in sorted(set(bes))]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression zero price breakpoint 1', [[['S', 90, -1, 0.0], ['C', 95, -1, 1.25], ['P', 90, 2, 2.0], ['P', 115, -2, 1.25]], 1], [39.75, None, [39.75]]], ['regression zero price breakpoint 2', [[['P', 105, -2, 0.5]], 1], [1.0, -209.0, [104.5]]], ['partial repair probe 1', [[['S', 95, -1, 0.0]], 1], [95.0, None, [95.0]]], ['partial repair probe 2', [[['C', 105, 1, 2.0], ['C', 95, 2, 3.4], ['S', 105, 1, 0.0], ['C', 115, -2, 2.0]], 100], [None, -10980.0, [99.9333]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['P', 95, -2, 0.5], ['S', 90, -1, 0.0]], 100], [-400.0, None, []]], ['normal control 2', [[['S', 115, 2, 0.0], ['C', 95, -2, 2.0], ['C', 85, -1, 1.25], ['C', 90, -1, 0.5]], 1], [-49.25, None, []]], ['normal control 3', [[['C', 90, -1, 2.0]], 100], [200.0, None, [92.0]]]], [['regression zero price breakpoint 1', [[['S', 115, -2, 0.0], ['P', 80, 1, 3.4]], 1], [306.6, None, [113.3]]], ['regression zero price breakpoint 2', [[['P', 100, -1, 1.25], ['C', 85, 1, 7.75], ['C', 100, 1, 0.5], ['C', 120, -1, 1.25]], 100], [None, -10575.0, [95.375]]], ['partial repair probe 1', [[['S', 120, -1, 0.0]], 1], [120.0, None, [120.0]]], ['partial repair probe 2', [[['S', 90, -1, 0.0]], 100], [9000.0, None, [90.0]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 85, -1, 3.4]], 100], [340.0, None, [88.4]]], ['normal control 2', [[['C', 85, -1, 5.1]], 1], [5.1, None, [90.1]]], ['normal control 3', [[['P', 95, 1, 2.0], ['C', 105, 2, 1.25], ['P', 105, -1, 5.1]], 100], [None, -940.0, [104.4]]]], [['regression zero price breakpoint 1', [[['P', 100, -2, 3.4], ['S', 115, 1, 0.0]], 1], [None, -308.2, [108.2]]], ['regression zero price breakpoint 2', [[['P', 100, -1, 2.0]], 100], [200.0, -9800.0, [98.0]]], ['partial repair probe 1', [[['S', 85, -2, 0.0], ['S', 85, 2, 0.0]], 1], [0.0, 0.0, [0.0, 85.0]]], ['partial repair probe 2', [[['C', 105, -1, 0.5], ['S', 115, -1, 0.0]], 100], [11550.0, None, [110.25]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 115, -1, 7.75]], 1], [7.75, None, [122.75]]], ['normal control 2', [[['S', 85, -1, 0.0], ['C', 80, -1, 2.0], ['P', 85, -1, 7.75]], 1], [9.75, None, [87.375]]], ['normal control 3', [[['C', 85, 1, 7.75], ['C', 95, -1, 5.1]], 1], [7.35, -2.65, [87.65]]]], [['regression zero price breakpoint 1', [[['C', 110, 1, 5.1], ['P', 115, -1, 5.1]], 1], [None, -115.0, [112.5]]], ['regression zero price breakpoint 2', [[['P', 80, 1, 0.5], ['C', 105, 1, 1.25], ['C', 105, 2, 5.1]], 1], [None, -11.95, [68.05, 108.9833]]], ['partial repair probe 1', [[['S', 105, 1, 0.0]], 100], [None, -10500.0, [105.0]]], ['partial repair probe 2', [[['C', 80, 1, 2.0], ['S', 90, 2, 0.0]], 1], [None, -182.0, [87.3333]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 80, 1, 2.0], ['C', 80, 2, 5.1], ['C', 110, -2, 0.5]], 100], [None, -1120.0, [83.7333]]], ['normal control 2', [[['P', 115, 1, 7.75], ['C', 85, 2, 7.75]], 100], [None, 675.0, []]], ['normal control 3', [[['C', 80, 1, 5.1], ['P', 90, 2, 1.25], ['P', 90, -1, 2.0]], 1], [None, 4.4, []]]], [['regression zero price breakpoint 1', [[['S', 80, 1, 0.0], ['C', 105, -1, 1.25]], 1], [26.25, -78.75, [78.75]]], ['regression zero price breakpoint 2', [[['P', 110, -1, 5.1], ['P', 100, -1, 0.5], ['P', 85, -2, 5.1]], 100], [1580.0, -36420.0, [97.1]]], ['partial repair probe 1', [[['C', 120, 1, 2.0], ['S', 90, 1, 0.0], ['S', 120, 2, 0.0], ['S', 100, 1, 0.0]], 100], [None, -43200.0, [108.0]]], ['partial repair probe 2', [[['S', 95, 1, 0.0], ['S', 115, 2, 0.0]], 1], [None, -325.0, [108.3333]]], ['boundary control 1', [[['C', 100, 1, 2.0]], 100], [None, -200.0, [102.0]]], ['normal control 1', [[['C', 100, 2, 2.0], ['C', 85, 1, 3.4]], 1], [None, -7.4, [92.4]]], ['normal control 2', [[['C', 110, -1, 7.75]], 1], [7.75, None, [117.75]]], ['normal control 3', [[['C', 110, 1, 1.25]], 100], [None, -125.0, [111.25]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Long puts and long stock report the wrong extreme at a zero price.

Inputs legs [kind C/P/S, strike (purchase price for stock S), signed qty, premium] and a multiplier. Expiry value V(S) = multiplier * sum(q*(intrinsic - premium)) for options and q*(S - price) for stock, in exact fractions. Breakpoints are 0 and every strike. Tail slope = V(top+1)-V(top). Max gain is None if the slope > 0 else the max over breakpoints; max loss is None if slope < 0 else the min. Breakevens are zeros at breakpoints and linear-interpolated sign changes between them and in the tail. Return [gain, loss, breakevens] rounded to 4.

split: open-access


prompt:
American puts lose their early exercise premium.

Inputs kind, spot S, strike K, rate r, volatility sigma, maturity T in years and steps. dt=T/steps, u=exp(sigma*sqrt(dt)), d=1/u, p=(exp(r*dt)-d)/(u-d), disc=exp(-r*dt). Roll back from terminal payoffs; at each node take max(continuation, immediate exercise at that node's price). Return the root rounded to 6.

split: open-access


prompt:
Tree prices drift from the stated continuous-compounding model.

Inputs kind, spot S, strike K, rate r, volatility sigma, maturity T in years and steps. dt=T/steps, u=exp(sigma*sqrt(dt)), d=1/u, p=(exp(r*dt)-d)/(u-d), disc=exp(-r*dt). Roll back from terminal payoffs; at each node take max(continuation, immediate exercise at that node's price). Return the root rounded to 6.

split: open-access


prompt:
Option values collapse toward zero for many steps.

Inputs kind, spot S, strike K, rate r, volatility sigma, maturity T in years and steps. dt=T/steps, u=exp(sigma*sqrt(dt)), d=1/u, p=(exp(r*dt)-d)/(u-d), disc=exp(-r*dt). Roll back from terminal payoffs; at each node take max(continuation, immediate exercise at that node's price). Return the root rounded to 6.

split: open-access


prompt:
The lattice does not recombine on the stated log grid.

Inputs kind, spot S, strike K, rate r, volatility sigma, maturity T in years and steps. dt=T/steps, u=exp(sigma*sqrt(dt)), d=1/u, p=(exp(r*dt)-d)/(u-d), disc=exp(-r*dt). Roll back from terminal payoffs; at each node take max(continuation, immediate exercise at that node's price). Return the root rounded to 6.

split: open-access


prompt:
Continuation values are biased toward the down state.

Inputs kind, spot S, strike K, rate r, volatility sigma, maturity T in years and steps. dt=T/steps, u=exp(sigma*sqrt(dt)), d=1/u, p=(exp(r*dt)-d)/(u-d), disc=exp(-r*dt). Roll back from terminal payoffs; at each node take max(continuation, immediate exercise at that node's price). Return the root rounded to 6.

split: open-access


prompt:
Options on dividend payers are priced as if dividends increased the forward.

Inputs kind, spot S, strike K, rate r, dividend yield q, volatility sigma and calendar days to expiry. T = days/365. At days == 0 return intrinsic value. Otherwise d1 = (ln(S/K) + (r - q + sigma^2/2)T)/(sigma sqrt T), d2 = d1 - sigma sqrt T, call = S e^{-qT} N(d1) - K e^{-rT} N(d2), put = K e^{-rT} N(-d2) - S e^{-qT} N(-d1). Round to 6.

split: open-access