Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
CubeCsvReader Class Reference

Read an NPV cube from a human readable text file. More...

#include <orea/cube/cubecsvreader.hpp>

+ Collaboration diagram for CubeCsvReader:

Public Member Functions

 CubeCsvReader (const std::string &filename)
 ctor More...
 
const std::string & filename () const
 Return the filename this reader is reader from. More...
 
void read (QuantLib::ext::shared_ptr< ore::analytics::NPVCube > &cube, std::map< std::string, std::string > &nettingSetMap)
 Read a cube from a csv file. More...
 

Private Attributes

std::string filename_
 

Detailed Description

Read an NPV cube from a human readable text file.

Definition at line 37 of file cubecsvreader.hpp.

Constructor & Destructor Documentation

◆ CubeCsvReader()

CubeCsvReader ( const std::string &  filename)

ctor

Definition at line 36 of file cubecsvreader.cpp.

const std::string & filename() const
Return the filename this reader is reader from.

Member Function Documentation

◆ filename()

const std::string & filename ( ) const

Return the filename this reader is reader from.

Definition at line 43 of file cubecsvreader.hpp.

43{ return filename_; }

◆ read()

void read ( QuantLib::ext::shared_ptr< ore::analytics::NPVCube > &  cube,
std::map< std::string, std::string > &  nettingSetMap 
)

Read a cube from a csv file.

Definition at line 38 of file cubecsvreader.cpp.

38 {
39
40 std::set<string> tradeIds;
41 std::vector<Date> dateVec;
42 std::set<Size> sampleIdxSet, depthIdxSet;
43 Date asof;
44
45 std::ifstream file;
46 file.open(filename_.c_str());
47 QL_REQUIRE(file.is_open(), "error opening file " << filename_);
48 bool headerAlreadyLoaded1 = false;
49 while (!file.eof()) {
50 string line;
51 getline(file, line);
52 // skip blank and comment lines
53 if (line.size() > 0 && line[0] != '#') {
54 if (!headerAlreadyLoaded1) {
55 headerAlreadyLoaded1 = true;
56 continue;
57 }
58 vector<string> tokens;
59 boost::trim(line);
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];
64 Size dateIdx = ore::data::parseInteger(tokens[2]);
65 Date gridDate = ore::data::parseDate(tokens[3]);
66 Size sampleIdx = ore::data::parseInteger(tokens[4]);
67 Size depthIdx = ore::data::parseInteger(tokens[5]);
68
69 if (dateIdx == 0) {
70 asof = gridDate;
71 } else if (std::find(dateVec.begin(), dateVec.end(), gridDate) == dateVec.end()) {
72 dateVec.push_back(gridDate);
73 }
74 tradeIds.insert(tradeId);
75 if (nettingSetMap.find(tradeId) == nettingSetMap.end())
76 nettingSetMap[tradeId] = nettingId;
77 sampleIdxSet.insert(sampleIdx);
78 depthIdxSet.insert(depthIdx);
79 }
80 }
81 file.close();
82
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; // zero represents t0 data
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));
95 if (i > 0) {
96 QL_REQUIRE(dateVec[i] > dateVec[i - 1], "CubeCsvReader - date grid must be monotonic increasing");
97 }
98 }
99 if (cubeDepth == 1)
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);
103
104 // Now re-open the file and loop through its contents AGAIN
105 std::ifstream file2;
106 file2.open(filename_.c_str());
107 QL_REQUIRE(file2.is_open(), "error opening file " << filename_);
108 bool headerAlreadyLoaded = false;
109 while (!file2.eof()) {
110 string line;
111 getline(file2, line);
112 // skip blank and comment lines
113 if (line.size() > 0 && line[0] != '#') {
114 if (!headerAlreadyLoaded) {
115 headerAlreadyLoaded = true;
116 continue;
117 }
118 vector<string> tokens;
119 boost::trim(line);
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];
124 Size dateIdx = ore::data::parseInteger(tokens[2]);
125 // Date gridDate = ore::data::parseDate(tokens[3]); Not needed
126 Size sampleIdx = ore::data::parseInteger(tokens[4]);
127 Size depthIdx = ore::data::parseInteger(tokens[5]);
128 Real value = ore::data::parseReal(tokens[6]);
129
130 auto pos_trade = cube->getTradeIndex(tradeId);
131
132 if (dateIdx == 0) {
133 cube->setT0(value, pos_trade, depthIdx);
134 } else {
135 QL_REQUIRE(sampleIdx > 0, "CubeCsvReader - input sampleIdx should be > 0");
136 cube->set(value, pos_trade, dateIdx - 1, sampleIdx - 1, depthIdx);
137 }
138 }
139 }
140 file2.close();
141}
SafeStack< ValueType > value
Date parseDate(const string &s)
Real parseReal(const string &s)
Integer parseInteger(const string &s)
Date asof(14, Jun, 2018)
+ Here is the call graph for this function:

Member Data Documentation

◆ filename_

std::string filename_
private

Definition at line 49 of file cubecsvreader.hpp.