19#include <boost/algorithm/string.hpp>
25#include <ql/errors.hpp>
38void CubeCsvReader::read(QuantLib::ext::shared_ptr<NPVCube>& cube, std::map<std::string, std::string>& nettingSetMap) {
40 std::set<string> tradeIds;
41 std::vector<Date> dateVec;
42 std::set<Size> sampleIdxSet, depthIdxSet;
47 QL_REQUIRE(file.is_open(),
"error opening file " <<
filename_);
48 bool headerAlreadyLoaded1 =
false;
53 if (line.size() > 0 && line[0] !=
'#') {
54 if (!headerAlreadyLoaded1) {
55 headerAlreadyLoaded1 =
true;
58 vector<string> tokens;
60 boost::split(tokens, line, boost::is_any_of(
",;\t"), boost::token_compress_on);
61 QL_REQUIRE(tokens.size() == 7,
"Invalid CubeCsvReader line, 7 tokens expected " << line);
62 string tradeId = tokens[0];
63 string nettingId = tokens[1];
71 }
else if (std::find(dateVec.begin(), dateVec.end(), gridDate) == dateVec.end()) {
72 dateVec.push_back(gridDate);
74 tradeIds.insert(tradeId);
75 if (nettingSetMap.find(tradeId) == nettingSetMap.end())
76 nettingSetMap[tradeId] = nettingId;
77 sampleIdxSet.insert(sampleIdx);
78 depthIdxSet.insert(depthIdx);
83 QL_REQUIRE(dateVec.size() > 0,
"CubeCsvReader - no simulation dates found");
84 QL_REQUIRE(tradeIds.size() > 0,
"CubeCsvReader - no trades found");
85 Size numSamples = sampleIdxSet.size() - 1;
86 Size cubeDepth = depthIdxSet.size();
87 QL_REQUIRE(numSamples > 0,
"CubeCsvReader - no simulation paths found");
88 QL_REQUIRE(cubeDepth > 0,
"CubeCsvReader - no cube depth");
89 QL_REQUIRE(nettingSetMap.size() == tradeIds.size(),
90 "CubeCsvReader - vector size mismatch - trade Ids vs netting map");
91 for (Size i = 0; i < dateVec.size(); ++i) {
92 QL_REQUIRE(dateVec[i] >
asof,
"CubeCsvReader - grid date " << QuantLib::io::iso_date(dateVec[i])
93 <<
"must be greater than asof "
94 << QuantLib::io::iso_date(
asof));
96 QL_REQUIRE(dateVec[i] > dateVec[i - 1],
"CubeCsvReader - date grid must be monotonic increasing");
100 cube = QuantLib::ext::make_shared<SinglePrecisionInMemoryCube>(
asof, tradeIds, dateVec, numSamples);
101 else if (cubeDepth > 1)
102 cube = QuantLib::ext::make_shared<SinglePrecisionInMemoryCubeN>(
asof, tradeIds, dateVec, numSamples, cubeDepth);
107 QL_REQUIRE(file2.is_open(),
"error opening file " <<
filename_);
108 bool headerAlreadyLoaded =
false;
109 while (!file2.eof()) {
111 getline(file2, line);
113 if (line.size() > 0 && line[0] !=
'#') {
114 if (!headerAlreadyLoaded) {
115 headerAlreadyLoaded =
true;
118 vector<string> tokens;
120 boost::split(tokens, line, boost::is_any_of(
",;\t"), boost::token_compress_on);
121 QL_REQUIRE(tokens.size() == 7,
"Invalid CubeCsvReader line, 7 tokens expected " << line);
122 string tradeId = tokens[0];
123 string nettingId = tokens[1];
130 auto pos_trade = cube->getTradeIndex(tradeId);
133 cube->setT0(
value, pos_trade, depthIdx);
135 QL_REQUIRE(sampleIdx > 0,
"CubeCsvReader - input sampleIdx should be > 0");
136 cube->set(
value, pos_trade, dateIdx - 1, sampleIdx - 1, depthIdx);
CubeCsvReader(const std::string &filename)
ctor
void read(QuantLib::ext::shared_ptr< ore::analytics::NPVCube > &cube, std::map< std::string, std::string > &nettingSetMap)
Read a cube from a csv file.
SafeStack< ValueType > value
A Class to read a cube file from csv input.
Date parseDate(const string &s)
Real parseReal(const string &s)
Integer parseInteger(const string &s)
A cube implementation that stores the cube in memory.