broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 1 and m % 12 == 0:
            if t <= x['steps']:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n - m + 1)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
        peak = max(peak, bal)
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: year boundary', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['sample 1', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['sample 2', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['sample 3', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 2770, 2770, 2770], 'peak_balance': 250000, 'end_balance': -23}], ['sample 4', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0, 0, 0], 'peak_balance': 250000, 'end_balance': -326758}], ['sample 5', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 45414, 45414], 'peak_balance': 1024671, 'end_balance': -2}], ['sample 6', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}]], [['regression: year boundary', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['sample 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['sample 2', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['sample 3', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526, 52144, 52144], 'peak_balance': 1176523, 'end_balance': 4}], ['sample 4', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 1}, {'year_payments': [500, 513, 9090, 9090, 9090], 'peak_balance': 285848, 'end_balance': -7}], ['sample 5', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 1173203, 'end_balance': 1173203}], ['sample 6', {'principal': 250000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 8485, 8485], 'peak_balance': 250000, 'end_balance': -6}]], [['regression: year boundary', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['sample 1', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['sample 2', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [20000, 22000, 56636], 'peak_balance': 1000000, 'end_balance': 0}], ['sample 3', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 0}, {'year_payments': [3000, 6284], 'peak_balance': 100000, 'end_balance': -2}], ['sample 4', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 0}, {'year_payments': [20000, 739, 739, 739, 739], 'peak_balance': 250000, 'end_balance': -10}], ['sample 5', {'principal': 1000000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 2}, {'year_payments': [8000, 8200, 8405, 50753, 50753], 'peak_balance': 1078159, 'end_balance': -8}], ['sample 6', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}]], [['regression: year boundary', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['sample 1', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['sample 2', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['sample 3', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 27064, 27064, 27064, 27064], 'peak_balance': 1087553, 'end_balance': -13}], ['sample 4', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['sample 5', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 0], 'peak_balance': 250000, 'end_balance': -266261}], ['sample 6', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [8000, 22616, 22616, 22616, 22616], 'peak_balance': 1000000, 'end_balance': 0}]], [['regression: year boundary', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['sample 1', {'principal': 100000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605, 666, 8252], 'peak_balance': 100000, 'end_balance': 1}], ['sample 2', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 121021, 'end_balance': 121021}], ['sample 3', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300, 35428, 35428, 35428], 'peak_balance': 1114096, 'end_balance': -5}], ['sample 4', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 1}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['sample 5', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 750, 'steps': 3}, {'year_payments': [500, 538, 578, 621, 24747], 'peak_balance': 287533, 'end_balance': 1}], ['sample 6', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300, 90126], 'peak_balance': 1047163, 'end_balance': -6}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 12 and (m - 2) % 12 == 0:
            if t <= x['steps']:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n - m + 1)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
        peak = max(peak, bal)
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: year boundary', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['sample 1', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['sample 2', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['sample 3', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 2770, 2770, 2770], 'peak_balance': 250000, 'end_balance': -23}], ['sample 4', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0, 0, 0], 'peak_balance': 250000, 'end_balance': -326758}], ['sample 5', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 45414, 45414], 'peak_balance': 1024671, 'end_balance': -2}], ['sample 6', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}]], [['regression: year boundary', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['sample 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['sample 2', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['sample 3', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526, 52144, 52144], 'peak_balance': 1176523, 'end_balance': 4}], ['sample 4', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 1}, {'year_payments': [500, 513, 9090, 9090, 9090], 'peak_balance': 285848, 'end_balance': -7}], ['sample 5', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 1173203, 'end_balance': 1173203}], ['sample 6', {'principal': 250000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 8485, 8485], 'peak_balance': 250000, 'end_balance': -6}]], [['regression: year boundary', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['sample 1', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['sample 2', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [20000, 22000, 56636], 'peak_balance': 1000000, 'end_balance': 0}], ['sample 3', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 0}, {'year_payments': [3000, 6284], 'peak_balance': 100000, 'end_balance': -2}], ['sample 4', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 0}, {'year_payments': [20000, 739, 739, 739, 739], 'peak_balance': 250000, 'end_balance': -10}], ['sample 5', {'principal': 1000000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 2}, {'year_payments': [8000, 8200, 8405, 50753, 50753], 'peak_balance': 1078159, 'end_balance': -8}], ['sample 6', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}]], [['regression: year boundary', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['sample 1', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['sample 2', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['sample 3', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 27064, 27064, 27064, 27064], 'peak_balance': 1087553, 'end_balance': -13}], ['sample 4', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['sample 5', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 0], 'peak_balance': 250000, 'end_balance': -266261}], ['sample 6', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [8000, 22616, 22616, 22616, 22616], 'peak_balance': 1000000, 'end_balance': 0}]], [['regression: year boundary', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['sample 1', {'principal': 100000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605, 666, 8252], 'peak_balance': 100000, 'end_balance': 1}], ['sample 2', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 121021, 'end_balance': 121021}], ['sample 3', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300, 35428, 35428, 35428], 'peak_balance': 1114096, 'end_balance': -5}], ['sample 4', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 1}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['sample 5', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 750, 'steps': 3}, {'year_payments': [500, 538, 578, 621, 24747], 'peak_balance': 287533, 'end_balance': 1}], ['sample 6', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300, 90126], 'peak_balance': 1047163, 'end_balance': -6}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
Payments step up in the twelfth month of each year.

