Savings vs Credit-Card Debt

A savings vs debt simulation comparing compounding interest effects.

Level:Beginner

debtreinforcing-loopexponential

  • Stocks:savings_balance, debt_balance
  • Feedback Loops:compound interest on savings (reinforcing), compound interest on debt (reinforcing)
  • Probes:savings_balance, debt_balance

Dynamic Behavior Patterns

Explore common system behaviors: exponential growth, goal-seeking decay, overshoot-and-collapse, and S-curve saturation.

Explore Dynamic Behavior Patterns
simulation.py

Savings versus credit-card debt

This pocket-sized model pits compounding savings against compounding debt. Adjust the monthly deposit and payment amounts to explore how the long‑term balance changes.


from tys import probe, progress

Simulate monthly balances of savings and debt.

def simulate(cfg: dict):

    import simpy
    env = simpy.Environment()

Parameters

    savings = cfg["initial_savings"]
    debt = cfg["initial_debt"]
    savings_rate = cfg["savings_rate"]      # monthly interest rate
    debt_rate = cfg["debt_rate"]            # monthly interest rate
    deposit = cfg["monthly_deposit"]        # deposit to savings each month
    payment = cfg["monthly_payment"]        # debt payment each month
    months = cfg["months"]

    done = env.event()

Apply interest and payments each month.

    def cycle():
        nonlocal savings, debt
        for m in range(months):
            savings *= 1 + savings_rate
            debt *= 1 + debt_rate
            savings += deposit
            debt = max(0, debt - payment)
            probe("savings_balance", env.now, savings)
            probe("debt_balance", env.now, debt)
            progress(int(100 * (m + 1) / months))
            yield env.timeout(1)
        done.succeed({"final_savings": savings, "final_debt": debt})

    env.process(cycle())
    env.run(until=done)
    return done.value


def requirements():
    return {
        "builtin": ["micropip", "pyyaml"],
        "external": ["simpy==4.1.1"],
    }
Default.yaml
initial_savings: 1000
initial_debt: 2000
savings_rate: 0.005
debt_rate: 0.015
monthly_deposit: 100
monthly_payment: 50
months: 36
Charts (Default)

savings_balance

savings_balance chartCSV
Samples36 @ 0.00–35.00
Valuesmin 1105.00, mean 3060.79, median 3029.94, max 5130.29, σ 1194.50

debt_balance

debt_balance chartCSV
Samples36 @ 0.00–35.00
Valuesmin 1054.48, mean 1556.11, median 1577.14, max 1980.00, σ 274.22
Final Results (Default)
MetricValue
final_savings5130.29
final_debt1054.48
FAQ
Which balance grows faster?
Each month savings and debt both accrue interest, but deposits add to savings while payments reduce the debt.
How is interest applied each month?
Balances are multiplied by 1 + savings_rate or debt_rate before deposits and payments.
Can the debt balance go negative?
No. After each payment the balance is clamped at zero once paid off.