Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SensitivityCubeStream Class Reference

#include <orea/engine/sensitivitycubestream.hpp>

+ Inheritance diagram for SensitivityCubeStream:
+ Collaboration diagram for SensitivityCubeStream:

Public Member Functions

 SensitivityCubeStream (const QuantLib::ext::shared_ptr< SensitivityCube > &cube, const std::string &currency)
 
 SensitivityCubeStream (const std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > &cubes, const std::string &currency)
 
SensitivityRecord next () override
 
void reset () override
 Resets the stream so that SensitivityRecord objects can be streamed again. More...
 
- Public Member Functions inherited from SensitivityStream
virtual ~SensitivityStream ()
 Destructor. More...
 
virtual SensitivityRecord next ()=0
 Returns the next SensitivityRecord in the stream. More...
 
virtual void reset ()=0
 Resets the stream so that SensitivityRecord objects can be streamed again. More...
 

Private Member Functions

void updateForNewTrade ()
 

Private Attributes

std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > cubes_
 Handle on the SensitivityCubes. More...
 
std::string currency_
 Currency of the sensitivities in the SensitivityCubes. More...
 
Size currentCubeIdx_
 Current cube index in vector. More...
 
std::set< RiskFactorKeycurrentDeltaKeys_
 Current delta risk factor keys to process and iterators. More...
 
std::set< std::pair< RiskFactorKey, RiskFactorKey > > currentCrossGammaKeys_
 
std::set< RiskFactorKey >::const_iterator currentDeltaKey_
 
std::set< std::pair< RiskFactorKey, RiskFactorKey > >::const_iterator currentCrossGammaKey_
 
std::map< std::string, QuantLib::Size >::const_iterator tradeIdx_
 Current trade iterator. More...
 
bool canComputeGamma_
 Can only compute gamma if the up and down risk factors align. More...
 

Detailed Description

Class for streaming SensitivityRecords from a SensitivityCube

Definition at line 37 of file sensitivitycubestream.hpp.

Constructor & Destructor Documentation

◆ SensitivityCubeStream() [1/2]

SensitivityCubeStream ( const QuantLib::ext::shared_ptr< SensitivityCube > &  cube,
const std::string &  currency 
)

Constructor providing the sensitivity cube and currency of the sensitivities

Definition at line 33 of file sensitivitycubestream.cpp.

34 : SensitivityCubeStream(std::vector<QuantLib::ext::shared_ptr<SensitivityCube>>{cube}, currency) {}
SensitivityCubeStream(const QuantLib::ext::shared_ptr< SensitivityCube > &cube, const std::string &currency)

◆ SensitivityCubeStream() [2/2]

SensitivityCubeStream ( const std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > &  cubes,
const std::string &  currency 
)

Constructor providing the sensitivity cubes and currency of the sensitivities

Definition at line 36 of file sensitivitycubestream.cpp.

38 : cubes_(cubes), currency_(currency), canComputeGamma_(false) {
39
40 // Set the value of canComputeGamma_ based on up and down risk factors.
41
42 canComputeGamma_ = true;
43 for (const auto& cube : cubes_) {
44 const auto& upFactors = cube->upFactors();
45 const auto& downFactors = cube->downFactors();
46 if (upFactors.size() == downFactors.size() &&
47 equal(upFactors.begin(), upFactors.end(), downFactors.begin(),
48 [](const auto& a, const auto& b) { return a.first == b.first; }))
49 continue;
50 canComputeGamma_ = false;
51 break;
52 }
53
54 reset();
55}
std::string currency_
Currency of the sensitivities in the SensitivityCubes.
std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > cubes_
Handle on the SensitivityCubes.
bool canComputeGamma_
Can only compute gamma if the up and down risk factors align.
void reset() override
Resets the stream so that SensitivityRecord objects can be streamed again.
Filter equal(Filter x, const Filter &y)
+ Here is the call graph for this function:

Member Function Documentation

◆ next()

SensitivityRecord next ( )
overridevirtual

Returns the next SensitivityRecord in the stream

Warning:
the cube must not change during successive calls to next()!

Implements SensitivityStream.

Definition at line 57 of file sensitivitycubestream.cpp.