x = {'principal', 'rate_bp', 'months', 'initial_payment', 'step_bp', 'steps'}. Month m (1-based) is in year t = (m-1)//12. For t < steps+1 the year payment starts at initial_payment and each new year multiplies the previous year payment by (10000+step_bp)/10000, rounded half-up. At the start of year steps+1 (if the loan runs that long) the payment is re-leveled on the balance over the remaining months (exact annuity, half-up) and stays fixed. Interest round_half_up(balance*bp/120000); balance += interest - payment (it may grow). Return {'year_payments', 'peak_balance', 'end_balance'}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 1 and (m - 1) % 12 == 0:
            if t <= x['steps']:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
        peak = max(peak, bal)
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: re-level term', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['control 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['control 2', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0, 0, 0], 'peak_balance': 250000, 'end_balance': -326758}], ['control 3', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 2}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 1000000, 'end_balance': 351915}], ['control 4', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 250, 'steps': 2}, {'year_payments': [8000, 8200], 'peak_balance': 100000, 'end_balance': -92367}], ['control 5', {'principal': 100000, 'rate_bp': 900, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 109781, 'end_balance': 109781}], ['control 6', {'principal': 100000, 'rate_bp': 900, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 2}, {'year_payments': [20000, 21500, 23113, 0, 0], 'peak_balance': 100000, 'end_balance': -899279}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526, 52144, 52144], 'peak_balance': 1176523, 'end_balance': 4}], ['control 1', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 2', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['control 3', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['control 4', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 1173203, 'end_balance': 1173203}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [8000, 8800, 0], 'peak_balance': 100000, 'end_balance': -106813}], ['control 6', {'principal': 1000000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 3}, {'year_payments': [3000, 3225], 'peak_balance': 1048088, 'end_balance': 1048088}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['control 1', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 2', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 3', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 750, 'steps': 1}, {'year_payments': [8000, 8600], 'peak_balance': 250000, 'end_balance': 82092}], ['control 4', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 500, 'step_bp': 750, 'steps': 2}, {'year_payments': [500, 538, 578], 'peak_balance': 1286575, 'end_balance': 1286575}], ['control 5', {'principal': 100000, 'rate_bp': 900, 'months': 36, 'initial_payment': 500, 'step_bp': 750, 'steps': 2}, {'year_payments': [500, 538, 578], 'peak_balance': 108792, 'end_balance': 108792}], ['control 6', {'principal': 100000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [20000, 22000, 0, 0, 0], 'peak_balance': 100000, 'end_balance': -626476}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['control 2', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['control 4', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 0], 'peak_balance': 250000, 'end_balance': -266261}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 500, 'step_bp': 750, 'steps': 3}, {'year_payments': [500, 538], 'peak_balance': 113004, 'end_balance': 113004}], ['control 6', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [8000, 8800, 9680], 'peak_balance': 100000, 'end_balance': -226221}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['control 1', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 121021, 'end_balance': 121021}], ['control 2', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 1}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 100000, 'end_balance': -725097}], ['control 4', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 8000, 'step_bp': 750, 'steps': 2}, {'year_payments': [8000, 8600, 9245, 0, 0], 'peak_balance': 100000, 'end_balance': -286836}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0], 'peak_balance': 250000, 'end_balance': -247387}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 1 and (m - 1) % 12 == 0:
            if t <= x['steps']:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n - m)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
        peak = max(peak, bal)
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: re-level term', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['control 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['control 2', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0, 0, 0], 'peak_balance': 250000, 'end_balance': -326758}], ['control 3', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 2}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 1000000, 'end_balance': 351915}], ['control 4', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 250, 'steps': 2}, {'year_payments': [8000, 8200], 'peak_balance': 100000, 'end_balance': -92367}], ['control 5', {'principal': 100000, 'rate_bp': 900, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 109781, 'end_balance': 109781}], ['control 6', {'principal': 100000, 'rate_bp': 900, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 2}, {'year_payments': [20000, 21500, 23113, 0, 0], 'peak_balance': 100000, 'end_balance': -899279}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526, 52144, 52144], 'peak_balance': 1176523, 'end_balance': 4}], ['control 1', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 2', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['control 3', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['control 4', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 1173203, 'end_balance': 1173203}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [8000, 8800, 0], 'peak_balance': 100000, 'end_balance': -106813}], ['control 6', {'principal': 1000000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 3}, {'year_payments': [3000, 3225], 'peak_balance': 1048088, 'end_balance': 1048088}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['control 1', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 2', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 3', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 750, 'steps': 1}, {'year_payments': [8000, 8600], 'peak_balance': 250000, 'end_balance': 82092}], ['control 4', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 500, 'step_bp': 750, 'steps': 2}, {'year_payments': [500, 538, 578], 'peak_balance': 1286575, 'end_balance': 1286575}], ['control 5', {'principal': 100000, 'rate_bp': 900, 'months': 36, 'initial_payment': 500, 'step_bp': 750, 'steps': 2}, {'year_payments': [500, 538, 578], 'peak_balance': 108792, 'end_balance': 108792}], ['control 6', {'principal': 100000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [20000, 22000, 0, 0, 0], 'peak_balance': 100000, 'end_balance': -626476}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['control 2', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['control 4', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 0], 'peak_balance': 250000, 'end_balance': -266261}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 500, 'step_bp': 750, 'steps': 3}, {'year_payments': [500, 538], 'peak_balance': 113004, 'end_balance': 113004}], ['control 6', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [8000, 8800, 9680], 'peak_balance': 100000, 'end_balance': -226221}]], [['regression: re-level term', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['control 1', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 121021, 'end_balance': 121021}], ['control 2', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 1}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 100000, 'end_balance': -725097}], ['control 4', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 8000, 'step_bp': 750, 'steps': 2}, {'year_payments': [8000, 8600, 9245, 0, 0], 'peak_balance': 100000, 'end_balance': -286836}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0], 'peak_balance': 250000, 'end_balance': -247387}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
The fixed payment after the graduated years leaves most of the balance unpaid at maturity.

x = {'principal', 'rate_bp', 'months', 'initial_payment', 'step_bp', 'steps'}. Month m (1-based) is in year t = (m-1)//12. For t < steps+1 the year payment starts at initial_payment and each new year multiplies the previous year payment by (10000+step_bp)/10000, rounded half-up. At the start of year steps+1 (if the loan runs that long) the payment is re-leveled on the balance over the remaining months (exact annuity, half-up) and stays fixed. Interest round_half_up(balance*bp/120000); balance += interest - payment (it may grow). Return {'year_payments', 'peak_balance', 'end_balance'}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 1 and (m - 1) % 12 == 0:
            if t < x['steps']:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n - m + 1)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
        peak = max(peak, bal)
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: step count limit', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 2770, 2770, 2770], 'peak_balance': 250000, 'end_balance': -23}], ['control 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['control 2', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['control 4', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 45414, 45414], 'peak_balance': 1024671, 'end_balance': -2}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['control 6', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 24789, 24789, 24789, 24789], 'peak_balance': 1055512, 'end_balance': -16}]], [['regression: step count limit', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['regression: step count limit, partial-repair probe', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526, 52144, 52144], 'peak_balance': 1176523, 'end_balance': 4}], ['control 1', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 2', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['control 3', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 1173203, 'end_balance': 1173203}], ['control 4', {'principal': 250000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 8485, 8485], 'peak_balance': 250000, 'end_balance': -6}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 5006, 5006], 'peak_balance': 106341, 'end_balance': -5}]], [['regression: step count limit', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 0}, {'year_payments': [3000, 6284], 'peak_balance': 100000, 'end_balance': -2}], ['control 2', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 0}, {'year_payments': [20000, 739, 739, 739, 739], 'peak_balance': 250000, 'end_balance': -10}], ['control 3', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 4', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 815], 'peak_balance': 100000, 'end_balance': 3}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 3}, {'year_payments': [3000, 3225], 'peak_balance': 100000, 'end_balance': 33645}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [3000, 19659], 'peak_balance': 250000, 'end_balance': -4}]], [['regression: step count limit', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['regression: step count limit, partial-repair probe', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['control 2', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 27064, 27064, 27064, 27064], 'peak_balance': 1087553, 'end_balance': -13}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['control 4', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [8000, 22616, 22616, 22616, 22616], 'peak_balance': 1000000, 'end_balance': 0}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 500, 'step_bp': 750, 'steps': 3}, {'year_payments': [500, 538], 'peak_balance': 113004, 'end_balance': 113004}]], [['regression: step count limit', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['control 1', {'principal': 100000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 9448], 'peak_balance': 106341, 'end_balance': 5}], ['control 2', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 100000, 'end_balance': -725097}], ['control 3', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 5364, 5364, 5364, 5364], 'peak_balance': 250000, 'end_balance': 13}], ['control 4', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 23367], 'peak_balance': 267199, 'end_balance': 0}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 6', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 15163], 'peak_balance': 250000, 'end_balance': 4}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 1 and (m - 1) % 12 == 0:
            if t <= x['steps'] + 1:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n - m + 1)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
        peak = max(peak, bal)
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: step count limit', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 2770, 2770, 2770], 'peak_balance': 250000, 'end_balance': -23}], ['control 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['control 2', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['control 4', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 45414, 45414], 'peak_balance': 1024671, 'end_balance': -2}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['control 6', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 24789, 24789, 24789, 24789], 'peak_balance': 1055512, 'end_balance': -16}]], [['regression: step count limit', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['regression: step count limit, partial-repair probe', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526, 52144, 52144], 'peak_balance': 1176523, 'end_balance': 4}], ['control 1', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 2', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['control 3', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 1173203, 'end_balance': 1173203}], ['control 4', {'principal': 250000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 8485, 8485], 'peak_balance': 250000, 'end_balance': -6}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 5006, 5006], 'peak_balance': 106341, 'end_balance': -5}]], [['regression: step count limit', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 0}, {'year_payments': [3000, 6284], 'peak_balance': 100000, 'end_balance': -2}], ['control 2', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 0}, {'year_payments': [20000, 739, 739, 739, 739], 'peak_balance': 250000, 'end_balance': -10}], ['control 3', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 4', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 815], 'peak_balance': 100000, 'end_balance': 3}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 3}, {'year_payments': [3000, 3225], 'peak_balance': 100000, 'end_balance': 33645}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [3000, 19659], 'peak_balance': 250000, 'end_balance': -4}]], [['regression: step count limit', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['regression: step count limit, partial-repair probe', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['control 2', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 27064, 27064, 27064, 27064], 'peak_balance': 1087553, 'end_balance': -13}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['control 4', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [8000, 22616, 22616, 22616, 22616], 'peak_balance': 1000000, 'end_balance': 0}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 500, 'step_bp': 750, 'steps': 3}, {'year_payments': [500, 538], 'peak_balance': 113004, 'end_balance': 113004}]], [['regression: step count limit', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['control 1', {'principal': 100000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 9448], 'peak_balance': 106341, 'end_balance': 5}], ['control 2', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 100000, 'end_balance': -725097}], ['control 3', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 5364, 5364, 5364, 5364], 'peak_balance': 250000, 'end_balance': 13}], ['control 4', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 23367], 'peak_balance': 267199, 'end_balance': 0}], ['control 5', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 3}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 6', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 15163], 'peak_balance': 250000, 'end_balance': 4}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
The schedule re-levels one year early and skips the last step.

