Calculator in Business and startups
ROI and payback
Works out simple and annualized ROI, payback years and NPV for an investment with a level yearly benefit.
- Changed 4 Dec 2026
- 10 test cases pass
- Version 1
- About 3,450 tokens
- Needed by the large model tested, 4 Dec 2026
In short
| Formula | I, U, A, n, R = initial_investment, upfront_benefit, annual_net_benefit, analysis_years, residual_value; r = discount_rate_percent ÷ 100totalNominalInflow = U + A × n + R; totalNetGain = totalNominalInflow − IsimpleRoiPercent = totalNetGain ÷ I × 100annualizedRoiPercent = ((totalNominalInflow ÷ I) ^ (1 ÷ n) − 1) × 100paybackYears = 0 if U ≥ I; (I − U) ÷ A if U + A × n ≥ I; n if R at year n's end closes it; else nonediscountedPaybackYears = the same on discounted inflows, interpolated by year y's A ÷ (1 + r) ^ ytotalDiscountedInflow = U + A ÷ (1 + r) + … + A ÷ (1 + r) ^ n + R ÷ (1 + r) ^ n; npv = totalDiscountedInflow − I |
| Units | Dollars, years, percent; results to 0.01, half up. |
| Defaults | I 50000, U 0, A 14000, n 5 (whole, 1 to 100), R 8000, discount_rate_percent 10 (0 to 1000). |
| Worked example | the defaults. simpleRoiPercent = (70000 + 8000 − 50000) ÷ 50000 × 100 = 56paybackYears = 50000 ÷ 14000 = 3.57npv = 14000 × 3.79079 + 8000 ÷ 1.61051 − 50000 = 8038.39 |
By hand: yes. A small model got these answers from the formulas above, without running code.
Say this to your agent
Use https://mysi.dev/roi-payback/ to find ROI, payback and NPV: 50,000 now, 14,000 a year for 5 years, 8,000 back, 10%. Nothing to install, no key, no settings. An agent that can open web pages and run Python opens the page, runs the tested code in its own sandbox and gives you the answer.
The code
Copy this into your Python tool. Run roi_payback_check() first, then call roi_payback() with your numbers. Give the answer with the cite line.
"""ROI and payback: simple and annualized return, payback years (plain and discounted) and net
present value of an up-front investment that returns a fixed net benefit every year.
Inputs: initial_investment (0.01 to 1e12, default 50000), upfront_benefit (0 to 1e12,
default 0, received at year 0), annual_net_benefit (0 to 1e12, default 14000),
analysis_years (whole years 1 to 100, default 5), residual_value (0 to 1e12, default 8000,
received at the end of the last year), discount_rate_percent (0 to 1000, default 10).
Returns simpleRoiPercent, annualizedRoiPercent, paybackYears, discountedPaybackYears (None when
not reached), totalNetGain, totalNominalInflow, totalDiscountedInflow, npv; money to cents.
mysi, ROI and payback, mysi.dev/roi-payback, version 1, changed 2026-12-04. MIT.
"""
import math
def _roi_payback_num(name, value, low, high, whole=False):
if isinstance(value, bool) or not isinstance(value, (int, float)) or not low <= value <= high \
or (whole and value != int(value)):
raise ValueError(f"{name} must be a {'whole ' * whole}number from {low} to {high}")
return int(value) if whole else float(value)
def _roi_payback_r2(x):
return math.floor(x * 100 + 0.5) / 100 # to the cent, halves up
def _roi_payback_years(cost, cumulative, residual_in_last):
# cumulative[y] = inflow received by the end of year y; the benefit arrives evenly through
# each year, so payback is interpolated inside the year. The residual comes at the very end.
for y, cur in enumerate(cumulative):
if cur < cost:
continue
if y == 0:
return 0.0
prev = cumulative[y - 1]
if y == len(cumulative) - 1 and residual_in_last > 0:
cur -= residual_in_last
if cur < cost:
return float(y)
if cur - prev <= 0:
return float(y)
return _roi_payback_r2(y - 1 + min(1, max(0, (cost - prev) / (cur - prev))))
return None
def roi_payback(initial_investment=50000, upfront_benefit=0, annual_net_benefit=14000,
analysis_years=5, residual_value=8000, discount_rate_percent=10):
cost = _roi_payback_num("initial_investment", initial_investment, 0.01, 1e12)
up = _roi_payback_num("upfront_benefit", upfront_benefit, 0, 1e12)
a = _roi_payback_num("annual_net_benefit", annual_net_benefit, 0, 1e12)
n = _roi_payback_num("analysis_years", analysis_years, 1, 100, whole=True)
res = _roi_payback_num("residual_value", residual_value, 0, 1e12)
r = _roi_payback_num("discount_rate_percent", discount_rate_percent, 0, 1000) / 100
nominal = [up + a * y + (res if y == n else 0) for y in range(n + 1)]
# each year's benefit is discounted at the end of its year (ordinary annuity), not mid-year
disc = [up + sum(a / (1 + r) ** i for i in range(1, y + 1)) for y in range(n + 1)]
disc[-1] += res / (1 + r) ** n
inflow, r2 = nominal[-1], _roi_payback_r2
gain = inflow - cost
annualized = -100.0 if inflow <= 0 else ((inflow / cost) ** (1 / n) - 1) * 100
return {
"simpleRoiPercent": r2(gain / cost * 100),
"annualizedRoiPercent": r2(annualized),
"paybackYears": _roi_payback_years(cost, nominal, res),
"discountedPaybackYears": _roi_payback_years(cost, disc, res / (1 + r) ** n),
"totalNetGain": r2(gain),
"totalNominalInflow": r2(inflow),
"totalDiscountedInflow": r2(disc[-1]),
"npv": r2(disc[-1] - cost),
}
ROI_PAYBACK_CASES = [
(dict(initial_investment=50000, upfront_benefit=0, annual_net_benefit=14000, analysis_years=5,
residual_value=8000, discount_rate_percent=10),
{"simpleRoiPercent": 56.0, "paybackYears": 3.57, "npv": 8038.39}),
(dict(initial_investment=1000, upfront_benefit=0, annual_net_benefit=400, analysis_years=3,
residual_value=0, discount_rate_percent=10),
{"simpleRoiPercent": 20.0, "paybackYears": 2.5, "npv": -5.26}),
(dict(initial_investment=1000, upfront_benefit=200, annual_net_benefit=500, analysis_years=2,
residual_value=100, discount_rate_percent=0),
{"simpleRoiPercent": 30.0, "paybackYears": 1.6, "npv": 300.0}),
]
def roi_payback_check(tolerance=1e-3):
for given, want in ROI_PAYBACK_CASES:
got = roi_payback(**given)
for name, value in want.items():
close = abs(got[name] - value) <= tolerance * max(1, abs(value))
assert close, (name, got[name], value)
return "ok"
if __name__ == "__main__":
import json, sys
roi_payback_check()
given = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a)
js = lambda v: v.lstrip("-")[:1].isdigit() or v[:1] in '[{".' or v in ("true", "false", "null")
given = {k: json.loads(v) if js(v) else v for k, v in given.items()}
if given:
print(json.dumps(roi_payback(**given))) Planning estimates only, not financial advice.
What it takes
| Input | Meaning | Default | Allowed |
|---|---|---|---|
| initial_investment | Up-front cost at year 0, in dollars; at least 0.01 | 50000 | 0.01 to 1,000,000,000,000 |
| upfront_benefit | Benefit received at once at year 0 (a rebate or grant), in dollars; never discounted | 0 | 0 to 1,000,000,000,000 |
| annual_net_benefit | Net benefit per year, in dollars; spread evenly for payback, discounted at each year end for NPV | 14000 | 0 to 1,000,000,000,000 |
| analysis_years | Length of the analysis in whole years | 5 | 1 to 100 |
| residual_value | Value recovered at the end of the last year, in dollars | 8000 | 0 to 1,000,000,000,000 |
| discount_rate_percent | Yearly discount rate or cost of capital, in percent (10 means 10%) | 10 | 0 to 1000 |
What it returns
| Output | Meaning |
|---|---|
| simpleRoiPercent | Total net gain over the period as a percent of the investment |
| annualizedRoiPercent | Compound yearly rate that turns the investment into the total inflow, in percent |
| paybackYears, discountedPaybackYears | Years until the inflows, plain or discounted, cover the investment; null when not reached in the period |
| totalNominalInflow, totalDiscountedInflow | All money coming back, as received and discounted to today, in dollars |
| totalNetGain | Total inflow minus the investment, in dollars |
| npv | Net present value: the discounted inflow minus the investment, in dollars |
Where the method comes from
The payback period, the discounted payback period and net present value follow the definitions in Brealey, Myers and Allen's Principles of Corporate Finance: payback counts the years until the money coming back covers the up-front cost, and net present value discounts each later cash flow to today at the cost of capital and subtracts the cost. Here the yearly benefit is treated as arriving evenly through the year when finding the payback year, but it is discounted as one sum at each year end, the usual ordinary-annuity convention of textbook net present value. The annualized return is the compound yearly rate that turns the investment into the total inflow over the period, the growth-rate framing Damodaran uses in Investment Valuation. It ignores the timing of the cash and is not an internal rate of return. The model fits a project with a level yearly benefit; it does not handle uneven cash flows, taxes, inflation or financing.
Sources
- Brealey, Myers and Allen, Principles of Corporate Finance (payback, discounted payback and net present value)published 2020
- Damodaran, Investment Valuation, 3rd edition (compound annual return)published 2012
Used by
- runway-planningSkill
Tested
Tested 4 Dec 2026: claude-haiku-5-5 was right on 3 of 3 questions without this page, so it was not tested with it; claude-fable-5-2 was right on 0 of 3 questions without this page and 3 of 3 with it.
Changes
- Moved from aibizhub.io as Python. Answers unchanged. The year-by-year timeline, the plus-and-minus 20% scenario table (multipliers nobody published), the warnings and the input echo are no longer returned. analysis_years must now be a whole number instead of being rounded.4 Dec 2026
Cite as: mysi, ROI and payback, mysi.dev/roi-payback, version 1, changed 2026-12-04. Code MIT.