Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
valuationengine.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 engine/valuationengine.hpp
20 \brief The cube valuation core
21 \ingroup simulation
22*/
23
24#pragma once
25
27
28#include <ql/time/date.hpp>
29
30#include <map>
31#include <set>
32
33namespace ore::data {
34class DateGrid;
35class Portfolio;
36class Trade;
37} // namespace ore::data
38
39namespace QuantExt {
40class ModelBuilder;
41}
42
43namespace ore {
44namespace analytics {
45
46class NPVCube;
47class CounterpartyCalculator;
48class ValuationCalculator;
49class SimMarket;
50
51using std::set;
52
53//! Valuation Engine
54/*!
55 The valuation engine's purpose is to generate an NPV cube.
56 Its buildCube loops over samples->dates->trades and prices the portfolio
57 under all samples and dates.
58
59 The number of dates is defined by the DateGrid passed to the constructor.
60 The number of trades is defined by the size of the portfolio passed to buildCube().
61 The number of samples is defined by the NPVCube that is passed to buildCube(), this
62 can be dynamic.
63
64 In addition to storing the resulting NPVs it can be given any number of calculators
65 that can store additional values in the cube.
66
67 \ingroup simulation
68*/
70public:
71 //! Constructor
73 //! Valuation date
74 const QuantLib::Date& today,
75 //! Simulation date grid
76 const QuantLib::ext::shared_ptr<ore::data::DateGrid>& dg,
77 //! Simulated market object
78 const QuantLib::ext::shared_ptr<analytics::SimMarket>& simMarket,
79 //! model builders to be updated
80 const set<std::pair<std::string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>>& modelBuilders =
81 set<std::pair<std::string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>>());
82
83 //! Build NPV cube
84 void buildCube(
85 //! Portfolio to be priced
86 const QuantLib::ext::shared_ptr<data::Portfolio>& portfolio,
87 //! Object for storing the results at trade level (e.g. NPVs, close-out NPVs, flows)
88 QuantLib::ext::shared_ptr<analytics::NPVCube> outputCube,
89 //! Calculators to use
90 std::vector<QuantLib::ext::shared_ptr<ValuationCalculator>> calculators,
91 //! Use sticky date in MPOR evaluation?
92 bool mporStickyDate = true,
93 //! Output cube for netting set-level results
94 QuantLib::ext::shared_ptr<analytics::NPVCube> outputCubeNettingSet = nullptr,
95 //! Output cube for storing counterparty-level survival probabilities
96 QuantLib::ext::shared_ptr<analytics::NPVCube> outputCptyCube = nullptr,
97 //! Calculators for filling counterparty-level results
98 std::vector<QuantLib::ext::shared_ptr<CounterpartyCalculator>> cptyCalculators = {},
99 //! Limit samples to one and fill the rest of the cube with random values
100 bool dryRun = false);
101
102private:
103 void recalibrateModels();
104 std::pair<double, double> populateCube(const QuantLib::Date& d, size_t cubeDateIndex, size_t sample,
105 bool isValueDate, bool isStickyDate, bool scenarioUpdated,
106 const std::map<std::string, QuantLib::ext::shared_ptr<ore::data::Trade>>& trades,
107 std::vector<bool>& tradeHasError,
108 const std::vector<QuantLib::ext::shared_ptr<ValuationCalculator>>& calculators,
109 QuantLib::ext::shared_ptr<analytics::NPVCube>& outputCube,
110 QuantLib::ext::shared_ptr<analytics::NPVCube>& outputCubeNettingSet,
111 const std::map<std::string, size_t>& counterparties,
112 const std::vector<QuantLib::ext::shared_ptr<CounterpartyCalculator>>& cptyCalculators,
113 QuantLib::ext::shared_ptr<analytics::NPVCube>& outputCptyCube);
114 void runCalculators(bool isCloseOutDate, const std::map<std::string, QuantLib::ext::shared_ptr<ore::data::Trade>>& trades,
115 std::vector<bool>& tradeHasError,
116 const std::vector<QuantLib::ext::shared_ptr<ValuationCalculator>>& calculators,
117 QuantLib::ext::shared_ptr<analytics::NPVCube>& outputCube,
118 QuantLib::ext::shared_ptr<analytics::NPVCube>& outputCubeSensis, const QuantLib::Date& d,
119 const QuantLib::Size cubeDateIndex, const QuantLib::Size sample, const std::string& label = "");
120 void runCalculators(bool isCloseOutDate, const std::map<std::string, QuantLib::Size>& counterparties,
121 const std::vector<QuantLib::ext::shared_ptr<CounterpartyCalculator>>& calculators,
122 QuantLib::ext::shared_ptr<analytics::NPVCube>& cptyCube, const QuantLib::Date& d,
123 const QuantLib::Size cubeDateIndex, const QuantLib::Size sample);
124 void tradeExercisable(bool enable, const std::map<std::string, QuantLib::ext::shared_ptr<ore::data::Trade>>& trades);
125 QuantLib::Date today_;
126 QuantLib::ext::shared_ptr<ore::data::DateGrid> dg_;
127 QuantLib::ext::shared_ptr<ore::analytics::SimMarket> simMarket_;
128 set<std::pair<std::string, QuantLib::ext::shared_ptr<QuantExt::ModelBuilder>>> modelBuilders_;
129};
130} // namespace analytics
131} // namespace ore
void runCalculators(bool isCloseOutDate, const std::map< std::string, QuantLib::Size > &counterparties, const std::vector< QuantLib::ext::shared_ptr< CounterpartyCalculator > > &calculators, QuantLib::ext::shared_ptr< analytics::NPVCube > &cptyCube, const QuantLib::Date &d, const QuantLib::Size cubeDateIndex, const QuantLib::Size sample)
QuantLib::ext::shared_ptr< ore::data::DateGrid > dg_
QuantLib::ext::shared_ptr< ore::analytics::SimMarket > simMarket_
std::pair< double, double > populateCube(const QuantLib::Date &d, size_t cubeDateIndex, size_t sample, bool isValueDate, bool isStickyDate, bool scenarioUpdated, const std::map< std::string, QuantLib::ext::shared_ptr< ore::data::Trade > > &trades, std::vector< bool > &tradeHasError, const std::vector< QuantLib::ext::shared_ptr< ValuationCalculator > > &calculators, QuantLib::ext::shared_ptr< analytics::NPVCube > &outputCube, QuantLib::ext::shared_ptr< analytics::NPVCube > &outputCubeNettingSet, const std::map< std::string, size_t > &counterparties, const std::vector< QuantLib::ext::shared_ptr< CounterpartyCalculator > > &cptyCalculators, QuantLib::ext::shared_ptr< analytics::NPVCube > &outputCptyCube)
set< std::pair< std::string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > modelBuilders_
void buildCube(const QuantLib::ext::shared_ptr< data::Portfolio > &portfolio, QuantLib::ext::shared_ptr< analytics::NPVCube > outputCube, std::vector< QuantLib::ext::shared_ptr< ValuationCalculator > > calculators, bool mporStickyDate=true, QuantLib::ext::shared_ptr< analytics::NPVCube > outputCubeNettingSet=nullptr, QuantLib::ext::shared_ptr< analytics::NPVCube > outputCptyCube=nullptr, std::vector< QuantLib::ext::shared_ptr< CounterpartyCalculator > > cptyCalculators={}, bool dryRun=false)
Build NPV cube.
void runCalculators(bool isCloseOutDate, const std::map< std::string, QuantLib::ext::shared_ptr< ore::data::Trade > > &trades, std::vector< bool > &tradeHasError, const std::vector< QuantLib::ext::shared_ptr< ValuationCalculator > > &calculators, QuantLib::ext::shared_ptr< analytics::NPVCube > &outputCube, QuantLib::ext::shared_ptr< analytics::NPVCube > &outputCubeSensis, const QuantLib::Date &d, const QuantLib::Size cubeDateIndex, const QuantLib::Size sample, const std::string &label="")
void tradeExercisable(bool enable, const std::map< std::string, QuantLib::ext::shared_ptr< ore::data::Trade > > &trades)