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

#include <orea/cube/jointnpvcube.hpp>

+ Inheritance diagram for JointNPVCube:
+ Collaboration diagram for JointNPVCube:

Public Member Functions

 JointNPVCube (const QuantLib::ext::shared_ptr< NPVCube > &cube1, const QuantLib::ext::shared_ptr< NPVCube > &cube2, const std::set< std::string > &ids={}, const bool requireUniqueIds=true, const std::function< Real(Real a, Real x)> &accumulator=[](Real a, Real x) { return a+x;}, const Real accumulatorInit=0.0)
 
 JointNPVCube (const std::vector< QuantLib::ext::shared_ptr< NPVCube > > &cubes, const std::set< std::string > &ids={}, const bool requireUniqueIds=true, const std::function< Real(Real a, Real x)> &accumulator=[](Real a, Real x) { return a+x;}, const Real accumulatorInit=0.0)
 
Size numIds () const override
 Return the length of each dimension. More...
 
Size numDates () const override
 
Size samples () const override
 
Size depth () const override
 
const std::map< std::string, Size > & idsAndIndexes () const override
 Get a map of id and their index position in this cube. More...
 
const std::vector< QuantLib::Date > & dates () const override
 Get the vector of dates for this cube. More...
 
QuantLib::Date asof () const override
 Return the asof date (T0 date) More...
 
Real getT0 (Size id, Size depth=0) const override
 Get a T0 value from the cube using index. More...
 
void setT0 (Real value, Size id, Size depth=0) override
 Set a value in the cube using index. More...
 
Real get (Size id, Size date, Size sample, Size depth=0) const override
 Get a value from the cube using index. More...
 
void set (Real value, Size id, Size date, Size sample, Size depth=0) override
 Set a value in the cube using index. More...
 
- Public Member Functions inherited from NPVCube
 NPVCube ()
 default ctor More...
 
 NPVCube (NPVCube &)=delete
 Do not allow cube copying. More...
 
NPVCubeoperator= (NPVCube const &)=delete
 
virtual ~NPVCube ()
 dtor More...
 
virtual Size numIds () const =0
 Return the length of each dimension. More...
 
virtual Size numDates () const =0
 
virtual Size samples () const =0
 
virtual Size depth () const =0
 
virtual const std::map< std::string, Size > & idsAndIndexes () const =0
 Get a map of id and their index position in this cube. More...
 
const std::set< std::string > ids () const
 Get a set of all ids in the cube. More...
 
virtual const std::vector< QuantLib::Date > & dates () const =0
 Get the vector of dates for this cube. More...
 
virtual QuantLib::Date asof () const =0
 Return the asof date (T0 date) More...
 
virtual Real getT0 (Size id, Size depth=0) const =0
 Get a T0 value from the cube using index. More...
 
virtual Real getT0 (const std::string &id, Size depth=0) const
 Get a T0 value from the cube using trade id. More...
 
virtual void setT0 (Real value, Size id, Size depth=0)=0
 Set a value in the cube using index. More...
 
virtual void setT0 (Real value, const std::string &id, Size depth=0)
 Set a value in the cube using trade id. More...
 
virtual Real get (Size id, Size date, Size sample, Size depth=0) const =0
 Get a value from the cube using index. More...
 
virtual void set (Real value, Size id, Size date, Size sample, Size depth=0)=0
 Set a value in the cube using index. More...
 
virtual Real get (const std::string &id, const QuantLib::Date &date, Size sample, Size depth=0) const
 Get a value from the cube using trade id and date. More...
 
virtual void set (Real value, const std::string &id, const QuantLib::Date &date, Size sample, Size depth=0)
 Set a value in the cube using trade id and date. More...
 
virtual void remove (Size id)
 
virtual void remove (Size id, Size sample)
 
Size getTradeIndex (const std::string &id) const
 

Private Member Functions

std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > cubeAndId (Size id) const
 

Private Attributes

const std::vector< QuantLib::ext::shared_ptr< NPVCube > > cubes_
 
const std::function< Real(Real a, Real x)> accumulator_
 
const Real accumulatorInit_
 
std::map< std::string, Size > idIdx_
 
std::vector< std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > > cubeAndId_
 

Additional Inherited Members

- Protected Member Functions inherited from NPVCube
virtual Size index (const std::string &id) const
 
virtual Size index (const QuantLib::Date &date) const
 

Detailed Description

Definition at line 38 of file jointnpvcube.hpp.

Constructor & Destructor Documentation

◆ JointNPVCube() [1/2]

JointNPVCube ( const QuantLib::ext::shared_ptr< NPVCube > &  cube1,
const QuantLib::ext::shared_ptr< NPVCube > &  cube2,
const std::set< std::string > &  ids = {},
const bool  requireUniqueIds = true,
const std::function< Real(Real a, Real x)> &  accumulator = [](Real a, Real x) { return a + x; },
const Real  accumulatorInit = 0.0 
)

ctor for two input cubes

Definition at line 29 of file jointnpvcube.cpp.

