Skip to content

Commit

Permalink
Merge pull request #24 from auto-differentiation-dev/feature/measure-…
Browse files Browse the repository at this point in the history
…bermudan

Adds capability to measure runtime for AAD Bermudan Swaption implementation
  • Loading branch information
auto-differentiation-dev authored Oct 2, 2024
2 parents 69aa4ad + 14a354f commit a1fcf4f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions Examples/AdjointBermudanSwaption/AdjointBermudanSwaptionXAD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ TODO: Use the implicit function theorem for calibration with AAD.
#include <ql/utilities/dataformatters.hpp>
#include <iomanip>
#include <iostream>
#include <chrono>

using namespace QuantLib;

Expand Down Expand Up @@ -181,6 +182,7 @@ Real priceWithSensi(const std::vector<Integer>& swapLengths,
Size numCols,
Real flatRate,
std::vector<Real>& gradient) {
tape.clearAll();
// register the independent inputs
auto swaptionVols_t = swaptionVols;
tape.registerInputs(swaptionVols_t);
Expand All @@ -194,6 +196,8 @@ Real priceWithSensi(const std::vector<Integer>& swapLengths,
tape.computeAdjoints();

// store adjoints in gradient vector
gradient.clear();
gradient.reserve(swaptionVols_t.size());
for (auto& vol : swaptionVols_t) {
gradient.push_back(derivative(vol));
}
Expand All @@ -217,10 +221,12 @@ void printResults(Real value, const std::vector<Real>& gradient) {
std::cout << std::endl;
}

int main(int, char*[]) {
int main(int argc, char* argv[]) {

try {

const int N = argc < 2 ? 1 : std::atol(argv[1]);

std::cout << std::endl;

// rate
Expand All @@ -243,9 +249,22 @@ int main(int, char*[]) {
#else
std::cout << "Pricing Bermudan swaption with sensitivities...\n";
std::vector<Real> gradient;
Real price =
priceWithSensi(swapLengths, swaptionVols, numRows, numCols, flatRate, gradient);

Real price = 0.0;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; ++i) {
price =
priceWithSensi(swapLengths, swaptionVols, numRows, numCols, flatRate, gradient);
}
auto end = std::chrono::high_resolution_clock::now();
auto time =
static_cast<double>(
std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()) *
1e-3 / N;
printResults(price, gradient);

std::cout << std::fixed << std::setprecision(9);
std::cout << "For " << N << " repetitions, it took on average " << time << " ms\n";
#endif

return 0;
Expand Down

0 comments on commit a1fcf4f

Please sign in to comment.