Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/volatility/noarbsabrinterpolatedsmilesection.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2014 Peter Caspers
5
6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8
9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy of the license along with this program; if not, please email
12
 <quantlib-dev@lists.sf.net>. The license is also available online at
13
 <https://www.quantlib.org/license.shtml>.
14
15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
#include <ql/experimental/volatility/noarbsabrinterpolatedsmilesection.hpp>
21
#include <ql/quotes/simplequote.hpp>
22
#include <ql/settings.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    NoArbSabrInterpolatedSmileSection::NoArbSabrInterpolatedSmileSection(
28
        const Date& optionDate,
29
        Handle<Quote> forward,
30
        const std::vector<Rate>& strikes,
31
        bool hasFloatingStrikes,
32
        Handle<Quote> atmVolatility,
33
        const std::vector<Handle<Quote> >& volHandles,
34
        Real alpha,
35
        Real beta,
36
        Real nu,
37
        Real rho,
38
        bool isAlphaFixed,
39
        bool isBetaFixed,
40
        bool isNuFixed,
41
        bool isRhoFixed,
42
        bool vegaWeighted,
43
        ext::shared_ptr<EndCriteria> endCriteria,
44
        ext::shared_ptr<OptimizationMethod> method,
45
        const DayCounter& dc)
46
0
    : SmileSection(optionDate, dc), forward_(std::move(forward)),
47
0
      atmVolatility_(std::move(atmVolatility)), volHandles_(volHandles), strikes_(strikes),
48
0
      actualStrikes_(strikes), hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()),
49
0
      alpha_(alpha), beta_(beta), nu_(nu), rho_(rho), isAlphaFixed_(isAlphaFixed),
50
0
      isBetaFixed_(isBetaFixed), isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed),
51
0
      vegaWeighted_(vegaWeighted), endCriteria_(std::move(endCriteria)),
52
0
      method_(std::move(method)) {
53
54
0
        LazyObject::registerWith(forward_);
55
0
        LazyObject::registerWith(atmVolatility_);
56
0
        for (auto& volHandle : volHandles_)
57
0
            LazyObject::registerWith(volHandle);
58
0
    }
Unexecuted instantiation: QuantLib::NoArbSabrInterpolatedSmileSection::NoArbSabrInterpolatedSmileSection(QuantLib::Date const&, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<double, std::__1::allocator<double> > const&, bool, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<QuantLib::Handle<QuantLib::Quote>, std::__1::allocator<QuantLib::Handle<QuantLib::Quote> > > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&)
Unexecuted instantiation: QuantLib::NoArbSabrInterpolatedSmileSection::NoArbSabrInterpolatedSmileSection(QuantLib::Date const&, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<double, std::__1::allocator<double> > const&, bool, QuantLib::Handle<QuantLib::Quote>, std::__1::vector<QuantLib::Handle<QuantLib::Quote>, std::__1::allocator<QuantLib::Handle<QuantLib::Quote> > > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&)
59
60
    NoArbSabrInterpolatedSmileSection::NoArbSabrInterpolatedSmileSection(
61
        const Date& optionDate,
62
        const Rate& forward,
63
        const std::vector<Rate>& strikes,
64
        bool hasFloatingStrikes,
65
        const Volatility& atmVolatility,
66
        const std::vector<Volatility>& volHandles,
67
        Real alpha,
68
        Real beta,
69
        Real nu,
70
        Real rho,
71
        bool isAlphaFixed,
72
        bool isBetaFixed,
73
        bool isNuFixed,
74
        bool isRhoFixed,
75
        bool vegaWeighted,
76
        ext::shared_ptr<EndCriteria> endCriteria,
77
        ext::shared_ptr<OptimizationMethod> method,
78
        const DayCounter& dc)
79
0
    : SmileSection(optionDate, dc),
80
0
      forward_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(forward)))),
81
0
      atmVolatility_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(atmVolatility)))),
82
0
      volHandles_(volHandles.size()), strikes_(strikes), actualStrikes_(strikes),
83
0
      hasFloatingStrikes_(hasFloatingStrikes), vols_(volHandles.size()), alpha_(alpha), beta_(beta),
84
0
      nu_(nu), rho_(rho), isAlphaFixed_(isAlphaFixed), isBetaFixed_(isBetaFixed),
85
0
      isNuFixed_(isNuFixed), isRhoFixed_(isRhoFixed), vegaWeighted_(vegaWeighted),
86
0
      endCriteria_(std::move(endCriteria)), method_(std::move(method)) {
87
88
0
        for (Size i = 0; i < volHandles_.size(); ++i)
89
0
            volHandles_[i] = Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(volHandles[i])));
90
0
    }
Unexecuted instantiation: QuantLib::NoArbSabrInterpolatedSmileSection::NoArbSabrInterpolatedSmileSection(QuantLib::Date const&, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&)
Unexecuted instantiation: QuantLib::NoArbSabrInterpolatedSmileSection::NoArbSabrInterpolatedSmileSection(QuantLib::Date const&, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double const&, std::__1::vector<double, std::__1::allocator<double> > const&, double, double, double, double, bool, bool, bool, bool, bool, boost::shared_ptr<QuantLib::EndCriteria>, boost::shared_ptr<QuantLib::OptimizationMethod>, QuantLib::DayCounter const&)
91
92
0
    void NoArbSabrInterpolatedSmileSection::createInterpolation() const {
93
0
         ext::shared_ptr<NoArbSabrInterpolation> tmp(new NoArbSabrInterpolation(
94
0
                     actualStrikes_.begin(), actualStrikes_.end(), vols_.begin(),
95
0
                     exerciseTime(), forwardValue_,
96
0
                     alpha_, beta_, nu_, rho_,
97
0
                     isAlphaFixed_, isBetaFixed_, isNuFixed_, isRhoFixed_, vegaWeighted_,
98
0
                     endCriteria_, method_));
99
0
         swap(tmp, noArbSabrInterpolation_);
100
0
    }
101
102
0
    void NoArbSabrInterpolatedSmileSection::performCalculations() const {
103
0
        forwardValue_ = forward_->value();
104
0
        vols_.clear();
105
0
        actualStrikes_.clear();
106
        // we populate the volatilities, skipping the invalid ones
107
0
        for (Size i=0; i<volHandles_.size(); ++i) {
108
0
            if (volHandles_[i]->isValid()) {
109
0
                if (hasFloatingStrikes_) {
110
0
                    actualStrikes_.push_back(forwardValue_ + strikes_[i]);
111
0
                    vols_.push_back(atmVolatility_->value() + volHandles_[i]->value());
112
0
                } else {
113
0
                    actualStrikes_.push_back(strikes_[i]);
114
0
                    vols_.push_back(volHandles_[i]->value());
115
0
                }
116
0
            }
117
0
        }
118
        // we are recreating the sabrinterpolation object unconditionnaly to
119
        // avoid iterator invalidation
120
0
        createInterpolation();
121
0
        noArbSabrInterpolation_->update();
122
0
    }
123
124
0
    Real NoArbSabrInterpolatedSmileSection::varianceImpl(Real strike) const {
125
0
        calculate();
126
0
        Real v = (*noArbSabrInterpolation_)(strike, true);
127
0
        return v*v*exerciseTime();
128
0
    }
129
130
}
131