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

Class for writing scenarios to file. More...

#include <orea/scenario/scenariowriter.hpp>

+ Inheritance diagram for ScenarioWriter:
+ Collaboration diagram for ScenarioWriter:

Public Member Functions

 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. More...
 
 ScenarioWriter (const std::string &filename, const char sep=',', const string &filemode="w+", const std::vector< RiskFactorKey > &headerKeys={})
 Constructor to write single scenarios. More...
 
 ScenarioWriter (const QuantLib::ext::shared_ptr< ScenarioGenerator > &src, QuantLib::ext::shared_ptr< ore::data::Report > report, const std::vector< RiskFactorKey > &headerKeys={})
 Constructor to write into an in-memory report for later io. More...
 
virtual ~ScenarioWriter ()
 Destructor. More...
 
virtual QuantLib::ext::shared_ptr< Scenarionext (const Date &d) override
 Return the next scenario for the given date. More...
 
void writeScenario (const QuantLib::ext::shared_ptr< Scenario > &s, const bool writeHeader)
 Write a single scenario. More...
 
virtual void reset () override
 Reset the generator so calls to next() return the first scenario. More...
 
void close ()
 Close the file if it is open, not normally needed by client code. More...
 
- Public Member Functions inherited from ScenarioGenerator
virtual ~ScenarioGenerator ()
 Default destructor. More...
 
virtual QuantLib::ext::shared_ptr< Scenarionext (const Date &d)=0
 Return the next scenario for the given date. More...
 
virtual void reset ()=0
 Reset the generator so calls to next() return the first scenario. More...
 

Private Member Functions

void open (const std::string &filename, const std::string &filemode="w+")
 

Private Attributes

QuantLib::ext::shared_ptr< ScenarioGeneratorsrc_
 
std::vector< RiskFactorKeykeys_
 
QuantLib::ext::shared_ptr< ore::data::Reportreport_
 
FILE * fp_
 
Date firstDate_
 
Size i_
 
const char sep_ = ','
 
std::vector< RiskFactorKeyheaderKeys_
 

Detailed Description

Class for writing scenarios to file.

Definition at line 34 of file scenariowriter.hpp.

Constructor & Destructor Documentation

◆ ScenarioWriter() [1/3]

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.

Definition at line 27 of file scenariowriter.cpp.

29 : src_(src), fp_(nullptr), i_(0), sep_(sep), headerKeys_(headerKeys) {
30 open(filename, filemode);
31}
std::vector< RiskFactorKey > headerKeys_
QuantLib::ext::shared_ptr< ScenarioGenerator > src_
void open(const std::string &filename, const std::string &filemode="w+")
+ Here is the call graph for this function:

◆ ScenarioWriter() [2/3]

ScenarioWriter ( const std::string &  filename,
const char  sep = ',',
const string &  filemode = "w+",
const std::vector< RiskFactorKey > &  headerKeys = {} 
)

Constructor to write single scenarios.

Definition at line 33 of file scenariowriter.cpp.

35 : fp_(nullptr), i_(0), sep_(sep), headerKeys_(headerKeys) {
36 open(filename, filemode);
37}
+ Here is the call graph for this function:

◆ ScenarioWriter() [3/3]

ScenarioWriter ( const QuantLib::ext::shared_ptr< ScenarioGenerator > &  src,
QuantLib::ext::shared_ptr< ore::data::Report report,
const std::vector< RiskFactorKey > &  headerKeys = {} 
)

Constructor to write into an in-memory report for later io.

Definition at line 39 of file scenariowriter.cpp.

42 : src_(src), report_(report), fp_(nullptr), i_(0), sep_(','), headerKeys_(headerKeys) {}
QuantLib::ext::shared_ptr< ore::data::Report > report_

◆ ~ScenarioWriter()

~ScenarioWriter ( )
virtual

Destructor.

Definition at line 49 of file scenariowriter.cpp.

49{ close(); }
void close()
Close the file if it is open, not normally needed by client code.
+ Here is the call graph for this function:

Member Function Documentation

◆ next()

QuantLib::ext::shared_ptr< Scenario > next ( const Date &  d)
overridevirtual

Return the next scenario for the given date.

Implements ScenarioGenerator.

Definition at line 66 of file scenariowriter.cpp.

66 {
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}
void writeScenario(const QuantLib::ext::shared_ptr< Scenario > &s, const bool writeHeader)
Write a single scenario.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeScenario()

void writeScenario ( const QuantLib::ext::shared_ptr< Scenario > &  s,
const bool  writeHeader 
)

Write a single scenario.

Definition at line 73 of file scenariowriter.cpp.

73 {
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}
std::vector< RiskFactorKey > keys_
std::string to_string(const LocationInfo &l)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reset()

void reset ( )
overridevirtual

Reset the generator so calls to next() return the first scenario.

Implements ScenarioGenerator.

Definition at line 51 of file scenariowriter.cpp.

51 {
52 if (src_)
53 src_->reset();
54 close();
55}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ close()

void close ( )

Close the file if it is open, not normally needed by client code.

Definition at line 57 of file scenariowriter.cpp.

57 {
58 if (fp_) {
59 fclose(fp_);
60 fp_ = nullptr;
61 }
62 if (report_)
63 report_->end();
64}
+ Here is the caller graph for this function:

◆ open()

void open ( const std::string &  filename,
const std::string &  filemode = "w+" 
)
private

Definition at line 44 of file scenariowriter.cpp.

44 {
45 fp_ = fopen(filename.c_str(), filemode.c_str());
46 QL_REQUIRE(fp_, "Error opening file " << filename << " for scenarios");
47}
+ Here is the caller graph for this function:

Member Data Documentation

◆ src_

QuantLib::ext::shared_ptr<ScenarioGenerator> src_
private

Definition at line 66 of file scenariowriter.hpp.

◆ keys_

std::vector<RiskFactorKey> keys_
private

Definition at line 67 of file scenariowriter.hpp.

◆ report_

QuantLib::ext::shared_ptr<ore::data::Report> report_
private

Definition at line 68 of file scenariowriter.hpp.

◆ fp_

FILE* fp_
private

Definition at line 69 of file scenariowriter.hpp.

◆ firstDate_

Date firstDate_
private

Definition at line 70 of file scenariowriter.hpp.

◆ i_

Size i_
private

Definition at line 71 of file scenariowriter.hpp.

◆ sep_

const char sep_ = ','
private

Definition at line 72 of file scenariowriter.hpp.

◆ headerKeys_

std::vector<RiskFactorKey> headerKeys_
private

Definition at line 73 of file scenariowriter.hpp.