Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
historicalscenarioloader.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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
23
24using namespace QuantLib;
25using QuantLib::io::iso_date;
26using namespace QuantLib;
27
28namespace ore {
29namespace analytics {
30
31QuantLib::ext::shared_ptr<Scenario> HistoricalScenarioLoader::getHistoricalScenario(const QuantLib::Date& date) const {
32 QL_REQUIRE(historicalScenarios_.size() > 0, "No Historical Scenarios Loaded");
33
34 auto it = std::find(dates_.begin(), dates_.end(), date);
35 QL_REQUIRE(it != dates_.end(), "HistoricalScenarioLoader can't find an index for date " << date);
36
37 Size index = std::distance(dates_.begin(), it);
38 return historicalScenarios_[index];
39};
40
41HistoricalScenarioLoader::HistoricalScenarioLoader(const QuantLib::ext::shared_ptr<HistoricalScenarioReader>& scenarioReader,
42 const Date& startDate, const Date& endDate,
43 const Calendar& calendar) {
44
45 QL_REQUIRE(scenarioReader, "The historical scenario loader must be provided with a valid scenario reader");
46
47 LOG("Loading historical scenarios from " << startDate << " to " << endDate);
48
49 // Variable used to ensure that scenarios from scenario reader are ordered ascending
50 Date previousDate;
51
52 // d will hold the dates on which we request historical scenarios in the loop below
53 Date d = calendar.adjust(startDate);
54
55 while (scenarioReader->next() && d <= endDate) {
56 Date scenarioDate = scenarioReader->date();
57 QL_REQUIRE(previousDate < scenarioDate, "Require that the scenario reader provides dates in "
58 << "ascending order but we got: " << iso_date(previousDate)
59 << " >= " << iso_date(scenarioDate));
60 previousDate = scenarioDate;
61
62 // If request date (d) is less than the scenario date, advance the request
63 // date until it is greater than or equal to the first scenario date but
64 // still less than or equal to the end date
65 if (scenarioDate > d) {
66 while (d < scenarioDate && d <= endDate) {
67 DLOG("No data in file for date " << iso_date(d));
68 d = calendar.advance(d, 1 * Days);
69 }
70 }
71
72 // Skip loading a scenario if its date is before the request date
73 if (scenarioDate < d) {
74 DLOG("Skipping scenario for date " << iso_date(scenarioDate) << " as it is before next requested date "
75 << iso_date(d));
76 continue;
77 }
78
79 // If we get to here request date (d) must be equal to the scenario's date
80 if (d <= endDate) {
81 // create scenario and store it
82 DLOG("Loading scenario for date " << iso_date(d));
83 historicalScenarios_.push_back(scenarioReader->scenario());
84 dates_.push_back(d);
85
86 // Advance the request date
87 d = calendar.advance(d, 1 * Days);
88 } else {
89 DLOG("Skipping scenario for date " << iso_date(scenarioDate) << " as it is past the loader's end date "
90 << iso_date(endDate));
91 }
92 }
93
94 LOG("Loaded " << historicalScenarios_.size() << " from " << startDate << " to " << endDate);
95}
96
98 const boost::shared_ptr<HistoricalScenarioReader>& scenarioReader,
99 const std::set<Date>& dates) {
100 while (scenarioReader->next()) {
101 Date scenarioDate = scenarioReader->date();
102
103 auto it = dates.find(scenarioDate);
104 if (it == dates.end())
105 continue;
106 else {
107 historicalScenarios_.push_back(scenarioReader->scenario());
108 dates_.push_back(scenarioDate);
109 }
110 if (dates_.size() == dates.size())
111 break;
112 }
113}
114
116 const std::vector<QuantLib::ext::shared_ptr<ore::analytics::Scenario>>& scenarios,
117 const std::set<QuantLib::Date>& dates) {
118 for (const auto& s : scenarios) {
119 Date scenarioDate = s->asof();
120
121 auto it = dates.find(scenarioDate);
122 if (it == dates.end())
123 continue;
124 else {
125 historicalScenarios_.push_back(s);
126 dates_.push_back(scenarioDate);
127 }
128 if (dates_.size() == dates.size())
129 break;
130 }
131}
132
133} // namespace analytics
134} // namespace ore
QuantLib::ext::shared_ptr< ore::analytics::Scenario > getHistoricalScenario(const QuantLib::Date &date) const
Get a Scenario for a given date.
std::vector< QuantLib::Date > & dates()
Set historical scenario dates.
std::vector< QuantLib::ext::shared_ptr< ore::analytics::Scenario > > historicalScenarios_
historical scenario loader
#define LOG(text)
#define DLOG(text)
Calendar calendar