x = {'principal', 'rate_bp', 'months', 'initial_payment', 'step_bp', 'steps'}. Month m (1-based) is in year t = (m-1)//12. For t < steps+1 the year payment starts at initial_payment and each new year multiplies the previous year payment by (10000+step_bp)/10000, rounded half-up. At the start of year steps+1 (if the loan runs that long) the payment is re-leveled on the balance over the remaining months (exact annuity, half-up) and stays fixed. Interest round_half_up(balance*bp/120000); balance += interest - payment (it may grow). Return {'year_payments', 'peak_balance', 'end_balance'}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 1 and (m - 1) % 12 == 0:
            if t <= x['steps']:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n - m + 1)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: peak tracking', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 45414, 45414], 'peak_balance': 1024671, 'end_balance': -2}], ['control 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['control 2', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['control 4', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 2770, 2770, 2770], 'peak_balance': 250000, 'end_balance': -23}], ['control 5', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0, 0, 0], 'peak_balance': 250000, 'end_balance': -326758}], ['control 6', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}]], [['regression: peak tracking', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['control 1', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 2', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['control 3', {'principal': 250000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 8485, 8485], 'peak_balance': 250000, 'end_balance': -6}], ['control 4', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [8000, 8800, 0], 'peak_balance': 100000, 'end_balance': -106813}], ['control 5', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 0}, {'year_payments': [20000, 36120, 36120], 'peak_balance': 1000000, 'end_balance': -6}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 8000, 'step_bp': 750, 'steps': 0}, {'year_payments': [8000, 14350], 'peak_balance': 250000, 'end_balance': 0}]], [['regression: peak tracking', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['control 1', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 2', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [20000, 22000, 56636], 'peak_balance': 1000000, 'end_balance': 0}], ['control 3', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 0}, {'year_payments': [3000, 6284], 'peak_balance': 100000, 'end_balance': -2}], ['control 4', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 0}, {'year_payments': [20000, 739, 739, 739, 739], 'peak_balance': 250000, 'end_balance': -10}], ['control 5', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 6', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300, 7729, 7729, 7729], 'peak_balance': 250000, 'end_balance': 15}]], [['regression: peak tracking', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 27064, 27064, 27064, 27064], 'peak_balance': 1087553, 'end_balance': -13}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['control 2', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 3', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['control 4', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['control 5', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 0], 'peak_balance': 250000, 'end_balance': -266261}], ['control 6', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [8000, 22616, 22616, 22616, 22616], 'peak_balance': 1000000, 'end_balance': 0}]], [['regression: peak tracking', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 121021, 'end_balance': 121021}], ['control 1', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['control 2', {'principal': 100000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605, 666, 8252], 'peak_balance': 100000, 'end_balance': 1}], ['control 3', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 1}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 4', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [8000, 8800, 2083, 2083, 2083], 'peak_balance': 250000, 'end_balance': -11}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 100000, 'end_balance': -725097}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 5364, 5364, 5364, 5364], 'peak_balance': 250000, 'end_balance': 13}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    n = x['months']
    bal = x['principal']
    pay = x['initial_payment']
    years = [pay]
    peak = bal
    for m in range(1, n + 1):
        t = (m - 1) // 12
        if m > 1 and (m - 1) % 12 == 0:
            if t <= x['steps']:
                pay = rnd(pay * (10000 + x['step_bp']), 10000)
            elif t == x['steps'] + 1:
                pay = level(bal, n - m + 1)
            years.append(pay)
        interest = rnd(bal * bp, 120000)
        bal += interest - pay
        peak = max(peak, bal - interest)
    return {'year_payments': years, 'peak_balance': peak, 'end_balance': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: peak tracking', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 45414, 45414], 'peak_balance': 1024671, 'end_balance': -2}], ['control 1', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 3}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 204568}], ['control 2', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 7921, 7921], 'peak_balance': 250000, 'end_balance': 9}], ['control 3', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}], ['control 4', {'principal': 250000, 'rate_bp': 900, 'months': 60, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 2770, 2770, 2770], 'peak_balance': 250000, 'end_balance': -23}], ['control 5', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 1}, {'year_payments': [20000, 20500, 0, 0, 0], 'peak_balance': 250000, 'end_balance': -326758}], ['control 6', {'principal': 100000, 'rate_bp': 600, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 0}, {'year_payments': [500, 8607], 'peak_balance': 100000, 'end_balance': -4}]], [['regression: peak tracking', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550], 'peak_balance': 105921, 'end_balance': 105921}], ['control 1', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 2}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 2', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300], 'peak_balance': 250000, 'end_balance': 201795}], ['control 3', {'principal': 250000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 0}, {'year_payments': [8000, 8485, 8485], 'peak_balance': 250000, 'end_balance': -6}], ['control 4', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [8000, 8800, 0], 'peak_balance': 100000, 'end_balance': -106813}], ['control 5', {'principal': 1000000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 0}, {'year_payments': [20000, 36120, 36120], 'peak_balance': 1000000, 'end_balance': -6}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 24, 'initial_payment': 8000, 'step_bp': 750, 'steps': 0}, {'year_payments': [8000, 14350], 'peak_balance': 250000, 'end_balance': 0}]], [['regression: peak tracking', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225, 97511], 'peak_balance': 1115035, 'end_balance': 6}], ['control 1', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 1}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 2', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [20000, 22000, 56636], 'peak_balance': 1000000, 'end_balance': 0}], ['control 3', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 0}, {'year_payments': [3000, 6284], 'peak_balance': 100000, 'end_balance': -2}], ['control 4', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 20000, 'step_bp': 250, 'steps': 0}, {'year_payments': [20000, 739, 739, 739, 739], 'peak_balance': 250000, 'end_balance': -10}], ['control 5', {'principal': 250000, 'rate_bp': 900, 'months': 24, 'initial_payment': 3000, 'step_bp': 750, 'steps': 2}, {'year_payments': [3000, 3225], 'peak_balance': 250000, 'end_balance': 217726}], ['control 6', {'principal': 250000, 'rate_bp': 1200, 'months': 60, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [3000, 3300, 7729, 7729, 7729], 'peak_balance': 250000, 'end_balance': 15}]], [['regression: peak tracking', {'principal': 1000000, 'rate_bp': 900, 'months': 60, 'initial_payment': 500, 'step_bp': 250, 'steps': 0}, {'year_payments': [500, 27064, 27064, 27064, 27064], 'peak_balance': 1087553, 'end_balance': -13}], ['control 1', {'principal': 100000, 'rate_bp': 900, 'months': 24, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500], 'peak_balance': 100000, 'end_balance': -422889}], ['control 2', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 3000, 'step_bp': 1000, 'steps': 2}, {'year_payments': [3000, 3300, 3630], 'peak_balance': 100000, 'end_balance': 1568}], ['control 3', {'principal': 1000000, 'rate_bp': 900, 'months': 36, 'initial_payment': 8000, 'step_bp': 250, 'steps': 1}, {'year_payments': [8000, 8200, 86087], 'peak_balance': 1000000, 'end_balance': 5}], ['control 4', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605], 'peak_balance': 100000, 'end_balance': 98050}], ['control 5', {'principal': 250000, 'rate_bp': 900, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 0], 'peak_balance': 250000, 'end_balance': -266261}], ['control 6', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 0}, {'year_payments': [8000, 22616, 22616, 22616, 22616], 'peak_balance': 1000000, 'end_balance': 0}]], [['regression: peak tracking', {'principal': 100000, 'rate_bp': 1200, 'months': 36, 'initial_payment': 500, 'step_bp': 250, 'steps': 2}, {'year_payments': [500, 513, 526], 'peak_balance': 121021, 'end_balance': 121021}], ['control 1', {'principal': 1000000, 'rate_bp': 600, 'months': 60, 'initial_payment': 20000, 'step_bp': 750, 'steps': 1}, {'year_payments': [20000, 21500, 18254, 18254, 18254], 'peak_balance': 1000000, 'end_balance': -11}], ['control 2', {'principal': 100000, 'rate_bp': 600, 'months': 60, 'initial_payment': 500, 'step_bp': 1000, 'steps': 3}, {'year_payments': [500, 550, 605, 666, 8252], 'peak_balance': 100000, 'end_balance': 1}], ['control 3', {'principal': 250000, 'rate_bp': 1200, 'months': 24, 'initial_payment': 3000, 'step_bp': 250, 'steps': 1}, {'year_payments': [3000, 3075], 'peak_balance': 250000, 'end_balance': 235565}], ['control 4', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 8000, 'step_bp': 1000, 'steps': 1}, {'year_payments': [8000, 8800, 2083, 2083, 2083], 'peak_balance': 250000, 'end_balance': -11}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 36, 'initial_payment': 20000, 'step_bp': 750, 'steps': 3}, {'year_payments': [20000, 21500, 23113], 'peak_balance': 100000, 'end_balance': -725097}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 60, 'initial_payment': 3000, 'step_bp': 250, 'steps': 0}, {'year_payments': [3000, 5364, 5364, 5364, 5364], 'peak_balance': 250000, 'end_balance': 13}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
