mirror of
https://github.com/bspeice/Quant.jl
synced 2025-07-04 23:35:12 -04:00
Add initial Quant.jl code for Black-Scholes
This commit is contained in:
22
src/Quant.jl
Normal file
22
src/Quant.jl
Normal 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
54
src/blackscholes.jl
Normal 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
|
Reference in New Issue
Block a user