Python For Finance: Control Flow, Data Structures and First Application (Part 2)
Python For Finance: Control Flow, Data Structures and First Application (Part 2)
Andras Niedermayer
Outline
1 Control Flow
2 Modules
4 Numpy functions
• usage:
In [1]: hello ( ’ Alice ’)
In [2]: hello ( ’ Bob ’)
• return values
def get_answers ( answer_number ):
if answer_number == 1:
return ’ You are the first . ’
elif answer_number == 2:
return ’ Twice as good . ’
else :
return ’ Something else than one ’\
’ or two ’
• usage:
In [1]: print ( get_answer (2))
spam ()
eggs = 31337
spam ()
eggs = ’ global ’
bacon ()
eggs = ’ global ’
spam ()
print ( eggs )
1 Control Flow
2 Modules
4 Numpy functions
def fib ( n ):
"""
prints the Fibonacci sequence from 1 to n
"""
a, b = 0, 1
while b < n :
print (b , end = " " )
a, b = b, a+b
print ()
def fib2 ( n ):
"""
returns the Fibonacci sequence until n
"""
res = []
a, b = 0, 1
while b < n :
res . append ( b )
a, b = b, a+b
return res
January 2019 Python for Finance - Lecture 3
Andras Niedermayer - Université Paris-Dauphine 13/34
Our Own Module
# imports
# if success ...
# or if failure ...
January 2019 Python for Finance - Lecture 3
Andras Niedermayer - Université Paris-Dauphine 17/34
Excercise 14
• Guessing game guess.py
"""
Picks a random number and lets
the player guess
"""
import random
1 Control Flow
2 Modules
4 Numpy functions
tuple
Simple collection of arbitrary objects. Limited methods.
list
A collection of arbitrary objects; many methods available.
dict
Dictionaries with key-value stores. Unordered and un-sortable. Maps
(generally) strings into strings or numbers.
Multiple methods:
1 d.keys()=[’Last’, ’First’, ’Country’]
2 d.values()=[’Doe’, ’John’, ’England’]
3 Mapping in a dictionary: d[’Last’]=’Doe’.
4 Setting an item: d[’Country’]=US
set
Mathematical sets: unordered collections of objects, repeated only
once.
s1 = set ([ ’a ’ , ’b ’ , ’c ’ , ’d ’ ])
s2 = set ([ ’e ’ , ’b ’ , ’c ’ , ’f ’ ])
Multiple methods:
1 s1.union(s2)={’a’,’b’,’c’,’d’,’e’,’f’}
2 s1.intersection(s2)={’b’,’c’}
3 s1.difference(s2)={’a’,’d’}
4 s1.symmetric difference(s2)={’a’,’d’,’e’,’f’}
m1 = np . array ([ v1 , v1 *2])
m1 = array ([[0.5 , 0.75 , 1. , 1.5 , 2.] ,
[1. , 1.5 , 2. , 3. , 4.]])
1 Flattening:
m1.ravel()=array([0.5, 0.75, 1., 1.5, 2., 1., 1.5,
2., 3., 4.])
2 Matrix size: m1.shape=(2, 5)
3 Reshape:
m1.reshape(5,-1)=array([[0.5, 0.75], [1., 1.5],
[2., 1.], [1.5, 2.], [3., 4.]])
4 Vertical and horizontal stacking: vstack and hstack.
Advantages of vectorization:
1 compact code, easy to read and understand.
2 faster execution.
1 Control Flow
2 Modules
4 Numpy functions
Documentation:
www.docs.scipy.org/doc/numpy/reference/routines.html
Name Description
np.dot(a, b) Dot product of a and b
np.linalg .det(a) Determinant of array a
np.linalg .solve(a, b) Solve linear system ax = b
np.linalg .eig (a) Eigenvalues of matrix a
np.sin(x), np.cos(x).. Trigonometric functions
np.exp(x), np.log (x),
np.power (x1, x2), np.sqrt(x) Arithmetic, exponents, logarithms
np.median(a), np.mean(a)
np.std(a), np.corrcoef (a, b) Summary stats of an array