Skip to content

Commit 0ad1584

Browse files
michaelnowotnypavanky
authored andcommitted
Adding heston model example
1 parent 2b4970e commit 0ad1584

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

examples/financial/heston_model.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/python
2+
3+
##############################################################################################
4+
# Copyright (c) 2015, Michael Nowotny
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without modification,
8+
# are permitted provided that the following conditions are met:
9+
#
10+
# 1. Redistributions of source code must retain the above copyright notice,
11+
# this list of conditions and the following disclaimer.
12+
#
13+
# 2. Redistributions in binary form must reproduce the above copyright notice,
14+
# this list of conditions and the following disclaimer in the documentation and/or other
15+
# materials provided with the distribution.
16+
#
17+
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
18+
# to endorse or promote products derived from this software without specific
19+
# prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27+
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
###############################################################################################
33+
34+
import arrayfire as af
35+
import math
36+
import time
37+
38+
def simulateHestonModel( T, N, R, mu, kappa, vBar, sigmaV, rho, x0, v0 ) :
39+
40+
deltaT = T / (float)(N - 1)
41+
42+
x = [af.constant(x0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32)]
43+
v = [af.constant(v0, R, dtype=af.Dtype.f32), af.constant(0, R, dtype=af.Dtype.f32)]
44+
45+
sqrtDeltaT = math.sqrt(deltaT)
46+
sqrtOneMinusRhoSquare = math.sqrt(1-rho**2)
47+
48+
m = af.constant(0, 2, dtype=af.Dtype.f32)
49+
m[0] = rho
50+
m[1] = sqrtOneMinusRhoSquare
51+
zeroArray = af.constant(0, R, 1, dtype=af.Dtype.f32)
52+
53+
for t in range(1, N) :
54+
tPrevious = (t + 1) % 2
55+
tCurrent = t % 2
56+
57+
dBt = af.randn(R, 2, dtype=af.Dtype.f32) * sqrtDeltaT
58+
59+
vLag = af.maxof(v[tPrevious], zeroArray)
60+
sqrtVLag = af.sqrt(vLag)
61+
62+
x[tCurrent] = x[tPrevious] + (mu - 0.5 * vLag) * deltaT + sqrtVLag * dBt[:, 0]
63+
v[tCurrent] = vLag + kappa * (vBar - vLag) * deltaT + sigmaV * (sqrtVLag * af.matmul(dBt, m))
64+
65+
return (x[tCurrent], af.maxof(v[tCurrent], zeroArray))
66+
67+
68+
def main():
69+
T = 1
70+
nT = 10 * T
71+
R_first = 1000
72+
R = 20000000
73+
74+
x0 = 0 # initial log stock price
75+
v0 = 0.087**2 # initial volatility
76+
r = math.log(1.0319) # risk-free rate
77+
rho = -0.82 # instantaneous correlation between Brownian motions
78+
sigmaV = 0.14 # variance of volatility
79+
kappa = 3.46 # mean reversion speed
80+
vBar = 0.008 # mean variance
81+
k = math.log(0.95) # strike price
82+
83+
# first run
84+
( x, v ) = simulateHestonModel( T, nT, R_first, r, kappa, vBar, sigmaV, rho, x0, v0 )
85+
86+
# Price plain vanilla call option
87+
tic = time.time()
88+
( x, v ) = simulateHestonModel( T, nT, R, r, kappa, vBar, sigmaV, rho, x0, v0 )
89+
af.sync()
90+
toc = time.time() - tic
91+
K = math.exp(k)
92+
zeroConstant = af.constant(0, R, dtype=af.Dtype.f32)
93+
C_CPU = math.exp(-r * T) * af.mean(af.maxof(af.exp(x) - K, zeroConstant))
94+
print("Time elapsed = {} secs".format(toc))
95+
print("Call price = {}".format(C_CPU))
96+
print(af.mean(v))
97+
98+
99+
if __name__ == "__main__":
100+
main()

0 commit comments

Comments
 (0)