Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Python for Finance. - Second Edition

You're reading from  Python for Finance. - Second Edition

Product type Book
Published in Jun 2017
Publisher
ISBN-13 9781787125698
Pages 586 pages
Edition 2nd Edition
Languages

Table of Contents (23) Chapters

Python for Finance Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Python Basics Introduction to Python Modules Time Value of Money Sources of Data Bond and Stock Valuation Capital Asset Pricing Model Multifactor Models and Performance Measures Time-Series Analysis Portfolio Theory Options and Futures Value at Risk Monte Carlo Simulation Credit Risk Analysis Exotic Options Volatility, Implied Volatility, ARCH, and GARCH Index

Chapter 3. Time Value of Money

In terms of finance per se, this chapter does not depend on the first two chapters. Since, in this book, Python is used as a computational tool to solve various finance problems, the minimum requirement is that readers should have installed Python plus NumPy and SciPy. In a sense, if a reader has installed Python via Anaconda, he/she will be fine without reading the first two chapters. Alternatively, readers could read Appendix A on how to install Python.

In this chapter, various concepts and formulae associated with finance will be introduced and discussed in detail. Since those concepts and formulae are so basic, readers who have taken one finance course, or professionals with a few years' working experience in the financial industry, could go through this chapter quickly. Again, one feature of this book, quite different from a typical finance textbook, is that Python is used as the computational tool. In particular, the following topics will be covered:

  • Present...

Introduction to time value of money


Let's use a very simple example to illustrate. Assume that $100 is deposited in a bank today with an annual interest rate of 10%. What is the value of the deposit one year later? Here is the timeline with the dates and cash flows:

Obviously, our annual interest payment will be $10, that is, 100*0.1=10. Thus, the total value will be 110, that is, 100 + 10. The original $100 is principal. Alternatively, we have the following result:

Assume that $100 will be kept in the bank for two years with the same 10% annual interest rate for two years. What will be the future value at the end of year two?

Since at the end of the first year, we have $110 and by applying the same logic, the future value at the end of year two should be:

Since 110 = 100*(1+0.1), then we have the following expression:

If $100 is deposited for five years with an annual interest rate of 10%, what is the future value at the end of year five? Based on the preceding logic, we could have the following...

Writing a financial calculator in Python


When discussing the various concepts of the time value of money, learners need a financial calculator or Excel to solve various related problems.

From the preceding illustrations, it is clear that several functions, such as scipy.pv(), could be used to estimate the present value of one future cash flow or present value of annuity. Actually, the functions related to finance contained in the SciPy module came from the numpy.lib.financial submodule:

>>> import numpy.lib.financial as fin
>>> dir(fin)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_convert_when', '_g_div_gp', '_rbl', '_when_to_num', 'absolute_import', 'division', 'fv', 'ipmt', 'irr', 'mirr', 'np', 'nper', 'npv', 'pmt', 'ppmt', 'print_function', 'pv', 'rate']
>>>
Below are a few examples, below. 
>>>import numpy.lib.financial as fin
>>> fin.pv(0.1,3,0,100)      # pv of one...

Definition of NPV and NPV rule


The Net Present Value (NPV) is defined by the following formula:

Here is an example. The initial investment is $100. The cash inflows in the next five years are $50, $60, $70, $100, and $20, starting from year one. If the discount rate is 11.2%, what is the project's NPV value? Since only six cash flows are involved, we could do the calculation manually:

>>> r=0.112
>>> -100+50/(1+r)+60/(1+r)**2+70/(1+r)**3+100/(1+r)**4+20/(1+r)**5
121.55722687966407
Using the scipy.npv() function, the estimation process could be simplified dramatically:
>>> import scipy as sp
>>> cashflows=[-100,50,60,70,100,20]
>>> sp.npv(0.112,cashflows)
121.55722687966407

Based on the preceding result, the NPV of this project is $121.56. A normal project is defined as follows: cash outflows first, then cash inflows. Anything else is an abnormal project. For a normal project, its NPV is negatively correlated with the discount rate; see the following...

Definition of IRR and IRR rule


The Internal Rate of Return (IRR) is defined as the discount rate that makes NPV equal zero. Assume that we invest $100 today and the future cash flows will be $30, $40, $40, and $50 for the next four years. Assuming that all cash flows happen at the end of the year, what is the IRR for this investment? In the following program, the scipy.irr() function is applied:

