Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
aggregationscenariodata.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 scenario/aggregationscenariodata.hpp
20 \brief this class holds data associated to scenarios
21 \ingroup scenario
22*/
23
24#pragma once
25
26#include <ql/errors.hpp>
27#include <ql/types.hpp>
28#include <ql/patterns/observable.hpp>
29
30#include <fstream>
31#include <map>
32#include <vector>
33
34namespace ore {
35namespace analytics {
36using QuantLib::Real;
37using QuantLib::Size;
38using std::map;
39using std::string;
40using std::vector;
41
42enum class AggregationScenarioDataType : unsigned int {
43 IndexFixing = 0,
44 FXSpot = 1,
45 Numeraire = 2,
46 CreditState = 3,
48 RecoveryRate = 5,
49 Generic = 6
50};
51
52//! Container for storing simulated market data
53/*! The indexes for dates and samples are (by convention) the
54 same as in the npv cube
55
56 \ingroup scenario
57*/
58class AggregationScenarioData : public QuantLib::Observable {
59public:
60 //! default ctor
62 //! Do not allow copying
65 //! dtor
67
68 //! Return the length of each dimension
69 virtual Size dimDates() const = 0;
70 virtual Size dimSamples() const = 0;
71
72 //! Check whether data is available for the given type
73 virtual bool has(const AggregationScenarioDataType& type, const string& qualifier = "") const = 0;
74
75 //! Get a value from the cube
76 virtual Real get(Size dateIndex, Size sampleIndex, const AggregationScenarioDataType& type,
77 const string& qualifier = "") const = 0;
78 //! Set a value in the cube
79 virtual void set(Size dateIndex, Size sampleIndex, Real value, const AggregationScenarioDataType& type,
80 const string& qualifier = "") = 0;
81
82 // Get available keys (type, qualifier)
83 virtual std::vector<std::pair<AggregationScenarioDataType, std::string>> keys() const = 0;
84
85 //! Set a value in the cube, assumes normal traversal of the cube (dates then samples)
86 virtual void set(Real value, const AggregationScenarioDataType& type, const string& qualifier = "") {
87 set(dIndex_, sIndex_, value, type, qualifier);
88 }
89 //! Go to the next point on the cube
90 /*! Go to the next point on the cube, assumes we do date, then samples
91 */
92 virtual void next() {
93 if (++dIndex_ == dimDates()) {
94 dIndex_ = 0;
95 sIndex_++;
96 }
97 }
98
99private:
101};
102
103//! A concrete in memory implementation of AggregationScenarioData
104/*! \ingroup scenario
105 */
107public:
111 Size dimDates() const override { return dimDates_; }
112 Size dimSamples() const override { return dimSamples_; }
113
114 bool has(const AggregationScenarioDataType& type, const string& qualifier = "") const override {
115 return data_.find(std::make_pair(type, qualifier)) != data_.end();
116 }
117
118 /*! might throw (the underlying's container exception),
119 if type is not known */
120 Real get(Size dateIndex, Size sampleIndex, const AggregationScenarioDataType& type,
121 const string& qualifier = "") const override {
122 check(dateIndex, sampleIndex, type, qualifier);
123 return data_.at(std::make_pair(type, qualifier))[dateIndex][sampleIndex];
124 }
125
126 std::vector<std::pair<AggregationScenarioDataType, std::string>> keys() const override {
127 std::vector<std::pair<AggregationScenarioDataType, std::string>> res;
128 for (auto const& k : data_)
129 res.push_back(k.first);
130 return res;
131 }
132
133 void set(Size dateIndex, Size sampleIndex, Real value, const AggregationScenarioDataType& type,
134 const string& qualifier = "") override {
135 check(dateIndex, sampleIndex, type, qualifier);
136 auto key = std::make_pair(type, qualifier);
137 auto it = data_.find(key);
138 if (it == data_.end()) {
139 data_.insert(make_pair(key, vector<vector<Real>>(dimDates_, vector<Real>(dimSamples_, 0.0))));
140 }
141 data_[key][dateIndex][sampleIndex] = value;
142 }
143
144private:
145 void check(Size dateIndex, Size sampleIndex, const AggregationScenarioDataType& type,
146 const string& qualifier) const {
147 QL_REQUIRE(dateIndex < dimDates_, "dateIndex (" << dateIndex << ") out of range 0..." << dimDates_ - 1);
148 QL_REQUIRE(sampleIndex < dimSamples_,
149 "sampleIndex (" << sampleIndex << ") out of range 0..." << dimSamples_ - 1);
150 return;
151 }
153 map<std::pair<AggregationScenarioDataType, string>, vector<vector<Real>>> data_;
154};
155
156inline std::ostream& operator<<(std::ostream& out, const AggregationScenarioDataType& t) {
157 switch (t) {
159 return out << "IndexFixing";
161 return out << "FXSpot";
163 return out << "Numeraire";
165 return out << "CreditState";
167 return out << "SurvivalWeight";
169 return out << "RecoveryRate";
171 return out << "Generic";
172 default:
173 return out << "Unknown aggregation scenario data type";
174 }
175}
176
177} // namespace analytics
178} // namespace ore
Container for storing simulated market data.
virtual void set(Real value, const AggregationScenarioDataType &type, const string &qualifier="")
Set a value in the cube, assumes normal traversal of the cube (dates then samples)
virtual Real get(Size dateIndex, Size sampleIndex, const AggregationScenarioDataType &type, const string &qualifier="") const =0
Get a value from the cube.
virtual Size dimDates() const =0
Return the length of each dimension.
virtual void next()
Go to the next point on the cube.
AggregationScenarioData & operator=(AggregationScenarioData const &)=delete
virtual Size dimSamples() const =0
virtual bool has(const AggregationScenarioDataType &type, const string &qualifier="") const =0
Check whether data is available for the given type.
AggregationScenarioData(AggregationScenarioData &)=delete
Do not allow copying.
virtual std::vector< std::pair< AggregationScenarioDataType, std::string > > keys() const =0
virtual void set(Size dateIndex, Size sampleIndex, Real value, const AggregationScenarioDataType &type, const string &qualifier="")=0
Set a value in the cube.
A concrete in memory implementation of AggregationScenarioData.
InMemoryAggregationScenarioData(Size dimDates, Size dimSamples)
Size dimDates() const override
Return the length of each dimension.
void set(Size dateIndex, Size sampleIndex, Real value, const AggregationScenarioDataType &type, const string &qualifier="") override
Set a value in the cube.
map< std::pair< AggregationScenarioDataType, string >, vector< vector< Real > > > data_
std::vector< std::pair< AggregationScenarioDataType, std::string > > keys() const override
Real get(Size dateIndex, Size sampleIndex, const AggregationScenarioDataType &type, const string &qualifier="") const override
void check(Size dateIndex, Size sampleIndex, const AggregationScenarioDataType &type, const string &qualifier) const
bool has(const AggregationScenarioDataType &type, const string &qualifier="") const override
Check whether data is available for the given type.
SafeStack< ValueType > value
std::ostream & operator<<(std::ostream &out, EquityReturnType t)