57 {
58
59 if (cubes_.size() == 0)
60 return SensitivityRecord();
61
62 while (tradeIdx_ != cubes_[currentCubeIdx_]->tradeIdx().end() && currentDeltaKey_ == currentDeltaKeys_.end() &&
64 ++tradeIdx_;
66 }
67
68 if (tradeIdx_ == cubes_[currentCubeIdx_]->tradeIdx().end()) {
69 if (currentCubeIdx_ < cubes_.size() - 1) {
71 tradeIdx_ = cubes_[currentCubeIdx_]->tradeIdx().begin();
73 return next();
74 } else {
75 return SensitivityRecord();
76 }
77 }
78
79 SensitivityRecord sr;
80 Size tradeIdx = tradeIdx_->second;
81 sr.tradeId = tradeIdx_->first;
82 sr.isPar = false;
83 sr.currency = currency_;
84 sr.baseNpv = cubes_[currentCubeIdx_]->npv(tradeIdx);
85
87 auto fd = cubes_[currentCubeIdx_]->upFactors().at(*currentDeltaKey_);
88 sr.key_1 = *currentDeltaKey_;
89 sr.desc_1 = fd.factorDesc;
90 sr.shift_1 = fd.targetShiftSize;
91 sr.delta = cubes_[currentCubeIdx_]->delta(tradeIdx_->first, *currentDeltaKey_);
93 sr.gamma = cubes_[currentCubeIdx_]->gamma(tradeIdx_->first, *currentDeltaKey_);
94 else
95 sr.gamma = Null<Real>();
98 auto fd = cubes_[currentCubeIdx_]->crossFactors().at(*currentCrossGammaKey_);
99 sr.key_1 = currentCrossGammaKey_->first;
100 sr.desc_1 = std::get<0>(fd).factorDesc;
101 sr.shift_1 = std::get<0>(fd).targetShiftSize;
102 sr.key_2 = currentCrossGammaKey_->second;
103 sr.desc_2 = std::get<1>(fd).factorDesc;
104 sr.shift_2 = std::get<1>(fd).targetShiftSize;
105 sr.gamma = cubes_[currentCubeIdx_]->crossGamma(tradeIdx_->first, *currentCrossGammaKey_);
107 }
108
109 TLOG("Next record is: " << sr);
110 return sr;
111}
std::set< std::pair< RiskFactorKey, RiskFactorKey > > currentCrossGammaKeys_
std::map< std::string, QuantLib::Size >::const_iterator tradeIdx_
Current trade iterator.
std::set< RiskFactorKey >::const_iterator currentDeltaKey_
std::set< std::pair< RiskFactorKey, RiskFactorKey > >::const_iterator currentCrossGammaKey_
std::set< RiskFactorKey > currentDeltaKeys_
Current delta risk factor keys to process and iterators.
Size currentCubeIdx_
Current cube index in vector.
#define TLOG(text)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reset()

void reset ( )
overridevirtual

Resets the stream so that SensitivityRecord objects can be streamed again.

Implements SensitivityStream.

Definition at line 147 of file sensitivitycubestream.cpp.

147 {
148 currentCubeIdx_ = 0;
149 if (cubes_.size() > 0) {
150 tradeIdx_ = cubes_[currentCubeIdx_]->tradeIdx().begin();
152 }
153}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ updateForNewTrade()

void updateForNewTrade ( )
private

Definition at line 113 of file sensitivitycubestream.cpp.

113 {
114 currentDeltaKeys_.clear();
116
117 if (tradeIdx_ != cubes_[currentCubeIdx_]->tradeIdx().end()) {
118
119 // add delta keys
120
121 for (auto const& [idx, _] : cubes_[currentCubeIdx_]->npvCube()->getTradeNPVs(tradeIdx_->second)) {
122 if (auto k = cubes_[currentCubeIdx_]->upDownFactor(idx); k.keytype != RiskFactorKey::KeyType::None)
123 currentDeltaKeys_.insert(k);
124 }
125
126 // add cross gamma keys
127
128 for (auto const& [crossPair, data] : cubes_[currentCubeIdx_]->crossFactors()) {
129 // scaling of cross gamma is not relevant here, so we use 1
130 if (!close_enough(cubes_[currentCubeIdx_]->crossGamma(tradeIdx_->second, std::get<0>(data).index,
131 std::get<1>(data).index, std::get<2>(data), 1.0, 1.0),
132 0.0)) {
134
135 // make sure, delta keys contain both cross keys, that's a guarantee of the SensitivityCubeStream
136
137 currentDeltaKeys_.insert(crossPair.first);
138 currentDeltaKeys_.insert(crossPair.second);
139 }
140 }
141 }
142
145}
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
SensitivityCube::crossPair crossPair
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ cubes_

std::vector<QuantLib::ext::shared_ptr<SensitivityCube> > cubes_
private

Handle on the SensitivityCubes.

Definition at line 57 of file sensitivitycubestream.hpp.

◆ currency_

std::string currency_
private

Currency of the sensitivities in the SensitivityCubes.

Definition at line 59 of file sensitivitycubestream.hpp.

◆ currentCubeIdx_

Size currentCubeIdx_
private

Current cube index in vector.

Definition at line 62 of file sensitivitycubestream.hpp.

◆ currentDeltaKeys_

std::set<RiskFactorKey> currentDeltaKeys_
private

Current delta risk factor keys to process and iterators.

Definition at line 65 of file sensitivitycubestream.hpp.

◆ currentCrossGammaKeys_

std::set<std::pair<RiskFactorKey,RiskFactorKey> > currentCrossGammaKeys_
private

Definition at line 66 of file sensitivitycubestream.hpp.

◆ currentDeltaKey_

std::set<RiskFactorKey>::const_iterator currentDeltaKey_
private

Definition at line 68 of file sensitivitycubestream.hpp.

◆ currentCrossGammaKey_

std::set<std::pair<RiskFactorKey,RiskFactorKey>>::const_iterator currentCrossGammaKey_
private

Definition at line 69 of file sensitivitycubestream.hpp.

◆ tradeIdx_

std::map<std::string,QuantLib::Size>::const_iterator tradeIdx_
private

Current trade iterator.

Definition at line 72 of file sensitivitycubestream.hpp.

◆ canComputeGamma_

bool canComputeGamma_
private

Can only compute gamma if the up and down risk factors align.

Definition at line 75 of file sensitivitycubestream.hpp.