-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFT_test.py
66 lines (46 loc) · 1.68 KB
/
DFT_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 21:47:02 2017
@author: Quintus
"""
import numpy as np
from numpy import exp
from matplotlib import pyplot as plt
#==============================================================================
# call DFT pricing
#==============================================================================
# parameters setup
S0 = 50
r = 0.05
q = 0.02
T = 0.25
sigma = 0.2
alpha = 1
N = 4096
dk = 0.005
dw = 2*np.pi/(N*dk)
k = -N*dk/2 + np.arange(0, N+1)*dk
w = -N*dw/2 + np.arange(0, N+1)*dw
def PSI(w, S0, r, q, T, sigma):
psiValue = exp(-r*T) * CF(w-(alpha+1)*1j, S0, r, q, T, sigma) / (alpha**2 + alpha - w**2 + (2*alpha+1)*1j*w)
return psiValue
def CF(w, S0, r, q, T, sigma):
'''
characteristic function of log price
'''
cfValue = exp((np.log(S0) + (r-q-sigma**2/2)*T) * 1j * w - 0.5*sigma**2*T*w**2)
return cfValue
weight = np.ones(N+1)
weight[0], weight[-1] = 0.5, 0.5
test = weight * exp(1j*np.pi*np.arange(0, N+1)) * PSI(w, S0, r, q, T, sigma)
sumTest = np.sum(test * exp(-1j * 2*np.pi * np.arange(0, N+1) * np.arange(0, N+1) / N))
callPrice = np.zeros(N+1)
for n in range(0, N+1):
callPrice[n] = exp(-alpha*k[n] + 1j * np.pi * n - 1j*np.pi*N/2) / dk * (1/N) \
* np.sum(test * exp(-1j * 2*np.pi * np.arange(0, N+1) * n / N))
plt.plot(exp(k)[:2900], callPrice[:2900])
#==============================================================================
# FFT
#==============================================================================
callPrice = exp(-alpha*k) / (2*np.pi) * (np.fft.fft(exp(1j * N*dk/2 * w) * weight * PSI(w, S0, r, q, T, sigma) * dw)).real