Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenariogenerator.hpp
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/*! \file scenario/scenariogenerator.hpp
20 \brief Scenario generator base classes
21 \ingroup scenario
22*/
23
24#pragma once
25
26#include <vector>
27
28#include <ql/shared_ptr.hpp>
30#include <ql/time/date.hpp>
31#include <ql/time/daycounters/actualactual.hpp>
32#include <ql/timegrid.hpp>
33
34namespace ore {
35namespace analytics {
36using QuantLib::TimeGrid;
37using std::vector;
38
39//! Scenario generator base class
40/*! \ingroup scenario
41 */
43public:
44 //! Default destructor
45 virtual ~ScenarioGenerator() {}
46
47 //! Return the next scenario for the given date.
48 virtual QuantLib::ext::shared_ptr<Scenario> next(const Date& d) = 0;
49
50 //! Reset the generator so calls to next() return the first scenario.
51 /*! This allows re-generation of scenarios if required. */
52 virtual void reset() = 0;
53};
54
55//! Scenario generator that generates an entire path
56/*! \ingroup scenario
57 */
59public:
60 // TODO: Why dates AND timegrid, why not DateGrid???
61 //! Constructor
62 ScenarioPathGenerator( //! Today's date
63 Date today,
64 //! Future evaluation dates
65 const vector<Date>& dates,
66 //! Associated time grid
67 TimeGrid timeGrid) // DayCounter dayCounter = ActualActual())
68 : today_(today), dates_(dates), timeGrid_(timeGrid) { // dayCounter_(dayCounter) {
69 QL_REQUIRE(dates.size() > 0, "empty date vector passed");
70 QL_REQUIRE(dates.front() > today, "date grid must start in the future");
71 }
72
73 virtual QuantLib::ext::shared_ptr<Scenario> next(const Date& d) override {
74 if (d == dates_.front()) { // new path
75 path_ = nextPath();
76 pathStep_ = 0;
77 }
78 QL_REQUIRE(pathStep_ < dates_.size(), "step mismatch");
79 if(d == dates_[pathStep_]){
80 return path_[pathStep_++]; // post increment
81 } else{
82 auto it = std::find(dates_.begin(), dates_.end(), d);
83 QL_REQUIRE(it != dates_.end(), "invalid date " << d);
84 return path_[std::distance(dates_.begin(), it)];
85 }
86 }
87
88protected:
89 virtual std::vector<QuantLib::ext::shared_ptr<Scenario>> nextPath() = 0;
90
91 Date today_;
92 vector<Date> dates_;
94 // DayCounter dayCounter_;
95 TimeGrid timeGrid_;
96 std::vector<QuantLib::ext::shared_ptr<Scenario>> path_;
97};
98
99// A simple scenario generator that contains a single scenario
101public:
103
104 void reset() override {}
105 QuantLib::ext::shared_ptr<ore::analytics::Scenario> next(const Date&) override { return s_; }
106
107 void setScenario(const QuantLib::ext::shared_ptr<ore::analytics::Scenario>& s) { s_ = s; }
108
109private:
110 QuantLib::ext::shared_ptr<ore::analytics::Scenario> s_;
111};
112
113} // namespace analytics
114} // namespace ore
Scenario generator base class.
virtual void reset()=0
Reset the generator so calls to next() return the first scenario.
virtual ~ScenarioGenerator()
Default destructor.
virtual QuantLib::ext::shared_ptr< Scenario > next(const Date &d)=0
Return the next scenario for the given date.
Scenario generator that generates an entire path.
virtual std::vector< QuantLib::ext::shared_ptr< Scenario > > nextPath()=0
virtual QuantLib::ext::shared_ptr< Scenario > next(const Date &d) override
Return the next scenario for the given date.
ScenarioPathGenerator(Date today, const vector< Date > &dates, TimeGrid timeGrid)
Constructor.
std::vector< QuantLib::ext::shared_ptr< Scenario > > path_
void setScenario(const QuantLib::ext::shared_ptr< ore::analytics::Scenario > &s)
QuantLib::ext::shared_ptr< ore::analytics::Scenario > s_
QuantLib::ext::shared_ptr< ore::analytics::Scenario > next(const Date &) override
Return the next scenario for the given date.
void reset() override
Reset the generator so calls to next() return the first scenario.
Scenario class.