Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
historicalsensipnlcalculator.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 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/historicalsensipnlcalculator.hpp
20 \brief Class for generating sensi pnl
21*/
22
23#pragma once
24
25#include <orea/cube/npvcube.hpp>
31
32#include <ql/math/matrix.hpp>
33#include <ql/shared_ptr.hpp>
34
35#include <boost/accumulators/accumulators.hpp>
36// Including <boost/accumulators/statistics.hpp> causes the following swig wrapper compiler errors
37// explicit specialization of 'boost::accumulators::feature_of<boost::accumulators::tag::weighted_skewness>'
38// explicit specialization of 'boost::accumulators::feature_of<boost::accumulators::tag::weighted_kurtosis>'
39// see https://github.com/boostorg/accumulators/issues/20
40// The following subset of includes is sufficient here and circumvents the swig errors
41#include <boost/accumulators/statistics/stats.hpp>
42#include <boost/accumulators/statistics/variance.hpp>
43#include <boost/accumulators/statistics/covariance.hpp>
44#include <boost/accumulators/statistics/variates/covariate.hpp>
45
46namespace ore {
47namespace analytics {
48
50public:
52 virtual ~PNLCalculator() {}
53 virtual void writePNL(QuantLib::Size scenarioIdx, bool isCall,
54 const RiskFactorKey& key_1, QuantLib::Real shift_1, QuantLib::Real delta,
55 QuantLib::Real gamma, QuantLib::Real deltaPnl, Real gammaPnl,
56 const RiskFactorKey& key_2 = RiskFactorKey(),
57 QuantLib::Real shift_2 = 0.0, const std::string& tradeId = "") {}
58 const bool isInTimePeriod(QuantLib::Date startDate, QuantLib::Date endDate);
59
60 void populatePNLs(const std::vector<QuantLib::Real>& allPnls, const std::vector<QuantLib::Real>& foPnls,
61 const std::vector<QuantLib::Date>& startDates, const std::vector<QuantLib::Date>& endDates);
62
63 using TradePnLStore = std::vector<std::vector<QuantLib::Real>>;
64 void populateTradePNLs(const TradePnLStore& allPnls, const TradePnLStore& foPnls);
65
66 const std::vector<QuantLib::Real>& pnls() { return pnls_; };
67 const std::vector<QuantLib::Real>& foPnls() { return foPnls_; };
68
69 const TradePnLStore& tradePnls() { return tradePnls_; }
71
72 void clear() {
73 pnls_.clear();
74 foPnls_.clear();
75 tradePnls_.clear();
76 foTradePnls_.clear();
77 }
78
79protected:
80 std::vector<QuantLib::Real> pnls_;
81 std::vector<QuantLib::Real> foPnls_;
84};
85
87public:
88 CovarianceCalculator(ore::data::TimePeriod covariancePeriod) : covariancePeriod_(covariancePeriod) {}
89 void initialise(const std::set<std::pair<RiskFactorKey, QuantLib::Size>>& keys);
90 void updateAccumulators(const QuantLib::ext::shared_ptr<NPVCube>& shiftCube, QuantLib::Date startDate, QuantLib::Date endDate, QuantLib::Size index);
91 void populateCovariance(const std::set<std::pair<RiskFactorKey, QuantLib::Size>>& keys);
92 const Matrix& covariance() const { return covariance_; }
93
94private:
95 typedef boost::accumulators::accumulator_set<
96 QuantLib::Real,
97 boost::accumulators::stats<boost::accumulators::tag::covariance<QuantLib::Real, boost::accumulators::tag::covariate1>>>
99 std::map<std::pair<QuantLib::Size, QuantLib::Size>, accumulator> accCov_;
101 QuantLib::Matrix covariance_;
102};
103
105public:
106 HistoricalSensiPnlCalculator(const QuantLib::ext::shared_ptr<HistoricalScenarioGenerator>& hisScenGen,
107 const QuantLib::ext::shared_ptr<SensitivityStream>& ss)
108 : hisScenGen_(hisScenGen), sensitivityStream_(ss) {}
109
110 void populateSensiShifts(QuantLib::ext::shared_ptr<NPVCube>& cube, const vector<RiskFactorKey>& keys,
111 QuantLib::ext::shared_ptr<ScenarioShiftCalculator> shiftCalculator);
112
113 void calculateSensiPnl(const std::set<SensitivityRecord>& srs,
114 const std::vector<RiskFactorKey>& rfKeys,
115 QuantLib::ext::shared_ptr<NPVCube>& shiftCube,
116 const std::vector<QuantLib::ext::shared_ptr<PNLCalculator>>& pnlCalculators,
117 const QuantLib::ext::shared_ptr<CovarianceCalculator>& covarianceCalculator,
118 const std::vector<std::string>& tradeIds = {},
119 const bool includeGammaMargin = true, const bool includeDeltaMargin = true,
120 const bool tradeLevel = false);
121
122private:
123 QuantLib::ext::shared_ptr<HistoricalScenarioGenerator> hisScenGen_;
124 //! Stream of sensitivity records used for the sensitivity based backtest
125 QuantLib::ext::shared_ptr<SensitivityStream> sensitivityStream_;
126};
127
128} // namespace analytics
129} // namespace ore
boost::accumulators::accumulator_set< QuantLib::Real, boost::accumulators::stats< boost::accumulators::tag::covariance< QuantLib::Real, boost::accumulators::tag::covariate1 > > > accumulator
CovarianceCalculator(ore::data::TimePeriod covariancePeriod)
void initialise(const std::set< std::pair< RiskFactorKey, QuantLib::Size > > &keys)
void updateAccumulators(const QuantLib::ext::shared_ptr< NPVCube > &shiftCube, QuantLib::Date startDate, QuantLib::Date endDate, QuantLib::Size index)
void populateCovariance(const std::set< std::pair< RiskFactorKey, QuantLib::Size > > &keys)
std::map< std::pair< QuantLib::Size, QuantLib::Size >, accumulator > accCov_
QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > hisScenGen_
HistoricalSensiPnlCalculator(const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &hisScenGen, const QuantLib::ext::shared_ptr< SensitivityStream > &ss)
QuantLib::ext::shared_ptr< SensitivityStream > sensitivityStream_
Stream of sensitivity records used for the sensitivity based backtest.
void populateSensiShifts(QuantLib::ext::shared_ptr< NPVCube > &cube, const vector< RiskFactorKey > &keys, QuantLib::ext::shared_ptr< ScenarioShiftCalculator > shiftCalculator)
void calculateSensiPnl(const std::set< SensitivityRecord > &srs, const std::vector< RiskFactorKey > &rfKeys, QuantLib::ext::shared_ptr< NPVCube > &shiftCube, const std::vector< QuantLib::ext::shared_ptr< PNLCalculator > > &pnlCalculators, const QuantLib::ext::shared_ptr< CovarianceCalculator > &covarianceCalculator, const std::vector< std::string > &tradeIds={}, const bool includeGammaMargin=true, const bool includeDeltaMargin=true, const bool tradeLevel=false)
const std::vector< QuantLib::Real > & foPnls()
void populateTradePNLs(const TradePnLStore &allPnls, const TradePnLStore &foPnls)
const bool isInTimePeriod(QuantLib::Date startDate, QuantLib::Date endDate)
const std::vector< QuantLib::Real > & pnls()
PNLCalculator(ore::data::TimePeriod pnlPeriod)
void populatePNLs(const std::vector< QuantLib::Real > &allPnls, const std::vector< QuantLib::Real > &foPnls, const std::vector< QuantLib::Date > &startDates, const std::vector< QuantLib::Date > &endDates)
std::vector< QuantLib::Real > foPnls_
virtual void writePNL(QuantLib::Size scenarioIdx, bool isCall, const RiskFactorKey &key_1, QuantLib::Real shift_1, QuantLib::Real delta, QuantLib::Real gamma, QuantLib::Real deltaPnl, Real gammaPnl, const RiskFactorKey &key_2=RiskFactorKey(), QuantLib::Real shift_2=0.0, const std::string &tradeId="")
std::vector< std::vector< QuantLib::Real > > TradePnLStore
Data types stored in the scenario class.
Definition: scenario.hpp:48
scenario generator that builds from historical shifts
The base NPV cube class.
Scenario class.
Class for calculating the shift multiple between two scenarios for a given key.
Struct for holding a sensitivity record.
Base class for sensitivity record streamer.