Workbench / Household Numbers: the Mathematics Capstone
S9 · B1·09
Signed Money and the Overdraft
Stage 9, after chapter 9: money in and out as one signed amount, a running balance that dips below zero before the salary arrives, overdraft interest charged day by day on the negative balance, and this month's spending against last month's, with "more" and "less" read correctly from the sign.
Stage 9: signed money and the overdraft
The brief
What you'll build. A month of the household's account as signed amounts in paise: positive for money in, negative for money out. The program keeps the running balance day by day, finds the lowest point, charges overdraft interest for every day the balance is below zero, and compares each kind of spending with last month, saying in words whether more or less went out.
Why a real household needs this. A month can end in credit and still have cost money: rent and bills often go out before the salary comes in, and the days in between are overdrawn (or covered by a credit card, at a similar cost). Seeing the dip, and what it cost, is the case for moving a due date or keeping a buffer.
Why now. Chapter 9 gives signed numbers, subtraction as adding the opposite, and comparisons that depend on direction: a change of −₹915 in a negative spending line means more spent. Stage 3 added a ledger's totals; this stage keeps its sign.
What you'll have at the end. signed.py; your own month's account as signed amounts; the cost of its lowest stretch; and a month-on-month comparison you can read at a glance.
Theory to own
- One signed column. Why can a single signed amount replace separate "in" and "out" columns without losing information? (Chapter: "Opposites"; exercises, C1.)
- The lowest point. Why can the balance at the end of the month be positive while the lowest balance was negative, and why does the lowest matter for interest? (Chapter: "Adding: moves on the line".)
- Interest on a negative balance. Interest is charged on the size of the overdraft, , for each day . Why is it , and why rather than ? (Chapter: "Order and absolute value".)
- Reading a change. Spending is negative. If groceries go from −₹12,900 to −₹13,815.25, the change (now − last) is −₹915.25. Why does a negative change mean more spent here? (Chapter: "Subtracting: add the opposite"; and "Why inequalities flip".)
Build it
Step 1: by hand. Write last month's account as signed amounts by date: income positive, payments negative. Keep a running balance and mark the lowest point and the days it was below zero.
Step 2: the code. Paise throughout; the interest is exact until it's rounded once:
"""Stage 9: money in and out as signed paise, a balance that dips below zero, overdraft interest, and changes from last month."""
from fractions import Fraction as F
def show(p):
sign = "−" if p < 0 else "+" if p > 0 else " "
r, s = divmod(abs(p), 100)
return f"{sign}₹{r:,}.{s:02d}"
# day of the month, what, signed amount in paise (+ money in, − money out)
month = [(1, "rent", -24_000_00), (3, "groceries", -6_480_00), (5, "electricity", -2_215_00),
(7, "salary", +85_000_00), (10, "card bill", -31_249_50), (18, "refund", +1_499_00),
(20, "EMI", -18_750_00), (27, "groceries", -7_335_25)]
opening = 12_500_00
rate = F(12, 100) / 365 # overdraft interest: 12% a year, charged daily on a negative balance
balance, interest, worst, by_day = opening, F(0), opening, {}
for day, what, amount in month:
by_day.setdefault(day, []).append(amount)
for day in range(1, 31):
for amount in by_day.get(day, []):
balance += amount
worst = min(worst, balance)
if balance < 0:
interest += -balance * rate # paise of interest for this day, exact
charge = (2 * interest.numerator + interest.denominator) // (2 * interest.denominator)
print("money in: ", show(sum(a for _, _, a in month if a > 0)), "| money out:", show(sum(a for _, _, a in month if a < 0)))
print("closing: ", show(balance), "| lowest:", show(worst), "| overdraft interest:", show(-charge))
last_month = {"rent": -24_000_00, "groceries": -12_900_00, "electricity": -2_715_00, "card bill": -28_100_00}
this_month = {}
for _, what, amount in month:
this_month[what] = this_month.get(what, 0) + amount
for what, before in last_month.items():
now = this_month.get(what, 0)
change = now - before # signed: positive means less money went out
words = "same" if change == 0 else (f"{show(abs(change))[1:]} less spent" if change > 0 else f"{show(abs(change))[1:]} more spent")
print(f"{what:<12} last {show(before):>13} now {show(now):>13} change {show(change):>12} ({words})")
money in: +₹86,499.00 | money out: −₹90,029.75
closing: +₹8,969.25 | lowest: −₹20,195.00 | overdraft interest: −₹32.66
rent last −₹24,000.00 now −₹24,000.00 change ₹0.00 (same)
groceries last −₹12,900.00 now −₹13,815.25 change −₹915.25 (₹915.25 more spent)
electricity last −₹2,715.00 now −₹2,215.00 change +₹500.00 (₹500.00 less spent)
card bill last −₹28,100.00 now −₹31,249.50 change −₹3,149.50 (₹3,149.50 more spent)
Step 3: read the output. ₹86,499 came in and ₹90,029.75 went out, and the balance still closed at +₹8,969.25 because the month started at ₹12,500. But rent, groceries and electricity all left before the salary on the 7th, so the account was overdrawn for six days, down to −₹20,195.00 on the 5th. At 12 % a year charged daily, those six days cost ₹32.66. In the comparison, electricity's change of +₹500 means ₹500 less went out (a smaller negative), while groceries' −₹915.25 and the card bill's −₹3,149.50 mean more: the sign of a change depends on which way the line points, so the program says it in words.
Step 4: your numbers, and a fix. Run it with your own month. Then try moving one payment (the rent, say) to after the salary date, or keeping a buffer equal to the lowest point, and measure what the overdraft interest becomes. (A real bank's rate, calculation method and charges differ; read your account's terms.)
Senior and architect lens
- Sign conventions belong in the schema. "Positive is money in" must be written down; systems that disagree (one stores refunds as positive charges, another as negative) produce exactly the errors in chapter 9's companion.
- Path, not just endpoints. A total or closing balance hides the dips; anything that charges, limits or alerts on a level (overdrafts, rate limits, stock, quotas) needs the running value, not the end-of-period one.
- Say the direction in words. Dashboards that show "−₹915" beside a spending line are ambiguous; "₹915 more spent" isn't. Compute with signs, display with words or colours that mean one thing.
Evidence
signed.pywith your own month, and its output.- Your hand-kept running balance with the lowest point marked.
- The overdraft cost before and after your fix in Step 4.
Carry forward
Stage 10, after chapter 10, brings in powers: money that grows by a percentage every period, compound interest on savings and on a loan in closed form, how long it takes to double, and why an EMI is what it is.