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
SensitivityAggregator Class Reference

#include <orea/engine/sensitivityaggregator.hpp>

+ Collaboration diagram for SensitivityAggregator:

Public Types

typedef std::pair< RiskFactorKey, RiskFactorKeyCrossPair
 

Public Member Functions

 SensitivityAggregator (const std::map< std::string, std::set< std::pair< std::string, QuantLib::Size > > > &categories)
 
 SensitivityAggregator (const std::map< std::string, std::function< bool(std::string)> > &categories)
 
void aggregate (SensitivityStream &ss, const QuantLib::ext::shared_ptr< ScenarioFilter > &filter=QuantLib::ext::make_shared< ScenarioFilter >())
 
void reset ()
 Reset the aggregator to it's initial state by clearing all aggregations. More...
 
const std::set< SensitivityRecord > & sensitivities (const std::string &category) const
 
void generateDeltaGamma (const std::string &category, std::map< RiskFactorKey, QuantLib::Real > &deltas, std::map< CrossPair, QuantLib::Real > &gammas)
 

Private Member Functions

void init ()
 Initialise the container of aggregated records. More...
 
void add (SensitivityRecord &sr, std::set< SensitivityRecord > &records)
 Add a sensitivity record to the set of aggregated records. More...
 
bool inCategory (const std::string &tradeId, const std::string &category) const
 Determine if the tradeId is in the given category. More...
 

Private Attributes

std::map< std::string, std::set< std::pair< std::string, QuantLib::Size > > > setCategories_
 
std::map< std::string, std::function< bool(std::string)> > categories_
 Container for category names and their definition via functions. More...
 
std::map< std::string, std::set< SensitivityRecord > > aggRecords_
 Sensitivity records aggregated according to categories_ More...
 

Detailed Description

Class for aggregating SensitivityRecords.

The SensitivityRecords are aggregated according to categories of predefined trade IDs.

Definition at line 40 of file sensitivityaggregator.hpp.

Member Typedef Documentation

◆ CrossPair

typedef std::pair<RiskFactorKey, RiskFactorKey> CrossPair

Return the deltas and gammas for the given category

Definition at line 75 of file sensitivityaggregator.hpp.

Constructor & Destructor Documentation

◆ SensitivityAggregator() [1/2]

SensitivityAggregator ( const std::map< std::string, std::set< std::pair< std::string, QuantLib::Size > > > &  categories)

Constructor that uses sets of trades to define the aggregation categories.

The categories map has a string key that defines the name of the category and a value that defines the set of trade IDs in that category.

◆ SensitivityAggregator() [2/2]

SensitivityAggregator ( const std::map< std::string, std::function< bool(std::string)> > &  categories)

Constructor that uses functions to define the aggregation categories.

The categories map has a string key that defines the name of the category. The map value is a function that when given a trade ID, returns a bool indicating if the trade ID is in the category.

Member Function Documentation

◆ aggregate()

void aggregate ( SensitivityStream ss,
const QuantLib::ext::shared_ptr< ScenarioFilter > &  filter = QuantLib::ext::make_shared<ScenarioFilter>() 
)

Update the aggregator with SensitivityRecords from the stream ss after applying the optional filter. If no filter is specified, all risk factors are aggregated.

Warning:
No checks are performed for duplicate records from the stream. It is the stream's responsibility to guard against duplicates if it needs to.

Definition at line 52 of file sensitivityaggregator.cpp.

52 {
53 // Ensure at start of stream
54 ss.reset();
55
56 // Loop over stream's records
57 while (SensitivityRecord sr = ss.next()) {
58 // Skip this record if the risk factor is not in the filter
59 if (!sr.isCrossGamma() && !filter->allow(sr.key_1))
60 continue;
61 if (sr.isCrossGamma() && (!filter->allow(sr.key_1) || !filter->allow(sr.key_2)))
62 continue;
63
64 // "Blank out" trade ID before adding
65 string tradeId = sr.tradeId;
66 sr.tradeId = "";
67
68 // Update aggRecords_ for each category
69 for (const auto& kv : categories_) {
70 // Check if the sensitivity record's trade ID is in the category
71 if (kv.second(tradeId)) {
72 DLOG("Updating aggregated sensitivities for category " << kv.first << " with record: " << sr);
73 add(sr, aggRecords_[kv.first]);
74 }
75 }
76 }
77}
std::map< std::string, std::function< bool(std::string)> > categories_
Container for category names and their definition via functions.
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_
SafeStack< Filter > filter
#define DLOG(text)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reset()

void reset ( )

Reset the aggregator to it's initial state by clearing all aggregations.