Peak balance disclosures report only the original principal.

x = {'principal', 'rate_bp', 'months', 'initial_payment', 'step_bp', 'steps'}. Month m (1-based) is in year t = (m-1)//12. For t < steps+1 the year payment starts at initial_payment and each new year multiplies the previous year payment by (10000+step_bp)/10000, rounded half-up. At the start of year steps+1 (if the loan runs that long) the payment is re-leveled on the balance over the remaining months (exact annuity, half-up) and stays fixed. Interest round_half_up(balance*bp/120000); balance += interest - payment (it may grow). Return {'year_payments', 'peak_balance', 'end_balance'}.

split: open-access


prompt:
Employees contributing to a 401(k) have too little social security withheld.

Input {ytd, lines:[[kind, cents]], base, exempt}. Social security wages are reg+ot+bonus+tips minus sec125 (401k deferrals do not reduce them), floored at 0. Exempt employees owe nothing ([0,0]). Taxable = min(wages, max(0, base-ytd)); tax = 6.2% rounded half-up to the cent. Return [taxable, tax].

split: open-access


prompt:
Tipped employees are under-withheld because reported tips never reach social security wages.

Input {ytd, lines:[[kind, cents]], base, exempt}. Social security wages are reg+ot+bonus+tips minus sec125 (401k deferrals do not reduce them), floored at 0. Exempt employees owe nothing ([0,0]). Taxable = min(wages, max(0, base-ytd)); tax = 6.2% rounded half-up to the cent. Return [taxable, tax].

