Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
parsensitivitycubestream.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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
20
23
26
27namespace ore {
28namespace analytics {
29
30// Note: iterator initialisation below works because currentDeltas_ is
31// (empty) initialised before itCurrent_
32ParSensitivityCubeStream::ParSensitivityCubeStream(const QuantLib::ext::shared_ptr<ZeroToParCube>& cube, const string& currency)
33 : zeroCubeIdx_(0), cube_(cube), currency_(currency), itCurrent_(currentDeltas_.begin()) {
34 QL_REQUIRE(!cube_->zeroCubes().empty(), "ParSensitivityCubeStream: cube contains no zero cubes");
35 tradeIdx_ = cube_->zeroCubes().front()->tradeIdx().begin();
36 init();
37}
38
40
42
43 while (itCurrent_ == currentDeltas_.end() && tradeIdx_ != cube_->zeroCubes()[zeroCubeIdx_]->tradeIdx().end()) {
44 // Move to next trade
45 tradeIdx_++;
46 // update par deltas
47 if (tradeIdx_ != cube_->zeroCubes()[zeroCubeIdx_]->tradeIdx().end()) {
48 DLOG("Retrieving par deltas for trade " << tradeIdx_->first);
49 currentDeltas_ = cube_->parDeltas(zeroCubeIdx_, tradeIdx_->second);
50 itCurrent_ = currentDeltas_.begin();
51 DLOG("There are " << currentDeltas_.size() << " par deltas for trade " << tradeIdx_->first);
52 }
53 }
54
55 if (tradeIdx_ != cube_->zeroCubes()[zeroCubeIdx_]->tradeIdx().end()) {
56 // Populate current sensitivity record
57 sr.tradeId = tradeIdx_->first;
58 sr.isPar = true;
60 sr.baseNpv = cube_->zeroCubes()[zeroCubeIdx_]->npv(tradeIdx_->second);
61 if (itCurrent_ != currentDeltas_.end()) {
62 DLOG("Processing par delta [" << itCurrent_->first << ", " << itCurrent_->second << "]");
63 sr.key_1 = itCurrent_->first;
64 auto fullDescription = cube_->zeroCubes()[zeroCubeIdx_]->factorDescription(sr.key_1);
65 sr.desc_1 = deconstructFactor(fullDescription).second;
66 sr.shift_1 = cube_->zeroCubes()[zeroCubeIdx_]->targetShiftSize(sr.key_1);
67 sr.delta = itCurrent_->second;
68 sr.gamma = Null<Real>();
69 // Move iterator to next par delta
70 itCurrent_++;
71 }
72 } else if (zeroCubeIdx_ < cube_->zeroCubes().size() - 1) {
74 init();
75 return next();
76 }
77
78 return sr;
79}
80
82 // Reset all
83 zeroCubeIdx_ = 0;
84 tradeIdx_ = cube_->zeroCubes()[zeroCubeIdx_]->tradeIdx().begin();
85 currentDeltas_ = {};
86 itCurrent_ = currentDeltas_.begin();
87 // Call init
88 init();
89}
90
92 // If we have trade IDs in the underlying cube
93 if (!cube_->zeroCubes()[zeroCubeIdx_]->tradeIdx().empty()) {
94 tradeIdx_ = cube_->zeroCubes()[zeroCubeIdx_]->tradeIdx().begin();
95 DLOG("Retrieving par deltas for trade " << tradeIdx_->first);
96 currentDeltas_ = cube_->parDeltas(zeroCubeIdx_, tradeIdx_->second);
97 itCurrent_ = currentDeltas_.begin();
98 DLOG("There are " << currentDeltas_.size() << " par deltas for trade " << tradeIdx_->first);
99 }
100}
101
102} // namespace analytics
103} // namespace ore
QuantLib::ext::shared_ptr< ZeroToParCube > cube_
Handle on the SensitivityCube.
std::string currency_
Currency of the sensitivities in the SensitivityCube.
std::map< std::string, QuantLib::Size >::const_iterator tradeIdx_
TradeId and index of current trade ID in the underlying cube.
ParSensitivityCubeStream(const QuantLib::ext::shared_ptr< ZeroToParCube > &cube, const std::string &currency)
std::map< ore::analytics::RiskFactorKey, QuantLib::Real >::iterator itCurrent_
Iterator to current delta.
void reset() override
Resets the stream so that SensitivityRecord objects can be streamed again.
ore::analytics::SensitivityRecord next() override
std::map< ore::analytics::RiskFactorKey, QuantLib::Real > currentDeltas_
Par deltas for current trade ID.
#define DLOG(text)
pair< RiskFactorKey, string > deconstructFactor(const string &factor)
Size size(const ValueType &v)
Class for streaming SensitivityRecords from a par sensitivity cube.
Struct for holding a sensitivity record.
Shift scenario generation.