Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
npvcube.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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/npvcube.hpp
20 \brief The base NPV cube class
21 \ingroup cube
22*/
23
24#pragma once
25
26#include <ql/shared_ptr.hpp>
27#include <ql/errors.hpp>
28#include <ql/time/date.hpp>
29#include <ql/types.hpp>
30#include <vector>
31#include <map>
32#include <set>
33
34namespace ore {
35namespace analytics {
36using QuantLib::Real;
37using QuantLib::Size;
38//! NPV Cube class stores both future and current NPV values.
39/*! The cube class stores future NPV values in a 4-D array.
40 *
41 * This abstract base class is just used for the storage of a cube.
42 * This class also stores the tradeIds, dates and vector of T0 NPVs
43 *
44 * The values in the cube must be set according to the following rules to ensure consistent behavior:
45 * - T0 values need to be set first using setT0(), in arbitrary order for (id, date, sample, depth), not all
46 * possible tuples have to be covered
47 * - after that the other values can be set using set(), again in arbitrary order for (id, date, sample, depth)
48 * and again not all possible tuples have to be covered
49 * - for each tuple (id, date, sample, depth) setT0() and set() should only be called once
50 *
51 \ingroup cube
52 */
53class NPVCube {
54public:
55 //! default ctor
57
58 //! Do not allow cube copying
59 NPVCube(NPVCube&) = delete;
60 NPVCube& operator=(NPVCube const&) = delete;
61
62 //! dtor
63 virtual ~NPVCube() {}
64
65 //! Return the length of each dimension
66 virtual Size numIds() const = 0;
67 virtual Size numDates() const = 0;
68 virtual Size samples() const = 0;
69 virtual Size depth() const = 0;
70
71 //! Get a map of id and their index position in this cube
72 virtual const std::map<std::string, Size>& idsAndIndexes() const = 0;
73
74 //! Get a set of all ids in the cube
75 const std::set<std::string> ids() const {
76 std::set<std::string> result;
77 for (const auto& [id, pos] : idsAndIndexes()) {
78 result.insert(id);
79 }
80 return result;
81 }
82
83 //! Get the vector of dates for this cube
84 virtual const std::vector<QuantLib::Date>& dates() const = 0; // TODO: Should this be the full date grid?
85
86 //! Return the asof date (T0 date)
87 virtual QuantLib::Date asof() const = 0;
88 //! Get a T0 value from the cube using index
89 virtual Real getT0(Size id, Size depth = 0) const = 0;
90 //! Get a T0 value from the cube using trade id
91 virtual Real getT0(const std::string& id, Size depth = 0) const { return getT0(index(id), depth); };
92 //! Set a value in the cube using index
93 virtual void setT0(Real value, Size id, Size depth = 0) = 0;
94 //! Set a value in the cube using trade id
95 virtual void setT0(Real value, const std::string& id, Size depth = 0) { setT0(value, index(id), depth); };
96
97 //! Get a value from the cube using index
98 virtual Real get(Size id, Size date, Size sample, Size depth = 0) const = 0;
99 //! Set a value in the cube using index
100 virtual void set(Real value, Size id, Size date, Size sample, Size depth = 0) = 0;
101
102 //! Get a value from the cube using trade id and date
103 virtual Real get(const std::string& id, const QuantLib::Date& date, Size sample, Size depth = 0) const {
104 return get(index(id), index(date), sample, depth);
105 };
106 //! Set a value in the cube using trade id and date
107 virtual void set(Real value, const std::string& id, const QuantLib::Date& date, Size sample, Size depth = 0) {
108 set(value, index(id), index(date), sample, depth);
109 }
110
111 /*! remove all values for a given id, i.e. change the state as if setT0() and set() has never been called for the id
112 the default implementation has generelly to be overriden in derived classes depending on how values are stored */
113 virtual void remove(Size id);
114
115 /*! simliar as above, but remove all values for a given id and scenario and keep T0 values */
116 virtual void remove(Size id, Size sample);
117
118 Size getTradeIndex(const std::string& id) const { return index(id); }
119
120protected:
121 virtual Size index(const std::string& id) const {
122 const auto& it = idsAndIndexes().find(id);
123 QL_REQUIRE(it != idsAndIndexes().end(), "NPVCube can't find an index for id " << id);
124 return it->second;
125 };
126
127 virtual Size index(const QuantLib::Date& date) const {
128 auto it = std::find(dates().begin(), dates().end(), date);
129 QL_REQUIRE(it != dates().end(), "NPVCube can't find an index for date " << date);
130 return std::distance(dates().begin(), it);
131 };
132
133};
134
135// impl
136
137inline void NPVCube::remove(Size id) {
138 for (Size date = 0; date < this->numDates(); ++date) {
139 for (Size depth = 0; depth < this->depth(); ++depth) {
140 setT0(0.0, id, depth);
141 for (Size sample = 0; sample < this->samples(); ++sample) {
142 set(0.0, id, date, sample, depth);
143 }
144 }
145 }
146}
147
148inline void NPVCube::remove(Size id, Size sample) {
149 for (Size date = 0; date < this->numDates(); ++date) {
150 for (Size depth = 0; depth < this->depth(); ++depth) {
151 set(0.0, id, date, sample, depth);
152 }
153 }
154}
155
156} // namespace analytics
157} // namespace ore
NPV Cube class stores both future and current NPV values.
Definition: npvcube.hpp:53
virtual void remove(Size id)
Definition: npvcube.hpp:137
virtual Real getT0(Size id, Size depth=0) const =0
Get a T0 value from the cube using index.
NPVCube(NPVCube &)=delete
Do not allow cube copying.
virtual Size numDates() const =0
virtual Size samples() const =0
virtual ~NPVCube()
dtor
Definition: npvcube.hpp:63
virtual Real get(Size id, Size date, Size sample, Size depth=0) const =0
Get a value from the cube using index.
virtual QuantLib::Date asof() const =0
Return the asof date (T0 date)
Size getTradeIndex(const std::string &id) const
Definition: npvcube.hpp:118
virtual Real getT0(const std::string &id, Size depth=0) const
Get a T0 value from the cube using trade id.
Definition: npvcube.hpp:91
virtual void set(Real value, Size id, Size date, Size sample, Size depth=0)=0
Set a value in the cube using index.
virtual Size numIds() const =0
Return the length of each dimension.
virtual Size depth() const =0
virtual Size index(const std::string &id) const
Definition: npvcube.hpp:121
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.
Definition: npvcube.hpp:103
virtual const std::vector< QuantLib::Date > & dates() const =0
Get the vector of dates for this cube.
const std::set< std::string > ids() const
Get a set of all ids in the cube.
Definition: npvcube.hpp:75
virtual void setT0(Real value, const std::string &id, Size depth=0)
Set a value in the cube using trade id.
Definition: npvcube.hpp:95
virtual const std::map< std::string, Size > & idsAndIndexes() const =0
Get a map of id and their index position in this cube.
virtual void setT0(Real value, Size id, Size depth=0)=0
Set a value in the cube using index.
virtual Size index(const QuantLib::Date &date) const
Definition: npvcube.hpp:127
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.
Definition: npvcube.hpp:107
NPVCube()
default ctor
Definition: npvcube.hpp:56
NPVCube & operator=(NPVCube const &)=delete
SafeStack< ValueType > value