QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
marketmodel.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 François du Vignaud
6 Copyright (C) 2007 Mark Joshi
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
22#include <ql/models/marketmodels/marketmodel.hpp>
23#include <ql/models/marketmodels/evolutiondescription.hpp>
24
25
26namespace QuantLib {
27
29 if (covariance_.empty()) {
30 covariance_.resize(numberOfSteps());
31 for (Size j=0; j<numberOfSteps(); ++j)
33 }
34 QL_REQUIRE(i<covariance_.size(),
35 "i (" << i <<
36 ") must be less than covariance_.size() (" << covariance_.size() << ")");
37 return covariance_[i];
38 }
39
40 const Matrix& MarketModel::totalCovariance(Size endIndex) const {
41 if (totalCovariance_.empty()) {
43 // call to covariance(0) triggers calculation, if necessary
44 // while covariance_[0] would not
46 for (Size j=1; j<numberOfSteps(); ++j)
48 }
49 QL_REQUIRE(endIndex<covariance_.size(),
50 "endIndex (" << endIndex <<
51 ") must be less than covariance_.size() (" << covariance_.size() << ")");
52 return totalCovariance_[endIndex];
53 }
54
55 std::vector<Volatility> MarketModel::timeDependentVolatility(Size i) const
56 {
57 QL_REQUIRE(i<numberOfRates(),
58 "index (" << i << ") must less than number of rates (" <<
59 numberOfRates() << ")");
60
61 std::vector<Volatility> result(numberOfSteps());
62 const std::vector<Time>& evolutionTime = evolution().evolutionTimes();
63
64 Time lastTime=0.0;
65 for (Size j=0; j<numberOfSteps(); ++j) {
66 Time tau = evolutionTime[j]-lastTime;
67 Real thisVariance = covariance(j)[i][i];
68 Real thisVol = std::sqrt(thisVariance/tau);
69 result[j] = thisVol;
70 lastTime = evolutionTime[j];
71 }
72 return result;
73 }
74
75}
const std::vector< Time > & evolutionTimes() const
std::vector< Volatility > timeDependentVolatility(Size i) const
Definition: marketmodel.cpp:55
virtual const Matrix & covariance(Size i) const
Definition: marketmodel.cpp:28
std::vector< Matrix > totalCovariance_
Definition: marketmodel.hpp:53
virtual const Matrix & totalCovariance(Size endIndex) const
Definition: marketmodel.cpp:40
std::vector< Matrix > covariance_
Definition: marketmodel.hpp:53
virtual const Matrix & pseudoRoot(Size i) const =0
virtual Size numberOfSteps() const =0
virtual const EvolutionDescription & evolution() const =0
virtual Size numberOfRates() const =0
Matrix used in linear algebra.
Definition: matrix.hpp:41
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 transpose(const Matrix &m)
Definition: matrix.hpp:700