32 : JointNPVCube({cube1, cube2}, ids, requireUniqueIds, accumulator, accumulatorInit) {}
JointNPVCube(const QuantLib::ext::shared_ptr< NPVCube > &cube1, const QuantLib::ext::shared_ptr< NPVCube > &cube2, const std::set< std::string > &ids={}, const bool requireUniqueIds=true, const std::function< Real(Real a, Real x)> &accumulator=[](Real a, Real x) { return a+x;}, const Real accumulatorInit=0.0)
const std::set< std::string > ids() const
Get a set of all ids in the cube.
Definition: npvcube.hpp:75

◆ JointNPVCube() [2/2]

JointNPVCube ( const std::vector< QuantLib::ext::shared_ptr< NPVCube > > &  cubes,
const std::set< std::string > &  ids = {},
const bool  requireUniqueIds = true,
const std::function< Real(Real a, Real x)> &  accumulator = [](Real a, Real x) { return a + x; },
const Real  accumulatorInit = 0.0 
)

ctor for n input cubes

  • If no ids are given, the ids in the input cubes define the ids in the resulting cube. The order is lexicographic. If ids are given they define the ids in the output cube.
  • If requireUniqueIds is true, there must be no duplicate ids in the input cubes. If requireUniqueIds is false, they may be duplicate ids in which case get() will return an aggregate of the entries in the input cubes over the matching ids using the accumulator function and accumulator initialization.
  • If one id in the result cube corresponds to several input cubes, it is not allowed to call set on this id, this will result in an exception.

Definition at line 34 of file jointnpvcube.cpp.

37 : NPVCube(), cubes_(cubes), accumulator_(accumulator), accumulatorInit_(accumulatorInit) {
38
39 // check we have at least one input cube
40
41 QL_REQUIRE(!cubes.empty(), "JointNPVCube: at least one cube must be given");
42
43 // check that the dimensions are consistent
44
45 for (Size i = 1; i < cubes.size(); ++i) {
46 QL_REQUIRE(cubes_[i]->numDates() == cubes_[0]->numDates(),
47 "JointNPVCube: numDates do not match for cube #"
48 << i << " (" << cubes[i]->numDates() << " vs. cube #0 (" << cubes_[0]->numDates() << ")");
49 QL_REQUIRE(cubes_[i]->samples() == cubes_[0]->samples(),
50 "JointNPVCube: samples do not match for cube #" << i << " (" << cubes_[i]->samples()
51 << " vs. cube #0 (" << cubes_[0]->samples() << ")");
52 QL_REQUIRE(cubes_[i]->depth() == cubes_[0]->depth(), "JointNPVCube: depth do not match for cube #"
53 << i << " (" << cubes_[i]->depth() << " vs. cube #0 ("
54 << cubes[0]->depth() << ")");
55 }
56
57 std::set<std::string> allIds;
58 if (!ids.empty()) {
59 // if ids are given, these define the ids in the result cube
60 allIds = ids;
61 } else {
62 // otherwise the ids in the source cubes define the ids in the result cube
63 for (Size i = 0; i < cubes_.size(); ++i) {
64 for (auto const& [id, ignored] : cubes_[i]->idsAndIndexes()) {
65 const auto& [ignored2, success] = allIds.insert(id);
66 QL_REQUIRE(!requireUniqueIds || success,
67 "JointNPVSensiCube: input cubes have duplicate id '" << id << "', this is not allowed");
68 }
69 }
70 }
71
72 // build list of result cube ids
73 Size pos = 0;
74 for (const auto& id : allIds) {
75 idIdx_[id] = pos++;
76 }
77
78 // populate cubeAndId_ vector which is the basis for the lookup
79 cubeAndId_.resize(idIdx_.size());
80 for (const auto& [id, jointPos] : idIdx_) {
81 for (auto const& c : cubes_) {
82 auto searchIt = c->idsAndIndexes().find(id);
83 if (searchIt != c->idsAndIndexes().end()) {
84 cubeAndId_[jointPos].insert(std::make_pair(c, searchIt->second));
85 }
86 }
87 // internal consistency checks
88 QL_REQUIRE(cubeAndId_[jointPos].size() >= 1,
89 "JointNPVCube: internal error, got no input cubes for id '" << id << "'");
90 QL_REQUIRE(!requireUniqueIds || cubeAndId_[jointPos].size() == 1,
91 "JointNPVCube: internal error, got more than one input cube for id '"
92 << id << "', but unique input ids qre required");
93 }
94}
const std::function< Real(Real a, Real x)> accumulator_
Size numDates() const override
const std::map< std::string, Size > & idsAndIndexes() const override
Get a map of id and their index position in this cube.
const std::vector< QuantLib::ext::shared_ptr< NPVCube > > cubes_
Size depth() const override
std::map< std::string, Size > idIdx_
Size samples() const override
std::vector< std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > > cubeAndId_
NPVCube()
default ctor
Definition: npvcube.hpp:56
Size size(const ValueType &v)
+ Here is the call graph for this function:

Member Function Documentation

◆ numIds()

Size numIds ( ) const
overridevirtual

Return the length of each dimension.

Implements NPVCube.

Definition at line 96 of file jointnpvcube.cpp.

96{ return idIdx_.size(); }

