"""SA-006 money-maths tools (owner 30 Nov 2026 13:33 UK "do 1 close this model and v3 pack"; numbers 13:35 "5000 300"). Plain code the model calls; the sums are done here, never by the model. Interest, rate conversion, loans, savings, percentages, and basic statistics. Amounts are plain numbers (any currency); rates are percentages (5 means 5%).""" import math, statistics as st def _n(x): if isinstance(x, (int, float)): return float(x) s = str(x).lower().replace("£", "").replace("$", "").replace("€", "").replace(",", "").replace("%", "").strip() mult = 1 if s.endswith("k"): mult, s = 1000, s[:-1] elif s.endswith("m"): mult, s = 1_000_000, s[:-1] return float(s) * mult def _m(x): return f"{x:,.2f}" def _list(x): if isinstance(x, (list, tuple)): return [_n(v) for v in x] return [_n(v) for v in str(x).replace(";", ",").replace(" ", ",").split(",") if v.strip()] FREQ = {"annually": 1, "yearly": 1, "semiannually": 2, "half-yearly": 2, "quarterly": 4, "monthly": 12, "weekly": 52, "daily": 365} def _f(x): if x is None: return 1 if isinstance(x, (int, float)): return int(x) s = str(x).lower().strip() return FREQ[s] if s in FREQ else int(float(s)) def compound_interest(principal, rate, years, compounding="annually", monthly_deposit=0): P, r, t, k, d = _n(principal), _n(rate) / 100, _n(years), _f(compounding), _n(monthly_deposit) fv = P * (1 + r / k) ** (k * t) dep = 0.0 if d: # deposits at the end of each month, growing at the same effective rate rm = (1 + r / k) ** (k / 12) - 1; n = round(12 * t) dep = d * n if rm == 0 else d * ((1 + rm) ** n - 1) / rm total = fv + dep; paid = P + d * round(12 * t) return f"Final value {_m(total)} after {t:g} years at {r*100:g}% compounded {k}x a year (paid in {_m(paid)}, interest earned {_m(total - paid)})" def simple_interest(principal, rate, years): P, r, t = _n(principal), _n(rate) / 100, _n(years); i = P * r * t return f"Simple interest {_m(i)}; final value {_m(P + i)}" def convert_rate(rate, given="nominal", compounding="monthly"): r, k = _n(rate) / 100, _f(compounding) if str(given).lower().startswith("nom"): e = (1 + r / k) ** k - 1; return f"Effective annual rate {e*100:.4f}% (from nominal {r*100:g}% compounded {k}x a year)" nom = k * ((1 + r) ** (1 / k) - 1); return f"Nominal rate {nom*100:.4f}% compounded {k}x a year (gives effective {r*100:g}%)" def loan_payment(amount, rate, years, payments_per_year=12): P, r, t, k = _n(amount), _n(rate) / 100, _n(years), _f(payments_per_year) n = round(k * t); i = r / k pay = P / n if i == 0 else P * i / (1 - (1 + i) ** -n) return f"Payment {_m(pay)} per period ({n} payments); total paid {_m(pay * n)}, total interest {_m(pay * n - P)}" def percentage(kind, a, b): a, b, k = _n(a), _n(b), str(kind).lower() if k == "of": return f"{a:g}% of {b:g} = {a * b / 100:.4f}".rstrip("0").rstrip(".") if k == "what_percent": return f"{a:g} is {a / b * 100:.4f}% of {b:g}" if k == "change": return f"Change from {a:g} to {b:g} = {(b - a) / a * 100:.4f}%" if k == "increase": return f"{a:g} increased by {b:g}% = {a * (1 + b / 100):.4f}" if k == "decrease": return f"{a:g} decreased by {b:g}% = {a * (1 - b / 100):.4f}" if k == "reverse": return f"Original value before a {b:g}% increase to {a:g} = {a / (1 + b / 100):.4f}" return "error: kind must be of, what_percent, change, increase, decrease, reverse" def stats(numbers): x = _list(numbers) if len(x) < 2: return "error: give at least two numbers" return (f"n={len(x)}, mean {st.mean(x):.4f}, median {st.median(x):.4f}, sample standard deviation {st.stdev(x):.4f}, " f"population standard deviation {st.pstdev(x):.4f}, min {min(x):g}, max {max(x):g}, range {max(x) - min(x):g}") def correlation(x, y): a, b = _list(x), _list(y) if len(a) != len(b) or len(a) < 3: return "error: x and y need the same length (at least 3)" return f"Pearson correlation r = {st.correlation(a, b):.4f} (n={len(a)})" MONEY_TOOLS = [ {"type": "function", "function": {"name": "compound_interest", "description": "Grow savings or an investment with compound interest, optionally with a regular monthly deposit. rate is a yearly % (5 means 5%).", "parameters": {"type": "object", "properties": {"principal": {"type": "number"}, "rate": {"type": "number"}, "years": {"type": "number"}, "compounding": {"type": "string", "enum": ["annually", "semiannually", "quarterly", "monthly", "daily"]}, "monthly_deposit": {"type": "number"}}, "required": ["principal", "rate", "years"]}}}, {"type": "function", "function": {"name": "simple_interest", "description": "Simple (non-compound) interest on an amount.", "parameters": {"type": "object", "properties": {"principal": {"type": "number"}, "rate": {"type": "number"}, "years": {"type": "number"}}, "required": ["principal", "rate", "years"]}}}, {"type": "function", "function": {"name": "convert_rate", "description": "Convert between a nominal yearly rate and the effective annual rate (APR/AER). given: 'nominal' or 'effective'.", "parameters": {"type": "object", "properties": {"rate": {"type": "number"}, "given": {"type": "string", "enum": ["nominal", "effective"]}, "compounding": {"type": "string", "enum": ["annually", "semiannually", "quarterly", "monthly", "daily"]}}, "required": ["rate", "given", "compounding"]}}}, {"type": "function", "function": {"name": "loan_payment", "description": "Repayment on a loan or mortgage: payment per period, total paid and total interest.", "parameters": {"type": "object", "properties": {"amount": {"type": "number"}, "rate": {"type": "number"}, "years": {"type": "number"}, "payments_per_year": {"type": "integer"}}, "required": ["amount", "rate", "years"]}}}, {"type": "function", "function": {"name": "percentage", "description": "Percentages. kind: 'of' (a% of b), 'what_percent' (a is what % of b), 'change' (% change from a to b), 'increase'/'decrease' (a changed by b%), 'reverse' (original value before a b% increase that gave a).", "parameters": {"type": "object", "properties": {"kind": {"type": "string", "enum": ["of", "what_percent", "change", "increase", "decrease", "reverse"]}, "a": {"type": "number"}, "b": {"type": "number"}}, "required": ["kind", "a", "b"]}}}, {"type": "function", "function": {"name": "stats", "description": "Mean, median, standard deviation (sample and population), min, max and range of a list of numbers.", "parameters": {"type": "object", "properties": {"numbers": {"type": "array", "items": {"type": "number"}}}, "required": ["numbers"]}}}, {"type": "function", "function": {"name": "correlation", "description": "Pearson correlation between two lists of numbers of the same length.", "parameters": {"type": "object", "properties": {"x": {"type": "array", "items": {"type": "number"}}, "y": {"type": "array", "items": {"type": "number"}}}, "required": ["x", "y"]}}}, ] FUNCS = {"compound_interest": compound_interest, "simple_interest": simple_interest, "convert_rate": convert_rate, "loan_payment": loan_payment, "percentage": percentage, "stats": stats, "correlation": correlation}