Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
csvscenariogenerator.cpp
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#include <boost/algorithm/string.hpp>
20#include <fstream>
21#include <string>
22
26
28using namespace ore::data;
29using QuantLib::Size;
30
31namespace ore {
32namespace analytics {
33
35 const QuantLib::ext::shared_ptr<ScenarioFactory> scenarioFactory, const char sep)
36 : sep_(sep), filename_(filename), scenarioFactory_(scenarioFactory) {
37 file_.open(filename_.c_str());
38 QL_REQUIRE(file_.is_open(), "error opening file " << filename_);
39 readKeys();
40}
41
43 QL_REQUIRE(!file_.eof(), "error reading file: No header found in scenariofile" << filename_);
44 string line;
45 getline(file_, line);
46 boost::trim(line);
47 string strsep(1, sep_);
48 vector<string> tokens;
49 boost::split(tokens, line, boost::is_any_of(strsep), boost::token_compress_on);
50 QL_REQUIRE(tokens.size() > 3, "No RiskFactorKeys found in " << filename_);
51 keys_.resize(tokens.size() - 3);
52 for (Size i = 3; i < tokens.size(); i++) {
53 keys_[i - 3] = parseRiskFactorKey(tokens[i]);
54 }
55}
56QuantLib::ext::shared_ptr<Scenario> CSVScenarioGenerator::next(const Date& d) {
57 // Read in the next line
58 QL_REQUIRE(!file_.eof(), "unexpected end of scenario file " << filename_);
59 string line;
60 getline(file_, line);
61
62 // Split line into tokens
63 vector<string> tokens;
64 boost::trim(line);
65 string strsep(1, sep_);
66 boost::split(tokens, line, boost::is_any_of(strsep), boost::token_compress_on);
67
68 // Check that dates match
69 QL_REQUIRE(to_string(d) == tokens[0], "Incompatible date " << tokens[0] << " in " << filename_);
70
71 // Build scenario
72 const QuantLib::ext::shared_ptr<Scenario> scenario = scenarioFactory_->buildScenario(d, true);
73
74 // Fill scenario with RiskFactorKeys
75 QL_REQUIRE(keys_.size() == tokens.size() - 3, "Erroneus line in " << filename_);
76 for (Size i = 3; i < tokens.size(); i++) {
77 scenario->add(keys_[i - 3], parseReal(tokens[i]));
78 }
79
80 return scenario;
81}
82
84 file_.seekg(std::ios::beg);
85 string dummy;
86 getline(file_, dummy);
87}
88
90} // namespace analytics
91} // namespace ore
const QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory_
CSVScenarioGenerator(const std::string &filename, const QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory, const char sep=',')
virtual QuantLib::ext::shared_ptr< Scenario > next(const Date &d) override
Return the next scenario for the given date.
virtual void reset() override
Reset the generator so calls to next() return the first scenario.
Real parseReal(const string &s)
RiskFactorKey parseRiskFactorKey(const string &str)
Definition: scenario.cpp:183
std::string to_string(const LocationInfo &l)