QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
tapcorrelations.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
21#include <cmath>
22
23namespace QuantLib {
24
26 Size matrixSize,
27 Size rank) {
28
29 // what if rank == 1?
30 QL_REQUIRE((rank-1) * (2*matrixSize - rank) == 2*angles.size(),
31 "rank-1) * (matrixSize - rank/2) == angles.size()");
32 Matrix m(matrixSize, matrixSize);
33
34 // first row filling
35 m[0][0] = 1.0;
36 for (Size j=1; j<matrixSize; ++j)
37 m[0][j] = 0.0;
38
39 // next ones...
40 Size k = 0; //angles index
41 for (Size i=1; i<m.rows(); ++i) {
42 Real sinProduct = 1.0;
43 Size bound = std::min(i,rank-1);
44 for (Size j=0; j<bound; ++j) {
45 m[i][j] = std::cos(angles[k]);
46 m[i][j] *= sinProduct;
47 sinProduct *= std::sin(angles[k]);
48 ++k;
49 }
50 m[i][bound] = sinProduct;
51 for (Size j=bound+1; j<m.rows(); ++j)
52 m[i][j] = 0;
53 }
54 return m;
55 }
56
58 Size matrixSize,
59 Size) {
60 Matrix m(matrixSize, matrixSize);
61 for (Size i=0; i<m.rows(); ++i) {
62 Real cosPhi, sinPhi;
63 if (i>0) {
64 cosPhi = std::cos(angles[i-1]);
65 sinPhi = std::sin(angles[i-1]);
66 } else {
67 cosPhi = 1.0;
68 sinPhi = 0.0;
69 }
70
71 for (Size j=0; j<i; ++j)
72 m[i][j] = sinPhi * m[i-1][j];
73
74 m[i][i] = cosPhi;
75
76 for (Size j=i+1; j<m.rows(); ++j)
77 m[i][j] = 0.0;
78 }
79 return m;
80 }
81
83 Size matrixSize,
84 Size rank) {
85 Array angles(x.size());
86 //we convert the unconstrained parameters in angles
87 for (Size i = 0; i < x.size(); ++i)
88 angles[i] = M_PI*.5 - std::atan(x[i]);
89 return triangularAnglesParametrization(angles, matrixSize, rank);
90 }
91
93 Size matrixSize,
94 Size rank) {
95 Array angles(x.size());
96 //we convert the unconstrained parameters in angles
97 for (Size i = 0; i < x.size(); ++i)
98 angles[i] = M_PI*.5 - std::atan(x[i]);
99 return lmmTriangularAnglesParametrization(angles, matrixSize, rank);
100 }
101
103 Real epsilon, Size matrixSize) {
104 Matrix m(matrixSize, 3);
105 for (Size i=0; i<m.rows(); ++i) {
106 Real t = t0 * (1 - std::exp(epsilon*Real(i)));
107 Real phi = std::atan(alpha * t);
108 m[i][0] = std::cos(t)*std::cos(phi);
109 m[i][1] = std::sin(t)*std::cos(phi);
110 m[i][2] = -std::sin(phi);
111 }
112 return m;
113 }
114
116 Size nbRows) {
117 QL_REQUIRE(parameters.size() == 3,
118 "the parameter array must contain exactly 3 values" );
119 return triangularAnglesParametrizationRankThree(parameters[0],
120 parameters[1],
121 parameters[2],
122 nbRows);
123
124 }
125
127 Array temp = values(x);
128 return DotProduct(temp, temp);
129 }
130
132 Array result((target_.rows()*(target_.columns()-1))/2);
133 Matrix pseudoRoot = f_(x, matrixSize_, rank_);
134 Matrix differences = pseudoRoot * transpose(pseudoRoot) - target_;
135 Size k = 0;
136 // then we store the elementwise differences in a vector.
137 for (Size i=0; i<target_.rows(); ++i) {
138 for (Size j=0; j<i; ++j){
139 result[k] = differences[i][j];
140 ++k;
141 }
142 }
143 return result;
144 }
145}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Real value(const Array &x) const override
method to overload to compute the cost function value in x
Array values(const Array &x) const override
method to overload to compute the cost function values in x
ext::function< Matrix(const Array &, Size, Size)> f_
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
#define M_PI
Definition: any.hpp:35
Matrix lmmTriangularAnglesParametrization(const Array &angles, Size matrixSize, Size)
Matrix triangularAnglesParametrizationRankThree(Real alpha, Real t0, Real epsilon, Size matrixSize)
Returns the rank reduced Triangular Angles Parametrized correlation matrix.
Matrix triangularAnglesParametrization(const Array &angles, Size matrixSize, Size rank)
Returns the Triangular Angles Parametrized correlation matrix.
Matrix triangularAnglesParametrizationRankThreeVectorial(const Array &parameters, Size nbRows)
Matrix triangularAnglesParametrizationUnconstrained(const Array &x, Size matrixSize, Size rank)
Matrix lmmTriangularAnglesParametrizationUnconstrained(const Array &x, Size matrixSize, Size rank)
Matrix transpose(const Matrix &m)
Definition: matrix.hpp:700
Real DotProduct(const Array &v1, const Array &v2)
Definition: array.hpp:553
Real alpha
Definition: sabr.cpp:200