Make sure the black-scholes implementation works

master
bspeice 2016-03-05 18:40:26 -05:00
parent 531ae12f0e
commit 299c0339e9
4 changed files with 20 additions and 36 deletions

View File

@ -0,0 +1 @@
StatsFuns

View File

@ -1,3 +1,4 @@
__precompile__(true)
"""
Quantitative Finance methods for Julia
@ -7,16 +8,16 @@ want seventeen different versions of the Black-Scholes equation floating
around, and re-writing a Geometric Brownian Motion simulation for every
new project is just tedious.
"""
module Quant.jl
module Quant
export
# Black-Scholes functionality
d1,
d2,
blackscholes_call,
blackscholes_put,
end
include("blackscholes.jl")
end

View File

@ -7,48 +7,26 @@
using StatsFuns
"""
Calculate the value of $d_1$ in the Black-Scholes Formula
Calculate the value of \$d_1\$ in the Black-Scholes Formula
"""
d1 = function(σ, T, t, S, K, r)
return (σ .* sqrt(T-t)).^-1 * (log(S ./ K) + (r + σ.^2 / 2).*(T-t))
function d1(S, K, σ, r, T, t=0)
return (log(S./K) + (r + σ.^2/2).*(T-t)) ./ (σ.*sqrt(T-t))
end
d1 = function(σ, T, S, K, r)
return d1(σ, T, 0, S, K, r)
end
d2 = function(d1_val, σ, T, t)
function d2(d1_val, σ, T, t=0)
return d1_val - σ .* sqrt(T-t)
end
d2 = function(d1_val, σ, T)
return d2(d1_val, σ, T, 0)
end
blackscholes_call = function(σ, T, t, S, K, r)
d1_val = d1(σ, T, t, S, K, r)
function blackscholes_call(S, K, σ, r, T, t=0)
d1_val = d1(S, K, σ, r, T, t)
d2_val = d2(d1_val, σ, T, t)
return normcdf(d1_val) .* S - normcdf(d2_val) .* K .* exp(-r .* (T - t))
end
blackscholes_call = function(σ, T, S, K, r)
d1_val = d1(σ, T, S, K, r)
d2_val = d2(d1_val, σ, T)
return normcdf(d1_val) .* S - normcdf(d2_val) .* K .* exp(-r .* T)
end
blackscholes_put = function(σ, T, t, S, K, r)
d1_val = d1(σ, T, t, S, K, r)
function blackscholes_put(S, K, σ, r, T, t=0)
d1_val = d1(S, K, σ, r, T, t)
d2_val = d2(d1_val, σ, T, t)
return normcdf(-d2_val).*K.*exp(-r.*(T-t)) - normcdf(-d1).*S
end
blackscholes_put = function(σ, T, S, K, r)
d1_val = d1(σ, T, S, K, r)
d2_val = d2(d1_val, σ, T)
return normcdf(-d2_val).*K.*exp(-r.*T) - normcdf(-d1).*S
return normcdf(-d2_val).*K.*exp(-r.*(T-t)) - normcdf(-d1_val).*S
end

View File

@ -1,5 +1,9 @@
using Base.Test
#include("../src/blackscholes.jl")
using Quant
@test_approx blackscholes_call(.25, 2, 1, 100, 95, .05) 15.047
@test 15.047 < blackscholes_call(100, 95, .25, .05, 2, 1) < 15.0471
@test blackscholes_call(100, 95, .25, .05, 2, 1) == blackscholes_call(100, 95, .25, .05, 1)
@test_approx blackscholes_call(.25, 1, 100, 95, .05) blackscholes_call(.25, 2, 1, 100, 95, .05)
@test 5.413 < blackscholes_put(100, 95, .25, .05, 2, 1) < 5.414
@test blackscholes_put(100, 95, .25, .05, 2, 1) == blackscholes_put(100, 95, .25, .05, 1)