Definition at line 79 of file sensitivityaggregator.cpp.

79 {
80 // Clear the aggregated sensitivities
81 aggRecords_.clear();
82
83 // Initialise the categorised records
84 init();
85}
void init()
Initialise the container of aggregated records.

◆ sensitivities()

const set< SensitivityRecord > & sensitivities ( const std::string &  category) const

Return the set of aggregated sensitivities for the given category

Definition at line 87 of file sensitivityaggregator.cpp.

87 {
88
89 auto it = aggRecords_.find(category);
90 QL_REQUIRE(it != aggRecords_.end(),
91 "The category " << category << " was not used in the construction of the SensitivityAggregator");
92
93 return it->second;
94}
+ Here is the caller graph for this function:

◆ generateDeltaGamma()

void generateDeltaGamma ( const std::string &  category,
std::map< RiskFactorKey, QuantLib::Real > &  deltas,
std::map< CrossPair, QuantLib::Real > &  gammas 
)

Definition at line 96 of file sensitivityaggregator.cpp.

97 {
98
99 auto srs = sensitivities(category);
100 for (const auto& sr : srs) {
101 if (!sr.isCrossGamma()) {
102 QL_REQUIRE(deltas.count(sr.key_1) == 0,
103 "Duplicate sensitivity entry for risk factor key " << sr.key_1 << " in the set");
104 deltas[sr.key_1] = sr.delta;
105 gammas[std::make_pair(sr.key_1, sr.key_1)] = sr.gamma;
106 } else {
107 auto keyPair =
108 sr.key_1 < sr.key_2 ? std::make_pair(sr.key_1, sr.key_2) : std::make_pair(sr.key_2, sr.key_1);
109 QL_REQUIRE(gammas.count(keyPair) == 0, "Duplicate sensitivity entry for cross gamma pair ["
110 << keyPair.first << ", " << keyPair.second << "] in the set");
111 gammas[keyPair] = sr.gamma;
112 }
113 }
114
115 // Final check that all cross gamma keys are in delta keys
116 for (const auto& kv : gammas) {
117 QL_REQUIRE(deltas.count(kv.first.first) > 0,
118 "The key " << kv.first.first << " is in the cross gammas but not in the deltas");
119 QL_REQUIRE(deltas.count(kv.first.second) > 0,
120 "The key " << kv.first.second << " is in the cross gammas but not in the deltas");
121 }
122}
const std::set< SensitivityRecord > & sensitivities(const std::string &category) const

◆ init()

void init ( )
private

Initialise the container of aggregated records.

Definition at line 124 of file sensitivityaggregator.cpp.

124 {
125 // Add an empty set for each of the categories
126 for (const auto& kv : categories_) {
127 aggRecords_[kv.first] = {};
128 }
129}

◆ add()

void add ( SensitivityRecord sr,
std::set< SensitivityRecord > &  records 
)
private

Add a sensitivity record to the set of aggregated records.

Definition at line 131 of file sensitivityaggregator.cpp.

131 {
132 // Try to insert sr. This will only pass if sr is not there already.
133 auto p = records.insert(sr);
134 if (!p.second) {
135 // If sr is already in the set, update it.
136 p.first->baseNpv += sr.baseNpv;
137 p.first->delta += sr.delta;
138 p.first->gamma += sr.gamma;
139 }
140}

◆ inCategory()

bool inCategory ( const std::string &  tradeId,
const std::string &  category 
) const
private

Determine if the tradeId is in the given category.

Definition at line 142 of file sensitivityaggregator.cpp.

142 {
143 QL_REQUIRE(setCategories_.count(category), "The category " << category << " is not valid");
144 auto tradeIds = setCategories_.at(category);
145 for (auto it = tradeIds.begin(); it != tradeIds.end(); ++it) {
146 if (it->first == tradeId)
147 return true;
148 }
149 return false;
150}
std::map< std::string, std::set< std::pair< std::string, QuantLib::Size > > > setCategories_

Member Data Documentation

◆ setCategories_

std::map<std::string, std::set<std::pair<std::string, QuantLib::Size> > > setCategories_
private

Container for category names and their definition via sets. This will be empty if constructor is provided functions directly.

Definition at line 83 of file sensitivityaggregator.hpp.

◆ categories_

std::map<std::string, std::function<bool(std::string)> > categories_
private

Container for category names and their definition via functions.

Definition at line 85 of file sensitivityaggregator.hpp.

◆ aggRecords_

std::map<std::string, std::set<SensitivityRecord> > aggRecords_
private

Sensitivity records aggregated according to categories_

Definition at line 87 of file sensitivityaggregator.hpp.