Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
HistoricalPnlGenerator Class Reference

#include <orea/engine/historicalpnlgenerator.hpp>

+ Inheritance diagram for HistoricalPnlGenerator:
+ Collaboration diagram for HistoricalPnlGenerator:

Public Types

using TradePnlStore = std::vector< std::vector< QuantLib::Real > >
 

Public Member Functions

 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)
 
 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")
 
void generateCube (const QuantLib::ext::shared_ptr< ScenarioFilter > &filter)
 
std::vector< QuantLib::Real > pnl (const ore::data::TimePeriod &period, const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
 
std::vector< QuantLib::Real > pnl (const ore::data::TimePeriod &period) const
 
std::vector< QuantLib::Real > pnl (const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
 
std::vector< QuantLib::Real > pnl () const
 
TradePnlStore tradeLevelPnl (const ore::data::TimePeriod &period, const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
 
TradePnlStore tradeLevelPnl (const ore::data::TimePeriod &period) const
 
TradePnlStore tradeLevelPnl (const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds) const
 
TradePnlStore tradeLevelPnl () 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. More...
 
ore::data::TimePeriod timePeriod () const
 Time period covered by the historical P&L generator. More...
 
- Public Member Functions inherited from ProgressReporter
 ProgressReporter ()
 
void registerProgressIndicator (const QuantLib::ext::shared_ptr< ProgressIndicator > &indicator)
 
void unregisterProgressIndicator (const QuantLib::ext::shared_ptr< ProgressIndicator > &indicator)
 
void unregisterAllProgressIndicators ()
 
void updateProgress (const unsigned long progress, const unsigned long total, const std::string &detail="")
 
void resetProgress ()
 
const std::set< QuantLib::ext::shared_ptr< ProgressIndicator > > & progressIndicators () const
 

Private Member Functions

QuantLib::Size indexAsof () const
 Get the index of the as of date in the cube. More...
 

Private Attributes

bool useSingleThreadedEngine_
 
QuantLib::ext::shared_ptr< ore::data::Portfolioportfolio_
 
QuantLib::ext::shared_ptr< ScenarioSimMarketsimMarket_
 
QuantLib::ext::shared_ptr< HistoricalScenarioGeneratorhisScenGen_
 
QuantLib::ext::shared_ptr< NPVCubecube_
 
QuantLib::ext::shared_ptr< ValuationEnginevaluationEngine_
 
QuantLib::ext::shared_ptr< ore::data::EngineDataengineData_
 
Size nThreads_
 
Date today_
 
QuantLib::ext::shared_ptr< ore::data::Loaderloader_
 
QuantLib::ext::shared_ptr< ore::data::CurveConfigurationscurveConfigs_
 
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameterstodaysMarketParams_
 
std::string configuration_
 
QuantLib::ext::shared_ptr< ScenarioSimMarketParameterssimMarketData_
 
QuantLib::ext::shared_ptr< ore::data::ReferenceDataManagerreferenceData_
 
ore::data::IborFallbackConfig iborFallbackConfig_
 
bool dryRun_
 
std::string context_
 
std::function< std::vector< QuantLib::ext::shared_ptr< ValuationCalculator > >()> npvCalculator_
 

Detailed Description

Class for generating historical P&L vectors for a given portfolio in a given currency.

In particular, assume that the portfolio has a base NPV, \(\Pi_0\), today i.e. at \(t_0\). This class takes a HistoricalScenarioGenerator which holds a set of historical market moves, over a given period \(\tau\) e.g. 10 business days, for a set of past dates \(\{d_1, d_2, \ldots, d_N\}\). This class calculates the P&L changes on the portfolio, \(\{\Delta_1, \Delta_2, \ldots, \Delta_N\}\), resulting from applying these market moves to the base market. In other words, \(\Delta_i = \Pi_i - \Pi_0 \) where \(\Pi_i\) is the portfolio NPV under the shifted market corresponding to date \(d_i\) for \(i = 1, 2, \ldots, N\).

In the calculation of P&L, the class allows the scenario shifts to be filtered and also the trades to be filtered.

Definition at line 52 of file historicalpnlgenerator.hpp.

Member Typedef Documentation

◆ TradePnlStore

using TradePnlStore = std::vector<std::vector<QuantLib::Real> >

Return a vector of historical trade level P&L values restricted to scenarios falling in period and restricted to the given tradeIds. The P&L values are calculated from the last cube generated by generateCube. The first dimension is time and the second dimension is tradeId.

Definition at line 117 of file historicalpnlgenerator.hpp.

Constructor & Destructor Documentation

◆ HistoricalPnlGenerator() [1/2]

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 
)

Constructor to use a single-threaded valuation engine

Parameters
baseCurrencycurrency in which the P&Ls will be calculated
portfolioportfolio of trades for which P&Ls will be calculated
simMarketsimulation market used for valuation
hisScenGenhistorical scenario generator
cubean NPV cube that will be populated by each call to generateCube
modelBuildersmodel builders to update during a val engine run
dryRunfor testing - limit the number of scenarios to one and fill the cube with random data

◆ HistoricalPnlGenerator() [2/2]

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" 
)

Constructor to use a multi-threaded valuation engine

Member Function Documentation

◆ generateCube()

void generateCube ( const QuantLib::ext::shared_ptr< ScenarioFilter > &  filter)

Generate a "cube" of P&L values for the trades in the portfolio on each of the scenarios provided by the historical scenario generator. The historical scenarios will have the given filter applied.

Definition at line 96 of file historicalpnlgenerator.cpp.

96 {
97
98 DLOG("Filling historical P&L cube for " << portfolio_->size() << " trades and " << hisScenGen_->numScenarios()
99 << " scenarios.");
100
102
103 valuationEngine_->unregisterAllProgressIndicators();
104 for (auto const& i : this->progressIndicators()) {
105 i->reset();
106 valuationEngine_->registerProgressIndicator(i);
107 }
108
109 hisScenGen_->reset();
110 simMarket_->filter() = filter;
111 simMarket_->reset();
112 simMarket_->scenarioGenerator() = hisScenGen_;
113 hisScenGen_->baseScenario() = simMarket_->baseScenario();
114 valuationEngine_->buildCube(portfolio_, cube_, npvCalculator_(), true, nullptr, nullptr, {}, dryRun_);
115
116 } else {
117 MultiThreadedValuationEngine engine(
118 nThreads_, today_, QuantLib::ext::make_shared<ore::analytics::DateGrid>(), hisScenGen_->numScenarios(), loader_,
120 filter, referenceData_, iborFallbackConfig_, true, true, true, {}, {}, {}, context_);
121 for (auto const& i : this->progressIndicators()) {
122 i->reset();
123 engine.registerProgressIndicator(i);
124 }
125 engine.buildCube(portfolio_, npvCalculator_, {}, true, dryRun_);
126 cube_ = QuantLib::ext::make_shared<JointNPVCube>(engine.outputCubes(), portfolio_->ids(), true);
127 }
128
129 DLOG("Historical P&L cube generated");
130}
QuantLib::ext::shared_ptr< ore::data::CurveConfigurations > curveConfigs_
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams_
QuantLib::ext::shared_ptr< ScenarioSimMarket > simMarket_
QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > hisScenGen_
QuantLib::ext::shared_ptr< ore::data::Portfolio > portfolio_
QuantLib::ext::shared_ptr< ore::data::ReferenceDataManager > referenceData_
ore::data::IborFallbackConfig iborFallbackConfig_
QuantLib::ext::shared_ptr< ValuationEngine > valuationEngine_
QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketData_
std::function< std::vector< QuantLib::ext::shared_ptr< ValuationCalculator > >()> npvCalculator_
QuantLib::ext::shared_ptr< ore::data::Loader > loader_
QuantLib::ext::shared_ptr< NPVCube > cube_
QuantLib::ext::shared_ptr< ore::data::EngineData > engineData_
const std::set< QuantLib::ext::shared_ptr< ProgressIndicator > > & progressIndicators() const
SafeStack< Filter > filter
#define DLOG(text)
+ Here is the call graph for this function:

◆ pnl() [1/4]

std::vector< QuantLib::Real > pnl ( const ore::data::TimePeriod period,
const std::set< std::pair< std::string, QuantLib::Size > > &  tradeIds 
) const

Return a vector of historical portfolio P&L values restricted to scenarios falling in period and restricted to the given tradeIds. The P&L values are calculated from the last cube generated by generateCube.

◆ pnl() [2/4]

vector< Real > pnl ( const ore::data::TimePeriod period) const

Return a vector of historical portfolio P&L values restricted to scenarios falling in period. The P&L values are calculated from the last cube generated by generateCube.

Definition at line 161 of file historicalpnlgenerator.cpp.

161{ return pnl(period, tradeIdIndexPairs()); }
std::vector< QuantLib::Real > pnl() const
std::set< std::pair< std::string, QuantLib::Size > > tradeIdIndexPairs() const
Set of trade ID and index pairs for all trades.

◆ pnl() [3/4]

std::vector< QuantLib::Real > pnl ( const std::set< std::pair< std::string, QuantLib::Size > > &  tradeIds) const

Return a vector of historical portfolio P&L values restricted to the given tradeIds. The P&L values are calculated from the last cube generated by generateCube.

◆ pnl() [4/4]

vector< Real > pnl ( ) const

Return a vector of historical portfolio P&L values for all scenarios generated by the historical scenario generator. The P&L values are calculated from the last cube generated by generateCube.

