|
| 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 math |
| 35 | +import time |
| 36 | + |
| 37 | +import arrayfire as af |
| 38 | + |
| 39 | + |
| 40 | +def simulateHestonModel(T, N, R, mu, kappa, vBar, sigmaV, rho, x0, v0): |
| 41 | + |
| 42 | + deltaT = T / (float)(N - 1) |
| 43 | + |
| 44 | + x = [af.constant(x0, (R,)), af.constant(0, (R,))] |
| 45 | + v = [af.constant(v0, (R,)), af.constant(0, (R,))] |
| 46 | + |
| 47 | + sqrtDeltaT = math.sqrt(deltaT) |
| 48 | + sqrtOneMinusRhoSquare = math.sqrt(1 - rho**2) |
| 49 | + |
| 50 | + m = af.constant(0, (2,)) |
| 51 | + m[0] = rho |
| 52 | + m[1] = sqrtOneMinusRhoSquare |
| 53 | + zeroArray = af.constant(0, (R, 1)) |
| 54 | + |
| 55 | + for t in range(1, N): |
| 56 | + tPrevious = (t + 1) % 2 |
| 57 | + tCurrent = t % 2 |
| 58 | + |
| 59 | + dBt = af.randn((R, 2)) * sqrtDeltaT |
| 60 | + |
| 61 | + vLag = af.maxof(v[tPrevious], zeroArray) |
| 62 | + sqrtVLag = af.sqrt(vLag) |
| 63 | + |
| 64 | + x[tCurrent] = x[tPrevious] + (mu - 0.5 * vLag) * deltaT + sqrtVLag * dBt[:, 0] |
| 65 | + v[tCurrent] = vLag + kappa * (vBar - vLag) * deltaT + sigmaV * (sqrtVLag * af.matmul(dBt, m)) |
| 66 | + |
| 67 | + return (x[tCurrent], af.maxof(v[tCurrent], zeroArray)) |
| 68 | + |
| 69 | + |
| 70 | +def main(): |
| 71 | + T = 1 |
| 72 | + nT = 20 * T |
| 73 | + R_first = 1000 |
| 74 | + R = 5000000 |
| 75 | + |
| 76 | + x0 = 0 # initial log stock price |
| 77 | + v0 = 0.087**2 # initial volatility |
| 78 | + r = math.log(1.0319) # risk-free rate |
| 79 | + rho = -0.82 # instantaneous correlation between Brownian motions |
| 80 | + sigmaV = 0.14 # variance of volatility |
| 81 | + kappa = 3.46 # mean reversion speed |
| 82 | + vBar = 0.008 # mean variance |
| 83 | + k = math.log(0.95) # strike price |
| 84 | + |
| 85 | + # first run |
| 86 | + (x, v) = simulateHestonModel(T, nT, R_first, r, kappa, vBar, sigmaV, rho, x0, v0) |
| 87 | + |
| 88 | + # Price plain vanilla call option |
| 89 | + tic = time.time() |
| 90 | + (x, v) = simulateHestonModel(T, nT, R, r, kappa, vBar, sigmaV, rho, x0, v0) |
| 91 | + af.sync(-1) |
| 92 | + toc = time.time() - tic |
| 93 | + K = math.exp(k) |
| 94 | + zeroConstant = af.constant(0, (R,)) |
| 95 | + C_CPU = math.exp(-r * T) * af.mean(af.maxof(af.exp(x) - K, zeroConstant)) |
| 96 | + print("Time elapsed = {} secs".format(toc)) |
| 97 | + print("Call price = {}".format(C_CPU)) |
| 98 | + print(af.mean(v)) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments