Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
jointnpvcube.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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
19/*! \file orea/cube/jointnpvcube.hpp
20 \brief join n cubes in terms of stored ids
21 \ingroup cube
22*/
23
24#pragma once
25
26#include <orea/cube/npvcube.hpp>
27
28#include <set>
29
30namespace ore {
31namespace analytics {
32
33using namespace ore::analytics;
34
35using QuantLib::Real;
36using QuantLib::Size;
37
38class JointNPVCube : public NPVCube {
39public:
40 /*! ctor for two input cubes */
42 const QuantLib::ext::shared_ptr<NPVCube>& cube1, const QuantLib::ext::shared_ptr<NPVCube>& cube2,
43 const std::set<std::string>& ids = {}, const bool requireUniqueIds = true,
44 const std::function<Real(Real a, Real x)>& accumulator = [](Real a, Real x) { return a + x; },
45 const Real accumulatorInit = 0.0);
46
47 /*! ctor for n input cubes
48 - If no ids are given, the ids in the input cubes define the ids in the resulting cube. The order is
49 lexicographic. If ids are given they define the ids in the output cube.
50 - If requireUniqueIds is true, there must be no duplicate ids in the input cubes. If requireUniqueIds is false,
51 they may be duplicate ids in which case get() will return an aggregate of the entries in the input cubes over
52 the matching ids using the accumulator function and accumulator initialization.
53 - If one id in the result cube corresponds to several input cubes, it is not allowed to call set on this id,
54 this will result in an exception.
55 */
57 const std::vector<QuantLib::ext::shared_ptr<NPVCube>>& cubes, const std::set<std::string>& ids = {},
58 const bool requireUniqueIds = true,
59 const std::function<Real(Real a, Real x)>& accumulator = [](Real a, Real x) { return a + x; },
60 const Real accumulatorInit = 0.0);
61
62 //! Return the length of each dimension
63 Size numIds() const override;
64 Size numDates() const override;
65 Size samples() const override;
66 Size depth() const override;
67
68 const std::map<std::string, Size>& idsAndIndexes() const override;
69 const std::vector<QuantLib::Date>& dates() const override;
70 QuantLib::Date asof() const override;
71
72 Real getT0(Size id, Size depth = 0) const override;
73 void setT0(Real value, Size id, Size depth = 0) override;
74
75 Real get(Size id, Size date, Size sample, Size depth = 0) const override;
76 void set(Real value, Size id, Size date, Size sample, Size depth = 0) override;
77
78private:
79 std::set<std::pair<QuantLib::ext::shared_ptr<NPVCube>, Size>> cubeAndId(Size id) const;
80
81 const std::vector<QuantLib::ext::shared_ptr<NPVCube>> cubes_;
82 const std::function<Real(Real a, Real x)> accumulator_;
83 const Real accumulatorInit_;
84
85 std::map<std::string, Size> idIdx_;
86 std::vector<std::set<std::pair<QuantLib::ext::shared_ptr<NPVCube>, Size>>> cubeAndId_;
87};
88
89} // namespace analytics
90} // namespace ore
const std::function< Real(Real a, Real x)> accumulator_
void set(Real value, Size id, Size date, Size sample, Size depth=0) override
Set a value in the cube using index.
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.
Real getT0(Size id, Size depth=0) const override
Get a T0 value from the cube using index.
std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > cubeAndId(Size id) const
Size numIds() const override
Return the length of each dimension.
const std::vector< QuantLib::ext::shared_ptr< NPVCube > > cubes_
const std::vector< QuantLib::Date > & dates() const override
Get the vector of dates for this cube.
Size depth() const override
QuantLib::Date asof() const override
Return the asof date (T0 date)
std::map< std::string, Size > idIdx_
Size samples() const override
void setT0(Real value, Size id, Size depth=0) override
Set a value in the cube using index.
std::vector< std::set< std::pair< QuantLib::ext::shared_ptr< NPVCube >, Size > > > cubeAndId_
Real get(Size id, Size date, Size sample, Size depth=0) const override
Get a value from the cube using index.
NPV Cube class stores both future and current NPV values.
Definition: npvcube.hpp:53
const std::set< std::string > ids() const
Get a set of all ids in the cube.
Definition: npvcube.hpp:75
SafeStack< ValueType > value
The base NPV cube class.