Definition at line 167 of file historicalpnlgenerator.cpp.

167{ return pnl(timePeriod()); }
ore::data::TimePeriod timePeriod() const
Time period covered by the historical P&L generator.

◆ tradeLevelPnl() [1/4]

TradePnlStore tradeLevelPnl ( const ore::data::TimePeriod period,
const std::set< std::pair< std::string, QuantLib::Size > > &  tradeIds 
) const

◆ tradeLevelPnl() [2/4]

TradePnlStore tradeLevelPnl ( const ore::data::TimePeriod period) const

Return a vector of historical trade level P&L values restricted to scenarios falling in period. The P&L values are calculated from the last cube generated by generateCube. The first dimension is time and the second dimension is tradeId.

Definition at line 216 of file historicalpnlgenerator.cpp.

216 {
217 return tradeLevelPnl(period, tradeIdIndexPairs());
218}

◆ tradeLevelPnl() [3/4]

TradePnlStore tradeLevelPnl ( const std::set< std::pair< std::string, QuantLib::Size > > &  tradeIds) const

Return a vector of historical trade level P&L values restricted to the given tradeIds. The P&L values are calculated from the last cube generated by generateCube. The first dimension is time and the second dimension is tradeId.

◆ tradeLevelPnl() [4/4]

TradePnlStore tradeLevelPnl ( ) const

Return a vector of historical trade level P&L values for all scenarios generated by the historical scenario generator. The P&L values are calculated from the last cube generated by generateCube. The first dimension is time and the second dimension is tradeId.

Definition at line 224 of file historicalpnlgenerator.cpp.

224{ return tradeLevelPnl(timePeriod()); }

◆ cube()

const QuantLib::ext::shared_ptr< NPVCube > & cube ( ) const

Return the last cube generated by generateCube.

Definition at line 226 of file historicalpnlgenerator.cpp.

226{ return cube_; }

◆ tradeIdIndexPairs()

set< pair< string, Size > > tradeIdIndexPairs ( ) const

Set of trade ID and index pairs for all trades.

Definition at line 228 of file historicalpnlgenerator.cpp.

228 {
229 set<pair<string, Size>> res;
230 Size i = 0;
231 for (const auto& [id, trade]: portfolio_->trades())
232 res.insert(std::make_pair(id, i++));
233 return res;
234}

