Savings vs Credit-Card Debt
Compares growth of savings and credit-card debt under compounding interest.
Level:Beginner
Dynamic Behavior Patterns
Explore common system behaviors: exponential growth, goal-seeking decay, overshoot-and-collapse, and S-curve saturation.
Explore Dynamic Behavior Patternssimulation.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"],
}
config.yaml
initial_savings: 1000
initial_debt: 2000
savings_rate: 0.005
debt_rate: 0.015
monthly_deposit: 100
monthly_payment: 50
months: 36