Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
correlationmatrix.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/utilities/correlationmatrix.hpp
20 \brief configuration class for building correlation matrices
21 \ingroup utilities
22*/
23
24#pragma once
25
26#include <map>
28#include <ql/math/matrix.hpp>
29#include <ql/quote.hpp>
30
31namespace ore {
32namespace data {
33
34/*! Struct for holding information on a factor in the correlation matrix. For example <code>{ IR, "EUR", 0 }</code>
35 is the first factor in the EUR interest rate process.
36*/
39 std::string name;
40 QuantLib::Size index;
41};
42
43//! \name Compare <code>CorrelationFactor</code>s.
44//@{
45bool operator<(const CorrelationFactor& lhs, const CorrelationFactor& rhs);
46bool operator==(const CorrelationFactor& lhs, const CorrelationFactor& rhs);
47bool operator!=(const CorrelationFactor& lhs, const CorrelationFactor& rhs);
48//@}
49
50//! Allow <code>CorrelationFactor</code>s to be written.
51std::ostream& operator<<(std::ostream& out, const CorrelationFactor& f);
52
53/*! Parse a correlation factor \p name. For example, a \p name like \c IR:EUR is parsed to a \c CorrelationFactor with
54 \c type, \c name and \c index set to \c IR, \c EUR and \c 0 respectively. Note that the name is of the form
55 \c type:name and the index is always set to 0 initially. The actual index is set separately.
56*/
57CorrelationFactor parseCorrelationFactor(const std::string& name, const char separator = ':');
58
59/*! The key for storing the correlation data is the pair of factors.
60*/
61typedef std::pair<CorrelationFactor, CorrelationFactor> CorrelationKey;
62
63/*! Correlation matrix builder class
64
65 Can be loaded with sets of individual correlations as pairs and will build a required correlation matrix.
66
67 \ingroup utilities
68*/
70public:
72
73 //! Clear all data
74 void reset();
75
76 //! \name Add Correlations
77 //@{
78 /*! Method to add a correlation between \p factor1 and \p factor2. The factor string is of the form
79 \c type:name and it is assumed that the factor belongs to a process driven by one factor. For example,
80 \c IR:EUR would refer to the single factor driving the EUR interest rate process.
81
82 For processes driven by more than one factor, use the \c addCorrelation methods below that take a
83 \c CorrelationFactor.
84 */
85 void addCorrelation(const std::string& factor1, const std::string& factor2, QuantLib::Real correlation);
86
87 //! \copydoc CorrelationMatrixBuilder::addCorrelation(const std::string&,const std::string&,QuantLib::Real)
88 void addCorrelation(const std::string& factor1, const std::string& factor2,
89 const QuantLib::Handle<QuantLib::Quote>& correlation);
90
91 //! Add correlation between factor \p f_1 and \p f_2.
92 void addCorrelation(const CorrelationFactor& f_1, const CorrelationFactor& f_2, QuantLib::Real correlation);
93
94 //! Add correlation quote between factor \p f_1 and \p f_2.
96 const QuantLib::Handle<QuantLib::Quote>& correlation);
97 //@}
98
99 //! \name Correlation Matrix
100 /*! For any of the correlation matrices returned below, if it is asked for a correlation between two factors
101 that has not been added to the matrix builder, it will return 0.0 and not raise an exception.
102 */
103 //@{
104 /*! Return a \f$2n-1\f$ square matrix for an IR/FX model, where \f$n\f$ is the number of currencies in the
105 \p ccys argument. This assumes that \c ccys[0] is the base currency.
106 */
107 QuantLib::Matrix correlationMatrix(const std::vector<std::string>& ccys);
108
109 /*! Return a \f$2n-1+m\f$ square matrix for an IR/FX/INF model, where \f$n\f$ is the number of currencies in the
110 \p ccys argument and \f$m\f$ is the number of inflation indices in the \p infIndices argument. This assumes
111 that \c ccys[0] is the base currency.
112 */
113 QuantLib::Matrix correlationMatrix(const std::vector<std::string>& ccys,
114 const std::vector<std::string>& infIndices);
115
116 /*! Return a \f$2n-1+m+k\f$ square matrix for an IR/FX/INF/CR model, where \f$n\f$ is the number of currencies in
117 the \p ccys argument, \f$m\f$ is the number of inflation indices in the \p infIndices argument and \f$k\f$ is
118 the number of credit names in the \p names argument. This assumes that \c ccys[0] is the base currency.
119 */
120 QuantLib::Matrix correlationMatrix(const std::vector<std::string>& ccys,
121 const std::vector<std::string>& infIndices,
122 const std::vector<std::string>& names);
123
124 /*! Return a \f$2n-1+m+k+p\f$ square matrix for an IR/FX/INF/CR/EQ model, where \f$n\f$ is the number of
125 currencies in the \p ccys argument, \f$m\f$ is the number of inflation indices in the \p infIndices argument,
126 \f$k\f$ is the number of credit names in the \p names argument and \f$p\f$ is the number of equity names in
127 the \p equities argument. This assumes that \c ccys[0] is the base currency.
128 */
129 QuantLib::Matrix correlationMatrix(const std::vector<std::string>& ccys,
130 const std::vector<std::string>& infIndices,
131 const std::vector<std::string>& names,
132 const std::vector<std::string>& equities);
133
134 // TODO: Add commodity
135
136 /*! Build the correlation matrix according to the information provided in \p processInfo. The ProcessInfo map uses
137 the CrossAssetModel asset type as the outer map key and therefore has the correct ordering i.e. IR, FX, etc.
138 For each CrossAssetModel asset type, there is a vector of pairs where the first element in the pair is the
139 name of the factor being modeled and the second element in the pair is the number of factors used in
140 modeling the name. In most cases, the number of factors is 1. The first element in the vector for asset type
141 IR is assumed to be the base currency.
142
143 */
144 typedef std::map<QuantExt::CrossAssetModel::AssetType, std::vector<std::pair<std::string, QuantLib::Size>>>
146 QuantLib::Matrix correlationMatrix(const ProcessInfo& processInfo);
147 //@}
148
149 //! Get the correlation between two factors
150 QuantLib::Handle<QuantLib::Quote> lookup(const std::string& f1, const std::string& f2);
151
152 //! Get the correlation between the factor \p f_1 and \p f_2.
153 QuantLib::Handle<QuantLib::Quote> getCorrelation(const CorrelationFactor& f_1, const CorrelationFactor& f_2) const;
154
155 //! Get the raw correlation data
156 const std::map<CorrelationKey, QuantLib::Handle<QuantLib::Quote>>& correlations();
157
158private:
159 /*! Create the process information for each of the factors. Legacy method where each process is assumed to be
160 driven by one factor. Used to support the legacy \c correlationMatrix methods above that accept vectors of
161 strings.
162 */
163 ProcessInfo createProcessInfo(const std::vector<std::string>& ccys,
164 const std::vector<std::string>& inflationIndices = {},
165 const std::vector<std::string>& creditNames = {},
166 const std::vector<std::string>& equityNames = {});
167
168 //! Perform some basic checks on the factor names.
169 void checkFactor(const CorrelationFactor& f) const;
170
171 /*! The pair of factors used as the key will always have the first element less than the second element. We use
172 \c createKey to ensure this ordering when adding elements to \c corrs_ and when looking up elements in
173 \c corrs_.
174 */
175 CorrelationKey createKey(const CorrelationFactor& f_1, const CorrelationFactor& f_2) const;
176
177 //! Store the correlation between two factors.
178 std::map<CorrelationKey, QuantLib::Handle<QuantLib::Quote>> corrs_;
179};
180
181} // namespace data
182} // namespace ore
QuantLib::Matrix correlationMatrix(const std::vector< std::string > &ccys)
std::map< CorrelationKey, QuantLib::Handle< QuantLib::Quote > > corrs_
Store the correlation between two factors.
void addCorrelation(const std::string &factor1, const std::string &factor2, QuantLib::Real correlation)
QuantLib::Handle< QuantLib::Quote > lookup(const std::string &f1, const std::string &f2)
Get the correlation between two factors.
void addCorrelation(const std::string &factor1, const std::string &factor2, const QuantLib::Handle< QuantLib::Quote > &correlation)
QuantLib::Handle< QuantLib::Quote > getCorrelation(const CorrelationFactor &f_1, const CorrelationFactor &f_2) const
Get the correlation between the factor f_1 and f_2.
CorrelationKey createKey(const CorrelationFactor &f_1, const CorrelationFactor &f_2) const
void addCorrelation(const CorrelationFactor &f_1, const CorrelationFactor &f_2, const QuantLib::Handle< QuantLib::Quote > &correlation)
Add correlation quote between factor f_1 and f_2.
ProcessInfo createProcessInfo(const std::vector< std::string > &ccys, const std::vector< std::string > &inflationIndices={}, const std::vector< std::string > &creditNames={}, const std::vector< std::string > &equityNames={})
void checkFactor(const CorrelationFactor &f) const
Perform some basic checks on the factor names.
void addCorrelation(const CorrelationFactor &f_1, const CorrelationFactor &f_2, QuantLib::Real correlation)
Add correlation between factor f_1 and f_2.
std::map< QuantExt::CrossAssetModel::AssetType, std::vector< std::pair< std::string, QuantLib::Size > > > ProcessInfo
const std::map< CorrelationKey, QuantLib::Handle< QuantLib::Quote > > & correlations()
Get the raw correlation data.
@ data
Definition: log.hpp:77
bool operator<(const Dividend &d1, const Dividend &d2)
bool operator!=(const Filter &a, const Filter &b)
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
bool operator==(const Dividend &d1, const Dividend &d)
std::pair< CorrelationFactor, CorrelationFactor > CorrelationKey
CorrelationFactor parseCorrelationFactor(const string &name, const char separator)
Serializable Credit Default Swap.
Definition: namespaces.docs:23
QuantExt::CrossAssetModel::AssetType type
string name