Python for Finance
Introduction and Basics of Python
Andras Niedermayer
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 2/45
About Myself
• Andras Niedermayer
• Professor of Economics at Université Paris-Dauphine
• Co-founder and CTO of a fintech company (SolvencyAnalytics
AG) that uses Python for data analysis
• mail: [email protected]
• office: P135
• slides will be availabe at http://andras.niedermayer.ch
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 3/45
Literature
Textbook
Hilpisch, Yves, Python for Finance: Analyze Big Financial Data,
2015, O’Reilly Publishing
Textbook
Yan, Yuxing, Python for Finance: Build real-life Python application
for quantitative finance and financial engineering, 2014, Pack
Publishing
Other sources
• Python 3 documentation at: https://docs.python.org/3/.
• Stanford lectures at
http://web.stanford.edu/~arbenson/cme193.html
• UPenn lectures at
http://www.cis.upenn.edu/~cis192/spring2014/
• Forums such as http://stackoverflow.com/
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 4/45
Structure of Course
• Introduction to the Python Programming Language
1 Basics of Python
2 Data structures and first application
3 Random numbers and Monte Carlo
4 Strings, files and internet access
5 Playing with data structures
6 Automating tasks
• Financial Applications with Python
1 Learning to use the Spyder Integrated Development Environment
and learning basic features of the Python programming language
2 Data analysis with the pandas library
3 Regressions, Interpolations, and Optimization
4 Simulations and Option Pricing
5 Portfolio management and principal component anlasysis
6 GARCH models, measures of market risk (Value at Risk and
Expected Shortfall)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 5/45
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 6/45
Why Python?
1 Very Popular, Most Popular Language for (Big) Data Analysis
both in Academia and Industry: see e.g.
http://pypl.github.io/PYPL.html
2 Interactive data analytics: immediate feedback and intuitive
data processing in IPython
3 Powerful Libraries: see e.g.
https://plot.ly/ipython-notebooks/
markowitz-portfolio-optimization/,
https://xkcd.com/353/
4 Open source: free, powerful, and a very dynamic community to
develop new packages.
5 Cross-platform: for Windows, OS X, and Linux.
6 Intuitive language: Closest to mathematical language and
pseudocode.
7 Dynamically typed: One does not need to declare variable types
statically (some do not like this!)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 7/45
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 8/45
Anaconda
1 One of the two major distributions of Python, along with
Canopy (Enthought).
2 Download (free! and cross-platform) from
https://store.continuum.io/cshop/anaconda/
3 Includes all the important packages for this course (any many
others).
4 There are two versions of Python: 2.7.x and 3.x.
Syntax is slightly different, functionality almost identical. In this
course we focus on 3.x.
5 Recommended coding approach: Jupyter Notebooks
6 Two alternatives: IPython shell and Spyder
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 9/45
Jupyter Notebook (cross-platform, browser based)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 10/45
IPython (cross-platform)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 11/45
Spyder (cross-platform)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 12/45
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 13/45
Launching Python
• Open a terminal (<Windows>+”cmd”)
• type “ipython” to launch the interactive Python interpreter
• alternative: type “jupyter notebook”, in the browser window
that opens, select “New” → “Notebook: Python 3”
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 14/45
Arithmetic operators
In the interpreter, write:
In [1]: 1 + 1
Out [1]: 2
In [2]: a = 1
In [3]: print ( a )
1
In [4]: a + 2
Out [4]: 3
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 15/45
Quitters
In [1]: quit ()
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 16/45
First Usage of Python
Assume we have a return of 100 e in a few years and an interest rate
of 10%. The present value of this payment is then
FV
PV =
(1 + R)n
where PV is the present value, FV the future value, R is the interest
rate and n the number of years
What is the value of a payment in one year?
In [1]: 100 / (1+0.1)
Out [1]: 9 0. 90 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 17/45
First Usage of Python
What about a payment in two years?
In [1]: 100 / (1+0.1)^2
------------------------------------------
---------------------------------
TypeError
Traceback ( most recent call last )
< ipython - input -1 -6 e22829b2b81 > in < module >()
----> 1 100/(1+0.1)^2
TypeError : unsupported operand type ( s ) for ^:
’ float ’ and ’ int ’
In Python you have to write “**” instead of “ˆ” for power
In [1]: 100 / (1+0.1)**2
Out [1]: 82.6446280991 7354
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 18/45
Pay attention to upper and lower case
In [1]: x =2
In [2]: X
------------------------------------------
---------------------------------
NameError
Traceback ( most recent call last )
< ipython - input -4 -253 bcac7dd80 > in < module >()
----> 1 X
NameError : name ’X ’ is not defined
Python is case sensitive.
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 19/45
Dynamically Typed Variables
In Python, variables are dynamically typed, so that they can change
their type at runtime
In [1]: x =2
In [2]: x
In [3]: x = ’ aa ’
In [4]: x
Python is case sensitive.
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 20/45
Help
Help!
In [1]: help ()
• Google + Stackoverflow
• http://python.org
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 21/45
Excercises
• Calculate the area of a circle with a radius of 10 cm
• Calculate the diagonal of a square with side length 1
• What is the circumference of a trapezoid with side lengths of
127 m each?
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 22/45
Solutions
• Excercise 1
In [1]: area = 3.14159 * 10**2
In [2]: area
Out [2]: 314.159
• Excercise 2
In [1]: diag = 2**0.5
In [2]: diag
Out [2]: 1.4142 135 62 37 30 95 1
• Excercise 3
In [1]: 127 + 127 + 127 + 127
Out [1]: 508
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 23/45
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 24/45
Variables
Let’s assign values to variables
In [1]: a = 1
In [2]: aa = ’ hello ’
In [3]: b = 2
In [4]: a + b
Out [4]: 3
Print the values of variables
In [1]: a
Out [1]: 1
In [2]: aa
Out [2]: ’ hello ’
In [3]: print ( a )
1
In [4]: print ( aa )
’ hello ’
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 25/45
Variables
Variables change values!
In [1]: x = 5
In [2]: x
Out [2]: 5
In [3]: x = x + 1
In [4]: x
Out [4]: 6
Variables in programming languages are different than variables in
mathematics
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 26/45
Variables
Some errors:
In [1]: aaa
------------------------------------------
NameError
Traceback ( most recent call last )
< ipython - input -20 -5 c9597f3c824 > in < module >()
----> 1 aaa
NameError : name ’ aaa ’ is not defined
In [1]: sqrt (2)
------------------------------------------
NameError
Traceback ( most recent call last )
< ipython - input -21 -40 e415486bd6 > in < module >()
----> 1 sqrt (2)
NameError : name ’ sqrt ’ is not defined
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 27/45
Variables
Bad naming of variables:
In [1]: x = 100
In [2]: y = 0.1
In [3]: z = x / (1 + y )
In [4]: print ( " The result is " , z )
The result is 90.90909090
Good naming of variables
In [1]: FV = 100
In [2]: R = 0.1
In [3]: PV = FV / (1 + R )
In [4]: print ( " The result is " , PV )
The result is 90.90909090
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 28/45
Variables
Introspection:
In [1]: dir ()
Out [1]: [ ’ __builtin__ ’ , ’ __doc__ ’ ,
’ __name__ ’ , ’ __package__ ’]
dir() allows you to access all define variable/names
In [1]: a = 1
In [2]: dir ()
Out [2]: [ ’ __builtin__ ’ , ’ __doc__ ’ ,
’ __name__ ’ , ’ __package__ ’ , ’a ’]
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 29/45
Variables
Deleting a variable:
In [1]: a = 1
In [2]: dir ()
Out [2]: [ ’ __builtin__ ’ , ’ __doc__ ’ ,
’ __name__ ’ , ’ __package__ ’ , ’a ’]
In [3]: del a
In [4]: dir ()
Out [4]: [ ’ __builtin__ ’ , ’ __doc__ ’ ,
’ __name__ ’ , ’ __package__ ’]
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 30/45
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 31/45
Basic Operations
Basic Operations:
In [1]: 1+1
In [2]: 1 -2
In [3]: 2*2
In [4]: 3/2
In [5]: 3/2.
In [6]: 3//2.
In [7]: int (2.5)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 32/45
The Mathematics module
For mathematics, use the module:
In [1]: dir ()
In [2]: import math
In [3]: dir ()
In [4]: dir ( math )
In [5]: math . sqrt (2)
In [6]: math . pow (2 ,2)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 33/45
Help Again
For help for a specific function or module:
In [1]: help ( math . pow )
In [2]: help ( math )
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 34/45
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 35/45
Antigravity
In [1]: import antigravity
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 36/45
Outline
1 Introduction
2 Why Python?
3 Python installation and environments
4 First Steps in Python
5 Variables
6 Basic Operations and Modules
7 Antigravity
8 Useful Functions and Variables
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 37/45
Useful functions
Choose the precision
In [1]: 3/7.0
In [2]: payment = 3/7.
In [3]: payment2 = round ( payment , 4)
In [4]: payment2
But attention
In [1]: payment * pow (10 ,6)
Out [1]: 428571.428571 4285
In [2]: payment2 * pow (10 ,6)
Out [2]: 428600.0
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 38/45
More maths
Mathematical constants and special functions
In [1]: math . e
In [2]: math . pi
In [3]: math . sin ( math . pi )
In [4]: math . cos ( math . pi )
In [4]: math . log ( math . e )
In [5]: math . exp (1)
But attention
In [1]: payment * pow (10 ,6)
Out [1]: 428571.428571 4285
In [2]: payment2 * pow (10 ,6)
Out [2]: 428600.0
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 39/45
Useful functions
How to print
In [1]: import math
In [2]: print ( ’ Pi = ’ , math . pi )
In [3]: print ( math . pi )
In [4]: print ( ’ Pi is {} or e is {} ’. format (
math . pi , math . e ))
Finding out the type of a variable
In [1]: x = 1
In [2]: type ( x )
In [3]: x = float (1)
In [4]: type ( x )
In [5]: if isinstance (x , ( int , float )):
... : print ( ’x is a number ’)
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 40/45
Useful functions
Combining strings (that is text)
In [1]: a = ’ Hello ’
In [2]: b = ’ world ’
In [3]: a+b
In [4]: a + ’ ’+ b
In [5]: print ( ’ {} {}! ’. format (a , b ))
Stripping and changing case
In [1]: x = ’ heLlo ’
In [2]: x . upper ()
In [3]: x . lower ()
In [4]: x . capitalize ()
In [5]: x . strip ()
In [6]: x . strip (). capitalize ()
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 41/45
Tuples
Combining strings (that is text)
In [1]: t = ( ’ JC ’ , 33)
In [2]: print ( t )
In [3]: len ( t )
In [4]: t [0]
In [5]: type ( t [1])
In [6]: type ( t )
In [7]: print ( ’ My name is {} and my age is {}. ’
. format ( t [0] , t [1]))
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 42/45
Excercises
4 How do you find all the built-in functions?
5 Convert the text ‘This excercise is simple’ to capital letters.
6 We have 41 persons in this class. If we want to make groups of
3 for projects, how many groups will there be and how many
people will there be in a non-complete group?
7 explain the following results:
In [1]: x = 5.566
In [2]: round (x ,2)
Out [2]: 5.57
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 43/45
Solutions
• Excercise 4
In [1]: dir ( __builtins__ )
• Exercise 5
In [1]: s = ’ This is a simple exercise ’. upper ()
In [2]: print ( s )
• Excercise 6
In [1]: print ( ’ With {} students there will \
... : be {} groups with {} students and \
... : there will remain {}. ’. format (
... : 41 , 41//3 , 3 , 41%3))
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 44/45
Solutions
• Exercise 7: The way round works in mathematics is that digits
below 5 are rounded down, digits greater or equal 5 are rounded
up, so round(3.4)=3, round(3.5)=4 and round(3.14,1)=3.1,
round(3.15,1)=3.2
Wednesday, January 9, 2019 Python for Finance - Lecture 1
Andras Niedermayer - Université Paris-Dauphine 45/45