Skip to content

Commit 7ae5a5c

Browse files
committed
Initial Commit
1 parent 590a3a1 commit 7ae5a5c

File tree

126 files changed

+597689
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+597689
-0
lines changed

.DS_Store

10 KB
Binary file not shown.

Computed Data/.DS_Store

6 KB
Binary file not shown.
14.8 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4.71 KB
Binary file not shown.
39.5 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10.9 KB
Binary file not shown.

Functions/.DS_Store

10 KB
Binary file not shown.
14 KB
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
## Author: Patrick Chang
2+
# Script file for the MM Complex Fourier Transform (Dirichlet)
3+
# Supporting Algorithms are at the start of the script
4+
# Include:
5+
# - Scale function to re-scale time to [0, 2 \pi]
6+
# Number of Fourier Coefficients automatically chosen so that events
7+
# are not aliased
8+
9+
#---------------------------------------------------------------------------
10+
11+
### Data Format:
12+
## p = [n x m] matrix of prices, log returns are computed in the function
13+
# non-trading times are indicated by NaNs
14+
## t = [n x m] matrix of trading times, non-trading times are indicated by NaNs
15+
# dimensions of p and t must match.
16+
## N = Optional input for cutoff frequency
17+
18+
#---------------------------------------------------------------------------
19+
20+
using ArgCheck; using LinearAlgebra
21+
22+
#---------------------------------------------------------------------------
23+
### Supporting functions
24+
25+
function scale(t)
26+
maxt = maximum(filter(!isnan, t))
27+
mint = minimum(filter(!isnan, t))
28+
29+
tau = (2*pi) .* (t .- mint) ./ (maxt - mint)
30+
return tau
31+
end
32+
33+
#---------------------------------------------------------------------------
34+
# Dirichlet Kernel implementaion
35+
# wave range from -N:N
36+
# Exploits c(-k) = \bar{c(k)} in the computation so that
37+
# we need only compute 1:N and sum(DiffP) for efficiency
38+
39+
function CFTcorrDK(p, t; kwargs...)
40+
## Pre-allocate arrays and check Data
41+
np = size(p)[1]
42+
mp = size(p)[2]
43+
nt = size(t)[1]
44+
45+
@argcheck size(p) == size(t) DimensionMismatch
46+
47+
# Re-scale trading times
48+
tau = scale(t)
49+
# Computing minimum time change
50+
# minumum step size to avoid smoothing
51+
dtau = diff(filter(!isnan, tau))
52+
taumin = minimum(filter((x) -> x>0, dtau))
53+
taumax = 2*pi
54+
# Sampling Freq.
55+
N0 = taumax/taumin
56+
57+
# Optional Cutoff - if not specified we use Nyquist Cutoff
58+
kwargs = Dict(kwargs)
59+
60+
if haskey(kwargs, :N)
61+
k = collect(1:1:kwargs[:N])
62+
else
63+
k = collect(1:1:floor(N0/2))
64+
end
65+
66+
Den = length(k)
67+
68+
#------------------------------------------------------
69+
70+
c_pos = zeros(ComplexF64, mp, 2*Den + 1)
71+
c_neg = zeros(ComplexF64, mp, 2*Den + 1)
72+
73+
for i in 1:mp
74+
psii = findall(!isnan, p[:,i])
75+
P = p[psii, i]
76+
Time = tau[psii, i]
77+
DiffP = diff(log.(P))
78+
79+
C = DiffP' * exp.(-1im * Time[1:(end-1),:] * k')
80+
81+
c_pos[i,:] = [C sum(DiffP) conj(C)]
82+
c_neg[i,:] = [conj(C) sum(DiffP) C]
83+
end
84+
85+
Sigma = 0.5 / (2*Den + 1) .* (c_pos*c_pos' + c_neg*c_neg')
86+
87+
Sigma = real(Sigma)
88+
var = diag(Sigma)
89+
sigma = sqrt.(var)
90+
rho = Sigma ./ (sigma * sigma')
91+
92+
return rho, Sigma, var
93+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
## Author: Patrick Chang
2+
# Script file for the MM Fast Fourier Transform with Zero Padding
3+
# Supporting Algorithms are at the start of the script
4+
# Include:
5+
# - Scale function to re-scale time to [0, 2 \pi]
6+
# - Zero-padding function
7+
# Number of Fourier Coefficients used is length of data
8+
9+
#---------------------------------------------------------------------------
10+
11+
### Data Format:
12+
## p = [n x m] matrix of prices, log returns are computed in the function
13+
# non-trading times are indicated by NaNs
14+
## t = [n x m] matrix of trading times, non-trading times are indicated by NaNs
15+
# dimensions of p and t must match.
16+
17+
#---------------------------------------------------------------------------
18+
19+
using ArgCheck; using LinearAlgebra;
20+
using FFTW
21+
22+
#---------------------------------------------------------------------------
23+
### Supporting functions
24+
25+
function scale(t)
26+
maxt = maximum(filter(!isnan, t))
27+
mint = minimum(filter(!isnan, t))
28+
29+
tau = (2*pi) .* (t .- mint) ./ (maxt - mint)
30+
return tau
31+
end
32+
33+
function zero_pad(cj, xj, N0)
34+
res = zeros(N0, 1)
35+
nj = length(xj)
36+
for j in 1:nj
37+
pos = Int(round(xj[j] * (N0/(2*pi))))
38+
res[pos+1] = cj[j]
39+
end
40+
return res
41+
end
42+
#ZPP = zero_pad(DiffP, tau[psii, 1], Int(ceil(N0)))
43+
#---------------------------------------------------------------------------
44+
45+
# Fast Fourier Transform (with Zero Padding) implementaion of the Dirichlet Kernel
46+
# Can be used for asynchronous data
47+
# No option for optional cutoff (N)
48+
49+
function FFTZPcorrDK(p, t)
50+
## Pre-allocate arrays and check Data
51+
np = size(p)[1]
52+
mp = size(p)[2]
53+
nt = size(t)[1]
54+
55+
#@argcheck size(p) == size(t) DimensionMismatch
56+
57+
# Re-scale trading times
58+
tau = scale(t)
59+
# Computing minimum time change
60+
# minumum step size to avoid smoothing
61+
dtau = diff(filter(!isnan, tau))
62+
taumin = minimum(filter((x) -> x>0, dtau))
63+
taumax = 2*pi
64+
# Sampling Freq.
65+
N0 = taumax/taumin
66+
67+
k = collect(-floor(N0/2):1:floor(N0/2))
68+
69+
Den = length(k)
70+
71+
#------------------------------------------------------
72+
c_pos = zeros(ComplexF64, mp, Den)
73+
c_neg = zeros(ComplexF64, mp, Den)
74+
75+
for i in 1:mp
76+
psii = findall(!isnan, p[:,i])
77+
P = p[psii, i]
78+
DiffP = diff(log.(P))
79+
psii = psii[1:(end-1)]
80+
Time = tau[psii, i]
81+
ZP = zeros(Den, 1)
82+
83+
ZP[1:Int(floor(N0))] = zero_pad(DiffP, Time, Int(floor(N0)))
84+
85+
C = fft(ZP)
86+
87+
C_pos = C
88+
C_neg = conj(C)
89+
90+
c_pos[i,:] = C_pos
91+
c_neg[i,:] = C_neg
92+
end
93+
94+
Sigma = zeros(ComplexF64, mp, mp)
95+
96+
Sigma = 0.5 / Den .* (c_pos*c_pos' + c_neg*c_neg')
97+
98+
Sigma = real(Sigma)
99+
var = diag(Sigma)
100+
sigma = sqrt.(var)
101+
rho = Sigma ./ (sigma * sigma')
102+
103+
return rho, Sigma, var
104+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## Author: Patrick Chang
2+
# Script file for the MM Fast Fourier Transform
3+
# Supporting Algorithms are at the start of the script
4+
# Include:
5+
# - Scale function to re-scale time to [0, 2 \pi]
6+
# Number of Fourier Coefficients used is length of data
7+
8+
#---------------------------------------------------------------------------
9+
10+
### Data Format:
11+
## p = [n x m] matrix of prices, log returns are computed in the function
12+
# non-trading times are indicated by NaNs
13+
14+
#---------------------------------------------------------------------------
15+
16+
using ArgCheck; using LinearAlgebra;
17+
using FFTW
18+
19+
#---------------------------------------------------------------------------
20+
21+
# Fast Fourier Transform implementaion of the Dirichlet Kernel
22+
# Can ONLY be used for fully synchronous data
23+
# No option for optional cutoff (N)
24+
25+
function FFTcorrDK(p)
26+
## Pre-allocate arrays and check Data
27+
np = size(p)[1]
28+
mp = size(p)[2]
29+
#nt = size(t)[1]
30+
31+
#@argcheck size(p) == size(t) DimensionMismatch
32+
33+
# # Re-scale trading times
34+
# tau = scale(t)
35+
# # Computing minimum time change
36+
# # minumum step size to avoid smoothing
37+
# dtau = diff(filter(!isnan, tau))
38+
# taumin = minimum(filter((x) -> x>0, dtau))
39+
# taumax = 2*pi
40+
# # Sampling Freq.
41+
# N0 = taumax/taumin
42+
43+
if (iseven(np))
44+
k = collect(0:1:(np-1)/2)
45+
else
46+
k = collect(0:1:(np+1)/2)
47+
end
48+
49+
Den = length(k)
50+
51+
#------------------------------------------------------
52+
c_pos = zeros(ComplexF64, mp, Den + (Den-1))
53+
c_neg = zeros(ComplexF64, mp, Den + (Den-1))
54+
55+
for i in 1:mp
56+
DiffP = diff(log.(p[:,i]))
57+
58+
C = rfft(DiffP)
59+
60+
C_pos = C
61+
C_neg = conj(C)
62+
63+
c_pos[i,:] = [C_pos; C_neg[2:end]]
64+
c_neg[i,:] = [C_neg; C_pos[2:end]]
65+
end
66+
67+
Sigma = zeros(ComplexF64, mp, mp)
68+
69+
Sigma = 0.5 / (Den + (Den-1)) .* (c_pos*c_pos' + c_neg*c_neg')
70+
71+
Sigma = real(Sigma)
72+
var = diag(Sigma)
73+
sigma = sqrt.(var)
74+
rho = Sigma ./ (sigma * sigma')
75+
76+
return rho, Sigma, var
77+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
## Author: Patrick Chang
2+
# Script file for the MM Complex Fourier Transform (Dirichlet)
3+
# Supporting Algorithms are at the start of the script
4+
# Include:
5+
# - Scale function to re-scale time to [0, 2 \pi]
6+
# Number of Fourier Coefficients automatically chosen so that events
7+
# are not aliased
8+
9+
#---------------------------------------------------------------------------
10+
11+
### Data Format:
12+
## p1;p2 = vector of stock prices - can include or not include NaNs
13+
## t1;t2 = vector of trading times - can include or not include NaNs
14+
## N = Optional input for cutoff frequency
15+
16+
#---------------------------------------------------------------------------
17+
18+
using ArgCheck; using LinearAlgebra
19+
20+
#---------------------------------------------------------------------------
21+
### Supporting functions
22+
23+
function scaleV2(t1, t2)
24+
maxt = maximum(filter(!isnan, [t1;t2]))
25+
mint = minimum(filter(!isnan, [t1;t2]))
26+
27+
tau1 = (2*pi) .* (t1 .- mint) ./ (maxt - mint)
28+
tau2 = (2*pi) .* (t2 .- mint) ./ (maxt - mint)
29+
return tau1, tau2
30+
end
31+
32+
function KanataniDK(t1, t2, N)
33+
t1 = filter(!isnan, t1)
34+
t2 = filter(!isnan, t2)
35+
L1 = length(t1)
36+
L2 = length(t2)
37+
38+
W = zeros(L1-1, L2-1)
39+
40+
for i in 1:(L1-1)
41+
for j in 1:(L2-1)
42+
if (t1[i] == t2[j])
43+
W[i,j] = 1
44+
else
45+
W[i,j] = (1/(2*N+1)) * sin((N + 0.5) * (t1[i] - t2[j]))/sin((t1[i] - t2[j])/2)
46+
end
47+
end
48+
end
49+
return W
50+
end
51+
52+
#---------------------------------------------------------------------------
53+
54+
# Kanatani Weight matrix implementaion using the Dirichlet Kernel
55+
56+
function KANcorrDK(p1, p2, t1, t2; kwargs...)
57+
t1 = filter(!isnan, t1)
58+
t2 = filter(!isnan, t2)
59+
60+
p1 = filter(!isnan, p1)
61+
p2 = filter(!isnan, p2)
62+
63+
# Re-scale trading times
64+
tau = scaleV2(t1, t2)
65+
tau1 = tau[1]
66+
tau2 = tau[2]
67+
# Computing minimum time change
68+
# minumum step size to avoid smoothing
69+
dtau1 = diff(tau1)
70+
dtau2 = diff(tau2)
71+
taumin = minimum([dtau1; dtau2])
72+
taumax = 2*pi
73+
# Sampling Freq.
74+
N0 = taumax/taumin
75+
76+
# Optional Cutoff - if not specified we use Nyquist Cutoff
77+
kwargs = Dict(kwargs)
78+
79+
if haskey(kwargs, :N)
80+
N = kwargs[:N]
81+
else
82+
N = trunc(Int, floor(N0/2))
83+
end
84+
85+
#------------------------------------------------------
86+
87+
DiffP1 = diff(log.(p1))
88+
DiffP2 = diff(log.(p2))
89+
90+
W1 = KanataniDK(tau1, tau1, N)
91+
W2 = KanataniDK(tau2, tau2, N)
92+
W12 = KanataniDK(tau1, tau2, N)
93+
94+
Sigma = zeros(Float64, 2, 2)
95+
Sigma[1, 1] = DiffP1' * W1 * DiffP1
96+
Sigma[2, 2] = DiffP2' * W2 * DiffP2
97+
Sigma[1, 2] = DiffP1' * W12 * DiffP2
98+
Sigma[2, 1] = Sigma[1, 2]
99+
100+
var = diag(Sigma)
101+
sigma = sqrt.(var)
102+
rho = Sigma ./ (sigma * sigma')
103+
104+
return rho, Sigma, var
105+
end

0 commit comments

Comments
 (0)