QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
marketmodeldifferences.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) 2007 François du Vignaud
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
24
25namespace QuantLib {
26
27 std::vector<Volatility> rateVolDifferences(
28 const MarketModel& marketModel1,
29 const MarketModel& marketModel2) {
30 QL_ENSURE(marketModel1.initialRates() == marketModel2.initialRates(),
31 "initialRates do not match");
32 const EvolutionDescription& evolutionDescription1
33 = marketModel1.evolution();
34 const EvolutionDescription& evolutionDescription2
35 = marketModel2.evolution();
36 QL_ENSURE(evolutionDescription1.evolutionTimes()
37 == evolutionDescription2.evolutionTimes(),
38 "Evolution times do not match");
39
40 const Matrix& totalCovariance1
41 = marketModel1.totalCovariance(marketModel1.numberOfSteps()-1);
42 const Matrix& totalCovariance2
43 = marketModel2.totalCovariance(marketModel2.numberOfSteps()-1);
44 const std::vector<Time>& maturities =
45 evolutionDescription1.evolutionTimes();
46
47 std::vector<Volatility> result(totalCovariance1.columns());
48 for (Size i=0; i<totalCovariance1.columns(); ++i) {
49 Real diff = totalCovariance1[i][i]-totalCovariance2[i][i];
50 result[i] = std::sqrt(diff/maturities[i]);
51 }
52 return result;
53 }
54
55 std::vector<Spread> rateInstVolDifferences(
56 const MarketModel& marketModel1,
57 const MarketModel& marketModel2,
58 Size index) {
59 QL_ENSURE(marketModel1.initialRates() == marketModel2.initialRates(),
60 "initialRates do not match");
61 const EvolutionDescription& evolutionDescription1
62 = marketModel1.evolution();
63 const EvolutionDescription& evolutionDescription2
64 = marketModel2.evolution();
65 QL_ENSURE(evolutionDescription1.evolutionTimes()
66 == evolutionDescription2.evolutionTimes(),
67 "Evolution times do not match");
68 QL_ENSURE(index<evolutionDescription1.numberOfSteps(),
69 "the index given is greater than the number of steps");
70
71 const std::vector<Time>& evolutionTimes
72 = evolutionDescription1.evolutionTimes();
73 std::vector<Spread> result(evolutionTimes.size());
74
75 Time previousEvolutionTime = 0;
76 for (Size i=0; i<evolutionTimes.size(); ++i) {
77 Time currentEvolutionTime = evolutionTimes[i];
78 Time dt = currentEvolutionTime - previousEvolutionTime;
79 const Matrix& covariance1 = marketModel1.covariance(i);
80 const Matrix& covariance2 = marketModel2.covariance(i);
81 Real diff = covariance1[index][index] - covariance2[index][index];
82 result[i] = std::sqrt(diff/dt);
83 previousEvolutionTime = currentEvolutionTime;
84 }
85 return result;
86 }
87
88 std::vector<Matrix> coterminalSwapPseudoRoots(
89 const PiecewiseConstantCorrelation& piecewiseConstantCorrelation,
90 const std::vector<ext::shared_ptr<PiecewiseConstantVariance> >&
91 piecewiseConstantVariances) {
92 QL_ENSURE(piecewiseConstantCorrelation.times()
93 == piecewiseConstantVariances.front()->rateTimes(),
94 "correlations and volatilities intertave");
95 std::vector<Matrix> pseudoRoots;
96 const std::vector<Time>& rateTimes
97 = piecewiseConstantVariances.front()->rateTimes();
98 for (Size i=1; i<rateTimes.size(); ++i) {
99 Time sqrtTau = std::sqrt(rateTimes[i]-rateTimes[i-1]);
100 const Matrix& correlations
101 = piecewiseConstantCorrelation.correlation(i);
102 Matrix pseudoRoot(correlations.rows(), correlations.rows());
103 for (Size j=0; j<correlations.rows(); ++j) {
104 Real volatility
105 = piecewiseConstantVariances[j]->volatility(i)*sqrtTau;
106 std::transform(correlations.row_begin(j),
107 correlations.row_end(j),
108 pseudoRoot.row_begin(j),
109 [=](Real x) -> Real { return x * volatility; });
110 }
111 pseudoRoots.push_back(pseudoRoot);
112 }
113 return pseudoRoots;
114 }
115
116}
117
Market-model evolution description.
const std::vector< Time > & evolutionTimes() const
base class for market models
Definition: marketmodel.hpp:39
virtual const Matrix & covariance(Size i) const
Definition: marketmodel.cpp:28
virtual const Matrix & totalCovariance(Size endIndex) const
Definition: marketmodel.cpp:40
virtual const std::vector< Rate > & initialRates() const =0
virtual Size numberOfSteps() const =0
virtual const EvolutionDescription & evolution() const =0
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_row_iterator row_begin(Size i) const
Definition: matrix.hpp:360
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
const_row_iterator row_end(Size i) const
Definition: matrix.hpp:378
virtual const Matrix & correlation(Size i) const
virtual const std::vector< Time > & times() const =0
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
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
std::vector< Matrix > coterminalSwapPseudoRoots(const PiecewiseConstantCorrelation &piecewiseConstantCorrelation, const std::vector< ext::shared_ptr< PiecewiseConstantVariance > > &piecewiseConstantVariances)
std::vector< Spread > rateInstVolDifferences(const MarketModel &marketModel1, const MarketModel &marketModel2, Size index)
std::vector< Volatility > rateVolDifferences(const MarketModel &marketModel1, const MarketModel &marketModel2)