Pages

Tuesday, March 27, 2012

Present and future values of a sum with Python

Not only am I interested in science and computer stuff, but I'm also interested in the worlds of business and finance. That's why today I'll look at computing the present and future values of a lump sum using Python.

Present and future values revolve around the idea of time-value of money: the idea that I'd rather have money now than later. Because of this, when someone says they will give me $1 a year from now, I have to discount that dollar to the present, or calculate its present value, to see how much it's worth right now to me.

Likewise, when calculating the future value of a sum, I'm seeing how much it's worth in the future.

Future value is useful when dealing with compound interest. For example, if I put $1,000 in a savings account right now that's paying 0.8% interest, how much money will be in the account in ten years?

While we could use a financial calculator (like an HP 12C financial calculator [affiliate link]), or one of the plethora of online calculators to solve this, I think it'd be more fun to write a Python function to calculate it for us!

import decimal

def fv(pv, rate, n):
    '''Calculates future value of present value.

    Returns the future value of a sum given the
    present value, a rate, and the number of periods.'''

    result = Decimal(pv * (1 + rate) ** n)

    return result

Note that I used some abbreviations for these variables: fv is the future value, pv is the present value, rate is for the interest rate per period (as a decimal), and n is the number of periods.

The function is very simple, and all it's doing is implementing the equation for the future value of a lump sum: one plus the interest rate, all raised to the number of periods, times the present value.

If we try this out in Python, we get the following (assuming you had the above code in finance.py):

>>> import finance
>>> from decimal import *
>>> finance.fv(Decimal(1000), Decimal(0.008), Decimal(10))
Decimal('1082.942308472838656347119069')

$1,083 isn't a lot of money, so we'll probably want to consider putting our money elsewhere, but the function is the important thing.

Now let's write the function for calculating the present value given a future value:

import decimal

def pv(fv, rate, n):
    '''Calculates present value of future value.

    Returns the present value of a sum given the
    future value, a rate, and the number of periods.'''

    result = Decimal(fv * (1 + rate) ** -n)

    return result

You may notice that this function is basically the inverse of the above function. Instead of raising to the number of periods, we raise to the number of periods as a negative number (thus working backwards), and we multiply it all by the future value as opposed to the present value.

Does it work? Let's try the inverse of the problem we just did. If we have $1,082.94 ten years from now, after compounding at 0.8% annual interest, how much did we start with? Note that due to rounding there may be a slight error.

>>> import finance
>>> from decimal import *
>>> finance.pv(Decimal(1082.94), Decimal(0.008), Decimal(10))
Decimal('999.9978683325782541661697781')

As I rounded the future value to two decimal places when entering it into the present value function, that may have introduced a small amount of error.

These functions only allow us to calculate the value of a lump sum. Later we'll create functions that allow us to calculate the value of a sum plus some stream of payments.

No comments:

Post a Comment