Skip to content

Commit

Permalink
switch FinanceModels to Extension (#45)
Browse files Browse the repository at this point in the history
* switch FInanceModels to Extension

* tweaks

* wrap up
  • Loading branch information
alecloudenback committed Jan 30, 2024
1 parent b47354d commit 2603bc9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
9 changes: 7 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
name = "EconomicScenarioGenerators"
uuid = "1d18cbdc-9ca7-45fd-a8b2-b9434f9145be"
authors = ["Alec Loudenback <alecloudenback@gmail.com> and contributors"]
version = "0.5.1"
version = "0.6.0"

[weakdeps]
FinanceModels = "77f2ae65-bdde-421f-ae9d-22f1af19dd76"

[deps]
Copulas = "ae264745-0b69-425e-9d9d-cf662c5eec93"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
FinanceCore = "b9b1ffdd-6612-4b69-8227-7663be06e089"
FinanceModels = "77f2ae65-bdde-421f-ae9d-22f1af19dd76"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Transducers = "28d57a85-8fef-5791-bfe6-a80928e7c999"

[extensions]
FinanceModelsExt = "FinanceModels"

[compat]
Copulas = "0.1"
FinanceCore = "2"
Expand Down
52 changes: 52 additions & 0 deletions ext/FinanceModelsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module FinanceModelsExt
import FinanceModels
import FinanceCore
import ForwardDiff

using EconomicScenarioGenerators
const ESG = EconomicScenarioGenerators

export YieldCurve

# make this an extension w/ FinanceModels?
function ESG.θ(M::ESG.HullWhite{T}, time, timestep) where {T<:Union{FinanceModels.Yield.AbstractYieldModel,FinanceCore.Rate}}
# https://quantpie.co.uk/srm/hull_white_sr.php
# https://quant.stackexchange.com/questions/8724/how-to-calibrate-hull-white-from-zero-curve
# https://mdpi-res.com/d_attachment/mathematics/mathematics-08-01719/article_deploy/mathematics-08-01719-v2.pdf?version=1603181408
a = M.a
f(t) = log(FinanceCore.discount(M.curve, t[1]))
δf = -only(ForwardDiff.hessian(f, [time]))::Float64
f_t = -only(ForwardDiff.gradient(f, [time]))::Float64

return δf + f_t * a + M.σ^2 / (2 * a) * (1 - exp(-2 * a * time))

end

function ESG.YieldCurve(sg::ESG.ScenarioGenerator{M,N,T,R}; model=FinanceModels.Spline.Linear()) where {M,N,T<:ESG.InterestRateModel,R}
times = sg.timestep:sg.timestep:(sg.endtime+sg.timestep)
# compute the accumulated discount factor (ZCB price)
zeros = cumsum(FinanceCore.rate.(sg .* sg.timestep)) ./ times

zero_vec = clamp.(zeros, 0.00001, 1)
return FinanceModels.Yield.Spline(model, times, zero_vec)

end

function ESG.YieldCurve(c::C; model=FinanceModels.Spline.Linear()) where {C<:ESG.Correlated}
fsg = first(c.sg)
Δt = fsg.timestep
times = Δt:Δt:(fsg.endtime+Δt)
# compute the accumulated discount factor (ZCB price)
rs = collect(c)
ycs = map(eachindex(c.sg)) do i
r = [x[i] for x in rs]
zeros = cumsum(FinanceCore.rate.(r .* Δt)) ./ times

zero_vec = clamp.(zeros, 0.00001, 1)

FinanceModels.Yield.Spline(model, times, zero_vec)
end

return ycs
end
end
5 changes: 2 additions & 3 deletions src/EconomicScenarioGenerators.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module EconomicScenarioGenerators

import ForwardDiff
import FinanceModels
import FinanceCore
using Transducers
using Transducers: @next, complete, __foldl__, asfoldable, next
Expand Down Expand Up @@ -129,10 +128,10 @@ end
__initial_value(sg::ScenarioGenerator) = __initial_value(sg.model, sg.timestep)
__initial_value(m, timestep) = m.initial

YieldCurve() = error("Must have FinanceModels imported and call this function on a ScenarioGenerator.")

include("Yields.jl")
export Vasicek, CoxIngersollRoss, HullWhite,
BlackScholesMerton, ConstantElasticityofVariance,
ScenarioGenerator, YieldCurve, Correlated
ScenarioGenerator, Correlated, YieldCurve

end
27 changes: 0 additions & 27 deletions src/Yields.jl

This file was deleted.

13 changes: 0 additions & 13 deletions src/interest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,6 @@ function nextvalue(M::HullWhite{T}, prior, time, timestep, variate) where {T}
prior + (θ_t - M.a * prior) * timestep + M.σ * (timestep) * variate
end

# make this an extension w/ FinanceModels?
function θ(M::HullWhite{T}, time, timestep) where {T<:Union{FinanceModels.Yield.AbstractYieldModel,FinanceCore.Rate}}
# https://quantpie.co.uk/srm/hull_white_sr.php
# https://quant.stackexchange.com/questions/8724/how-to-calibrate-hull-white-from-zero-curve
# https://mdpi-res.com/d_attachment/mathematics/mathematics-08-01719/article_deploy/mathematics-08-01719-v2.pdf?version=1603181408
a = M.a
f(t) = log(FinanceCore.discount(M.curve, t[1]))
δf = -only(ForwardDiff.hessian(f, [time]))::Float64
f_t = -only(ForwardDiff.gradient(f, [time]))::Float64

return δf + f_t * a + M.σ^2 / (2 * a) * (1 - exp(-2 * a * time))

end

function θ(M::HullWhite{T}, time, timestep) where {T<:Real}
# https://quantpie.co.uk/srm/hull_white_sr.php
Expand Down

2 comments on commit 2603bc9

@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.

Registration pull request created: JuliaRegistries/General/99814

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.0 -m "<description of version>" 2603bc9ec40f3816617bb2645d15e6b2b16e853c
git push origin v0.6.0

Please sign in to comment.