QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
abcdvol.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006, 2007 Ferdinando Ametrano
5 Copyright (C) 2006 Mark Joshi
6 Copyright (C) 2005, 2006 Klaus Spanderen
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
26
27using std::vector;
28
29namespace QuantLib {
30
32 Real b,
33 Real c,
34 Real d,
35 const vector<Real>& ks,
36 const ext::shared_ptr<PiecewiseConstantCorrelation>& corr,
37 const EvolutionDescription& evolution,
38 const Size numberOfFactors,
39 const vector<Rate>& initialRates,
40 const vector<Spread>& displacements)
41 : numberOfFactors_(numberOfFactors),
42 numberOfRates_(initialRates.size()),
43 numberOfSteps_(evolution.evolutionTimes().size()),
44 initialRates_(initialRates),
45 displacements_(displacements),
46 evolution_(evolution),
47 pseudoRoots_(numberOfSteps_, Matrix(numberOfRates_, numberOfFactors_))
48 {
49 const vector<Time>& rateTimes = evolution.rateTimes();
50 QL_REQUIRE(numberOfRates_==rateTimes.size()-1,
51 "mismatch between number of rates (" << numberOfRates_ <<
52 ") and rate times");
54 "mismatch between number of rates (" << numberOfRates_ <<
55 ") and displacements (" << displacements.size() << ")");
56 QL_REQUIRE(numberOfRates_==ks.size(),
57 "mismatch between number of rates (" << numberOfRates_ <<
58 ") and ks (" << ks.size() << ")");
59 // QL_REQUIRE(numberOfRates_<=numberOfFactors_*numberOfSteps_,
60 // "number of rates (" << numberOfRates_ <<
61 // ") greater than number of factors (" << numberOfFactors_
62 // << ") times number of steps (" << numberOfSteps_ << ")");
64 "number of factors (" << numberOfFactors <<
65 ") cannot be greater than numberOfRates (" <<
66 numberOfRates_ << ")");
68 "number of factors (" << numberOfFactors <<
69 ") must be greater than zero");
70
71 AbcdFunction abcd(a, b, c, d);
72 Time effStopTime = 0.0;
73 const vector<Time>& corrTimes = corr->times();
74 const vector<Time>& evolTimes = evolution.evolutionTimes();
76 for (Size k=0, kk=0; k<numberOfSteps_; ++k) {
77 // one covariance per evolution step
78 std::fill(covariance.begin(), covariance.end(), 0.0);
79
80 // there might be more than one correlation matrix
81 // in a single evolution step,
82 for (; corrTimes[kk]<evolTimes[k]; ++kk) {
83 Time effStartTime = effStopTime;
84 effStopTime = corrTimes[kk];
85 const Matrix& corrMatrix = corr->correlation(kk);
86 for (Size i=0; i<numberOfRates_; ++i) {
87 for (Size j=i; j<numberOfRates_; ++j) {
88 Real cov = ks[i]*ks[j]* abcd.covariance(effStartTime,
89 effStopTime,
90 rateTimes[i],
91 rateTimes[j]);
92 covariance[i][j] += cov * corrMatrix[i][j];
93 }
94 }
95 }
96 // last part in the evolution step
97 Time effStartTime = effStopTime;
98 effStopTime = evolTimes[k];
99 const Matrix& corrMatrix = corr->correlation(kk);
100 for (Size i=0; i<numberOfRates_; ++i) {
101 for (Size j=i; j<numberOfRates_; ++j) {
102 Real cov = ks[i]*ks[j]* abcd.covariance(effStartTime,
103 effStopTime,
104 rateTimes[i],
105 rateTimes[j]);
106 covariance[i][j] += cov * corrMatrix[i][j];
107 }
108 }
109 // no more use for the kk-th correlation matrix
110 while (kk<corrTimes.size() && corrTimes[kk]<=evolTimes[k])
111 ++kk;
112
113 // make it symmetric
114 for (Size i=0; i<numberOfRates_; ++i) {
115 for (Size j=i+1; j<numberOfRates_; ++j) {
116 covariance[j][i] = covariance[i][j];
117 }
118 }
119
121 numberOfFactors, 1.0,
123
125 "step " << k
126 << " abcd vol wrong number of rows: "
127 << pseudoRoots_[k].rows()
128 << " instead of " << numberOfRates_);
130 "step " << k
131 << " abcd vol wrong number of columns: "
132 << pseudoRoots_[k].columns()
133 << " instead of " << numberOfFactors);
134 }
135 }
136
137}
Abcd functional form for instantaneous volatility
Definition: abcd.hpp:34
Real covariance(Time t, Time T, Time S) const
Definition: abcd.cpp:43
const std::vector< Spread > & displacements() const override
Definition: abcdvol.hpp:72
AbcdVol(Real a, Real b, Real c, Real d, const std::vector< Real > &ks, const ext::shared_ptr< PiecewiseConstantCorrelation > &corr, const EvolutionDescription &evolution, Size numberOfFactors, const std::vector< Rate > &initialRates, const std::vector< Spread > &displacements)
Definition: abcdvol.cpp:31
std::vector< Matrix > pseudoRoots_
Definition: abcdvol.hpp:63
const EvolutionDescription & evolution() const override
Definition: abcdvol.hpp:76
Size numberOfFactors() const override
Definition: abcdvol.hpp:84
Market-model evolution description.
const std::vector< Time > & rateTimes() const
const std::vector< Time > & evolutionTimes() const
virtual const Matrix & covariance(Size i) const
Definition: marketmodel.cpp:28
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_iterator begin() const
Definition: matrix.hpp:327
const_iterator end() const
Definition: matrix.hpp:335
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
ext::function< Real(Real)> b
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 rankReducedSqrt(const Matrix &matrix, Size maxRank, Real componentRetainedPercentage, SalvagingAlgorithm::Type sa)
Definition: pseudosqrt.cpp:427
pseudo square root of a real symmetric matrix