Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new sample pricing a portfolio of LIBOR swaptions with derivatives #126

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ endif()

# add a sample - link to XAD and add common to include path
function(xad_add_sample name)
add_executable(${name} ${ARGN})
cmake_parse_arguments(ARGS "" "" "SOURCES;TEST_ARGS" ${ARGN})

if(NOT ARGS_SOURCES)
message(FATAL_ERROR "The SOURCES argument must be given for samples")
endif()

add_executable(${name} ${ARGS_SOURCES})
target_link_libraries(${name} PRIVATE XAD::xad)
target_include_directories(${name} PRIVATE ../common)
get_target_property(msvcrt XAD::xad MSVC_RUNTIME_LIBRARY)
if(MSVC AND msvcrt)
set_target_properties(${name} PROPERTIES MSVC_RUNTIME_LIBRARY "${msvcrt}")
endif()
if(NOT XAD_SAMPLES_MAIN)
add_test(NAME sample_${name} COMMAND ${name})
add_test(NAME sample_${name} COMMAND ${name} ${ARGS_TEST_ARGS})
set_target_properties(${name} PROPERTIES FOLDER "samples")
if(XAD_ENABLE_ADDRESS_SANITIZER)
target_compile_options(${name} PRIVATE ${xad_cxx_asan_flags})
Expand All @@ -72,5 +78,6 @@ add_subdirectory(SwapPricer)
add_subdirectory(fwd_adj_2nd)
add_subdirectory(Hessian)
add_subdirectory(Jacobian)
add_subdirectory(LiborSwaptionPricer)


2 changes: 1 addition & 1 deletion samples/Hessian/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
#
##############################################################################

xad_add_sample(Hessian main.cpp)
xad_add_sample(Hessian SOURCES main.cpp)
2 changes: 1 addition & 1 deletion samples/Jacobian/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
#
##############################################################################

xad_add_sample(Jacobian main.cpp)
xad_add_sample(Jacobian SOURCES main.cpp)
36 changes: 36 additions & 0 deletions samples/LiborSwaptionPricer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
##############################################################################
#
# Cmake file for 1st order adjoint mode for a Monte-Carlo Libor Swaption
# portfolio pricer.
#
# This file is part of XAD, a comprehensive C++ library for
# automatic differentiation.
#
# Copyright (C) 2010-2024 Xcelerit Computing Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

xad_add_sample(LiborSwaptionPricer
SOURCES
LiborData.hpp
LiborFunctions.hpp
LiborPricers.hpp
LiborPricers.cpp
main.cpp
TEST_ARGS
100
test
)
56 changes: 56 additions & 0 deletions samples/LiborSwaptionPricer/LiborData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************

Data structures used to price a portfolio of LIBOR swaptions.

This file is part of XAD, a comprehensive C++ library for
automatic differentiation.

The code is an adapted version of Prof. Mike Giles, available here:
https://people.maths.ox.ac.uk/~gilesm/codes/libor_AD/testlinadj.cpp

Copyright (C) 2010-2024 Xcelerit Computing Ltd.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/

#pragma once

#include <vector>


/// Represents portfolio of swaptions, with maturities and corresponding
/// swap rates
struct SwaptionPortfolio
{
std::vector<double> swaprates;
std::vector<int> maturities;
};

/// Market parameters required for pricing
struct MarketParameters
{
double delta = 0.;
std::vector<double> lambda;
std::vector<double> L0;
};

/// Holds portfolio price and derivatives
struct Results
{
double price = 0.;
double d_delta = 0.;
std::vector<double> d_L0;
std::vector<double> d_lambda;
};
101 changes: 101 additions & 0 deletions samples/LiborSwaptionPricer/LiborFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*******************************************************************************

Functions used to price a portfolio of LIBOR swaptions.

This file is part of XAD, a comprehensive C++ library for
automatic differentiation.

The code is an adapted version of Prof. Mike Giles, available here:
https://people.maths.ox.ac.uk/~gilesm/codes/libor_AD/testlinadj.cpp

Copyright (C) 2010-2024 Xcelerit Computing Ltd.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/

#pragma once

#include <vector>

//////////////////// Pricing functions ////////////////
// Defined as templates, so that they can be used ///
// with active and passive types
///////////////////////////////////////////////////////

/// Path generation - calculates LIBOR rates at the given times,
/// Based on Gaussian randdom numbers
template <class ADT>
void path_gen(const ADT& delta, std::vector<ADT>& L, const std::vector<ADT>& lambda,
const std::vector<double>& z)
{
using std::exp;
using std::sqrt;

for (size_t n = 0; n < z.size(); n++)
{
ADT sqez = sqrt(delta) * z[n];

ADT v = 0.0;
for (size_t i = n + 1; i < L.size(); i++)
{
ADT lam = lambda[i - n - 1];
ADT con1 = delta * lam;
v += (con1 * L[i]) / (1.0 + delta * L[i]);
L[i] *= exp(con1 * v + lam * (sqez - 0.5 * con1));
}
}
}

/// Value the swap portfolio for the given LIBOR rates
template <class ADT>
ADT value_portfolio(const ADT delta, const std::vector<int>& maturities,
const std::vector<double>& swaprates, const std::vector<ADT>& L,
std::vector<ADT>& Btmp, std::vector<ADT>& Stmp)
{
const size_t NN = L.size();
const size_t N = NN / 2;
const size_t Nopt = swaprates.size();

// temporaries, passed as parameters to avoid re-allocating
Btmp.resize(NN);
Stmp.resize(NN);

ADT b = 1.0;
ADT s = 0.0;

for (size_t n = N; n < NN; ++n)
{
b = b / (1.0 + delta * L[n]);
s = s + delta * b;
Btmp[n] = b;
Stmp[n] = s;
}

ADT v = 0.0;

for (size_t i = 0; i < Nopt; i++)
{
int m = maturities[i] + N - 1;
ADT swapval = Btmp[m] + swaprates[i] * Stmp[m] - 1.0;
if (swapval < 0.)
v += -100.0 * swapval;
}

// apply discount

for (size_t n = 0; n < N; n++) v = v / (1.0 + delta * L[n]);

return v;
}
Loading
Loading