Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
sensitivityaggregator.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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/sensitivityaggregator.hpp
20 \brief Class for aggregating SensitivityRecords
21 */
22
23#pragma once
24
27
28#include <functional>
29#include <map>
30#include <set>
31#include <string>
32
33namespace ore {
34namespace analytics {
35
36/*! Class for aggregating SensitivityRecords.
37
38 The SensitivityRecords are aggregated according to categories of predefined trade IDs.
39*/
41public:
42 /*! Constructor that uses sets of trades to define the aggregation categories.
43
44 The \p categories map has a string key that defines the name of the category and a value
45 that defines the set of trade IDs in that category.
46 */
47 SensitivityAggregator(const std::map<std::string, std::set<std::pair<std::string, QuantLib::Size>>>& categories);
48
49 /*! Constructor that uses functions to define the aggregation categories.
50
51 The \p categories map has a string key that defines the name of the category. The map value
52 is a function that when given a trade ID, returns a bool indicating if the trade ID is in the
53 category.
54 */
55 SensitivityAggregator(const std::map<std::string, std::function<bool(std::string)>>& categories);
56
57 /*! Update the aggregator with SensitivityRecords from the stream \p ss after applying the
58 optional filter. If no filter is specified, all risk factors are aggregated.
59
60 \warning No checks are performed for duplicate records from the stream. It is the stream's
61 responsibility to guard against duplicates if it needs to.
62 */
63 void aggregate(SensitivityStream& ss, const QuantLib::ext::shared_ptr<ScenarioFilter>& filter =
64 QuantLib::ext::make_shared<ScenarioFilter>());
65
66 //! Reset the aggregator to it's initial state by clearing all aggregations
67 void reset();
68
69 /*! Return the set of aggregated sensitivities for the given \p category
70 */
71 const std::set<SensitivityRecord>& sensitivities(const std::string& category) const;
72
73 /*! Return the deltas and gammas for the given \p category
74 */
75 typedef std::pair<RiskFactorKey, RiskFactorKey> CrossPair;
76 void generateDeltaGamma(const std::string& category, std::map<RiskFactorKey, QuantLib::Real>& deltas,
77 std::map<CrossPair, QuantLib::Real>& gammas);
78
79private:
80 /*! Container for category names and their definition via sets. This will be
81 empty if constructor is provided functions directly.
82 */
83 std::map<std::string, std::set<std::pair<std::string, QuantLib::Size>>> setCategories_;
84 //! Container for category names and their definition via functions
85 std::map<std::string, std::function<bool(std::string)>> categories_;
86 //! Sensitivity records aggregated according to <code>categories_</code>
87 std::map<std::string, std::set<SensitivityRecord>> aggRecords_;
88
89 //! Initialise the container of aggregated records
90 void init();
91 //! Add a sensitivity record to the set of aggregated \p records
92 void add(SensitivityRecord& sr, std::set<SensitivityRecord>& records);
93 //! Determine if the \p tradeId is in the given \p category
94 bool inCategory(const std::string& tradeId, const std::string& category) const;
95};
96
97} // namespace analytics
98} // namespace ore
void init()
Initialise the container of aggregated records.
void generateDeltaGamma(const std::string &category, std::map< RiskFactorKey, QuantLib::Real > &deltas, std::map< CrossPair, QuantLib::Real > &gammas)
const std::set< SensitivityRecord > & sensitivities(const std::string &category) const
std::map< std::string, std::function< bool(std::string)> > categories_
Container for category names and their definition via functions.
bool inCategory(const std::string &tradeId, const std::string &category) const
Determine if the tradeId is in the given category.
std::map< std::string, std::set< std::pair< std::string, QuantLib::Size > > > setCategories_
SensitivityAggregator(const std::map< std::string, std::set< std::pair< std::string, QuantLib::Size > > > &categories)
void aggregate(SensitivityStream &ss, const QuantLib::ext::shared_ptr< ScenarioFilter > &filter=QuantLib::ext::make_shared< ScenarioFilter >())
SensitivityAggregator(const std::map< std::string, std::function< bool(std::string)> > &categories)
void add(SensitivityRecord &sr, std::set< SensitivityRecord > &records)
Add a sensitivity record to the set of aggregated records.
std::map< std::string, std::set< SensitivityRecord > > aggRecords_
Sensitivity records aggregated according to categories_
std::pair< RiskFactorKey, RiskFactorKey > CrossPair
void reset()
Reset the aggregator to it's initial state by clearing all aggregations.
Base Class for streaming SensitivityRecords.
A Market class that can be updated by Scenarios.
Base class for sensitivity record streamer.