5.16.1 Problem
You want to know how much
something will be worth in the future,
given its value today and an expected interest rate,
i (for example, calculating the amount of money
you can accumulate in a savings account given an initial deposit).
5.16.2 Solution
You must calculate the appreciation of an asset over time at the
assumed interest rate. This is often referred to as the
future value of an asset. For a given interest
rate, i, an asset appreciates by a factor of (1
+ i) for each period (such as a year).
5.16.3 Discussion
For the purposes of illustration, let's calculate
the future value of an asset using the bruteforce method.
If you deposit $1 in a bank account that earns 5% interest per year,
one year from now, you will have earned 5 cents in interest. The
total account, including principal and interest, will be worth $1.05.
The math for this is:
FV = PV * (1 + i);
where FV is the future value (the amount of
money you'll have next year, sometimes called
FV1), PV is the
present value (the amount of money you deposited
initially, sometimes called FV0), and
i is the interest rate (expressed as a decimal,
such as 0.05).
So if you deposit $100 at a 5% interest rate, the future value at the
end of one year is $105, which is determined as follows:
FV1 = 100 * (1 + .05);
Typically, interest is compounded over time, so at the end of the
second year, the value is:
FV2 = FV1 * (1 + i);
Here is a function that calculates the future value by brute force
for an arbitrary number of periods. We name the parameter that
represents the interest rate interest (instead of
i) so as not to mistake it for an index variable
(and we'll use periods as our
loop variable to avoid the reverse confusion):
Math.bruteFutureValue = function (interest, n, PV) {
// PV = initial deposit (present value)
// interest = periodic interest rate
// n = number of periods
// Start with the future value equal to the present value.
FV = PV;
// Now compound it over n periods at an interest rate of interest.
for (var periods = 1; periods <= n; periods++) {
FV = FV * (1 + interest);
}
return FV;
};
// Example usage:
// How much will $500 appreciate in 10 years at a 5% rate?
trace ("It will be worth: " + Math.bruteFutureValue(.05, 10, 500));
You can use the custom CurrencyFormat( ) method
from Recipe 5.6
to format the result for display.
It doesn't matter whether the interest is compounded
daily, weekly, monthly, or annually, provided that the specified
interest rate is correct for the number of periods. For example, if
compounding monthly, be sure to multiply the number of years by 12
and divide the annual interest rate by 12.
// How much will $500 appreciate in 10 years at a 5% rate with monthly compounding?
trace ("It will be worth: " + Math.bruteFutureValue(.05/12, 10*12, 500));
So the bruteforce method appears to work, but it has two drawbacks.
One drawback is that it can be slow if you
are doing a lot of calculations. The
other problem is that it can't be used to calculate
socalled continuous compounding.
Let's revisit this formula for the value of the
account at the end of the second year:
FV2 = FV1 * (1 + i);
Substituting PV * (1 + i) for
FV1, we get:
FV2 = (PV * (1 + i)) * (1 + i);
This can also be written as:
FV2 = PV * ((1 + i) * (1 + i));
The last portion is (1 + i) squared, so the
formula can be written as:
FV2 = PV * Math.pow((1 + i), 2);
where 2 is the number of periods.
Extrapolating, you can guess that the future value after
n periods would be:
FVN = PV * Math.pow((1 + i), n);
So we can speed up our calculation by using the following
implementation:
Math.futureValue = function (interest, n, PV) {
// PV = initial deposit (present value)
// interest = periodic interest rate
// n = number of payment periods
multiplier = Math.pow ((1 + interest), n);
return (PV * multiplier);
};
You'll see that you get the same result as with our
earlier bruteforce approach:
// How much will $500 appreciate in 10 years at a 5% rate with monthly compounding?
trace ("It will be worth: " + Math.futureValue(.05/12, 10*12, 500));
but our new code will be much faster for a large number of periods
(when n is large).
However, to perform continuous compounding, the number of periods,
n, becomes infinite. Although the derivation for
the formula is beyond the scope of this book, continuous compounding
can be implemented as follows:
Math.continuousCompounding = function (interest, n, PV) {
// PV = initial deposit (present value)
// interest = periodic interest rate
// n = number of payment periods
multiplier = Math.pow (Math.E, n*interest);
return (PV * multiplier);
};
// How much will $500 appreciate in 10 years at a 5% rate with continuous
// compounding?
trace ("It will be worth: " + Math.continuousCompounding (.05, 10, 500));
The above calculations assume an interest rate,
i, which may not be known or even predictable in
many scenarios. For example, you can't predict the
exact return of the stock market over any given period (although you
can make educated guesses based on historic data if the time period
is long enough). So, typically, you should allow the user to specify
an interest rate and perform the calculations under various
scenarios.

Remember to represent the interest rate as a decimal, such as 0.01.
If you specify an interest rate of 1.0, it is equivalent to 100%
interest! And be sure to specify the correct interest rate for the
chosen number of periods (such as the monthly interest rate rather
than the annual interest rate).


If you are calculating the future value of a fixed investment (such
as a bond or certificate of deposit), you would know the fixed rate
it returns. However, you might not be able to predict the rate of
inflation. Everyone knows that inflation reduces the effective value
of money in the future. To calculate the socalled real
rate of interest, subtract the rate of inflation from the
stated interest rate. For example, if your bond earns 5%, but you
expect inflation to be 4%, you can use the above examples with an
interest rate of 1% to calculate how much your money will be worth in
the future relative to today's dollars.
5.16.4 See Also
Recipe 5.6, Recipe 5.17, Recipe 5.18,
and Recipe 5.19. For the exact derivation of
continuous compounding and other financial formulas, see any
competent collegelevel financial accounting or applied mathematics
book. You can find much of the interesting
math online by Googling for "continuous compounding
derivation." For example, see http://ljsavage.wharton.upenn.edu/~waterman/Teaching/IntroMath99/Class04/Notes/node13.htm.