Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
historicalpnlgenerator.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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 orea/engine/historicalpnlgenerator.hpp
20 \brief Class for generating portfolio P&Ls based on historical scenarios
21*/
22
23#pragma once
24
32#include <ql/types.hpp>
33#include <vector>
34
35namespace ore {
36namespace analytics {
37
38/*! Class for generating historical P&L vectors for a given portfolio in a given currency.
39
40 In particular, assume that the portfolio has a base NPV, \f$\Pi_0\f$, today i.e. at
41 \f$t_0\f$. This class takes a HistoricalScenarioGenerator which holds a set of historical
42 market moves, over a given period \f$\tau\f$ e.g. 10 business days, for a set of past dates
43 \f$\{d_1, d_2, \ldots, d_N\}\f$. This class calculates the P&L changes on the portfolio,
44 \f$\{\Delta_1, \Delta_2, \ldots, \Delta_N\}\f$, resulting from applying these market moves to
45 the base market. In other words, \f$\Delta_i = \Pi_i - \Pi_0 \f$ where \f$\Pi_i\f$ is the
46 portfolio NPV under the shifted market corresponding to date \f$d_i\f$ for
47 \f$i = 1, 2, \ldots, N\f$.
48
49 In the calculation of P&L, the class allows the scenario shifts to be filtered and also the
50 trades to be filtered.
51*/
53public:
54 /*! Constructor to use a single-threaded valuation engine
55 \param baseCurrency currency in which the P&Ls will be calculated
56 \param portfolio portfolio of trades for which P&Ls will be calculated
57 \param simMarket simulation market used for valuation
58 \param hisScenGen historical scenario generator
59 \param cube an NPV cube that will be populated by each call to generateCube
60 \param modelBuilders model builders to update during a val engine run
61 \param dryRun for testing - limit the number of scenarios to one and fill the cube with random data
62 */
63 HistoricalPnlGenerator(const std::string& baseCurrency, const QuantLib::ext::shared_ptr<ore::data::Portfolio>& portfolio,
64 const QuantLib::ext::shared_ptr<ScenarioSimMarket>& simMarket,
65 const QuantLib::ext::shared_ptr<HistoricalScenarioGenerator>& hisScenGen,
66 const QuantLib::ext::shared_ptr<NPVCube>& cube,
67 const set<std::pair<string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>>& modelBuilders = {},
68 bool dryRun = false);
69
70 /*! Constructor to use a multi-threaded valuation engine */
71 HistoricalPnlGenerator(const string& baseCurrency, const QuantLib::ext::shared_ptr<Portfolio>& portfolio,
72 const QuantLib::ext::shared_ptr<HistoricalScenarioGenerator>& hisScenGen,
73 const QuantLib::ext::shared_ptr<EngineData>& engineData, const Size nThreads, const Date& today,
74 const QuantLib::ext::shared_ptr<ore::data::Loader>& loader,
75 const QuantLib::ext::shared_ptr<ore::data::CurveConfigurations>& curveConfigs,
76 const QuantLib::ext::shared_ptr<ore::data::TodaysMarketParameters>& todaysMarketParams,
77 const std::string& configuration,
78 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters>& simMarketData,
79 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData = nullptr,
80 const IborFallbackConfig& iborFallbackConfig = IborFallbackConfig::defaultConfig(),
81 bool dryRun = false, const std::string& context = "historical pnl generation");
82
83 /*! Generate a "cube" of P&L values for the trades in the portfolio on each of
84 the scenarios provided by the historical scenario generator. The historical
85 scenarios will have the given \p filter applied.
86 */
87 void generateCube(const QuantLib::ext::shared_ptr<ScenarioFilter>& filter);
88
89 /*! Return a vector of historical portfolio P&L values restricted to scenarios
90 falling in \p period and restricted to the given \p tradeIds. The P&L values
91 are calculated from the last cube generated by generateCube.
92 */
93 std::vector<QuantLib::Real> pnl(const ore::data::TimePeriod& period,
94 const std::set<std::pair<std::string, QuantLib::Size>>& tradeIds) const;
95
96 /*! Return a vector of historical portfolio P&L values restricted to scenarios
97 falling in \p period. The P&L values are calculated from the last cube
98 generated by generateCube.
99 */
100 std::vector<QuantLib::Real> pnl(const ore::data::TimePeriod& period) const;
101
102 /*! Return a vector of historical portfolio P&L values restricted to the given \p tradeIds.
103 The P&L values are calculated from the last cube generated by generateCube.
104 */
105 std::vector<QuantLib::Real> pnl(const std::set<std::pair<std::string, QuantLib::Size>>& tradeIds) const;
106
107 /*! Return a vector of historical portfolio P&L values for all scenarios generated
108 by the historical scenario generator. The P&L values are calculated from the
109 last cube generated by generateCube.
110 */
111 std::vector<QuantLib::Real> pnl() const;
112
113 /*! Return a vector of historical trade level P&L values restricted to scenarios falling in \p period and
114 restricted to the given \p tradeIds. The P&L values are calculated from the last cube generated by
115 generateCube. The first dimension is time and the second dimension is tradeId.
116 */
117 using TradePnlStore = std::vector<std::vector<QuantLib::Real>>;
119 const std::set<std::pair<std::string, QuantLib::Size>>& tradeIds) const;
120
121 /*! Return a vector of historical trade level P&L values restricted to scenarios falling in \p period. The P&L
122 values are calculated from the last cube generated by generateCube. The first dimension is time and the
123 second dimension is tradeId.
124 */
126
127 /*! Return a vector of historical trade level P&L values restricted to the given \p tradeIds. The P&L
128 values are calculated from the last cube generated by generateCube. The first dimension is time and the
129 second dimension is tradeId.
130 */
131 TradePnlStore tradeLevelPnl(const std::set<std::pair<std::string, QuantLib::Size>>& tradeIds) const;
132
133 /*! Return a vector of historical trade level P&L values for all scenarios generated by the historical scenario
134 generator. The P&L values are calculated from the last cube generated by generateCube. The first dimension is
135 time and the second dimension is tradeId.
136 */
138
139 /*! Return the last cube generated by generateCube.
140 */
141 const QuantLib::ext::shared_ptr<NPVCube>& cube() const;
142
143 //! Set of trade ID and index pairs for all trades.
144 std::set<std::pair<std::string, QuantLib::Size>> tradeIdIndexPairs() const;
145
146 //! Time period covered by the historical P&L generator.
148
149private:
151
152 QuantLib::ext::shared_ptr<ore::data::Portfolio> portfolio_;
153 QuantLib::ext::shared_ptr<ScenarioSimMarket> simMarket_;
154 QuantLib::ext::shared_ptr<HistoricalScenarioGenerator> hisScenGen_;
155 QuantLib::ext::shared_ptr<NPVCube> cube_;
156 QuantLib::ext::shared_ptr<ValuationEngine> valuationEngine_;
157
158 // additional parameters needed for multi-threaded ctor
159 QuantLib::ext::shared_ptr<ore::data::EngineData> engineData_;
161 Date today_;
162 QuantLib::ext::shared_ptr<ore::data::Loader> loader_;
163 QuantLib::ext::shared_ptr<ore::data::CurveConfigurations> curveConfigs_;
164 QuantLib::ext::shared_ptr<ore::data::TodaysMarketParameters> todaysMarketParams_;
165 std::string configuration_;
166 QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketData_;
167 QuantLib::ext::shared_ptr<ore::data::ReferenceDataManager> referenceData_;
169
171 std::string context_;
172
173 std::function<std::vector<QuantLib::ext::shared_ptr<ValuationCalculator>>()> npvCalculator_;
174
175 //! Get the index of the as of date in the cube.
176 QuantLib::Size indexAsof() const;
177};
178
179} // namespace analytics
180} // namespace ore
QuantLib::ext::shared_ptr< ore::data::CurveConfigurations > curveConfigs_
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams_
TradePnlStore tradeLevelPnl(const ore::data::TimePeriod &period, const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
HistoricalPnlGenerator(const string &baseCurrency, const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &hisScenGen, const QuantLib::ext::shared_ptr< EngineData > &engineData, const Size nThreads, const Date &today, const QuantLib::ext::shared_ptr< ore::data::Loader > &loader, const QuantLib::ext::shared_ptr< ore::data::CurveConfigurations > &curveConfigs, const QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > &todaysMarketParams, const std::string &configuration, const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &simMarketData, const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceData=nullptr, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig(), bool dryRun=false, const std::string &context="historical pnl generation")
QuantLib::ext::shared_ptr< ScenarioSimMarket > simMarket_
std::vector< std::vector< QuantLib::Real > > TradePnlStore
QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > hisScenGen_
HistoricalPnlGenerator(const std::string &baseCurrency, const QuantLib::ext::shared_ptr< ore::data::Portfolio > &portfolio, const QuantLib::ext::shared_ptr< ScenarioSimMarket > &simMarket, const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &hisScenGen, const QuantLib::ext::shared_ptr< NPVCube > &cube, const set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > &modelBuilders={}, bool dryRun=false)
QuantLib::ext::shared_ptr< ore::data::Portfolio > portfolio_
QuantLib::ext::shared_ptr< ore::data::ReferenceDataManager > referenceData_
std::vector< QuantLib::Real > pnl(const ore::data::TimePeriod &period, const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
void generateCube(const QuantLib::ext::shared_ptr< ScenarioFilter > &filter)
ore::data::IborFallbackConfig iborFallbackConfig_
QuantLib::ext::shared_ptr< ValuationEngine > valuationEngine_
TradePnlStore tradeLevelPnl(const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketData_
std::function< std::vector< QuantLib::ext::shared_ptr< ValuationCalculator > >()> npvCalculator_
ore::data::TimePeriod timePeriod() const
Time period covered by the historical P&L generator.
QuantLib::ext::shared_ptr< ore::data::Loader > loader_
QuantLib::Size indexAsof() const
Get the index of the as of date in the cube.
std::vector< QuantLib::Real > pnl() const
const QuantLib::ext::shared_ptr< NPVCube > & cube() const
std::set< std::pair< std::string, QuantLib::Size > > tradeIdIndexPairs() const
Set of trade ID and index pairs for all trades.
std::vector< QuantLib::Real > pnl(const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
QuantLib::ext::shared_ptr< NPVCube > cube_
QuantLib::ext::shared_ptr< ore::data::EngineData > engineData_
static IborFallbackConfig defaultConfig()
scenario generator that builds from historical shifts
A Market class that can be updated by Scenarios.
The cube valuation core.