QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
expcorrelations.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Marco Bianchetti
6 Copyright (C) 2007 Giorgio Facchinetti
7 Copyright (C) 2007 François du Vignaud
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23#include <ql/math/comparison.hpp>
24#include <ql/models/marketmodels/correlations/expcorrelations.hpp>
25#include <ql/models/marketmodels/correlations/timehomogeneousforwardcorrelation.hpp>
26#include <ql/models/marketmodels/utilities.hpp>
27#include <ql/utilities/dataformatters.hpp>
28#include <utility>
29
30namespace QuantLib {
31
32 Matrix exponentialCorrelations(const std::vector<Time>& rateTimes,
33 Real longTermCorr,
34 Real beta,
35 Real gamma,
36 Time time) {
37 // preliminary checks
38 checkIncreasingTimes(rateTimes);
39 QL_REQUIRE(longTermCorr<=1.0 && longTermCorr>=0.0,
40 "Long term correlation (" << longTermCorr <<
41 ") outside [0;1] interval");
42 QL_REQUIRE(beta>=0.0,
43 "beta (" << beta <<
44 ") must be greater than zero");
45 QL_REQUIRE(gamma<=1.0 && gamma>=0.0,
46 "gamma (" << gamma <<
47 ") outside [0;1] interval");
48
49 // Calculate correlation matrix
50 Size nbRows = rateTimes.size()-1;
51 Matrix correlations(nbRows, nbRows, 0.0);
52 for (Size i=0; i<nbRows; ++i) {
53 // correlation is defined only between
54 // (alive) stochastic rates...
55 if (time<=rateTimes[i]) {
56 correlations[i][i] = 1.0;
57 for (Size j=0; j<i; ++j) {
58 if (time<=rateTimes[j]) {
59 correlations[i][j] = correlations[j][i] =
60 longTermCorr + (1.0-longTermCorr) *
61 std::exp(-beta*std::fabs(
62 std::pow(rateTimes[i]-time, gamma) -
63 std::pow(rateTimes[j]-time, gamma)
64 )
65 );
66 }
67 }
68 }
69 }
70 return correlations;
71 }
72
73
75 Real longTermCorr,
76 Real beta,
77 Real gamma,
78 std::vector<Time> times)
79 : numberOfRates_(rateTimes.empty() ? 0 : rateTimes.size() - 1), longTermCorr_(longTermCorr),
80 beta_(beta), gamma_(gamma), rateTimes_(rateTimes), times_(std::move(times)) {
81
82 QL_REQUIRE(numberOfRates_>1,
83 "Rate times must contain at least two values");
84
86
87 // corrTimes must include all rateTimes but the last
88 if (times_.empty())
89 times_ = std::vector<Time>(rateTimes_.begin(),
90 rateTimes_.end()-1);
91 else
93
94 if (close(gamma,1.0)) {
95 std::vector<Time> temp(rateTimes_.begin(), rateTimes_.end()-1);
96 QL_REQUIRE(times_==temp,
97 "corr times " << io::sequence(times_)
98 << " must be equal to (all) rate times (but the last) "
99 << io::sequence(temp));
101 rateTimes_, longTermCorr_, beta_, 1.0, 0.0);
104 } else {
105 // FIXME should check here that all rateTimes but the last
106 // are included in rateTimes
107 QL_REQUIRE(times_.back()<=rateTimes_[numberOfRates_],
108 "last corr time " << times_.back() <<
109 "is after next-to-last rate time " <<
111 correlations_.resize(times_.size());
112 Time time = times_[0]/2.0;
115 for (Size k=1; k<times_.size(); ++k) {
116 time = (times_[k]+times_[k-1])/2.0;
119 }
120 }
121 }
122
123 const std::vector<Time>&
125 return times_;
126 }
127
128 const std::vector<Time>&
130 return rateTimes_;
131 }
132
133 const std::vector<Matrix>&
135 return correlations_;
136 }
137
139 return numberOfRates_;
140 }
141
142}
const std::vector< Time > & rateTimes() const override
const std::vector< Time > & times() const override
ExponentialForwardCorrelation(const std::vector< Time > &rateTimes, Real longTermCorr=0.5, Real beta=0.2, Real gamma=1.0, std::vector< Time > times=std::vector< Time >())
const std::vector< Matrix > & correlations() const override
Matrix used in linear algebra.
Definition: matrix.hpp:41
static std::vector< Matrix > evolvedMatrices(const Matrix &fwdCorrelation)
detail::sequence_holder< typename Container::const_iterator > sequence(const Container &c)
output STL-compliant containers as space-separated sequences
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Matrix exponentialCorrelations(const std::vector< Time > &rateTimes, Real longTermCorr, Real beta, Real gamma, Time time)
void checkIncreasingTimes(const std::vector< Time > &times)
check for strictly increasing times, first time greater than zero
Definition: utilities.cpp:92
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
STL namespace.