Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
interpolatedcorrelationcurve.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file interpolatedcorrelationcurve.hpp
20 \brief interpolated correlation term structure
21*/
22
23#ifndef quantext_interpolated_correlation_curve_hpp
24#define quantext_interpolated_correlation_curve_hpp
25
26#include <ql/math/interpolations/backwardflatinterpolation.hpp>
27#include <ql/math/interpolations/linearinterpolation.hpp>
28#include <ql/patterns/lazyobject.hpp>
29#include <ql/termstructures/interpolatedcurve.hpp>
31
32namespace QuantExt {
33using namespace QuantLib;
34
35//! CorrelationTermStructure based on interpolation of correlations
36/*! \ingroup correlationtermstructures */
37template <class Interpolator>
39 protected InterpolatedCurve<Interpolator>,
40 public LazyObject {
41public:
42 /*! InterpolatedCorrelationCurve has floating referenceDate (Settings::evaluationDate())
43 */
44 InterpolatedCorrelationCurve(const std::vector<Time>& times, const std::vector<Handle<Quote> >& correlations,
45 const DayCounter& dayCounter, const Calendar& calendar,
46 const Interpolator& interpolator = Interpolator());
47 //@}
48 //! \name TermStructure interface
49 //@{
50 Date maxDate() const override { return Date::maxDate(); } // flat extrapolation
51 Time maxTime() const override { return QL_MAX_REAL; }
52 //@}
53 //! \name Observer interface
54 //@{
55 void update() override;
56 //@}
57private:
58 //! \name LazyObject interface
59 //@{
60 void performCalculations() const override;
61 //@}
62protected:
63 //! \name CorrelationTermStructure implementation
64 //@{
65 Real correlationImpl(Time t, Real) const override;
66 //@}
67 std::vector<Handle<Quote> > quotes_;
68};
69
70// inline definitions
71
72#ifndef __DOXYGEN__
73
74// template definitions
75
76template <class T> Real InterpolatedCorrelationCurve<T>::correlationImpl(Time t, Real) const {
77 calculate();
78 if (t <= this->times_[0]) {
79 return this->data_[0];
80 } else if (t <= this->times_.back()) {
81 return this->interpolation_(t, true);
82 }
83 // flat extrapolation
84 return this->data_.back();
85}
86
87template <class T>
89 const std::vector<Handle<Quote> >& quotes,
90 const DayCounter& dayCounter, const Calendar& calendar,
91 const T& interpolator)
92 : CorrelationTermStructure(0, calendar, dayCounter), InterpolatedCurve<T>(std::vector<Time>(), std::vector<Real>(),
93 interpolator),
94 quotes_(quotes) {
95
96 QL_REQUIRE(times.size() > 1, "too few times: " << times.size());
97 this->times_.resize(times.size());
98 this->times_[0] = times[0];
99 for (Size i = 1; i < times.size(); i++) {
100 QL_REQUIRE(times[i] > times[i - 1], "times not sorted");
101 this->times_[i] = times[i];
102 }
103
104 QL_REQUIRE(this->quotes_.size() == this->times_.size(),
105 "quotes/times count mismatch: " << this->quotes_.size() << " vs " << this->times_.size());
106
107 // initalise data vector, values are copied from quotes in performCalculations()
108 this->data_.resize(this->times_.size());
109 for (Size i = 0; i < this->times_.size(); i++)
110 this->data_[0] = 0.0;
111
112 this->interpolation_ =
113 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
114 this->interpolation_.update();
115
116 // register with each of the quotes
117 for (Size i = 0; i < this->quotes_.size(); i++) {
118 QL_REQUIRE(std::fabs(this->quotes_[i]->value()) <= 1.0,
119 "correlation not in range (-1.0,1.0): " << this->data_[i]);
120
121 registerWith(this->quotes_[i]);
122 }
123}
124
125template <class T> inline void InterpolatedCorrelationCurve<T>::update() {
126 LazyObject::update();
127 CorrelationTermStructure::update();
128}
129
130template <class T> inline void InterpolatedCorrelationCurve<T>::performCalculations() const {
131
132 for (Size i = 0; i < this->times_.size(); ++i)
133 this->data_[i] = quotes_[i]->value();
134 this->interpolation_ =
135 this->interpolator_.interpolate(this->times_.begin(), this->times_.end(), this->data_.begin());
136 this->interpolation_.update();
137}
138
139#endif
140
143} // namespace QuantExt
144
145#endif
CorrelationTermStructure based on interpolation of correlations.
void performCalculations() const override
Real correlationImpl(Time t, Real) const override
Correlation calculation.
InterpolatedCorrelationCurve(const std::vector< Time > &times, const std::vector< Handle< Quote > > &correlations, const DayCounter &dayCounter, const Calendar &calendar, const Interpolator &interpolator=Interpolator())
Term structure of correlations.
InterpolatedCorrelationCurve< BackwardFlat > BackwardFlatCorrelationCurve
InterpolatedCorrelationCurve< Linear > PiecewiseLinearCorrelationCurve