While working on some formulas, I was trying to solve for each variable. The first set of formulas deal with interest that gets calculated once per year and are fairly simple to come up with:
n = lambda iy, pv, fv: math.log(fv / pv) / math.log(iy + 1)
iy = lambda n, pv, fv: (fv / pv) ** (1 / n) - 1
pv = lambda n, iy, fv: fv / (iy + 1) ** n
fv = lambda n, iy, pv: pv * (iy + 1) ** n
The second set of formulas add a fifth variable called A
that denotes how many times per year the interest gets calculated. Twelve would be a reason number for interest calculated once per month:
n = lambda iy, pv, fv, a: math.log(fv / pv) / (math.log(iy / a + 1) * a)
iy = lambda n, pv, fv, a: ((fv / pv) ** (1 / (n * a)) - 1) * a
pv = lambda n, iy, fv, a: fv / (iy / a + 1) ** (n * a)
fv = lambda n, iy, pv, a: pv * (iy / a + 1) ** (n * a)
While trying to solve for A
, I came up with the following formula but do not know how to solve it:
A * log(IY / A + 1) = log(FV / PV) / N
My problem is that I am not sure how to get the values out of the log
on the left side. What is A
?
The process for getting the current formula was as follows:
FV = PV * (IY / A + 1) ** (N * A)
FV / PV = (IY / A + 1) ** (N * A)
N * A = log(FV / PV) / log(IY / A + 1)
A * log(IY / A + 1) = log(FV / PV) / N
Addendum 1:
After some more work with the formula, the following was developed, but the results do not appear to be helpful. While solving for the other A
, the first that was practically extracted got buried:
log(IY / A + 1) = log(FV / PV) / (N * A)
IY / A + 1 = e ** (log(FV / PV) / (N * A))
IY / A = e ** (log(FV / PV) / (N * A)) - 1
A = IY / (e ** (log(FV / PV) / (N * A)) - 1)
Addendum 2:
Reading through Claude Leibovici's answer led me further down the path of trying to solve for A
in the forumulas up above. From the first attempt to solve, the result became much simpler indeed:
log(IY / A + 1) = log(FV / PV) / (IY * N) * (IY / A)
b = -log(FV / PV) / (IY * N)
IY / A = W(b * e ** b) / b - 1
A = IY / (W(b * e ** b) / b - 1)
A = IY / (W(b * e ** b) / b - b / b)
A = IY / ((W(b * e ** b) - b) / b)
A = (IY * b) / (W(b * e ** b) - b)
From this formula, some code may be written in an attempt to duplicate the method needed to solve for A
. Unfortunately, the resulting answer is a large, negative number but should be twelve:
#! /usr/bin/env python3
import math
def main():
var_n = 35 # thirty-five year investment
var_iy = .04 # four percent interest per year
var_pv = 30000 # thirty thousand dollar investment
var_a = 12 # compounded twelve times per year
var_fv = round(fv(var_n, var_iy, var_pv, var_a), 2)
print('Future Value =', var_fv)
var_a = a(var_n, var_iy, var_pv, var_fv)
print('Compounded Per Year =', var_a)
fv = lambda n, iy, pv, a: pv * (iy / a + 1) ** (n * a)
def a(n, iy, pv, fv):
b = -math.log(fv / pv) / (iy * n)
return (iy * b) / (lambert_w(b * math.exp(b)) - b)
def lambert_w(z, tolerance=1e-8):
a = z
while True:
b = math.exp(a)
c = a * b
d = a - (c - z) / (c + b)
if abs(a - d) <= tolerance:
return a
a = d
if __name__ == '__main__':
main()
Question: Can someone identify my mistake and help to correct the formulas shown above?
A
can be solved using functions that would be included in most programming languages. $\endgroup$