Add initial Quant.jl code for Black-Scholes

master
bspeice 2016-03-05 17:49:30 -05:00
commit 531ae12f0e
5 changed files with 82 additions and 0 deletions

0
REQUIRE Normal file
View File

22
src/Quant.jl Normal file
View File

@ -0,0 +1,22 @@
"""
Quantitative Finance methods for Julia
Designed to implement many helpful methods that are often repeated; we don't
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
export
# Black-Scholes functionality
d1,
d2,
blackscholes_call,
blackscholes_put,
include("blackscholes.jl")
end

54
src/blackscholes.jl Normal file
View File

@ -0,0 +1,54 @@
###
# Black-Scholes model functionality for Julia.
#
# Designed to be a reference implementation of the Black-Scholes option
# pricing formula, supporting the original formula and greeks calculation
###
using StatsFuns
"""
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))
end
d1 = function(σ, T, S, K, r)
return d1(σ, T, 0, S, K, r)
end
d2 = function(d1_val, σ, T, t)
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)
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)
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
end

5
test/blackscholes.jl Normal file
View File

@ -0,0 +1,5 @@
using Quant
@test_approx blackscholes_call(.25, 2, 1, 100, 95, .05) 15.047
@test_approx blackscholes_call(.25, 1, 100, 95, .05) blackscholes_call(.25, 2, 1, 100, 95, .05)

1
test/runtests.jl Normal file
View File

@ -0,0 +1 @@
include("blackscholes.jl")