Last active
October 19, 2017 21:26
-
-
Save bkamins/7ecb8bf493cac8d6f6f3c141eedbaf4b to your computer and use it in GitHub Desktop.
Asian option pricing in Julia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function v_asian_sample(T, r, K, σ, X₀, m::Integer) | |
X = X₀ | |
x̂ = zero(X) | |
Δ = T/m | |
for i in 1:m | |
X *= exp((r-σ^2/2)*Δ + σ*√Δ*randn()) | |
x̂ += X | |
end | |
exp(-r*T)*max(x̂/m - K, 0) | |
end | |
function v_asian_sample_vec(T, r, K, σ, X₀, m::Integer, X) | |
Δ = T/m | |
randn!(X) | |
X .*= σ*√Δ | |
X .+= (r-σ^2/2)*Δ | |
X .= exp.(cumsum!(X, X)) | |
exp(-r*T)*max(mean(X)*X₀ - K, 0) | |
end | |
function v_asian_loop(T, r, K, σ, X₀, m, n, fun) | |
if fun == v_asian_sample | |
mean(fun(T, r, K, σ, X₀, m) for i in 1:n) | |
else | |
X = Vector{Float64}(m) | |
mean(fun(T, r, K, σ, X₀, m, X) for i in 1:n) | |
end | |
end | |
function v_asian(T, r, K, σ, X₀, m, n, fun) | |
v_asian_loop(promote(T, r, K, σ, X₀)..., m, n, fun) | |
end | |
for f in [v_asian_sample, v_asian_sample_vec], i in 1:2 | |
println(f) | |
@time v_asian(1, 0.05, 55, 0.3, 50, 1000, 100_000, f) | |
end | |
# Output: | |
# | |
# v_asian_sample | |
# 1.733317 seconds (69.79 k allocations: 3.802 MiB) | |
# v_asian_sample | |
# 1.591367 seconds (8 allocations: 128 bytes) | |
# v_asian_sample_vec | |
# 2.861599 seconds (1.29 M allocations: 38.848 MiB, 2.02% gc time) | |
# v_asian_sample_vec | |
# 2.344178 seconds (800.01 k allocations: 12.215 MiB, 2.51% gc time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment