Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
basecorrelationstructure.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 Quaternion Risk Management Ltd
3
4 This file is part of ORE, a free-software/open-source library
5 for transparent pricing and risk analysis - http://opensourcerisk.org
6
7 ORE is free software: you can redistribute it and/or modify it
8 under the terms of the Modified BSD License. You should have received a
9 copy of the license along with this program.
10 The license is also available online at <http://opensourcerisk.org>
11
12 This program is distributed on the basis that it will form a useful
13 contribution to risk analytics and model standardisation, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
16*/
17
18#include <ql/time/schedule.hpp>
20
21namespace QuantExt {
22
24 : CorrelationTermStructure(), bdc_(Unadjusted) {}
25
26BaseCorrelationTermStructure::BaseCorrelationTermStructure(const Date& refDate, const Calendar& cal,
27 BusinessDayConvention bdc, const std::vector<Period>& tenors,
28 const std::vector<Real>& detachmentPoints,
29 const DayCounter& dc, const Date& startDate,
30 boost::optional<DateGeneration::Rule> rule)
31 : CorrelationTermStructure(refDate, cal, dc), bdc_(bdc), startDate_(startDate), rule_(rule), tenors_(tenors), detachmentPoints_(detachmentPoints) {
32 // Ensure tenors are sorted and positive
33 validate();
35}
36
37BaseCorrelationTermStructure::BaseCorrelationTermStructure(Natural settlementDays, const Calendar& cal,
38 BusinessDayConvention bdc, const std::vector<Period>& tenors,
39 const std::vector<double>& detachmentPoints,
40 const DayCounter& dc, const Date& startDate,
41 boost::optional<DateGeneration::Rule> rule)
42 : CorrelationTermStructure(settlementDays, cal, dc), bdc_(bdc), startDate_(startDate), rule_(rule), tenors_(tenors),
43 detachmentPoints_(detachmentPoints) {
44 validate();
46}
47
49 Period prevTenor(0, Days);
50 for (size_t i = 0; i < tenors_.size(); ++i) {
51 QL_REQUIRE(tenors_[i] > prevTenor, "Tenors need to be sorted and larger than 0 * Days");
52 }
53
54 // Ensure detachmentpoints are sorted and between 0.0 and 1.0
55 double prevDetachmentPoint = 0.0;
56 for (size_t i = 0; i < detachmentPoints_.size(); ++i) {
57 QL_REQUIRE(detachmentPoints_[i] > prevDetachmentPoint,
58 "Detachmentpoints need to be sorted and between (0, 1].");
59 QL_REQUIRE(detachmentPoints_[i] < 1.0 || QuantLib::close_enough(detachmentPoints_[i], 1.0),
60 "Detachmentpoints need to be sorted and between (0, 1].");
61 }
62}
63
65 const Date& refDate = referenceDate();
66 Date start = startDate_ == Date() ? refDate : startDate_;
67 Calendar cldr = calendar();
68
69 for (Size i = 0; i < tenors_.size(); i++) {
70 Date d;
71 if (rule_) {
72 d = start + tenors_[i];
73 if (*rule_ == DateGeneration::CDS2015 || *rule_ == DateGeneration::CDS ||
74 *rule_ == DateGeneration::OldCDS) {
75 d = cdsMaturity(start, tenors_[i], *rule_);
76 }
77 } else {
78 d = cldr.advance(start, tenors_[i], bdc_);
79 }
80
81 dates_.push_back(d);
82 times_.push_back(timeFromReference(d));
83
84 QL_REQUIRE(!dates_.empty(), "no dates left after removing expired dates");
85 }
86}
87
88void BaseCorrelationTermStructure::checkRange(Time t, Real strike, bool extrapolate) const {
89 bool extrapolationNeeded =
90 t < minTime() || t > maxTime() || strike < minDetachmentPoint() || strike > maxDetachmentPoint();
91 QL_REQUIRE(extrapolate || allowsExtrapolation() || !extrapolationNeeded,
92 "No extrapolation allowed, require t = "
93 << t << " to be between (" << minTime() << ", " << maxTime() << ") and detachmentPoint = " << strike
94 << " to be between (" << minDetachmentPoint() << ", " << maxDetachmentPoint() << ").");
95}
96
97} // namespace QuantExt
abstract base correlation structure and an 2d-interpolated base correlation structure
virtual void checkRange(Time t, Real strike, bool extrapolate) const override
Extra time range check for minimum time, then calls TermStructure::checkRange.
boost::optional< DateGeneration::Rule > rule_
virtual Time minTime() const override
The minimum time for which the curve can return values.