Intro
Intro
Education
▶ PhD Computer Science,
National University of Singapore, 2005 - 2010
▶ BSc Computer Science,
Fudan University, 2001 - 2005
Numerical Optimization
Assignments 70%: 4 assignments, 15% each for the first 2, 20% each
for the last 2
probability theory,
calculas,
stochastic calculus
Most concepts that are used will be covered, briefly introduced, or referred
to recommended reading materials.
No textbook
▶ interest rates
▶ ...
There are few cases that application of standard formulas can solve
the problem
µt
log SeK ± 12 σ 2 t
d± = √ (1)
σ t
How to implement it so that the cost of calculation is minimum?
repeat = int(1e8)
opTiming("x = 5.0 + 7.0", "add", repeat)
opTiming("x = 5.0 * 7.0", "mul", repeat)
opTiming("x = 5.0 / 7.0", "div", repeat)
opTiming("x = math.log(7.0)", "log", repeat)
opTiming("x = math.exp(7.0)", "exp", repeat)
opTiming("x = math.sqrt(7.0)", "sqrt", repeat)
method 2
m1 = """
S = 100;K = 105;vol = 0.1;t=2;mu=0.01
d1 = (math.log(S * math.exp(mu*t) / K) + vol * vol * t / 2) / vol / math.sqrt(t)
"""
m2 = """
S = 100;K = 105;vol = 0.1;t=2;mu=0.01
stdev = vol * math.sqrt(t)
d1 = (math.log(S / K) + mu*t) / stdev + stdev / 2
"""
repeat = int(1e7)
opTiming(m1, ’m1’, repeat)
opTiming(m2, ’m2’, repeat)
110112 (7)
1 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 (8)
= 16 + 8 + 0 + 2 + 1 (9)
= 2710 (10)
1 × 23 + 1 × 22 + 0 × 21 + 1 × 20 + 1 × 2−1 (11)
= 8 + 4 + 0 + 1 + 0.5 (12)
= 13.510 (13)
For example, if we look at two signed fixed<4, 1> numbers: 1011 and
1101
1 print(toFixedPoint(20, 8, 3))
2 print(toFixedPoint(20, 9, 3))
Many other ERC20 tokens’ smart contracts were found having the
same bug
Numerical Methods in QF (QF5204) Introduction 24 / 40
Microsoft Exchange Server Y2K22 Bug
The bug was due to “dates that are stored in the format
yymmddHHMM converted to a signed 32-bit integer overflowed on 1
January 2022, as 231 − 1 = 2147483647” — 2201010001 cannot be
represented as uint32.
1 import numpy as np
2 for f in (np.float32, np.float64, float):
3 finfo = np.finfo(f)
4 print(finfo.dtype, "\t exponent bits = ", finfo.nexp, "\t significand bits = ", finfo.nmant)
▶ truncation error
arises when we approximate a continuous model with a discrete one
1 x = 10776321
2 nsteps = 1235
3 s = x / nsteps
4 y = 0
5 for i in range(nsteps):
6 y += s
7 print(x - y)
1 x = 0.1234567891234567890
2 y = 0.1234567891
3 scale = 1e16
4 z1 = (x-y) * scale
5 print("z1 = ", z1)
6
7 z2 = (x*scale - y*scale)
8 print("z2 = ", z2)
h2 2
f (x + h) = f (x) + hf ′ (x) + f (x) + . . . (26)
2!
Assume that f is continuous and f ′ (x), f 2 (x), . . . , f (n) (x) exists over (a, b),
let x0 ∈ [a, b], then for every x ∈ (a, b), there exists a number c that lies
between x0 and x such that
n
X f (k) (x0 ) f (n+1) (x0 )
f (x) = f (x0 ) + (x − x0 )k + (x − c)n+1 (27)
k! (n + 1)!
k=1
Local truncation error is the error that our increment function causes
during a single iteration.
In the case of taylor expansion,
We say that the numerical method has order p if for any sufficiently
smooth solution of the initial value problem, the local truncation error is
O(hp+1 ).
y0 = f (x0 )
x1 = x0 + ∆h, y1 = y0 + f ′ (x0 )∆h
...
xi = xi−1 + ∆h, yi = yi−1 + f ′ (xi−1 )∆h
...
xn = xn−1 + ∆h, f (x0 + h) ≈ yn = yn−1 + f ′ (xn−1 )∆h
|xc −x|
▶ Relative error: x where xc is the computed value and x is the exact
value
lim xn = L (29)
n→∞
if for any given ϵ > 0, there exists an N such that for any n > N, we
have |xn − L| < ϵ.
|en+1 | |xn+1 − L|
lim p
= lim =λ (30)
n→∞ |en | n→∞ |xn − L|p
Possible tests
▶ the absolute change is sufficiently small |xk+1 − xk | ≤ tol
−xk
▶ the relative change is sufficiently small | xk+1
xk+1 | ≤ tol