split: open-access


prompt:
Employees already past the wage base get a negative taxable amount and a negative tax (a refund) on the paycheck.

Input {ytd, lines:[[kind, cents]], base, exempt}. Social security wages are reg+ot+bonus+tips minus sec125 (401k deferrals do not reduce them), floored at 0. Exempt employees owe nothing ([0,0]). Taxable = min(wages, max(0, base-ytd)); tax = 6.2% rounded half-up to the cent. Return [taxable, tax].

split: open-access


prompt:
Paychecks that cross the wage base withhold on the full paycheck instead of only the portion below the base.

Input {ytd, lines:[[kind, cents]], base, exempt}. Social security wages are reg+ot+bonus+tips minus sec125 (401k deferrals do not reduce them), floored at 0. Exempt employees owe nothing ([0,0]). Taxable = min(wages, max(0, base-ytd)); tax = 6.2% rounded half-up to the cent. Return [taxable, tax].

split: open-access


prompt:
Withholding is a cent short whenever 6.2% of taxable wages has a fractional cent of one half or more.

Input {ytd, lines:[[kind, cents]], base, exempt}. Social security wages are reg+ot+bonus+tips minus sec125 (401k deferrals do not reduce them), floored at 0. Exempt employees owe nothing ([0,0]). Taxable = min(wages, max(0, base-ytd)); tax = 6.2% rounded half-up to the cent. Return [taxable, tax].

