Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
sensicube.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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/sensicube.hpp
20 \brief A cube implementation that stores the cube in memory
21 \ingroup cube
22*/
23#pragma once
24
25
30
31#include <ql/errors.hpp>
32
33#include <boost/make_shared.hpp>
34#include <boost/math/special_functions/relative_difference.hpp>
35
36#include <fstream>
37#include <iostream>
38#include <vector>
39
40
41namespace ore {
42namespace analytics {
43
44//! SensiCube stores only npvs not equal to the base npvs
45template <typename T> class SensiCube : public NPVSensiCube {
46public:
47 SensiCube(const std::set<std::string>& ids, const QuantLib::Date& asof, QuantLib::Size samples, const T& t = T())
49 tradeNPVs_(ids.size(), map<Size, T>()) {
50 Size pos = 0;
51 for (const auto& id : ids) {
52 idIdx_[id] = pos++;
53 }
54 }
55
56 //! Return the length of each dimension
57 QuantLib::Size numIds() const override { return idIdx_.size(); }
58 QuantLib::Size samples() const override { return samples_; }
59
60 //! Get the vector of ids for this cube
61 const std::map<std::string, Size>& idsAndIndexes() const override { return idIdx_; }
62
63 //! Get the vector of dates for this cube
64 const std::vector<QuantLib::Date>& dates() const override { return dates_; }
65
66 //! Return the asof date (T0 date)
67 QuantLib::Date asof() const override { return asof_; }
68
69 //! Get a T0 value from the cube
70 Real getT0(Size i, Size) const override {
71 this->check(i, 0, 0);
72 return this->t0Data_[i];
73 }
74
75 //! Set a value in the cube
76 void setT0(Real value, Size i, Size) override {
77 this->check(i, 0, 0);
78 this->t0Data_[i] = static_cast<T>(value);
79 }
80
81 //! Get a value from the cube
82 Real get(Size i, Size j, Size k, Size) const override {
83 this->check(i, j, k);
84
85 auto itr = this->tradeNPVs_[i].find(k);
86 if (itr != tradeNPVs_[i].end()) {
87 return itr->second;
88 } else {
89 return this->t0Data_[i];
90 }
91 }
92
93 //! Set a value in the cube
94 void set(Real value, Size i, Size j, Size k, Size) override {
95 this->check(i, j, k);
96 T castValue = static_cast<T>(value);
97 if (boost::math::epsilon_difference<T>(castValue, t0Data_[i]) > 42) {
98 this->tradeNPVs_[i][k] = castValue;
99 relevantScenarios_.insert(k);
100 }
101 }
102
103 void remove(Size i) override {
104 this->check(i,0,0);
105 this->t0Data_[i] = 0.0;
106 this->tradeNPVs_[i].clear();
107 }
108
109 void remove(Size i, Size k) override {
110 this->check(i,0,k);
111 this->tradeNPVs_[i].erase(k);
112 }
113
114 std::map<QuantLib::Size, QuantLib::Real> getTradeNPVs(QuantLib::Size i) const override { return tradeNPVs_[i]; }
115
116 std::set<QuantLib::Size> relevantScenarios() const override { return relevantScenarios_; }
117
118private:
119 std::map<std::string, Size> idIdx_;
120 QuantLib::Date asof_;
121 std::vector<QuantLib::Date> dates_;
122 QuantLib::Size samples_;
123
124protected:
125 std::vector<T> t0Data_;
126 std::vector<std::map<QuantLib::Size, T>> tradeNPVs_;
127 std::set<QuantLib::Size> relevantScenarios_;
128
129 void check(QuantLib::Size i, QuantLib::Size j, QuantLib::Size k) const {
130 QL_REQUIRE(i < numIds(), "Out of bounds on ids (i=" << i << ")");
131 QL_REQUIRE(j < depth(), "Out of bounds on depth (j=" << j << ")");
132 QL_REQUIRE(k < samples(), "Out of bounds on samples (k=" << k << ")");
133 }
134};
135
136//! Sensi cube with single precision floating point numbers.
138
139//! Sensi cube with double precision floating point numbers.
141
142} // namespace analytics
143} // namespace ore
const std::set< std::string > ids() const
Get a set of all ids in the cube.
Definition: npvcube.hpp:75
NPVSensiCube class stores NPVs resulting from risk factor shifts on an as of date.
QuantLib::Size depth() const override
The depth in the NPVSensiCube is exactly one.
SensiCube stores only npvs not equal to the base npvs.
Definition: sensicube.hpp:45
QuantLib::Size samples() const override
Definition: sensicube.hpp:58
QuantLib::Size samples_
Definition: sensicube.hpp:122
std::set< QuantLib::Size > relevantScenarios() const override
Definition: sensicube.hpp:116
std::vector< std::map< QuantLib::Size, T > > tradeNPVs_
Definition: sensicube.hpp:126
const std::map< std::string, Size > & idsAndIndexes() const override
Get the vector of ids for this cube.
Definition: sensicube.hpp:61
void setT0(Real value, Size i, Size) override
Set a value in the cube.
Definition: sensicube.hpp:76
std::vector< QuantLib::Date > dates_
Definition: sensicube.hpp:121
QuantLib::Size numIds() const override
Return the length of each dimension.
Definition: sensicube.hpp:57
void remove(Size i, Size k) override
Definition: sensicube.hpp:109
Real getT0(Size i, Size) const override
Get a T0 value from the cube.
Definition: sensicube.hpp:70
const std::vector< QuantLib::Date > & dates() const override
Get the vector of dates for this cube.
Definition: sensicube.hpp:64
void remove(Size i) override
Definition: sensicube.hpp:103
void set(Real value, Size i, Size j, Size k, Size) override
Set a value in the cube.
Definition: sensicube.hpp:94
std::vector< T > t0Data_
Definition: sensicube.hpp:125
QuantLib::Date asof() const override
Return the asof date (T0 date)
Definition: sensicube.hpp:67
std::map< QuantLib::Size, QuantLib::Real > getTradeNPVs(QuantLib::Size i) const override
Definition: sensicube.hpp:114
void check(QuantLib::Size i, QuantLib::Size j, QuantLib::Size k) const
Definition: sensicube.hpp:129
std::map< std::string, Size > idIdx_
Definition: sensicube.hpp:119
std::set< QuantLib::Size > relevantScenarios_
Definition: sensicube.hpp:127
SensiCube(const std::set< std::string > &ids, const QuantLib::Date &asof, QuantLib::Size samples, const T &t=T())
Definition: sensicube.hpp:47
Real get(Size i, Size j, Size k, Size) const override
Get a value from the cube.
Definition: sensicube.hpp:82
SafeStack< ValueType > value
Size size(const ValueType &v)
An NPV cube for storing NPVs resulting from risk factor shifts.