Skip to content

Commit

Permalink
BSM tests (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecloudenback committed Jul 18, 2022
1 parent ae8a2b6 commit 0780043
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/equity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ struct BlackScholesMerton{T,U,V} <:EquityModel
end
function nextrate(M::BlackScholesMerton,prior,time,timestep)
r, q, σ = M.r, M.q, M.σ
prior + (r-q)*prior * timestep + prior * M.σ * sqrt(timestep) * randn()
# Hull Options, Futures, & Other Derivatives, 10th ed., pg 470
return (
prior *
exp((r- q - σ^2 / 2) * timestep +
σ * sqrt(timestep) * randn())
)
end

"""
Expand Down
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
EconomicScenarioGenerators = "1d18cbdc-9ca7-45fd-a8b2-b9434f9145be"
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Yields = "d7e99b2f-e7f3-4d9e-9f01-2338fc023ad3"
14 changes: 13 additions & 1 deletion test/equity.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset "EquityModel" begin

@testset "Vasicek" begin
@testset "BlackScholesMerton" begin
m = BlackScholesMerton(0.01,0.02,.15,100.)

s = ScenarioGenerator(
Expand All @@ -19,6 +19,18 @@

@test length(s) == 61

prices = [last(collect(s)) for _ in 1:10_000]

dist = price_distribution(s)

# we expect that the prices are lognormally distributed
# and reject the null hypothesis that they are not if p less than some threshold
# therefore, for test to pass we should fail to reject the null hypothesis and p
# should be large
t = HypothesisTests.ExactOneSampleKSTest(prices,dist)
@test HypothesisTests.pvalue(t) > 0.05


@testset "#14 - odd time steps" begin
m = BlackScholesMerton(0.01,0.02,.15,100.)

Expand Down
6 changes: 3 additions & 3 deletions test/interest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@testset "InterestRateModel" begin

@testset "Vasicek" begin
m = Vasicek(0.136,0.0168,0.0119,Continuous(0.01))
m = Vasicek(0.136,0.0168,0.0119,Yields.Continuous(0.01))
s = ScenarioGenerator(
1., # timestep
30., # projection horizon
Expand All @@ -23,7 +23,7 @@
end

@testset "CoxIngersollRoss" begin
m = CoxIngersollRoss(0.136,0.0168,0.0119,Continuous(0.01))
m = CoxIngersollRoss(0.136,0.0168,0.0119,Yields.Continuous(0.01))
s = ScenarioGenerator(
1., # timestep
30., # projection horizon
Expand Down Expand Up @@ -72,7 +72,7 @@
end

@testset "with Rate" begin
m = HullWhite(.1,.005,Continuous(0.05))
m = HullWhite(.1,.005,Yields.Continuous(0.05))

s = ScenarioGenerator(
1., # timestep
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using EconomicScenarioGenerators
using Test
using Yields
using Distributions
using HypothesisTests

include("utils.jl")
include("interest.jl")
include("equity.jl")
15 changes: 15 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# http://www.timworrall.com/fin-40008/bscholes.pdf

function price_distribution(m::BlackScholesMerton,T)
r, q, σ = m.r, m.q, m.σ
return LogNormal(
log(m.initial) + (r-q-σ^2/2) * T,
sqrt(T) * σ
)
end

function price_distribution(s::ScenarioGenerator{N,T}) where {N,T<:BlackScholesMerton}
m = s.model
t = s.endtime
price_distribution(m,t)
end

2 comments on commit 0780043

@alecloudenback
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: "Tag with name v0.3.0 already exists and points to a different commit"

Please sign in to comment.