split: open-access


prompt:
After the threshold is crossed, each paycheck applies the extra 0.9% to all year-to-date excess wages again.

Input [ytd_wages, wages] in cents. Regular Medicare is 1.45% of wages rounded half-up. Additional Medicare is 0.9% of the part of this paycheck that pushes year-to-date wages above 200,000.00, rounded half-up separately. Return [medicare, additional_wages, medicare + additional].

split: open-access


prompt:
Employees below the threshold get additional Medicare wages larger than the paycheck itself.

Input [ytd_wages, wages] in cents. Regular Medicare is 1.45% of wages rounded half-up. Additional Medicare is 0.9% of the part of this paycheck that pushes year-to-date wages above 200,000.00, rounded half-up separately. Return [medicare, additional_wages, medicare + additional].

split: open-access


prompt:
The total Medicare line is off by a cent from the sum of its two printed components.

Input [ytd_wages, wages] in cents. Regular Medicare is 1.45% of wages rounded half-up. Additional Medicare is 0.9% of the part of this paycheck that pushes year-to-date wages above 200,000.00, rounded half-up separately. Return [medicare, additional_wages, medicare + additional].

split: open-access


prompt:
Additional Medicare withholding is ten times too large once the threshold is crossed.

Input [ytd_wages, wages] in cents. Regular Medicare is 1.45% of wages rounded half-up. Additional Medicare is 0.9% of the part of this paycheck that pushes year-to-date wages above 200,000.00, rounded half-up separately. Return [medicare, additional_wages, medicare + additional].

