Skip to content
Course Notes: Introduction to Financial Concepts in Python
  • AI Chat
  • Code
  • Report
  • Spinner

    Course Notes

    Use this workspace to take notes, store code snippets, and build your own interactive cheatsheet!

    Note that the data from the course is not yet added to this workspace. You will need to navigate to the course overview page, download any data you wish to use, and add it to the file browser.

    !pip install numpy-financial;
    # Import any packages you want to use here
    import numpy as np
    import numpy_financial as npf
    import plotly.graph_objects as go

    Present and Future Value

    The value of money itself changes over time. Computing the future value of an investment allows one to compare different scenarios since the outcome is denominated in similar units (future dollars).

    Numpy-financial has a function np.pv() that allows one to compute the present value of an investment given inflation rate and number of years. The function returns a negative number, which simply means that one is spending cash in the present.

    To compute the present value of $100 received 3 years from now at 1.0% inflation rate, call numpy-financials's .pv() function, passing in the discount rate, number of periods, and the future value.

    # Add your code snippets here
    present_amnt = npf.pv(rate=0.01, nper=3, pmt=0, fv=100)
    round(present_amnt,2)

    If, one the other hand we want to calculate the future value of an investment, we can use the .fv() function, passing in the annual interest rate, number of periods, and the initial value as a negative number, indicating that we're spending that amount

    future_amt = npf.fv(rate=0.05, nper=3, pmt=0, pv=-100)
    round(future_amt,2)

    We can now put the two together to:

    • first, forecast the future value of an investment given a rate of return
    • second, dicsount the future value of the investment by a projected inflation rate
    investment = npf.fv(rate=0.08, nper=10, pmt=0, pv=-10000)
    dicsounted_investment = npf.pv(rate=0.03, nper=10, pmt=0, fv=investment)
    
    print(f"An investment of $10,000 will yield ${investment:.2f} in 10 years at 8% interest. After adjusting for 3% inflation, it'll be worth ${-dicsounted_investment:.2f} in today's dollars")

    Net Present Value

    Most financial decisions rely on more than just a single number. Instead, they rely on comparing cash flows.

    Cash flows are a series of gains or losses from an investment over time.

    Consider the following two projects:

    • Project 1 requires an initial investment of $100, generating a series of cash flows for the next four years.
    • Project 2, on the other hand generates $100 immediately, and in year 1 as well, then requiring a net $100 investment in year two, finally generating $200 and $300 in years 3 and 4 respectively.

    Assume a 3% discount rate.

    YearProject 1 Cashflow ($)Project 2 Cashflow ($)
    0(100)100
    1100100
    2125(100)
    3150200
    4175300

    It might be tempting to simply sum the cashflows, but doing so would be ill-advised since cashflow is generated at a different time. We need to convert each cash flow to either present or future value before doing any comparisons.

    Net Present Value (NPV) is the sum of all discounted cashflows and outflows of a given project, and is used to measure the profitability of a potential investment. Projects with larget cashflows and lower discount rates will have higher NPV. While convenient, NPV does not allow for the comparison of projects with different sizes or lengths, and also requires the assumption of a discount rate.

    The numpy-financial library has a function .npv() that calculates the net present value of a series of cashflows:

    project_1 = np.array([-100, 100, 125, 150, 175])
    project_2 = np.array([100, 100, -100, 200, 300])
    
    cash_flow_p1 = npf.npv(rate=0.03, values=project_1)
    cash_flow_p2 = npf.npv(rate=0.03, values=project_2)
    
    print(f"Project 1 Net Present Value: ${cash_flow_p1:.2f}")
    print(f"Project 2 Net Present Value: ${cash_flow_p2:.2f}")

    Consider two more projects. Calculate the net present values given the following cash flows:

    YearProject 1 Cashflow ($)Project 2 Cashflow ($)
    1(250)(250)
    2100200
    3200(250)
    4300300
    5400300

    In this example, project 1 only requires and initial investment of $250, generating a slowly increasing series of cash flows over the next 4 years.

    Project 2 requires an initial investment of $250, and an additional $250 in year 3. However, project 2 continues to generate larger cashflows.

    Assuming both projects don't generate any more cash flows after the 5th year, which project is worth investing in? The best way to decide is by comparing the NPV of both projects.

    project_1 = np.array([-250, 100, 200, 300, 400])
    project_2 = np.array([-250, 300, -250, 300, 300])
    
    cash_flow_p1 = npf.npv(rate=0.03, values=project_1)
    cash_flow_p2 = npf.npv(rate=0.03, values=project_2)
    
    print(f"Project 1 Net Present Value: ${cash_flow_p1:.2f}")
    print(f"Project 2 Net Present Value: ${cash_flow_p2:.2f}")

    Diminishing Cashflows

    In the same manner compound interest can rapidly grow an investment over time, compounded discount factors can quickly dimish it towards zero.

    For example, $100 at a 3% annual discount for 1 year will be worth roughly $97.08:

    This number shrinks quite rapidly as the discounting periods increase:


    Let's calculate the present value of a single $100 payment received 30, 50 and 100 years from now with an annual inflation rate of 3%:

    final_value = 100
    
    for yrs in [30, 50, 100]:
        val = npf.pv(rate=0.03, nper=yrs, pmt=0, fv=final_value)
        print(f"${final_value} investment is worth ${-val:.2f} in today's dollars after {yrs} years")