QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
lfmhullwhiteparam.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/math/matrixutilities/pseudosqrt.hpp>
21#include <ql/legacy/libormarketmodels/lfmhullwhiteparam.hpp>
22
23namespace QuantLib {
24
26 const ext::shared_ptr<LiborForwardModelProcess> & process,
27 const ext::shared_ptr<OptionletVolatilityStructure> & capletVol,
28 const Matrix& correlation, Size factors)
29 : LfmCovarianceParameterization(process->size(), factors),
30 diffusion_ (size_-1, factors_),
31 fixingTimes_(process->fixingTimes()) {
32
33 Matrix sqrtCorr(size_-1, factors_, 1.0);
34 if (correlation.empty()) {
35 QL_REQUIRE(factors_ == 1,
36 "correlation matrix must be given for "
37 "multi factor models");
38 } else {
39 QL_REQUIRE( correlation.rows() == size_-1
40 && correlation.rows() == correlation.columns(),
41 "wrong dimesion of the correlation matrix");
42
43 QL_REQUIRE(factors_ <= size_-1,
44 "too many factors for given LFM process");
45
46 Matrix tmpSqrtCorr = pseudoSqrt(correlation,
48
49 // reduce to n factor model
50 // "Reconstructing a valid correlation matrix from invalid data"
51 // (<http://www.quarchome.org/correlationmatrix.pdf>)
52 for (Size i=0; i < size_-1; ++i) {
53 Real p = std::sqrt(std::inner_product(
54 tmpSqrtCorr[i],tmpSqrtCorr[i]+factors_,
55 tmpSqrtCorr[i], Real(0.0)));
56 std::transform(
57 tmpSqrtCorr[i], tmpSqrtCorr[i]+factors_, sqrtCorr[i],
58 [=](Real x) -> Real { return x / p; });
59 }
60 }
61
62 std::vector<Volatility> lambda;
63 const std::vector<Time> fixingTimes = process->fixingTimes();
64 const std::vector<Date> fixingDates = process->fixingDates();
65
66 for (Size i = 1; i < size_; ++i) {
67 Real cumVar = 0.0;
68 for (Size j = 1; j < i; ++j) {
69 cumVar += lambda[i-j-1] * lambda[i-j-1]
70 * (fixingTimes[j+1] - fixingTimes[j]);
71 }
72
73 const Volatility vol = capletVol->volatility(fixingDates[i], 0.0);
74 const Volatility var = vol * vol
75 * capletVol->dayCounter().yearFraction(fixingDates[0],
76 fixingDates[i]);
77
78 lambda.push_back(std::sqrt( (var - cumVar)
79 / (fixingTimes[1] - fixingTimes[0])) );
80
81 for (Size q=0; q<factors_; ++q) {
82 diffusion_[i-1][q] = sqrtCorr[i-1][q] * lambda.back();
83 }
84 }
85
87 }
88
89
91 return std::upper_bound(fixingTimes_.begin(), fixingTimes_.end(), t)
92 - fixingTimes_.begin();
93 }
94
95
97 Matrix tmp(size_, factors_, 0.0);
98 const Size m = nextIndexReset(t);
99
100 for (Size k=m; k<size_; ++k) {
101 for (Size q=0; q<factors_; ++q) {
102 tmp[k][q] = diffusion_[k-m][q];
103 }
104 }
105 return tmp;
106 }
107
109 Matrix tmp(size_, size_, 0.0);
110 const Size m = nextIndexReset(t);
111
112 for (Size k=m; k<size_; ++k) {
113 for (Size i=m; i<size_; ++i) {
114 tmp[k][i] = covariance_[k-m][i-m];
115 }
116 }
117
118 return tmp;
119 }
120
122
123 Matrix tmp(size_, size_, 0.0);
124
125 Size last = std::lower_bound(fixingTimes_.begin(),
126 fixingTimes_.end(), t)
127 - fixingTimes_.begin();
128
129 for (Size i=0; i<last; ++i) {
130 const Time dt = ((i+1<last)? fixingTimes_[i+1] : t )
131 - fixingTimes_[i];
132
133 for (Size k=i; k<size_-1; ++k) {
134 for (Size l=i; l<size_-1; ++l) {
135 tmp[k+1][l+1]+= covariance_[k-i][l-i]*dt;
136 }
137 }
138 }
139
140 return tmp;
141 }
142
143}
144
1-D array used in linear algebra.
Definition: array.hpp:52
Libor market model parameterization
Matrix covariance(Time t, const Array &x=Null< Array >()) const override
Matrix diffusion(Time t, const Array &x=Null< Array >()) const override
Matrix integratedCovariance(Time t, const Array &x=Null< Array >()) const override
LfmHullWhiteParameterization(const ext::shared_ptr< LiborForwardModelProcess > &process, const ext::shared_ptr< OptionletVolatilityStructure > &capletVol, const Matrix &correlation=Matrix(), Size factors=1)
Matrix used in linear algebra.
Definition: matrix.hpp:41
bool empty() const
Definition: matrix.hpp:520
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
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
Matrix pseudoSqrt(const Matrix &matrix, SalvagingAlgorithm::Type sa)
Definition: pseudosqrt.cpp:347
Matrix transpose(const Matrix &m)
Definition: matrix.hpp:700