◆ timePeriod()

TimePeriod timePeriod ( ) const

Time period covered by the historical P&L generator.

Definition at line 236 of file historicalpnlgenerator.cpp.

236 {
237 vector<Date> dates{hisScenGen_->startDates().front(), hisScenGen_->endDates().back()};
238 return TimePeriod(dates);
239}
+ Here is the call graph for this function:

◆ indexAsof()

Size indexAsof ( ) const
private

Get the index of the as of date in the cube.

Definition at line 241 of file historicalpnlgenerator.cpp.

241 {
242 Date asof = useSingleThreadedEngine_ ? simMarket_->asofDate() : today_;
243 const auto& dates = cube_->dates();
244 auto it = std::find(dates.begin(), dates.end(), asof);
245 QL_REQUIRE(it != dates.end(), "Can't find an index for asof date " << asof << " in cube");
246 return std::distance(dates.begin(), it);
247}
Date asof(14, Jun, 2018)
+ Here is the call graph for this function:

Member Data Documentation

◆ useSingleThreadedEngine_

bool useSingleThreadedEngine_
private

Definition at line 150 of file historicalpnlgenerator.hpp.

◆ portfolio_

QuantLib::ext::shared_ptr<ore::data::Portfolio> portfolio_
private

Definition at line 152 of file historicalpnlgenerator.hpp.

◆ simMarket_

QuantLib::ext::shared_ptr<ScenarioSimMarket> simMarket_
private

Definition at line 153 of file historicalpnlgenerator.hpp.

◆ hisScenGen_

QuantLib::ext::shared_ptr<HistoricalScenarioGenerator> hisScenGen_
private

Definition at line 154 of file historicalpnlgenerator.hpp.

◆ cube_

QuantLib::ext::shared_ptr<NPVCube> cube_
private

Definition at line 155 of file historicalpnlgenerator.hpp.

◆ valuationEngine_

QuantLib::ext::shared_ptr<ValuationEngine> valuationEngine_
private

Definition at line 156 of file historicalpnlgenerator.hpp.

◆ engineData_

QuantLib::ext::shared_ptr<ore::data::EngineData> engineData_
private

Definition at line 159 of file historicalpnlgenerator.hpp.

◆ nThreads_

Size nThreads_
private

Definition at line 160 of file historicalpnlgenerator.hpp.

◆ today_

Date today_
private

Definition at line 161 of file historicalpnlgenerator.hpp.

◆ loader_

QuantLib::ext::shared_ptr<ore::data::Loader> loader_
private

Definition at line 162 of file historicalpnlgenerator.hpp.

◆ curveConfigs_

QuantLib::ext::shared_ptr<ore::data::CurveConfigurations> curveConfigs_
private

Definition at line 163 of file historicalpnlgenerator.hpp.

◆ todaysMarketParams_

QuantLib::ext::shared_ptr<ore::data::TodaysMarketParameters> todaysMarketParams_
private

Definition at line 164 of file historicalpnlgenerator.hpp.

◆ configuration_

std::string configuration_
private

Definition at line 165 of file historicalpnlgenerator.hpp.

◆ simMarketData_

QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketData_
private

Definition at line 166 of file historicalpnlgenerator.hpp.

◆ referenceData_

QuantLib::ext::shared_ptr<ore::data::ReferenceDataManager> referenceData_
private

Definition at line 167 of file historicalpnlgenerator.hpp.

◆ iborFallbackConfig_

ore::data::IborFallbackConfig iborFallbackConfig_
private

Definition at line 168 of file historicalpnlgenerator.hpp.

◆ dryRun_

bool dryRun_
private

Definition at line 170 of file historicalpnlgenerator.hpp.

◆ context_

std::string context_
private

Definition at line 171 of file historicalpnlgenerator.hpp.

◆ npvCalculator_

std::function<std::vector<QuantLib::ext::shared_ptr<ValuationCalculator> >()> npvCalculator_
private

Definition at line 173 of file historicalpnlgenerator.hpp.