Professional Documents
Culture Documents
QF205 Week 04
QF205 Week 04
Slide
001
001/149
QF205 - Computing Technology For Finance
Week 04
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
Preparations
Dr. Zhao Yibao Get your laptop ready.
Senior Lecturer
Of Quantitative
Launch Jupyter notebook and open “QF205 Notebook WK04.ipynb”.
Finance
SMU Classification: Restricted
Slide 2
002
001/149
Quick Review of Week 03
QF205
COMPUTING
TECHNOLOGY
String Operations
FOR FINANCE
repeated with *.
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
first index 0. no
character having There is separate
QF205
COMPUTING
TECHNOLOGY
character type; a character is
FOR FINANCE
simply a string of size one.
(An example is on next slide.)
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
0 1 2 3 4 5
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
-6 -5 -4 -3 -2 -1
QF205
COMPUTING
0 1 2 3 4 5 TECHNOLOGY
FOR FINANCE
-6 -5 -4 -3 -2 -1
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
a:b
means from position a QF205
COMPUTING
(included) to position b TECHNOLOGY
FOR FINANCE
(excluded), from left to right.
0 1 2
QF205
COMPUTING
(indices) TECHNOLOGY
FOR FINANCE
(slices)
(extended slices)
Dr. Zhao Yibao
Senior Lecturer
https://docs.python.org/3/library/stdtypes.html#common-sequence-operations Of Quantitative
Finance
SMU Classification: Restricted
Slide
018
001/149
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
word[0:2]
QF205
COMPUTING
TECHNOLOGY
from 0 to 2
FOR FINANCE
=word[0]+word[1]
word[2:5]
from 2 to 5
Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
=word[2]+word[3]+word[4]
Finance
SMU Classification: Restricted
Slide
019
001/149
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
? word[-5:5]
? from -5 to 5
i.e. word[-5]+word[-4]+word[-3]+word[-2]+word[-1]+word[0]+word[1]
+word[2]+word[3]+word[4]
0 1 2 3 4 5
a 6-letter word -6 -5 -4 -3 -2 -1
word[-5:5]
QF205
COMPUTING original: from -5 to 5 +6
TECHNOLOGY
FOR FINANCE
Stage 1: from 1 to 5
Stage 2: from 1 to 5
Dr. Zhao Yibao
Senior Lecturer =word[1]+word[2]+word[3]+word[4]
Of Quantitative
Finance
SMU Classification: Restricted
Slide
021
001/149
0 1 2 3 4 5
a 6-letter word -6 -5 -4 -3 -2 -1
word[-8:7]
QF205
COMPUTING original: from -8 to 7 +6
TECHNOLOGY
FOR FINANCE
Stage 1: from -2 to 7 -2 0, 7 6
Stage 2: from 0 to 6
Dr. Zhao Yibao =word[0]+word[1]+word[2]+word[3]+word[4]+word[5]
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
022
001/149
Stage 1: negative i or j plus length
(omitted omitted)
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE If i or j is negative, the index is relative to the end of
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
o) from -5 to 5
1) from 1 to 5
2) from 1 to 5 Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
028
001/149
Q: What is the output?
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
o) from 5 to 2
1) from 5 to 2
Dr. Zhao Yibao
2) from 5 to 2
Senior Lecturer
Of Quantitative
<empty string>
Finance
SMU Classification: Restricted
Slide
029
002/149
o) from -8 to -7
1) from -2 to -1
2) from 0 to 0 empty
o) from 7 to 8 QF205
COMPUTING
1) from 7 to 8 TECHNOLOGY
FOR FINANCE
2) from 6 to 6 empty
<empty string>
<empty string> o) from -8 to 7
1) from -2 to 7 Dr. Zhao Yibao
2) from 0 to 6 Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
030
001/149
“end”
values
Extended slice indices have useful defaults:
if k>0, an omitted first index i* defaults
QF205
COMPUTING
to 0, an omitted second index j* defaults
TECHNOLOGY
FOR FINANCE to len(s).
if k<0, an omitted first index i* defaults
to len(s)-1, an omitted second index
Dr. Zhao Yibao
j* defaults to -1.
Senior Lecturer Slide 25
Of Quantitative
Finance
SMU Classification: Restricted
Slide
031
001/149
o) from omitted to 2
1) from omitted to 2
2) from 0 to 2
QF205
COMPUTING
o) from 4 to omitted
TECHNOLOGY
FOR FINANCE 1) from 4 to omitted
2) from 4 to 6
o) from -2 to omitted
Dr. Zhao Yibao 1) from 4 to omitted
Senior Lecturer
Of Quantitative 2) from 4 to 6
Finance
SMU Classification: Restricted
Slide
032
001/149 o) from omitted to omitted with step 3
1) from omitted to omitted with step 3
2) from 0 to 10 with step 3
=word[0]+word[3]+word[6]+word[9]
TypeError
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
y 'Python'
word
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
y 'Python'
word '---Python'
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
.upper(), .lower()
.count() Q: Do you still
.find(), .rfind() remember
.replace() these two??
.split(), .rsplit() QF205
COMPUTING
.format() TECHNOLOGY
FOR FINANCE
QF205
list, which can be written as a list of comma-separated
COMPUTING
TECHNOLOGY values (items) between square
FOR FINANCE
brackets. Lists might contain items of different types, but usually the
items all have the same type.
1 4 9 16 25
0 1 2 3 4
-5 -4 -3 -2 -1
indexing an item
1 squares y
2
QF205
COMPUTING 3 (a list object) 1 (an int object)
TECHNOLOGY
FOR FINANCE
1 SAME ID [0] 4 (an int object)
2 [1]
3 SAME ID 9 (an int object)
[2]
[3] 16 (an int object)
Dr. Z: For simplicity, we can treat
Dr. Zhao Yibao [4]
Senior Lecturer squares[0]/y[0], squares[1]/y[1], …, 25 (an int object)
Of Quantitative squares[4]/y[4] as names/identifiers.
Finance
SMU Classification: Restricted
Slide
046
001/149
QF205
Shallow copy of lists
COMPUTING
TECHNOLOGY
FOR FINANCE
Deep copy of lists
http://www.python-course.eu/python3_deep_copy.php
squares y
1
1
[0] [0]
2 4
[1] [1]
QF205 3
COMPUTING [2] 9 [2]
TECHNOLOGY
FOR FINANCE 1 DIFFERENT IDs [3] [3]
16
2
3 NO CHANGE in IDs
[4] [4]
25
[:]
Slicing creates a
Dr. Zhao Yibao
Senior Lecturer
new list, known
Of Quantitative
Finance
as a shallow copy.
SMU Classification: Restricted
Slide
048
001/149
Nested Lists (lists containing other lists)
lst1=['a','b',['ab','ba']]
[0] 'a'
QF205
COMPUTING
TECHNOLOGY lst1 [1] 'b'
FOR FINANCE [0] 'ab'
[2]
[1] 'ba'
lst1 lst2
[1] 'ba'
lst1=['a','b',['ab','ba']] # Q: len(lst1)=?
lst2=lst1[:] # a shallow copy
lst2[0]='c'
'c'
print(lst1, lst2)
'a' QF205
COMPUTING
lst2[2][1]='d' lst1 'b' lst2
TECHNOLOGY
FOR FINANCE
print(lst1, lst2)
(Output is on 'ab'
2
1 QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205 'a'
COMPUTING
TECHNOLOGY lst1 'b' lst2
FOR FINANCE
'ab'
'ba'
What will happen if we proceed with
Dr. Zhao Yibao ? lst2[0]='c'
Senior Lecturer
Of Quantitative ? lst2[2][1]='d'
Finance
SMU Classification: Restricted
Slide
(continued) 053
002/149
lst2[2][1]='d' 'a'
QF205
COMPUTING
print(lst1, lst2) lst1 'b' lst2
TECHNOLOGY
FOR FINANCE
(Output is on 'ab'
'ba'
2
1
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
elementwise sum
1
2
QF205 3
COMPUTING
4
TECHNOLOGY
FOR FINANCE
2. list.extend(iterable) Extend the list by appending all the items from the iterable.
4. list.remove(x) Remove the first item from the list whose value is x.
5. list.pop([i]) Remove the item at the given position (i) or the last item in the list, and return it. QF205
COMPUTING
6. list.clear() Remove all items from the list TECHNOLOGY
FOR FINANCE
7. list.index(x[, start[,end]]) Return zero-based index in the list of the first item whose value is x.
list.append(x)
list.extend(x)
list.pop() QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
list.pop(i)
(type constructor)
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
2:5
Assignment to slices is also
2:5
possible, and this can even
change the size of the list Dr. Zhao Yibao
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
1
2
Dr. Zhao Yibao
1 Senior Lecturer
2 Of Quantitative
Finance
SMU Classification: Restricted
Slide
063
002/149
(continued)
t[0] 'First $20,000 Next $10,000'
method chaining/cascading
1
2 QF205
3 COMPUTING
4 TECHNOLOGY
FOR FINANCE
5
1
2 .split()
3
4
5
.replace('$', '')
Dr. Zhao Yibao
.replace(',', '') Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
064
001/149
https://docs.python.org/3/library/stdtypes.html#str.split
(see In-Class Exercise 31)
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
Tuples
COMPUTING
TECHNOLOGY
FOR FINANCE https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range
https://docs.python.org/3/library/stdtypes.html#tuple
QF205
COMPUTING
TECHNOLOGY
x=(1, 2, 3, [4, 5])
FOR FINANCE A tuple object 1
[0] 2
x [1] A list object An int object
3 4
[2] [0] Rebinding is NOT allowed.
[3] [1] 5
Dr. Zhao Yibao
Senior Lecturer Rebinding is allowed.
Of Quantitative (fixed size) (changeable size)
Finance
SMU Classification: Restricted
Slide
074
001/149
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
operations
COMPUTING
TECHNOLOGY
FOR FINANCE
and these.
Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
common-sequence-operations
https://docs.python.org/3/library/stdtypes.html# 077
002/149
Membership Test
Operations
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
https://docs.python.org/3/reference/expressions.html#membership-test-operations
operators in not
The and
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
in test for membership.
x in s evaluates to True if x is
a member of s, and False
QF205
COMPUTING
TECHNOLOGY
otherwise.
FOR FINANCE
QF205
COMPUTING
True if and only if x is a substring
TECHNOLOGY
FOR FINANCE
of y.
return True.
Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
084
001/149
x not in s
not x in s
QF205
COMPUTING
not (x in s)
TECHNOLOGY
FOR FINANCE
Boolean Operations
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE and, or, not
https://docs.python.org/3.6/library/stdtypes.html#boolean-operations-and-or-not
3
higher
2 priority
QF205
1 COMPUTING
TECHNOLOGY
FOR FINANCE
same
QF205
SyntaxError COMPUTING
TECHNOLOGY
FOR FINANCE
List: []
Tuple: ()
1
2 Mixed Types
3
4 QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
1 in, not in
2
3
0 1 2
Why “False”?
Dr. Zhao Yibao
4 Senior Lecturer
0 1 2 Of Quantitative
Finance
SMU Classification: Restricted
Slide
093
002/149
empty list
empty tuple
1
2
3
4 QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
1
2
in, not in
3
4
empty Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
094
002/149
one-element list
This is not a tuple.
one-element tuple, or one-tuple
one-element tuple, or one-tuple
1
2 QF205
COMPUTING
3 TECHNOLOGY
4 It is actually the FOR FINANCE
1
2
comma which
3 makes a tuple, not Dr. Zhao Yibao
QF205
COMPUTING
Iterables
TECHNOLOGY
FOR FINANCE (Source: http://nvie.com/posts/iterators-vs-generators/)
Lists
Tuples
Sets
Dictionaries
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
HERE!
Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
(Source: http://nvie.com/posts/iterators-vs-generators/) Finance
SMU Classification: Restricted
Slide
097
002/149
Strings
1
2
QF205
3 tuple() COMPUTING
Lists Tuples TECHNOLOGY
FOR FINANCE
1 list()
2
3
isinstance(object, classinfo)
https://docs.python.org/3/library/datatypes.html Slide
099
002/149
https://docs.python.org/3/library/collections.abc.html
QF205
COMPUTING
isinstance() TECHNOLOGY
FOR FINANCE
side 149
enumerate()
QF205
COMPUTING https://docs.python.org/3/library/functions.html#enumerate
TECHNOLOGY
FOR FINANCE
which is an iterator.
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
enumerate(iterable, start=0)
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
lambda expressions
QF205
COMPUTING
TECHNOLOGY https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions
FOR FINANCE
https://docs.python.org/3/reference/expressions.html#lambda
QF205
lambda is a Python keyword.
COMPUTING
TECHNOLOGY
FOR FINANCE
(see Week 02 notes)
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
lambda x: x-1
lambda x: x[1]-1 anonymous Dr. Zhao Yibao
Senior Lecturer
lambda x,y: x+y Of Quantitative
Finance
SMU Classification: Restricted
Slide
109
001/149
filter(), map()
QF205
COMPUTING
TECHNOLOGY
https://docs.python.org/3/library/functions.html#filter
FOR FINANCE
https://docs.python.org/3/library/functions.html#map
QF205
COMPUTING
4 QF205
COMPUTING
TECHNOLOGY
1 f(2)=2*2=4, g(1,2)=1+2=3 FOR FINANCE
2 element-wise square using map()
3 element-wise summation using map() lambda
4 using filter()
map
?? 1 True, 0 False ?? filter Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
112
001/149
next()
QF205
COMPUTING
TECHNOLOGY https://docs.python.org/3/library/functions.html#next
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
QF205
default is returned COMPUTING
TECHNOLOGY
when iterator is exhausted. FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
Discussion:
What do these 2 lines of
(2, 5) ?? code do? Is it possible to
Dr. Zhao Yibao (Dr. Z: The answer will be given show the position of the
Senior Lecturer
Of Quantitative today in another in-class exercise.) number 5 too, e.g. (2, 5)?
Finance
SMU Classification: Restricted
Slide
116
001/149
bool()
QF205
COMPUTING
TECHNOLOGY
https://docs.python.org/3/library/functions.html#bool
FOR FINANCE
class bool([x])
None
False
Zero
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
Comparisons
QF205
COMPUTING https://docs.python.org/3.6/library/stdtypes.html#comparisons
TECHNOLOGY
FOR FINANCE https://docs.python.org/3/reference/expressions.html#comparisons
eight comparison
There are
QF205
COMPUTING
operations in Python .
same priority
TECHNOLOGY
FOR FINANCE
They all have the (which is
higher than that of the Boolean operations).
1 2 3 4 5 6 7 8
Dr. Zhao Yibao < <= > >= == != is is not
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
124
001/149
Comparisons can be chained arbitrarily; for example,
x<y<=z is equivalent to x<y and y<=z,
except that y is evaluated only once (but
in both cases z is not evaluated at all
when x<y is found to be false).
QF205
COMPUTING
Dr. Z: I remember! and is a short-circuit operator.
TECHNOLOGY
FOR FINANCE
value comparison
2 order
3 comparison
QF205 4
COMPUTING
TECHNOLOGY
FOR FINANCE 5 equality
Value comparison 6 comparison
Order comparison
7
Equality comparison identity comparison
Dr. Zhao Yibao
Senior Lecturer
Identity comparison 8
Of Quantitative
Finance
SMU Classification: Restricted
Slide
126
002/149
f(2) is
evaluated only once. Dr. Zhao Yibao
f(4) is not Senior Lecturer
Of Quantitative
evaluated. https://docs.python.org/3/tutorial/inputoutput.html#input-and-output Finance
SMU Classification: Restricted
Slide
127
001/149
Objects of different
types, except different
numeric types, never
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
compare equal.
Dr. Z: Does it mean they are “not equal” or … ?
Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
A Special Number: Infinity 128
002/149
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
math.inf
1
2
3
4
Every finite float
QF205
5
6
number is less than COMPUTING
TECHNOLOGY
FOR FINANCE
1 math.inf.
2
3
4
5 Dr. Zhao Yibao
Senior Lecturer
6 Of Quantitative
Finance
SMU Classification: Restricted
Slide
131
001/149
QF205
4 COMPUTING
TECHNOLOGY
FOR FINANCE
The if statement
QF205
COMPUTING
TECHNOLOGY https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
FOR FINANCE
if condition: False
condition
indentedStatementBlock
QF205
COMPUTING
True TECHNOLOGY
FOR FINANCE
True
False QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
indentedStatementBlock1 indentedStatementBlock2
Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
139
001/149
False
Flow Chart:
c1
True False
c2
if condition1:
True False
indentedStatementBlock1 iSB1 c3
elif condition2:
True
indentedStatementBlock2 iSB2 c4
False
elif condition3:
QF205
COMPUTING
indentedStatementBlock3 iSB3
True False
c5
TECHNOLOGY elif condition4:
FOR FINANCE indentedStatementBlock4 True
iSB4
elif condition5:
indentedStatementBlock5
else: iSB5
indentedStatementBlock6
iSB6
Dr. Zhao Yibao
Senior Lecturer
(Dr. Z: Only one block
Of Quantitative
Finance
is executed.)
SMU Classification: Restricted
Slide
Q: Write a Python program to … 5 minutes? 140
002/149
Case 1
QF205
Case 2 COMPUTING
TECHNOLOGY
FOR FINANCE
Case 3
Dr. Zhao Yibao
Senior Lecturer
Ans: y=12450.00 Of Quantitative
Finance
SMU Classification: Restricted
Slide
141
001/149
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
xi[i]<=x<xi[i+1]
y=bi[i]+mi[i]/100*(x-xi[i])
i=0, 1, ..., 8 QF205
COMPUTING
Case 2 OR TECHNOLOGY
FOR FINANCE
xi[i-1]<=x<xi[i]
y=bi[i-1]+mi[i-1]/100*(x-xi[i-1])
i=1, 2, ..., 9
xi[9]<=x
Dr. Zhao Yibao
Case 3 y=bi[9]+mi[9]/100*(x-xi[9]) Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
143
001/149
Using the list method append(), we can
combine case 2 and case 3 into one.
import math
xi=[20000, 30000, 40000, 80000, 120000, 160000, 200000, 240000, 280000, 320000]
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
xi.append(math.inf)
x=150_000
xi=[20000, 30000, 40000, 80000, 120000, 160000, 200000, 240000, 280000, 320000, math.inf]
xi=[20000, 30000, 40000, 80000, 120000, 160000, 200000, 240000, 280000, 320000, math.inf]
Slide
144
002/149
x=150_000
xi[i-1]<=x<xi[i]
if i==0: #case 1
y=0
else: #case 2 and case 3 Dr. Zhao Yibao
y=bi[i-1]+mi[i-1]/100*(x-xi[i-1]) Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
145
002/149
1
QF205
2
COMPUTING
TECHNOLOGY
3 FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
Ans: y=62150.00
FOR FINANCE
QF205
COMPUTING
TECHNOLOGY
FOR FINANCE
Ans: y=0
Dr. Zhao Yibao
Senior Lecturer
Of Quantitative
Finance
SMU Classification: Restricted
Slide
148
001/149
W3-027 W4-113
W2-131
W4-100
W2-168 W2-168
W4-110
QF205
COMPUTING W2-159 W2-080 W4-074
TECHNOLOGY
FOR FINANCE W2-091 W2-017
W4-061
W4-110
W2-059 W2-164