Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
sparsenpvcube.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
20
22
23#include <ql/errors.hpp>
24#include <ql/math/comparison.hpp>
25
26#include <boost/make_shared.hpp>
27
28#include <fstream>
29
30namespace ore {
31namespace analytics {
32
33template <typename T> SparseNpvCube<T>::SparseNpvCube() {}
34
35template <typename T>
36SparseNpvCube<T>::SparseNpvCube(const Date& asof, const std::set<std::string>& ids, const std::vector<Date>& dates,
37 Size samples, Size depth, const T& t)
38 : asof_(asof), dates_(dates), samples_(samples), depth_(depth) {
39 QL_REQUIRE(ids.size() > 0, "SparseNpvCube::SparseNpvCube no ids specified");
40 QL_REQUIRE(dates.size() > 0, "SparseNpvCube::SparseNpvCube no dates specified");
41 QL_REQUIRE(samples > 0, "SparseNpvCube::SparseNpvCube samples must be > 0");
42 QL_REQUIRE(depth > 0, "SparseNpvCube::SparseNpvCube depth must be > 0");
43 Size check = std::numeric_limits<Size>::max();
44 check /= ids.size();
45 check /= dates.size();
46 check /= depth;
47 QL_REQUIRE(check >= 1, "SparseNpvCube::SparseNpvCube: total size exceeded: ids ("
48 << ids_.size() << ") * dates (" << dates.size() << ") * depth (" << depth << ") > "
49 << std::numeric_limits<Size>::max());
50 Size pos = 0;
51 for (const auto& id : ids) {
52 ids_[id] = pos++;
53 }
54}
55
56template <typename T> Size SparseNpvCube<T>::numIds() const { return ids_.size(); }
57template <typename T> Size SparseNpvCube<T>::numDates() const { return dates_.size(); }
58template <typename T> Size SparseNpvCube<T>::samples() const { return samples_; }
59template <typename T> Size SparseNpvCube<T>::depth() const { return depth_; }
60template <typename T> Date SparseNpvCube<T>::asof() const { return asof_; }
61template <typename T> const std::map<std::string, Size>& SparseNpvCube<T>::idsAndIndexes() const { return ids_; }
62template <typename T> const std::vector<QuantLib::Date>& SparseNpvCube<T>::dates() const { return dates_; }
63
64template <typename T> Size SparseNpvCube<T>::pos(Size i, Size j, Size d) const {
65 return (i * (numDates() + 1) + j) * depth() + d;
66}
67
68template <typename T> Real SparseNpvCube<T>::getT0(Size i, Size d) const {
69 this->check(i, 0, 0, d);
70 auto v = data_.find(pos(i, 0, d));
71 if (v != data_.end())
72 return static_cast<Real>(v->second[0]);
73 else
74 return static_cast<Real>(0.0);
75}
76
77template <typename T> void SparseNpvCube<T>::setT0(Real value, Size i, Size d) {
78 this->check(i, 0, 0, d);
79 if (QuantLib::close_enough(value, 0.0))
80 return;
81 data_[pos(i, 0, d)] = std::vector<T>(1, static_cast<T>(value));
82}
83
84template <typename T> Real SparseNpvCube<T>::get(Size i, Size j, Size k, Size d) const {
85 this->check(i, j, k, d);
86 auto v = data_.find(pos(i, j + 1, d));
87 if (v != data_.end())
88 return static_cast<Real>(v->second[k]);
89 else
90 return static_cast<Real>(0.0);
91}
92
93template <typename T> void SparseNpvCube<T>::set(Real value, Size i, Size j, Size k, Size d) {
94 this->check(i, j, k, d);
95 if (QuantLib::close_enough(value, 0.0))
96 return;
97 auto v = data_.find(pos(i, j + 1, d));
98 if (v != data_.end()) {
99 v->second[k] = static_cast<T>(value);
100 } else {
101 std::vector<T> tmp(samples(), 0.0);
102 tmp[k] = static_cast<T>(value);
103 data_[pos(i, j + 1, d)] = tmp;
104 }
105}
106
107template <typename T> void SparseNpvCube<T>::check(Size i, Size j, Size k, Size d) const {
108 QL_REQUIRE(i < numIds(), "Out of bounds on ids (i=" << i << ", numIds=" << numIds() << ")");
109 QL_REQUIRE(j < numDates(), "Out of bounds on dates (j=" << j << ", numDates=" << numDates() << ")");
110 QL_REQUIRE(k < samples(), "Out of bounds on samples (k=" << k << ", samples=" << samples() << ")");
111 QL_REQUIRE(d < depth(), "Out of bounds on depth (d=" << d << ", depth=" << depth() << ")");
112}
113
114// template instantiations for Real and float
115
116template SparseNpvCube<Real>::SparseNpvCube(const Date& asof, const std::set<std::string>& ids,
117 const std::vector<Date>& dates, Size samples, Size depth, const Real& t);
118template Size SparseNpvCube<Real>::numIds() const;
119template Size SparseNpvCube<Real>::numDates() const;
120template Size SparseNpvCube<Real>::samples() const;
121template Size SparseNpvCube<Real>::depth() const;
122template Date SparseNpvCube<Real>::asof() const;
123template const std::map<std::string, Size>& SparseNpvCube<Real>::idsAndIndexes() const;
124template const std::vector<QuantLib::Date>& SparseNpvCube<Real>::dates() const;
125template Size SparseNpvCube<Real>::pos(Size i, Size j, Size d) const;
126template Real SparseNpvCube<Real>::getT0(Size i, Size d) const;
127template void SparseNpvCube<Real>::setT0(Real value, Size i, Size d);
128template Real SparseNpvCube<Real>::get(Size i, Size j, Size k, Size d) const;
129template void SparseNpvCube<Real>::set(Real value, Size i, Size j, Size k, Size d);
130template void SparseNpvCube<Real>::check(Size i, Size j, Size k, Size d) const;
131
133template SparseNpvCube<float>::SparseNpvCube(const Date& asof, const std::set<std::string>& ids,
134 const std::vector<Date>& dates, Size samples, Size depth, const float& t);
135template Size SparseNpvCube<float>::numIds() const;
136template Size SparseNpvCube<float>::numDates() const;
137template Size SparseNpvCube<float>::samples() const;
138template Size SparseNpvCube<float>::depth() const;
139template Date SparseNpvCube<float>::asof() const;
140template const std::map<std::string, Size>& SparseNpvCube<float>::idsAndIndexes() const;
141template const std::vector<QuantLib::Date>& SparseNpvCube<float>::dates() const;
142template Size SparseNpvCube<float>::pos(Size i, Size j, Size d) const;
143template Real SparseNpvCube<float>::getT0(Size i, Size d) const;
144template void SparseNpvCube<float>::setT0(Real value, Size i, Size d);
145template Real SparseNpvCube<float>::get(Size i, Size j, Size k, Size d) const;
146template void SparseNpvCube<float>::set(Real value, Size i, Size j, Size k, Size d);
147template void SparseNpvCube<float>::check(Size i, Size j, Size k, Size d) const;
148
149} // namespace analytics
150} // namespace ore
const std::set< std::string > ids() const
Get a set of all ids in the cube.
Definition: npvcube.hpp:75
Size pos(Size i, Size j, Size d) const
Date asof() const override
Return the asof date (T0 date)
Real get(Size i, Size j, Size k, Size d) const override
Get a value from the cube using index.
Size numIds() const override
Return the length of each dimension.
Size depth() const override
Real getT0(Size i, Size d) const override
Get a T0 value from the cube using index.
void setT0(Real value, Size i, Size d) override
Set a value in the cube using index.
Size numDates() const override
void set(Real value, Size i, Size j, Size k, Size d) override
Set a value in the cube using index.
Size samples() const override
std::map< std::string, Size > ids_
void check(Size i, Size j, Size k, Size d) const
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::Date > & dates() const override
Get the vector of dates for this cube.
SafeStack< ValueType > value
in memory cube, storing only non-zero entries for (id, date, depth)
Date asof(14, Jun, 2018)