Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Typedefs | Functions | Variables
sensitivityaggregator.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <orea/engine/sensitivityaggregator.hpp>
#include <orea/engine/sensitivityinmemorystream.hpp>
#include <oret/toplevelfixture.hpp>
#include <ql/math/comparison.hpp>
#include <test/oreatoplevelfixture.hpp>

Go to the source code of this file.

Typedefs

using RFType = RiskFactorKey::KeyType
 

Functions

set< SensitivityRecordfilter (set< SensitivityRecord > records, const string &tradeId)
 
void check (const set< SensitivityRecord > &exp, const set< SensitivityRecord > &res, const string &category)
 
 BOOST_AUTO_TEST_CASE (testGeneralAggregationSetCategories)
 
 BOOST_AUTO_TEST_CASE (testGeneralAggregationFunctionCategories)
 

Variables

set< SensitivityRecordexpAggregationAll
 

Typedef Documentation

◆ RFType

using RFType = RiskFactorKey::KeyType

Definition at line 37 of file sensitivityaggregator.cpp.

Function Documentation

◆ filter()

set< SensitivityRecord > filter ( set< SensitivityRecord records,
const string &  tradeId 
)

Definition at line 117 of file sensitivityaggregator.cpp.

117 {
118 set<SensitivityRecord> res;
119 for (auto sr : records) {
120 if (sr.tradeId == tradeId) {
121 sr.tradeId = "";
122 res.insert(sr);
123 }
124 }
125
126 return res;
127}

◆ check()

void check ( const set< SensitivityRecord > &  exp,
const set< SensitivityRecord > &  res,
const string &  category 
)

Definition at line 130 of file sensitivityaggregator.cpp.

130 {
131 BOOST_CHECK_EQUAL_COLLECTIONS(exp.begin(), exp.end(), res.begin(), res.end());
132 for (set<SensitivityRecord>::iterator itExp = exp.begin(), itRes = res.begin(); itExp != exp.end();
133 itExp++, itRes++) {
134 BOOST_CHECK_MESSAGE(QuantLib::close(itExp->baseNpv, itRes->baseNpv),
135 "Category = " << category << ": Base NPVs (exp = " << itExp->baseNpv
136 << " vs. actual = " << itRes->baseNpv
137 << ") are not equal for aggregated expected record " << *itExp);
138 BOOST_CHECK_MESSAGE(QuantLib::close(itExp->delta, itRes->delta),
139 "Category = " << category << ": Deltas (exp = " << itExp->delta
140 << " vs. actual = " << itRes->delta
141 << ") are not equal for aggregated expected record " << *itExp);
142 BOOST_CHECK_MESSAGE(QuantLib::close(itExp->gamma, itRes->gamma),
143 "Category = " << category << ": Gammas (exp = " << itExp->gamma
144 << " vs. actual = " << itRes->gamma
145 << ") are not equal for aggregated expected record " << *itExp);
146 }
147}
RandomVariable exp(RandomVariable x)
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( testGeneralAggregationSetCategories  )

Definition at line 153 of file sensitivityaggregator.cpp.

153 {
154
155 BOOST_TEST_MESSAGE("Testing general aggregation using sets of trades for categories");
156
157 // Streamer
158 SensitivityInMemoryStream ss(records.begin(), records.end());
159
160 // Categories for aggregator
161 map<string, set<std::pair<std::string, QuantLib::Size>>> categories;
162 // No aggregation, just single trade categories
163 set<pair<string, QuantLib::Size>> trades = {make_pair("trade_001", 0), make_pair("trade_003", 1),
164 make_pair("trade_004", 2), make_pair("trade_005", 3),
165 make_pair("trade_006", 4)};
166
167 for (const auto& trade : trades) {
168 categories[trade.first] = {trade};
169 }
170 // Aggregate over all trades except trade_002
171 categories["all_except_002"] = trades;
172
173 // Create aggregator and call aggregate
174 SensitivityAggregator sAgg(categories);
175 sAgg.aggregate(ss);
176
177 // Containers for expected and actual results respectively below
178 set<SensitivityRecord> exp;
179 set<SensitivityRecord> res;
180
181 // Test results for single trade categories
182 for (const auto& trade : trades) {
183 exp = filter(records, trade.first);
184 res = sAgg.sensitivities(trade.first);
185 BOOST_TEST_MESSAGE("Testing for category with single trade " << trade.first);
186 check(exp, res, trade.first);
187 }
188
189 // Test results for the aggregated "All" category
190 BOOST_TEST_MESSAGE("Testing for category 'all_except_002'");
191 res = sAgg.sensitivities("all_except_002");
192 check(expAggregationAll, res, "all_except_002");
193}
Class for streaming SensitivityRecords from csv file.
SafeStack< Filter > filter
set< SensitivityRecord > expAggregationAll
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( testGeneralAggregationFunctionCategories  )

Definition at line 195 of file sensitivityaggregator.cpp.

195 {
196
197 BOOST_TEST_MESSAGE("Testing general aggregation using functions for categories");
198
199 // Streamer
200 SensitivityInMemoryStream ss(records.begin(), records.end());
201
202 // Category functions for aggregator
203 map<string, function<bool(string)>> categories;
204 // No aggregation, just single trade categories
205 set<pair<string, QuantLib::Size>> trades = {make_pair("trade_001", 0), make_pair("trade_003", 1),
206 make_pair("trade_004", 2), make_pair("trade_005", 3),
207 make_pair("trade_006", 4)};
208
209 for (const auto& trade : trades) {
210 categories[trade.first] = [&trade](string tradeId) { return tradeId == trade.first; };
211 }
212 // Aggregate over all trades except trade_002
213 categories["all_except_002"] = [&trades](string tradeId) {
214 for (auto it = trades.begin(); it != trades.end(); ++it) {
215 if (it->first == tradeId)
216 return true;
217 }
218 return false;
219 };
220
221 // Create aggregator and call aggregate
222 SensitivityAggregator sAgg(categories);
223 sAgg.aggregate(ss);
224
225 // Containers for expected and actual results respectively below
226 set<SensitivityRecord> exp;
227 set<SensitivityRecord> res;
228
229 // Test results for single trade categories
230 for (const auto& trade : trades) {
231 exp = filter(records, trade.first);
232 res = sAgg.sensitivities(trade.first);
233 BOOST_TEST_MESSAGE("Testing for category with single trade " << trade.first);
234 check(exp, res, trade.first);
235 }
236
237 // Test results for the aggregated "All" category
238 BOOST_TEST_MESSAGE("Testing for category 'all_except_002'");
239 res = sAgg.sensitivities("all_except_002");
240 check(expAggregationAll, res, "all_except_002");
241}
+ Here is the call graph for this function:

Variable Documentation

◆ expAggregationAll

set<SensitivityRecord> expAggregationAll
Initial value:
= {
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 1), "1M", 0.0001, RiskFactorKey(), "", 0.0, "USD", -156337.99, 38.13, 0 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 2), "3M", 0.0001, RiskFactorKey(), "", 0.0, "USD", -156337.99, 114.53, 0 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 3), "6M", 0.0001, RiskFactorKey(), "", 0.0, "USD", -213862.68, 101.17, 0 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 4), "1Y", 0.0001, RiskFactorKey(), "", 0.0, "USD", -213862.68, 1295.33, -0.12 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 1), "1M", 0.0001, RiskFactorKey(), "", 0.0, "USD", -156337.99, -37.48, 0 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 2), "3M", 0.0001, RiskFactorKey(), "", 0.0, "USD", -156337.99, -112.57, 0 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 3), "6M", 0.0001, RiskFactorKey(), "", 0.0, "USD", -213862.68, -99.35, 0 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 4), "1Y", 0.0001, RiskFactorKey(), "", 0.0, "USD", -213862.68, -1277.58, 0.11 },
{ "", false, RiskFactorKey(RFType::FXSpot, "CNYUSD", 0), "spot", 0.001534, RiskFactorKey(), "", 0.0, "USD", -370200.67, -241172.95, 0 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 1), "1M", 0.0001, RiskFactorKey(RFType::DiscountCurve, "CNY", 2), "3M", 0.0001, "USD", -156337.99, 0.00, 0.00 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 1), "1M", 0.0001, RiskFactorKey(RFType::FXSpot, "CNYUSD", 0), "spot", 0.001534, "USD", -156337.99, 0.00, 0.38 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 2), "3M", 0.0001, RiskFactorKey(RFType::FXSpot, "CNYUSD", 0), "spot", 0.001534, "USD", -156337.99, 0.00, 1.15 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 3), "6M", 0.0001, RiskFactorKey(RFType::DiscountCurve, "CNY", 4), "1Y", 0.0001, "USD", -213862.68, 0.00, -0.01 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 3), "6M", 0.0001, RiskFactorKey(RFType::FXSpot, "CNYUSD", 0), "spot", 0.001534, "USD", -213862.68, 0.00, 1.01 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "CNY", 4), "1Y", 0.0001, RiskFactorKey(RFType::FXSpot, "CNYUSD", 0), "spot", 0.001534, "USD", -213862.68, 0.00, 12.96 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 1), "1M", 0.0001, RiskFactorKey(RFType::DiscountCurve, "USD", 2), "3M", 0.0001, "USD", -156337.99, 0.00, 0.00 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 3), "6M", 0.0001, RiskFactorKey(RFType::DiscountCurve, "USD", 4), "1Y", 0.0001, "USD", -213862.68, 0.00, 0.01 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "TWD", 1), "1M", 0.0001, RiskFactorKey(), "", 0.0, "EUR", 0, 0, 0.00 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "TWD", 2), "3M", 0.0001, RiskFactorKey(), "", 0.0, "EUR", 0, 0, 0.00 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 1), "1M", 0.0001, RiskFactorKey(), "", 0.0, "EUR", 0, 0, 0.00 },
{ "", false, RiskFactorKey(RFType::DiscountCurve, "USD", 2), "3M", 0.0001, RiskFactorKey(), "", 0.0, "EUR", 0, 0, 0.00 },
{ "", false, RiskFactorKey(RFType::FXSpot, "TWDUSD", 0), "spot", 0.0002, RiskFactorKey(), "", 0.0, "EUR", 0, 0, 0.00 }
}
Data types stored in the scenario class.
Definition: scenario.hpp:48

Definition at line 89 of file sensitivityaggregator.cpp.