Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenariowriter.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
21
23
24namespace ore {
25namespace analytics {
26
27ScenarioWriter::ScenarioWriter(const QuantLib::ext::shared_ptr<ScenarioGenerator>& src, const std::string& filename,
28 const char sep, const string& filemode, const std::vector<RiskFactorKey>& headerKeys)
29 : src_(src), fp_(nullptr), i_(0), sep_(sep), headerKeys_(headerKeys) {
30 open(filename, filemode);
31}
32
33ScenarioWriter::ScenarioWriter(const std::string& filename, const char sep, const string& filemode,
34 const std::vector<RiskFactorKey>& headerKeys)
35 : fp_(nullptr), i_(0), sep_(sep), headerKeys_(headerKeys) {
36 open(filename, filemode);
37}
38
39ScenarioWriter::ScenarioWriter(const QuantLib::ext::shared_ptr<ScenarioGenerator>& src,
40 QuantLib::ext::shared_ptr<ore::data::Report> report,
41 const std::vector<RiskFactorKey>& headerKeys)
42 : src_(src), report_(report), fp_(nullptr), i_(0), sep_(','), headerKeys_(headerKeys) {}
43
44void ScenarioWriter::open(const std::string& filename, const std::string& filemode) {
45 fp_ = fopen(filename.c_str(), filemode.c_str());
46 QL_REQUIRE(fp_, "Error opening file " << filename << " for scenarios");
47}
48
50
52 if (src_)
53 src_->reset();
54 close();
55}
56
58 if (fp_) {
59 fclose(fp_);
60 fp_ = nullptr;
61 }
62 if (report_)
63 report_->end();
64}
65
66QuantLib::ext::shared_ptr<Scenario> ScenarioWriter::next(const Date& d) {
67 QL_REQUIRE(src_, "No ScenarioGenerator found.");
68 QuantLib::ext::shared_ptr<Scenario> s = src_->next(d);
69 writeScenario(s, i_ == 0);
70 return s;
71}
72
73void ScenarioWriter::writeScenario(const QuantLib::ext::shared_ptr<Scenario>& s, const bool writeHeader) {
74 const Date d = s->asof();
75 // take a copy of the keys here to ensure the order is preserved
76 keys_ = s->keys();
77 std::sort(keys_.begin(), keys_.end());
78 if (fp_) {
79 if (writeHeader) {
80 QL_REQUIRE(keys_.size() > 0, "No keys in scenario");
81 fprintf(fp_, "Date%cScenario%cNumeraire%c%s", sep_, sep_, sep_, to_string(keys_[0]).c_str());
82 for (Size i = 1; i < keys_.size(); i++)
83 fprintf(fp_, "%c%s", sep_, to_string(keys_[i]).c_str());
84 fprintf(fp_, "\n");
85
86 // set the first date, this will bump i_ to 1 below
87 firstDate_ = d;
88 }
89 if (d == firstDate_)
90 i_++;
91
92 fprintf(fp_, "%s%c%zu%c%.8f", to_string(d).c_str(), sep_, i_, sep_, s->getNumeraire());
93 for (auto k : keys_)
94 fprintf(fp_, "%c%.8f", sep_, s->get(k));
95 fprintf(fp_, "\n");
96 fflush(fp_);
97 }
98
99 if (report_) {
100 if (writeHeader) {
101 QL_REQUIRE(keys_.size() > 0, "No keys in scenario");
102 if (headerKeys_.empty())
104 report_->addColumn("Date", string());
105 report_->addColumn("Scenario", Size());
106 report_->addColumn("Numeraire", double(), 8);
107 for (Size i = 0; i < headerKeys_.size(); i++)
108 report_->addColumn(to_string(headerKeys_[i]), double(), 8);
109 // set the first date, this will bump i_ to 1 below
110 firstDate_ = d;
111 }
112 if (d == firstDate_)
113 i_++;
114 report_->next();
115 report_->add(to_string(d));
116 report_->add(i_);
117 report_->add(s->getNumeraire());
118 for (auto k : headerKeys_) {
119 if (s->has(k))
120 report_->add(s->get(k));
121 else
122 report_->add(QuantLib::Null<QuantLib::Real>());
123 }
124 }
125}
126
127} // namespace analytics
128} // namespace ore
std::vector< RiskFactorKey > headerKeys_
void writeScenario(const QuantLib::ext::shared_ptr< Scenario > &s, const bool writeHeader)
Write a single scenario.
void close()
Close the file if it is open, not normally needed by client code.
virtual QuantLib::ext::shared_ptr< Scenario > next(const Date &d) override
Return the next scenario for the given date.
virtual ~ScenarioWriter()
Destructor.
ScenarioWriter(const QuantLib::ext::shared_ptr< ScenarioGenerator > &src, const std::string &filename, const char sep=',', const string &filemode="w+", const std::vector< RiskFactorKey > &headerKeys={})
Constructor.
std::vector< RiskFactorKey > keys_
QuantLib::ext::shared_ptr< ScenarioGenerator > src_
QuantLib::ext::shared_ptr< ore::data::Report > report_
virtual void reset() override
Reset the generator so calls to next() return the first scenario.
void open(const std::string &filename, const std::string &filemode="w+")
std::string to_string(const LocationInfo &l)
ScenarioWriter class.