# Interest rate

## Finance

Compound interest is the eighth wonder of the world

• He who understands it… earns it!
• He who doesn't … pays it!

Albert Einstein

### Terminology

A bit of terminology first:

$\color{blue} P - principal / price / present\ value \\ Px - principal + interest\ after\ 'x'\ years \\ \color{red} r - annual\ interest\ rate \\ \color{green} t - time\ in\ years$

### Simple interest rate

Let P be the principal that is borrowed, after first year the balance that needs to be paid back is the principal P plus the interest:

$\\ P1 = P + r*P$

after two years we have the principal plus the interest for 2 years: $\\ P2 = P + r*P + r*P \\$

then after 3 years … $\\ P3 = P + r*P + r*P + r*P \\$

we can see that r*P term repeats for each year, generalize for t years: $\\ Pt = P + t*r*P \\$

and finally extract P and remember the formula for later use: $\color{blue} S = P*(1 + r*t)$

Let's see a simple example, borrow $100 at 12% annual rate for 20 years:  p, r, t = 100, 0.12, 20 s = p * (1 + t*r) print('$%.2f' % s)
$1089.26  #### Daily compounding 365 days in a year and n=365  n = 365 c = p * (1 + r/n)**(t*n) print('$%.2f' % c)
$1101.88  Again,$1101 vs. $1089 vs$964, the more often the compounding, the bigger the final balance.

#### Continuously compounding

What about hourly compounding or even per minute or per millisecond? we can increase n and get the results but I want to find a formula that fits them all starting with: $\color{blue} C = P*(1 + \frac{r}{n})^{t*n}$

Since we compound more and more often then n gets bigger and bigger and approaches infinity and we can rewrite the formula as a limit: $C = \lim_{n \to +\infty} P*(1 + \frac{r}{n})^{t*n}$

Define x variable as: $x = \frac{n}{r} \\ n = x * r$

then x is direct proportional with n, as n approaches infinity, then x approaches infinity as well, limit formula holds and we can substitute n with x: $C = \lim_{x \to +\infty} P*(1 + \frac{r}{x*r})^{t*x*r}$ extract constant P outside the limit, cancel out r and commute the exponents: $C = P * (\lim_{x \to +\infty} (1 + \frac{1}{x})^x)^{r*t}$

What does the above formula looks like? Calculus anyone? Looks like the Euler's number $e = \lim_{x \to +\infty} (1 + \frac{1}{x})^x$

that gets substituted and we end up with the magic continuous compounding formula: $\color{red} C = P * e^{r*t}$

  from math import e
c = p * e**(r*t)
print('$%.2f' % c) $1102.32


Takeaway: Before moving further it is important to understand the difference between APY (annual percentage yield) and APR (annual percentage rate), the former include compounding interest, the latter does not. In other words APR is simple interest and APY is compounding interest.

### Discounted interest-rate

What about the other way around, we are given the final (compounding value) and need to calculate the present value?

Simple, re-arranging the compounding formula above we have:

$P = \frac{C}{e^{r*t}}$ $\color{red} P = C * e^{-r*t}$

where P is called the present value or discounted value and is very useful in financial valuation.