24#include <boost/filesystem.hpp>
25#include <boost/iostreams/device/file_descriptor.hpp>
27#include <boost/iostreams/filter/gzip.hpp>
29#include <boost/iostreams/filtering_stream.hpp>
39bool use_compression(
const std::string& filename) {
43 std::string extension = boost::filesystem::path(filename).extension().string();
44 return extension !=
".csv" && extension !=
".txt";
50std::string getMetaData(
const std::string& line,
const std::string& tag,
const bool mandatory =
true) {
54 QL_REQUIRE(!mandatory || line.substr(0, 1) ==
"#",
55 "internal error: getMetaData(" << line <<
", " << tag <<
"): line does not start with #");
56 QL_REQUIRE(!mandatory || line.substr(2, tag.size()) == tag,
57 "internal error: getMetaData(" << line <<
", " << tag <<
") failed, tag is not matched.");
58 return line.substr(0, 1) ==
"#" && line.substr(2, tag.size()) == tag ? line.substr(15) : std::string();
69 bool gzip = use_compression(filename);
70 std::ifstream in1(filename, gzip ? (std::ios::binary | std::ios::in) : std::ios::in);
71 boost::iostreams::filtering_stream<boost::iostreams::input> in;
74 in.push(boost::iostreams::gzip_decompressor());
82 std::getline(in, line);
84 std::getline(in, line);
86 std::getline(in, line);
88 std::getline(in, line);
90 std::getline(in, line);
93 std::getline(in, line);
94 getMetaData(line,
"dates");
95 std::vector<QuantLib::Date> dates;
96 for (Size i = 0; i < numDates; ++i) {
97 std::getline(in, line);
101 std::getline(in, line);
102 getMetaData(line,
"ids");
103 std::set<std::string> ids;
104 for (Size i = 0; i < numIds; ++i) {
105 std::getline(in, line);
106 ids.insert(line.substr(2));
109 std::getline(in, line);
110 if (std::string md = getMetaData(line,
"scenGenDta",
false); !md.empty()) {
113 std::getline(in, line);
114 DLOG(
"overwrite scenario generator data with meta data from cube: " << md);
117 if (std::string md = getMetaData(line,
"storeFlows",
false); !md.empty()) {
119 std::getline(in, line);
120 DLOG(
"overwrite storeFlows with meta data from cube: " << md);
123 if (std::string md = getMetaData(line,
"storeCrSt",
false); !md.empty()) {
125 std::getline(in, line);
126 DLOG(
"overwrite storeCreditStateNPVs with meta data from cube: " << md);
129 QuantLib::ext::shared_ptr<NPVCube> cube;
130 if (doublePrecision && depth <= 1) {
131 cube = QuantLib::ext::make_shared<DoublePrecisionInMemoryCube>(
asof, ids, dates, samples, 0.0);
132 }
else if (doublePrecision && depth > 1) {
133 cube = QuantLib::ext::make_shared<DoublePrecisionInMemoryCubeN>(
asof, ids, dates, samples, depth, 0.0);
134 }
else if (!doublePrecision && depth <= 1) {
135 cube = QuantLib::ext::make_shared<SinglePrecisionInMemoryCube>(
asof, ids, dates, samples, 0.0f);
136 }
else if (!doublePrecision && depth > 1) {
137 cube = QuantLib::ext::make_shared<SinglePrecisionInMemoryCubeN>(
asof, ids, dates, samples, depth, 0.0f);
141 vector<string> tokens;
144 std::getline(in, line);
147 boost::split(tokens, line, [](
char c) {
return c ==
','; });
148 QL_REQUIRE(tokens.size() == 5,
"loadCube(): invalid data line '" << line <<
"', expected 5 tokens");
155 cube->setT0(
value,
id, depth);
157 cube->set(
value,
id, date - 1, sample, depth);
161 LOG(
"loaded cube from " << filename <<
": asof = " <<
asof <<
", dim = " << numIds <<
" x " << numDates <<
" x "
162 << samples <<
" x " << depth <<
", " << nData <<
" data lines read.");
171 bool gzip = use_compression(filename);
172 std::ofstream out1(filename, gzip ? (std::ios::binary | std::ios::out) : std::ios::out);
173 boost::iostreams::filtering_stream<boost::iostreams::output> out;
176 out.push(boost::iostreams::gzip_compressor());
183 out <<
"# numIds : " << std::to_string(cube.
cube->numIds()) <<
"\n";
184 out <<
"# numDates : " << std::to_string(cube.
cube->numDates()) <<
"\n";
187 out <<
"# dates : \n";
188 for (
auto const& d : cube.
cube->dates())
192 std::map<Size, std::string> ids;
193 for (
auto const& d : cube.
cube->idsAndIndexes()) {
194 ids[d.second] = d.first;
196 for (
auto const& d : ids) {
197 out <<
"# " << d.second <<
"\n";
201 std::string scenGenDataXml =
203 out <<
"# scenGenDta : " << scenGenDataXml <<
"\n";
206 out <<
"# storeFlows : " << std::boolalpha << *cube.
storeFlows <<
"\n";
214 out << std::setprecision(doublePrecision ? std::numeric_limits<double>::max_digits10
215 : std::numeric_limits<float>::max_digits10);
219 out <<
"#id,date,sample,depth,value\n";
220 for (Size i = 0; i < cube.
cube->numIds(); ++i) {
221 out << i <<
",0,0,0," << cube.
cube->getT0(i) <<
"\n";
222 for (Size j = 0; j < cube.
cube->numDates(); ++j) {
223 for (Size k = 0; k < cube.
cube->samples(); ++k) {
224 for (Size d = 0; d < cube.
cube->depth(); ++d) {
225 double value = cube.
cube->get(i, j, k, d);
227 out << i <<
"," << (j + 1) <<
"," << k <<
"," << d <<
"," <<
value <<
"\n";
238 bool gzip = use_compression(filename);
239 std::ifstream in1(filename, gzip ? (std::ios::binary | std::ios::in) : std::ios::in);
240 boost::iostreams::filtering_stream<boost::iostreams::input> in;
243 in.push(boost::iostreams::gzip_decompressor());
251 std::getline(in, line);
253 std::getline(in, line);
256 vector<string> tokens;
258 std::getline(in, line);
260 std::vector<std::pair<AggregationScenarioDataType, std::string>> keys;
261 for (Size i = 0; i < numKeys; ++i) {
262 std::getline(in, line);
263 boost::split(tokens, line.substr(2), [](
char c) { return c ==
','; });
264 QL_REQUIRE(tokens.size() == 2,
265 "loadAggregationScenarioData(): invalid data line '" << line <<
"', expected 2 tokens");
269 std::getline(in, line);
271 QuantLib::ext::shared_ptr<InMemoryAggregationScenarioData> result =
272 QuantLib::ext::make_shared<InMemoryAggregationScenarioData>(dimDates, dimSamples);
274 std::getline(in, line);
278 std::getline(in, line);
281 boost::split(tokens, line, [](
char c) {
return c ==
','; });
282 QL_REQUIRE(tokens.size() == 4,
283 "loadAggregationScenarioData(): invalid data line '" << line <<
"', expected 4 tokens");
288 QL_REQUIRE(key < keys.size(),
"loadAggregationScenarioData(): invalid data line '" << line <<
"', key (" << key
289 <<
") is out of range 0..."
290 << (keys.size() - 1));
291 result->set(date - 1, sample,
value, keys[key].first, keys[key].second);
295 LOG(
"loaded aggregation scenario data from " << filename <<
": dimDates = " << dimDates
296 <<
", dimSamples = " << dimSamples <<
", keys = " << keys.size()
297 <<
", " << nData <<
" data lines read.");
306 bool gzip = use_compression(filename);
307 std::ofstream out1(filename, gzip ? (std::ios::binary | std::ios::out) : std::ios::out);
308 boost::iostreams::filtering_stream<boost::iostreams::output> out;
311 out.push(boost::iostreams::gzip_compressor());
317 out <<
"# dimDates : " << std::to_string(cube.
dimDates()) <<
"\n";
318 out <<
"# dimSamples : " << std::to_string(cube.
dimSamples()) <<
"\n";
320 auto keys = cube.
keys();
322 out <<
"# keys : " << std::to_string(keys.size()) <<
"\n";
323 for (
auto const& k : keys) {
324 out <<
"# " << (
unsigned int)k.first <<
"," << k.second <<
"\n";
329 out << std::setprecision(std::numeric_limits<double>::max_digits10);
333 out <<
"#date,sample,key,value\n";
334 for (Size i = 0; i < cube.
dimDates(); ++i) {
335 for (Size j = 0; j < cube.
dimSamples(); ++j) {
336 for (Size k = 0; k < keys.size(); ++k) {
337 out << (i + 1) <<
"," << j <<
"," << k <<
"," << cube.
get(i, j, keys[k].first, keys[k].second) <<
"\n";
Container for storing simulated market data.
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 Size dimSamples() const =0
virtual std::vector< std::pair< AggregationScenarioDataType, std::string > > keys() const =0
SafeStack< ValueType > value
load / save cubes and agg scen data from / to disk
Date parseDate(const string &s)
bool parseBool(const string &s)
Real parseReal(const string &s)
Integer parseInteger(const string &s)
A cube implementation that stores the cube in memory.
QuantLib::ext::shared_ptr< AggregationScenarioData > loadAggregationScenarioData(const std::string &filename)
AggregationScenarioDataType
void saveCube(const std::string &filename, const NPVCubeWithMetaData &cube, const bool doublePrecision)
void saveAggregationScenarioData(const std::string &filename, const AggregationScenarioData &cube)
NPVCubeWithMetaData loadCube(const std::string &filename, const bool doublePrecision)
std::string to_string(const LocationInfo &l)