Skip to content

Commit

Permalink
Refactor AMC regression calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
FrameConsult authored and sschlenkrich committed Dec 4, 2023
1 parent c7fb3e7 commit b11d880
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/src/payoffs/payoffs.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,5 @@ DiffFusion.string
```

```@docs
DiffFusion.calibrate_regression!
DiffFusion.calibrate_regression
```
26 changes: 26 additions & 0 deletions src/chainrules/payoffs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# do not differentiate AMC regression setting

ChainRulesCore.@non_differentiable reset_regression!(
p::AmcPayoff,
path::Union{AbstractPath, Nothing},
make_regression::Union{Function, Nothing},
)

ChainRulesCore.@non_differentiable reset_regression!(
p::UnaryNode,
path::Union{AbstractPath, Nothing},
make_regression::Union{Function, Nothing},
)

ChainRulesCore.@non_differentiable reset_regression!(
p::BinaryNode,
path::Union{AbstractPath, Nothing},
make_regression::Union{Function, Nothing},
)

ChainRulesCore.@non_differentiable reset_regression!(
p::Union{Leaf, CompoundedRate, Optionlet, Swaption},
path::Union{AbstractPath, Nothing},
make_regression::Union{Function, Nothing},
)
11 changes: 5 additions & 6 deletions src/payoffs/AmcPayoffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ end


"""
calibrate_regression!(links::AmcPayoffLinks, regr::AmcPayoffRegression)
calibrate_regression(links::AmcPayoffLinks, regr::AmcPayoffRegression)
Calibrate the regression for an AMC payoff.
"""
function calibrate_regression!(links::AmcPayoffLinks, regr::AmcPayoffRegression)
function calibrate_regression(links::AmcPayoffLinks, regr::AmcPayoffRegression)
if length(links.z) > 0 && !isnothing(regr.path) && !isnothing(regr.make_regression)
T = zeros(length(regr.path))
for x in links.x
Expand All @@ -92,10 +92,9 @@ function calibrate_regression!(links::AmcPayoffLinks, regr::AmcPayoffRegression)
T = T .* numeraire(regr.path, links.obs_time, links.curve_key)
Z = hcat([ z(regr.path) for z in links.z ]...)'
#
regr.regression = regr.make_regression(Z, T)
return true
return regr.make_regression(Z, T)
end
return false # cannot do calibration
return nothing # cannot calibrate
end

"""
Expand All @@ -105,7 +104,7 @@ Calculate the common components of AMC payoffs for a given valuation path.
"""
function at(links::AmcPayoffLinks, regr::AmcPayoffRegression, path::AbstractPath)
if isnothing(regr.regression) # try to calibrate
calibrate_regression!(links, regr)
regr.regression = calibrate_regression(links, regr)
end
if length(links.z) > 0 && !isnothing(regr.regression)
# use regression to calculate payoff
Expand Down
8 changes: 4 additions & 4 deletions test/unittests/payoffs/amc_payoffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ using Test
p3 = DiffFusion.AmcOne(5.0, x, y, z, path, make_regression, "Std")
p4 = DiffFusion.AmcSum(5.0, x, z, path, make_regression, "Std")
#
@test DiffFusion.calibrate_regression!(p1.links, p1.regr) == true
@test DiffFusion.calibrate_regression!(p2.links, p2.regr) == true
@test DiffFusion.calibrate_regression!(p3.links, p3.regr) == true
@test DiffFusion.calibrate_regression!(p4.links, p4.regr) == true
@test !isnothing(DiffFusion.calibrate_regression(p1.links, p1.regr))
@test !isnothing(DiffFusion.calibrate_regression(p2.links, p2.regr))
@test !isnothing(DiffFusion.calibrate_regression(p3.links, p3.regr))
@test !isnothing(DiffFusion.calibrate_regression(p4.links, p4.regr))
#
DiffFusion.reset_regression!(p1, path, make_regression)
DiffFusion.reset_regression!(p2, path, make_regression)
Expand Down

0 comments on commit b11d880

Please sign in to comment.