Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
inmemorycube.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/inmemorycube.hpp
20 \brief A cube implementation that stores the cube in memory
21 \ingroup cube
22*/
23
24#pragma once
25
26#include <fstream>
27#include <vector>
28
29#include <ql/errors.hpp>
30
31#include <boost/make_shared.hpp>
32#include <orea/cube/npvcube.hpp>
33#include <set>
34
35namespace ore {
36namespace analytics {
37using QuantLib::Date;
38using QuantLib::Real;
39using QuantLib::Size;
40using std::vector;
41
42//! InMemoryCube stores the cube in memory using nested STL vectors
43/*! InMemoryCube stores the cube in memory using nested STL vectors, this class is a template
44 * to allow both single and double precision implementations.
45 *
46 * The use of nested STL vectors is adds a small memory overhead (~ 1 to 2%)
47
48 \ingroup cube
49 */
50template <typename T> class InMemoryCubeBase : public NPVCube {
51public:
52 //! default ctor
53 InMemoryCubeBase(const Date& asof, const std::set<std::string>& ids, const vector<Date>& dates, Size samples,
54 const T& t = T())
56 data_(ids.size(), vector<vector<T>>(dates.size(), vector<T>(samples, t))) {
57 QL_REQUIRE(ids.size() > 0, "InMemoryCube::InMemoryCube no ids specified");
58 QL_REQUIRE(dates.size() > 0, "InMemoryCube::InMemoryCube no dates specified");
59 QL_REQUIRE(samples > 0, "InMemoryCube::InMemoryCube samples must be > 0");
60 size_t pos = 0;
61 for (const auto& id : ids) {
62 idIdx_[id] = pos++;
63 }
64
65 }
66
67 //! default constructor
69
70 //! Return the length of each dimension
71 Size numIds() const override { return idIdx_.size(); }
72 Size numDates() const override { return dates_.size(); }
73 virtual Size samples() const override { return samples_; }
74
75 //! Return a map of all ids and their position in the cube
76 const std::map<std::string, Size>& idsAndIndexes() const override { return idIdx_; }
77 //! Get the vector of dates for this cube
78 const std::vector<QuantLib::Date>& dates() const override { return dates_; }
79
80 //! Return the asof date (T0 date)
81 QuantLib::Date asof() const override { return asof_; }
82
83protected:
84 void check(Size i, Size j, Size k, Size d) const {
85 QL_REQUIRE(i < numIds(), "Out of bounds on ids (i=" << i << ", numIds=" << numIds() << ")");
86 QL_REQUIRE(j < numDates(), "Out of bounds on dates (j=" << j << ", numDates=" << numDates() << ")");
87 QL_REQUIRE(k < samples(), "Out of bounds on samples (k=" << k << ", samples=" << samples() << ")");
88 QL_REQUIRE(d < depth(), "Out of bounds on depth (d=" << d << ", depth=" << depth() << ")");
89 }
90
91 QuantLib::Date asof_;
92 vector<QuantLib::Date> dates_;
94 vector<T> t0Data_;
95 vector<vector<vector<T>>> data_;
96
97 std::map<std::string, Size> idIdx_;
98};
99
100//! InMemoryCube of fixed depth 1
101/*! This implementation stores the type directly in an InMemoryCubeBase to avoid us having
102 * nested vectors all of length 1
103 */
104template <typename T> class InMemoryCube1 : public InMemoryCubeBase<T> {
105public:
106 //! ctor
107 InMemoryCube1(const Date& asof, const std::set<std::string>& ids, const vector<Date>& dates, Size samples,
108 const T& t = T())
109 : InMemoryCubeBase<T>(asof, ids, dates, samples, t) {}
110
111 //! default
113
114 //! Fixed depth
115 Size depth() const override { return 1; }
116
117 //! Get a T0 value from the cube
118 virtual Real getT0(Size i, Size d) const override {
119 this->check(i, 0, 0, d);
120 return this->t0Data_[i];
121 }
122
123 //! Set a value in the cube
124 virtual void setT0(Real value, Size i, Size d) override {
125 this->check(i, 0, 0, d);
126 this->t0Data_[i] = static_cast<T>(value);
127 }
128
129 //! Get a value from the cube
130 Real get(Size i, Size j, Size k, Size d) const override {
131 this->check(i, j, k, d);
132 return this->data_[i][j][k];
133 }
134
135 //! Set a value in the cube
136 void set(Real value, Size i, Size j, Size k, Size d) override {
137 this->check(i, j, k, d);
138 this->data_[i][j][k] = static_cast<T>(value);
139 }
140};
141
142//! InMemoryCube of variable depth
143/*! This implementation stores a vector an InMemoryCubeBase
144 */
145template <typename T> class InMemoryCubeN : public InMemoryCubeBase<vector<T>> {
146public:
147 //! ctor
148 InMemoryCubeN(const Date& asof, const std::set<std::string>& ids, const vector<Date>& dates, Size samples, Size depth,
149 const T& t = T())
150 : InMemoryCubeBase<vector<T>>(asof, ids, dates, samples, vector<T>(depth, t)) {}
151
152 //! default
154
155 //! Depth
156 Size depth() const override { return this->data_[0][0][0].size(); } // we don't want any members in this class
157
158 //! Get a T0 value from the cube
159 virtual Real getT0(Size i, Size d) const override {
160 this->check(i, 0, 0, d);
161 return this->t0Data_[i][d];
162 }
163
164 //! Set a value in the cube
165 virtual void setT0(Real value, Size i, Size d) override {
166 this->check(i, 0, 0, d);
167 this->t0Data_[i][d] = static_cast<T>(value);
168 }
169
170 //! Get a value from the cube
171 Real get(Size i, Size j, Size k, Size d) const override {
172 this->check(i, j, k, d);
173 return this->data_[i][j][k][d];
174 }
175
176 //! Set a value in the cube
177 void set(Real value, Size i, Size j, Size k, Size d) override {
178 this->check(i, j, k, d);
179 this->data_[i][j][k][d] = static_cast<T>(value);
180 }
181};
182
183//! InMemoryCube of depth 1 with single precision floating point numbers.
185
186//! InMemoryCube of depth 1 with double precision floating point numbers.
188
189//! InMemoryCube of depth N with single precision floating point numbers.
191
192//! InMemoryCube of depth N with double precision floating point numbers.
194} // namespace analytics
195} // namespace ore
InMemoryCube of fixed depth 1.
virtual Real getT0(Size i, Size d) const override
Get a T0 value from the cube.
virtual void setT0(Real value, Size i, Size d) override
Set a value in the cube.
Size depth() const override
Fixed depth.
Real get(Size i, Size j, Size k, Size d) const override
Get a value from the cube.
InMemoryCube1(const Date &asof, const std::set< std::string > &ids, const vector< Date > &dates, Size samples, const T &t=T())
ctor
void set(Real value, Size i, Size j, Size k, Size d) override
Set a value in the cube.
InMemoryCube stores the cube in memory using nested STL vectors.
Size numDates() const override
const std::map< std::string, Size > & idsAndIndexes() const override
Return a map of all ids and their position in the cube.
Size numIds() const override
Return the length of each dimension.
vector< vector< vector< T > > > data_
void check(Size i, Size j, Size k, Size d) const
virtual Size samples() const override
const std::vector< QuantLib::Date > & dates() const override
Get the vector of dates for this cube.
InMemoryCubeBase()
default constructor
QuantLib::Date asof() const override
Return the asof date (T0 date)
std::map< std::string, Size > idIdx_
vector< QuantLib::Date > dates_
InMemoryCubeBase(const Date &asof, const std::set< std::string > &ids, const vector< Date > &dates, Size samples, const T &t=T())
default ctor
InMemoryCube of variable depth.
InMemoryCubeN(const Date &asof, const std::set< std::string > &ids, const vector< Date > &dates, Size samples, Size depth, const T &t=T())
ctor
virtual Real getT0(Size i, Size d) const override
Get a T0 value from the cube.
virtual void setT0(Real value, Size i, Size d) override
Set a value in the cube.
Size depth() const override
Depth.
Real get(Size i, Size j, Size k, Size d) const override
Get a value from the cube.
void set(Real value, Size i, Size j, Size k, Size d) override
Set a value in the cube.
NPV Cube class stores both future and current NPV values.
Definition: npvcube.hpp:53
virtual Size depth() const =0
const std::set< std::string > ids() const
Get a set of all ids in the cube.
Definition: npvcube.hpp:75
SafeStack< ValueType > value
Size size(const ValueType &v)
The base NPV cube class.