Coverage Report

Created: 2025-12-08 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/volatility/extendedblackvariancecurve.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Frank Hövermann
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/extendedblackvariancecurve.hpp>
21
#include <ql/math/interpolations/linearinterpolation.hpp>
22
#include <utility>
23
24
namespace QuantLib {
25
26
    ExtendedBlackVarianceCurve::ExtendedBlackVarianceCurve(const Date& referenceDate,
27
                                                           const std::vector<Date>& dates,
28
                                                           std::vector<Handle<Quote> > volatilities,
29
                                                           DayCounter dayCounter,
30
                                                           bool forceMonotoneVariance)
31
0
    : BlackVarianceTermStructure(referenceDate), dayCounter_(std::move(dayCounter)),
32
0
      maxDate_(dates.back()), volatilities_(std::move(volatilities)),
33
0
      forceMonotoneVariance_(forceMonotoneVariance) {
34
0
        QL_REQUIRE(dates.size() == volatilities_.size(),
35
0
                   "size mismatch between dates and volatilities");
36
37
0
        QL_REQUIRE(dates[0] > referenceDate,
38
0
                   "cannot have dates_[0] <= referenceDate");
39
40
0
        variances_ = std::vector<Real>(dates.size()+1);
41
0
        times_ = std::vector<Time>(dates.size()+1);
42
43
0
        times_[0] = 0.0;
44
0
        for (Size j=1; j<=dates.size(); ++j) {
45
0
            times_[j] = timeFromReference(dates[j-1]);
46
0
            QL_REQUIRE(times_[j]>times_[j-1],
47
0
                       "dates must be sorted unique!");
48
0
        }
49
50
0
        setVariances();
51
0
        setInterpolation<Linear>();
52
53
0
        for (auto& volatilitie : volatilities_)
54
0
            registerWith(volatilitie);
55
0
    }
Unexecuted instantiation: QuantLib::ExtendedBlackVarianceCurve::ExtendedBlackVarianceCurve(QuantLib::Date const&, std::__1::vector<QuantLib::Date, std::__1::allocator<QuantLib::Date> > const&, std::__1::vector<QuantLib::Handle<QuantLib::Quote>, std::__1::allocator<QuantLib::Handle<QuantLib::Quote> > >, QuantLib::DayCounter, bool)
Unexecuted instantiation: QuantLib::ExtendedBlackVarianceCurve::ExtendedBlackVarianceCurve(QuantLib::Date const&, std::__1::vector<QuantLib::Date, std::__1::allocator<QuantLib::Date> > const&, std::__1::vector<QuantLib::Handle<QuantLib::Quote>, std::__1::allocator<QuantLib::Handle<QuantLib::Quote> > >, QuantLib::DayCounter, bool)
56
57
0
    void ExtendedBlackVarianceCurve::setVariances() {
58
0
        variances_[0] = 0.0;
59
0
        for (Size j=1; j<=volatilities_.size(); j++) {
60
0
            Volatility sigma = volatilities_[j-1]->value();
61
0
            variances_[j] = times_[j] * sigma * sigma;
62
0
            QL_REQUIRE(variances_[j]>=variances_[j-1]
63
0
                       || !forceMonotoneVariance_,
64
0
                       "variance must be non-decreasing");
65
0
        }
66
0
    }
67
68
0
    void ExtendedBlackVarianceCurve::update() {
69
0
        setVariances();
70
0
        varianceCurve_.update();
71
0
        notifyObservers();
72
0
    }
73
74
0
    Real ExtendedBlackVarianceCurve::blackVarianceImpl(Time t, Real) const {
75
0
        if (t<=times_.back()) {
76
0
            return varianceCurve_(t, true);
77
0
        } else {
78
0
            return varianceCurve_(times_.back(), true)*t/times_.back();
79
0
        }
80
0
    }
81
82
}
83