>>>import scipy as sp
>>> cashflows=[-100,30,40,40,50]
>>> sp.irr(cashflows)
       0.2001879105140867

We could verify whether such a rate does make NPV equal zero. Since the NPV is zero, 20.02% is indeed an IRR:

>>> r=sp.irr(cashflows)
>>> sp.npv(r,cashflows)
    1.7763568394002505e-14
>>>

For a normal project, the IRR rule is given here:

Here, Rc is the cost of capital. This IRR rule holds only for a normal project. Let's look at the following investment opportunity. The initial investment is $100 today and $50 next year. The cash inflows for the...

Definition of payback period and payback period rule


A payback period is defined as the number of years needed to recover the initial investment. Assume that the initial investment is $100. If every year the firm could recover $30, then the payback period is 3.3 years:

>>import fincal
>>>cashflows=[-100,30,30,30,30,30]
>>> fincal.payback_period(cashflows)
    3.3333333333333335

The decision rule for the payback rule is given here:

Here, T is the payback period for a project while Tc is the maximum number of years required to recover the initial investment. Thus, if Tc is four, the preceding project with a payback period of 3.3 should be accepted.

The major advantage of the payback period rule is its simplicity. However, there are many shortcomings for such a rule. First, it does not consider the time value of money. In the previous case, $30 received at the end of the first year is the same as $30 received today. Second, any cash flows after the payback period is ignored...

Writing your own financial calculator in Python


It could be viewed as a great achievement when a new Python learner could write his/her own financial calculator. The basic knowledge to do so includes the following:

  • Knowledge on how to write a function

  • What are the related finance formulae?

For the latter, we have learnt from the preceding sections, such as the formula to calculate the present value of one future cash flow. Let's write the simplest Python function to double an input value:

def dd(x):
    return 2*x

Here, def is the keyword for writing a function, dd is the function name, and x in the parentheses is an input variable. For Python, the indentation is critical. The preceding indentation indicates that the second line is the part of the dd function. Calling this function is the same as calling other built-in Python functions:

>>>dd(5)
 10
>>>dd(3.42)
 6.84

Now, let's write our simplest financial calculator. First, launch Python and use its editor to enter the following...

Two general formulae for many functions


This section is optional since it is quite complex in terms of mathematical expression. Skipping this section would not have any impact on the understanding of the other chapters. Thus, this section is for advanced learners. Up to now in this chapter, we have learnt the usage of several functions, such as pv(), fv(), nper(), pmt(), and rate() included in the SciPy module or numpy.lib.financial submodule. The first general formula is related to the present value:

On the right-hand side of the preceding equation, the first one is the present value of one future cash flow, while the second part is the present value of annuity. The variable type takes a value of zero (default value); it is the present value of a normal annuity, while it is an annuity due if type takes a value of 1. The negative sign is for the sign convention. If using the same notation as that used for the functions contained in SciPy and numpy.lib.financial, we have the following formula...

Exercises


  1. What is the present value of $206 received in 10 years with an annual discount rate of 2.5%?

  2. What is the future value of perpetuity with a periodic annual payment of $1 and a 2.4% annual discount rate?

  3. For a normal project, its NPV is negatively correlated with the discount rate. Why?

  4. John deposits $5,000 in the bank for 25 years. If the annual rate is 0.25% per year, what is the future value?

  5. If the annual payment is $55 with 20 years remaining, what is the present value if the annual discount rate is 5.41%, compounded semi-annually?

  6. If Mary plans to have $2,400 by the end of year 5, how much does she have to save each year if the corresponding annual rate is 3.12%?

  7. Why have we got a negative number of periods in the following code?

    >>>import scipy as sp
    >>> sp.nper(0.012,200,5000,0)
    -21.99461003591637
  8. If a firm's earnings per share grows from $2 to $4 over a 9-year period (the total growth is 100%), what is its annual growth rate?

  9. In this chapter, while writing a present...

Summary


In this chapter, many basic concepts related to finance were introduced, such as present value of one future cash flow, present value of perpetuity, present value of annuity, future value of one cash flow/annuity, and the concept of present of annuity due. The several decision rules were discussed in detail, such as the NPV rule, IRR rule, and payback period rule. For the next chapter, we will discuss how to retrieve data associated with economics, finance, and accounting from several open sources such as Yahoo!Finance, Google finance, Prof. French's data library, and Federal Research's economic data library.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Python for Finance. - Second Edition
Published in: Jun 2017 Publisher: ISBN-13: 9781787125698
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}