QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
lfmcovarproxy.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006 Klaus Spanderen
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
20#include <ql/legacy/libormarketmodels/lfmcovarproxy.hpp>
21#include <ql/math/integrals/kronrodintegral.hpp>
22#include <utility>
23
24namespace QuantLib {
25 LfmCovarianceProxy::LfmCovarianceProxy(ext::shared_ptr<LmVolatilityModel> volaModel,
26 const ext::shared_ptr<LmCorrelationModel>& corrModel)
27
28 : LfmCovarianceParameterization(corrModel->size(), corrModel->factors()),
29 volaModel_(std::move(volaModel)), corrModel_(corrModel) {
30
31 QL_REQUIRE(volaModel_->size() == corrModel_->size(),
32 "different size for the volatility (" << volaModel_->size() <<
33 ") and correlation (" << corrModel_->size() <<
34 ") models");
35 }
36
37 ext::shared_ptr<LmVolatilityModel>
39 return volaModel_;
40 }
41
42 ext::shared_ptr<LmCorrelationModel>
44 return corrModel_;
45 }
46
48
49 Matrix pca = corrModel_->pseudoSqrt(t, x);
50 Array vol = volaModel_->volatility(t, x);
51 for (Size i=0; i<size_; ++i) {
52 std::transform(pca.row_begin(i), pca.row_end(i),
53 pca.row_begin(i),
54 [&](Real x) -> Real { return x * vol[i]; });
55 }
56
57 return pca;
58 }
59
61
62 Array volatility = volaModel_->volatility(t, x);
63 Matrix correlation = corrModel_->correlation(t, x);
64
65 Matrix tmp(size_, size_);
66 for (Size i=0; i<size_; ++i) {
67 for (Size j=0; j<size_; ++j) {
68 tmp[i][j] = volatility[i]*correlation[i][j]*volatility[j];
69 }
70 }
71
72 return tmp;
73 }
74
75 class LfmCovarianceProxy::Var_Helper {
76 public:
77 Var_Helper(const LfmCovarianceProxy* proxy, Size i, Size j);
78
79 Real operator()(Real t) const;
80 private:
81 const Size i_, j_;
82 const LmVolatilityModel* const volaModel_;
83 const LmCorrelationModel* const corrModel_;
84 };
85
86 LfmCovarianceProxy::Var_Helper::Var_Helper(const LfmCovarianceProxy* proxy,
87 Size i, Size j)
88 : i_(i),
89 j_(j),
90 volaModel_(proxy->volaModel_.get()),
91 corrModel_(proxy->corrModel_.get()) {
92 }
93
94 Real LfmCovarianceProxy::Var_Helper::operator()(Real t) const {
95 Volatility v1, v2;
96
97 if (i_ == j_) {
98 v1 = v2 = volaModel_->volatility(i_, t);
99 } else {
100 v1 = volaModel_->volatility(i_, t);
101 v2 = volaModel_->volatility(j_, t);
102 }
103
104 return v1 * corrModel_->correlation(i_, j_, t) * v2;
105 }
106
108 Size i, Size j, Time t, const Array& x) const {
109
110 if (corrModel_->isTimeIndependent()) {
111 try {
112 // if all objects support these methods
113 // thats by far the fastest way to get the
114 // integrated covariance
115 return corrModel_->correlation(i, j, 0.0, x)
116 * volaModel_->integratedVariance(j, i, t, x);
117 }
118 catch (Error&) {
119 // okay proceed with the
120 // slow numerical integration routine
121 }
122 }
123
124 QL_REQUIRE(x.empty(), "can not handle given x here");
125
126 Real tmp=0.0;
127 Var_Helper helper(this, i, j);
128
129 GaussKronrodAdaptive integrator(1e-10, 10000);
130 for (Size k=0; k<64; ++k) {
131 tmp+=integrator(helper, k*t/64., (k+1)*t/64.);
132 }
133 return tmp;
134 }
135
136}
137
1-D array used in linear algebra.
Definition: array.hpp:52
bool empty() const
whether the array is empty
Definition: array.hpp:499
Base error class.
Definition: errors.hpp:39
Integral of a 1-dimensional function using the Gauss-Kronrod methods.
Libor market model parameterization
proxy for a libor forward model covariance parameterization
Matrix covariance(Time t, const Array &x=Null< Array >()) const override
Matrix diffusion(Time t, const Array &x=Null< Array >()) const override
ext::shared_ptr< LmVolatilityModel > volatilityModel() const
ext::shared_ptr< LmCorrelationModel > correlationModel() const
virtual Real integratedCovariance(Size i, Size j, Time t, const Array &x=Null< Array >()) const
const ext::shared_ptr< LmCorrelationModel > corrModel_
LfmCovarianceProxy(ext::shared_ptr< LmVolatilityModel > volaModel, const ext::shared_ptr< LmCorrelationModel > &corrModel)
const ext::shared_ptr< LmVolatilityModel > volaModel_
libor forward correlation model
Definition: lmcorrmodel.hpp:35
caplet volatility model
Definition: lmvolmodel.hpp:33
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_row_iterator row_begin(Size i) const
Definition: matrix.hpp:360
const_row_iterator row_end(Size i) const
Definition: matrix.hpp:378
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.