@@ -33,28 +33,38 @@ Samples `log(S_T)` using the exact Broadie-Kaya method.
33
33
function sample_V_T (rng:: AbstractRNG , d:: HestonDistribution )
34
34
κ, θ, σ, V0, T = d. κ, d. θ, d. σ, d. V0, d. T
35
35
36
- ν = 4 κ * θ / σ^ 2 # Degrees of freedom
37
- ψ = 4 κ * exp (- κ * T) * V0 / (σ^ 2 * (1 - exp (- κ * T))) # Noncentrality parameter
38
- c = σ^ 2 * (1 - exp (- κ * T)) / (4 κ ) # Scaling factor
36
+ d = 4 * κ * θ / σ^ 2 # Degrees of freedom
37
+ λ = 4 * κ * exp (- κ * T) * V0 / (σ^ 2 * (1 - exp (- κ * T))) # Noncentrality parameter
38
+ c = σ^ 2 * (1 - exp (- κ * T)) / (4 * κ ) # Scaling factor
39
39
40
- V_T = c * Distributions. rand (rng, NoncentralChisq (ν, ψ ))
40
+ V_T = c * Distributions. rand (rng, NoncentralChisq (d, λ ))
41
41
return V_T
42
42
end
43
43
44
- function sample_integral_V (VT, rng, dist:: HestonDistribution )
44
+ function integral_V_cdf (VT, rng, dist:: HestonDistribution )
45
45
Φ (u) = integral_var_char (u, VT, dist)
46
46
integrand (x) = u -> sin (u * x) / u * real (Φ (u))
47
- F (x) = 2 / π * quadgk (integrand (x), 0 , Inf )[1 ] # specify like in paper (trapz)
47
+ F (x) = 2 / π * quadgk (integrand (x), 0 , 100 ; maxevals= 10000 )[1 ] # specify like in paper (trapz)
48
+ return F
49
+ end
50
+
51
+ function sample_integral_V (VT, rng, dist:: HestonDistribution )
52
+ F = integral_V_cdf (VT, rng, dist)
48
53
lower_bound = 0.0
49
- upper_bound = 1000
54
+ upper_bound = 1
50
55
# Generate samples
51
56
unif = Uniform (0 ,1 ) # Define uniform distribution
52
57
u = Distributions. rand (rng, unif)
53
58
return inverse_cdf_rootfinding (F, u, lower_bound, upper_bound)
54
59
end
55
60
56
61
function inverse_cdf_rootfinding (cdf_func, u, y_min, y_max)
57
- return find_zero (y -> cdf_func (y) - u, (y_min, y_max); atol= 1E-2 , maxiters= 5 ) # Solve F(y) = u like in paper (newton 2nd order)
62
+ func = y -> cdf_func (y) - u
63
+ if (func (y_min)* func (y_max) < 0 )
64
+ return find_zero (y -> cdf_func (y) - u, (y_min, y_max); atol= 1E-5 , maxiters= 100 ) # Solve F(y) = u like in paper (newton 2nd order)
65
+ else
66
+ return find_zero (y -> cdf_func (y) - u, y_min; atol= 1E-5 , maxiters= 100 )
67
+ end
58
68
end
59
69
60
70
using SpecialFunctions
@@ -64,19 +74,20 @@ function adjust_argument(z)
64
74
θ = angle (z) # Compute current argument
65
75
m = round (Int, θ / π) # Find the nearest integer multiple of π
66
76
z_adjusted = z * exp (- im * m * π) # Shift argument back into (-π, π]
67
- return z_adjusted
77
+ return z_adjusted, m
68
78
end
69
79
70
80
""" Compute the modified Bessel function I_{-ν}(z) with argument correction. """
71
81
function besseli_corrected (nu, z)
72
- z_adj = adjust_argument (z) # Ensure argument is in (-π, π]
73
- return besseli (nu, z_adj) # Compute Bessel function with corrected input
82
+ z_adj, m = adjust_argument (z) # Ensure argument is in (-π, π]
83
+ # println("adjusted besseli: ",m, " value ", nu, " val: ", z_adj)
84
+ return exp (im * m * π * nu) * besseli (nu, z_adj) # Compute Bessel function with corrected input
74
85
end
75
86
76
87
function integral_var_char (a, VT, dist:: HestonDistribution )
77
- κ, θ, σ, ρ, V0, T, S0, r = dist. κ, dist. θ, dist. σ, dist. ρ, dist . V0, dist. T, dist . S0, dist . r
88
+ κ, θ, σ, V0, T = dist. κ, dist. θ, dist. σ, dist. V0, dist. T
78
89
γ (a) = √ (κ^ 2 - 2 * σ^ 2 * a * im)
79
- d = 4 * θ * κ / σ^ 2
90
+ d = 4 * κ * θ / σ^ 2 # Degrees of freedom
80
91
81
92
ζ (x) = (1 - exp (- x * T)) / x
82
93
first_term = exp (- 0.5 * (γ (a) - κ) * T) * ζ (κ) / ζ (γ (a))
@@ -85,6 +96,10 @@ function integral_var_char(a, VT, dist::HestonDistribution)
85
96
second_term = exp ((V0 + VT) / σ^ 2 * ( η (κ) - η (γ (a)) ))
86
97
87
98
ν (x) = √ (V0 * VT) * 4 * x * exp (- 0.5 * x * T) / σ^ 2 / (1 - exp (- x * T))
99
+
100
+ # println("kappa ", ν( κ))
101
+ # println("gamma: ",γ(a))
102
+ # println("a ",a)
88
103
numerator = besseli_corrected (0.5 * d - 1 , ν (γ (a)))
89
104
denominator = besseli_corrected (0.5 * d - 1 , ν (κ))
90
105
third_term = numerator / denominator
0 commit comments