split: open-access


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

N = 1
observations = []
def solve(x):
    periods = {'weekly': 52, 'biweekly': 26, 'semimonthly': 24, 'monthly': 12}[x['freq']]
    annual = x['wage'] * periods
    adjusted = max(0, annual - x['allow'] * 430000)
    mult = 2 if x['status'] == 'married' else 1
    brackets = [(400000 * mult, 10), (1500000 * mult, 12), (5000000 * mult, 22)]
    tax = 0
    for i, (lo, rate) in enumerate(brackets):
        hi = brackets[i + 1][0] if i + 1 < len(brackets) else None
        if adjusted > lo:
            top = adjusted
            tax += (top - lo) * rate
    wh = (tax * 2 + 100 * periods) // (200 * periods)
    return wh
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('regression (boundary)', {'wage': 57693, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 5577), ('partial-repair probe', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 22359, 'freq': 'semimonthly', 'allow': 5, 'status': 'married'}, 0), ('normal control', {'wage': 89742, 'freq': 'monthly', 'allow': 1, 'status': 'married'}, 0), ('normal control', {'wage': 17607, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 0), ('normal control', {'wage': 6134, 'freq': 'semimonthly', 'allow': 0, 'status': 'single'}, 0)], [('regression', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('regression', {'wage': 998152, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 168152), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('partial-repair probe', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 52312, 'freq': 'semimonthly', 'allow': 5, 'status': 'single'}, 0), ('normal control', {'wage': 28087, 'freq': 'biweekly', 'allow': 1, 'status': 'single'}, 0), ('normal control', {'wage': 97334, 'freq': 'monthly', 'allow': 6, 'status': 'single'}, 0), ('normal control', {'wage': 18119, 'freq': 'weekly', 'allow': 3, 'status': 'married'}, 0)], [('regression', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('regression', {'wage': 992194, 'freq': 'weekly', 'allow': 3, 'status': 'married'}, 190902), ('partial-repair probe', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('partial-repair probe', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 10871, 'freq': 'weekly', 'allow': 6, 'status': 'single'}, 0), ('normal control', {'wage': 836, 'freq': 'semimonthly', 'allow': 1, 'status': 'single'}, 0), ('normal control', {'wage': 30453, 'freq': 'biweekly', 'allow': 2, 'status': 'married'}, 0), ('normal control', {'wage': 36754, 'freq': 'biweekly', 'allow': 1, 'status': 'single'}, 483)], [('regression', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('regression', {'wage': 1223812, 'freq': 'monthly', 'allow': 5, 'status': 'married'}, 134822), ('partial-repair probe', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 124657, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 1966), ('normal control', {'wage': 27188, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 0), ('normal control', {'wage': 157996, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 5300), ('normal control', {'wage': 143245, 'freq': 'biweekly', 'allow': 5, 'status': 'married'}, 2978)], [('regression', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('regression', {'wage': 475908, 'freq': 'monthly', 'allow': 6, 'status': 'single'}, 25476), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('partial-repair probe', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 23859, 'freq': 'semimonthly', 'allow': 2, 'status': 'married'}, 0), ('normal control', {'wage': 50426, 'freq': 'semimonthly', 'allow': 5, 'status': 'married'}, 0), ('normal control', {'wage': 128102, 'freq': 'monthly', 'allow': 5, 'status': 'married'}, 0), ('normal control', {'wage': 6608, 'freq': 'semimonthly', 'allow': 0, 'status': 'married'}, 0)]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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(x):
    periods = {'weekly': 52, 'biweekly': 26, 'semimonthly': 24, 'monthly': 12}[x['freq']]
    annual = x['wage'] * periods
    adjusted = max(0, annual - x['allow'] * 430000)
    mult = 2 if x['status'] == 'married' else 1
    brackets = [(400000 * mult, 10), (1500000 * mult, 12), (5000000 * mult, 22)]
    tax = 0
    for i, (lo, rate) in enumerate(brackets):
        hi = brackets[i + 1][0] if i + 1 < len(brackets) else None
        if adjusted > lo:
            top = adjusted if hi is None else min(adjusted, hi - 1)
            tax += (top - lo) * rate
    wh = (tax * 2 + 100 * periods) // (200 * periods)
    return wh
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('regression (boundary)', {'wage': 57693, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 5577), ('partial-repair probe', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 22359, 'freq': 'semimonthly', 'allow': 5, 'status': 'married'}, 0), ('normal control', {'wage': 89742, 'freq': 'monthly', 'allow': 1, 'status': 'married'}, 0), ('normal control', {'wage': 17607, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 0), ('normal control', {'wage': 6134, 'freq': 'semimonthly', 'allow': 0, 'status': 'single'}, 0)], [('regression', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('regression', {'wage': 998152, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 168152), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('partial-repair probe', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 52312, 'freq': 'semimonthly', 'allow': 5, 'status': 'single'}, 0), ('normal control', {'wage': 28087, 'freq': 'biweekly', 'allow': 1, 'status': 'single'}, 0), ('normal control', {'wage': 97334, 'freq': 'monthly', 'allow': 6, 'status': 'single'}, 0), ('normal control', {'wage': 18119, 'freq': 'weekly', 'allow': 3, 'status': 'married'}, 0)], [('regression', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('regression', {'wage': 992194, 'freq': 'weekly', 'allow': 3, 'status': 'married'}, 190902), ('partial-repair probe', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('partial-repair probe', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 10871, 'freq': 'weekly', 'allow': 6, 'status': 'single'}, 0), ('normal control', {'wage': 836, 'freq': 'semimonthly', 'allow': 1, 'status': 'single'}, 0), ('normal control', {'wage': 30453, 'freq': 'biweekly', 'allow': 2, 'status': 'married'}, 0), ('normal control', {'wage': 36754, 'freq': 'biweekly', 'allow': 1, 'status': 'single'}, 483)], [('regression', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('regression', {'wage': 1223812, 'freq': 'monthly', 'allow': 5, 'status': 'married'}, 134822), ('partial-repair probe', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 124657, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 1966), ('normal control', {'wage': 27188, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 0), ('normal control', {'wage': 157996, 'freq': 'semimonthly', 'allow': 4, 'status': 'married'}, 5300), ('normal control', {'wage': 143245, 'freq': 'biweekly', 'allow': 5, 'status': 'married'}, 2978)], [('regression', {'wage': 460175, 'freq': 'biweekly', 'allow': 4, 'status': 'married'}, 42839), ('regression', {'wage': 475908, 'freq': 'monthly', 'allow': 6, 'status': 'single'}, 25476), ('partial-repair probe', {'wage': 223432, 'freq': 'monthly', 'allow': 0, 'status': 'single'}, 20979), ('partial-repair probe', {'wage': 497372, 'freq': 'semimonthly', 'allow': 2, 'status': 'single'}, 77789), ('boundary control', {'wage': 0, 'freq': 'weekly', 'allow': 0, 'status': 'single'}, 0), ('boundary control', {'wage': 125000, 'freq': 'semimonthly', 'allow': 1, 'status': 'married'}, 7375), ('normal control', {'wage': 23859, 'freq': 'semimonthly', 'allow': 2, 'status': 'married'}, 0), ('normal control', {'wage': 50426, 'freq': 'semimonthly', 'allow': 5, 'status': 'married'}, 0), ('normal control', {'wage': 128102, 'freq': 'monthly', 'allow': 5, 'status': 'married'}, 0), ('normal control', {'wage': 6608, 'freq': 'semimonthly', 'allow': 0, 'status': 'married'}, 0)]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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:
Employees whose income reaches a higher bracket are withheld far too much because lower brackets tax the whole income.

Input {wage, freq, allow, status}. Annualize: wage*periods (weekly 52, biweekly 26, semimonthly 24, monthly 12); subtract 4,300.00 per allowance, floor 0. Marginal brackets over 4,000/15,000/50,000 dollars at 10/12/22% (married thresholds doubled). Annual tax (in cent-percent units) is divided by periods and rounded half-up to cents once.

split: open-access