Compound Interest
Install and import#
npm install fintech-algorithmsimport { compoundInterest } from "fintech-algorithms/foundations/financial-arithmetic-time-value-and-returns/compound-interest";Signature#
compoundInterest(input)Grows a principal at a periodic rate applied compoundsPerPeriod times within each period, so earned interest starts earning interest itself.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | D00Input | A plain object. Every F02 topic first reads principal, rate and periods and validates them before any per-topic branch runs. This topic then reads compoundsPerPeriod, the number of compounding steps inside one period, coerced with Number. |
Returns#
{ endingValue: number; interest: number }
endingValue is principal * (1 + rate / compoundsPerPeriod) raised to compoundsPerPeriod * periods. interest is that value less the original principal.
Errors#
- When input is not a plain object — throws TypeError
- When principal is negative, periods is negative, or rate is at or below -1 — throws RangeError
- When compoundsPerPeriod is zero or negative — throws RangeError
Complexity: time O(1),
space O(1).
Worked example#
verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.
Input#
{
"principal": 1000,
"rate": 0.05,
"periods": 3,
"compoundsPerPeriod": 12,
"futureValue": 1200,
"cashFlows": [-1000, 400, 400, 400],
"startValue": 100,
"endValue": 110,
"returns": [0.1, -0.05, 0.08],
"frequency": 12,
"periodicReturn": 0.01
}Call#
compoundInterest(input)Returns#
object with 2 fields: endingValue, interest
{
"endingValue": 1161.4722313334678,
"interest": 161.4722313334678
}Diagrams#
Calculation flow#
Compound Interest — four-part map
flowchart LR
A["Name the input"] --> B["Apply: A = P x (1 + r)^t; general frequency: A = P x (1 + r/n)^(n x t)"]
B --> C["Check units and boundary"]
C --> D["Explain the output"]
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- INVESTOR_COMPOUND - Compound Interest — U.S. Securities and Exchange Commission
- INVESTOR_CDS - Investor Bulletin: Brokered CDs — U.S. Securities and Exchange Commission
- CFA_TVM - Time Value of Money in Finance — CFA Institute
- Author-derived and synthetic boundary