◆ numDates()

Size numDates ( ) const
overridevirtual

Implements NPVCube.

Definition at line 98 of file jointnpvcube.cpp.

98{ return cubes_[0]->numDates(); }
+ Here is the caller graph for this function:

◆ samples()

Size samples ( ) const
overridevirtual

Implements NPVCube.

Definition at line 100 of file jointnpvcube.cpp.

100{ return cubes_[0]->samples(); }
+ Here is the caller graph for this function:

◆ depth()

Size depth ( ) const
overridevirtual

Implements NPVCube.

Definition at line 102 of file jointnpvcube.cpp.

102{ return cubes_[0]->depth(); }
+ Here is the caller graph for this function:

◆ idsAndIndexes()

const std::map< std::string, Size > & idsAndIndexes ( ) const
overridevirtual

Get a map of id and their index position in this cube.

Implements NPVCube.

Definition at line 104 of file jointnpvcube.cpp.

104{ return idIdx_; }
+ Here is the caller graph for this function:

◆ dates()

const std::vector< QuantLib::Date > & dates ( ) const
overridevirtual

Get the vector of dates for this cube.

Implements NPVCube.

Definition at line 106 of file jointnpvcube.cpp.

106{ return cubes_[0]->dates(); }

◆ asof()

QuantLib::Date asof ( ) const
overridevirtual

Return the asof date (T0 date)

Implements NPVCube.

Definition at line 108 of file jointnpvcube.cpp.

108{ return cubes_[0]->asof(); }

◆ getT0()

Real getT0 ( Size  id,
Size  depth = 0 
) const
overridevirtual

Get a T0 value from the cube using index.

Implements NPVCube.

Definition at line 116 of file jointnpvcube.cpp.

116 {
117 auto cids = cubeAndId(id);
118 if (cids.size() == 1)
119 return cids.begin()->first->getT0(cids.begin()->second, depth);
120 Real tmp = accumulatorInit_;
121 for (auto const& p : cids)
122 tmp = accumulator_(tmp, p.first->getT0(p.second, depth));
123 return tmp;
124}
std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > cubeAndId(Size id) const
+ Here is the call graph for this function:

◆ setT0()

void setT0 ( Real  value,
Size  id,
Size  depth = 0 
)
overridevirtual

Set a value in the cube using index.

Implements NPVCube.

Definition at line 126 of file jointnpvcube.cpp.

126 {
127 auto c = cubeAndId(id);
128 QL_REQUIRE(c.size() == 1,
129 "JointNPVCube::setT0(): not allowed, because id '" << id << "' occurs in more than one input cube");
130 (*c.begin()).first->setT0(value, (*c.begin()).second, depth);
131}
+ Here is the call graph for this function:

◆ get()

Real get ( Size  id,
Size  date,
Size  sample,
Size  depth = 0 
) const
overridevirtual

Get a value from the cube using index.

Implements NPVCube.

Definition at line 133 of file jointnpvcube.cpp.

133 {
134 auto cids = cubeAndId(id);
135 if (cids.size() == 1)
136 return cids.begin()->first->get(cids.begin()->second, date, sample, depth);
137 Real tmp = accumulatorInit_;
138 for (auto const& p : cids)
139 tmp = accumulator_(tmp, p.first->get(p.second, date, sample, depth));
140 return tmp;
141}
+ Here is the call graph for this function:

◆ set()

void set ( Real  value,
Size  id,
Size  date,
Size  sample,
Size  depth = 0 
)
overridevirtual

Set a value in the cube using index.

Implements NPVCube.

Definition at line 143 of file jointnpvcube.cpp.

143 {
144 auto c = cubeAndId(id);
145 QL_REQUIRE(c.size() == 1,
146 "JointNPVCube::set(): not allowed, because id '" << id << "' occurs in more than one input cube");
147 (*c.begin()).first->set(value, (*c.begin()).second, date, sample, depth);
148}
+ Here is the call graph for this function:

◆ cubeAndId()

std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > cubeAndId ( Size  id) const
private

Definition at line 110 of file jointnpvcube.cpp.

110 {
111 QL_REQUIRE(id < cubeAndId_.size(),
112 "JointNPVCube: id (" << id << ") out of range, have " << cubeAndId_.size() << " ids");
113 return cubeAndId_[id];
114}
+ Here is the caller graph for this function:

Member Data Documentation

◆ cubes_

const std::vector<QuantLib::ext::shared_ptr<NPVCube> > cubes_
private

Definition at line 81 of file jointnpvcube.hpp.

◆ accumulator_

const std::function<Real(Real a, Real x)> accumulator_
private

Definition at line 82 of file jointnpvcube.hpp.

◆ accumulatorInit_

const Real accumulatorInit_
private

Definition at line 83 of file jointnpvcube.hpp.

◆ idIdx_

std::map<std::string, Size> idIdx_
private

Definition at line 85 of file jointnpvcube.hpp.

◆ cubeAndId_

std::vector<std::set<std::pair<QuantLib::ext::shared_ptr<NPVCube>, Size> > > cubeAndId_
private

Definition at line 86 of file jointnpvcube.hpp.