Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
correlationtermstructure.cpp
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
20
21using namespace QuantLib;
22
23namespace QuantExt {
24
25CorrelationTermStructure::CorrelationTermStructure(const DayCounter& dc) : TermStructure(dc) {}
26
27CorrelationTermStructure::CorrelationTermStructure(const Date& referenceDate, const Calendar& cal, const DayCounter& dc)
28 : TermStructure(referenceDate, cal, dc) {}
29
30CorrelationTermStructure::CorrelationTermStructure(Natural settlementDays, const Calendar& cal, const DayCounter& dc)
31 : TermStructure(settlementDays, cal, dc) {}
32
33Real CorrelationTermStructure::correlation(Time t, Real strike, bool extrapolate) const {
34 checkRange(t, strike, extrapolate);
35
36 // Fail if correlation is negative
37 Real correlation = correlationImpl(t, strike);
38 QL_REQUIRE(correlation >= -1 && correlation <= 1,
39 "Correlation returned from CorrelationTermStructure must be between -1 and 1 (" << correlation << ")");
40
41 return correlation;
42}
43
44Real CorrelationTermStructure::correlation(const Date& d, Real strike, bool extrapolate) const {
45 return correlation(timeFromReference(d), strike, extrapolate);
46}
47
49 // Default implementation
50 return 0.0;
51}
52
53void CorrelationTermStructure::checkRange(Time t, Real strike, bool extrapolate) const {
54 QL_REQUIRE(extrapolate || allowsExtrapolation() || t >= minTime() || close_enough(t, minTime()),
55 "time (" << t << ") is before min curve time (" << minTime() << ")");
56
57 // Now, do the usual TermStructure checks
58 TermStructure::checkRange(t, extrapolate);
59}
60
62 : CorrelationTermStructure(c->dayCounter()), c_(c) {
63 registerWith(c_);
64}
65
67 return -c_->correlation(t, strike);
68}
69
70CorrelationValue::CorrelationValue(const Handle<CorrelationTermStructure>& correlation, const Time t, const Real strike)
71 : correlation_(correlation), t_(t), strike_(strike) {
72 registerWith(correlation_);
73}
74
76 QL_REQUIRE(!correlation_.empty(), "no source correlation term structure given");
77 return correlation_->correlation(t_, strike_);
78}
79
80bool CorrelationValue::isValid() const { return !correlation_.empty(); }
81
82void CorrelationValue::update() { notifyObservers(); }
83
84} // namespace QuantExt
virtual Time minTime() const
The minimum time for which the curve can return values.
virtual Real correlationImpl(Time t, Real strike) const =0
Correlation calculation.
Real correlation(Time t, Real strike=Null< Real >(), bool extrapolate=false) const
virtual void checkRange(Time t, Real strike, bool extrapolate) const
Extra time range check for minimum time, then calls TermStructure::checkRange.
CorrelationTermStructure(const DayCounter &dc=DayCounter())
Handle< CorrelationTermStructure > correlation_
CorrelationValue(const Handle< CorrelationTermStructure > &correlation, const Time t, const Real strike=Null< Real >())
virtual Real correlationImpl(Time t, Real strike) const override
Correlation calculation.
NegativeCorrelationTermStructure(const Handle< CorrelationTermStructure > &c)
Term structure of correlations.
Filter close_enough(const RandomVariable &x, const RandomVariable &y)