Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
clonedscenariogenerator.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
20
22
23namespace ore {
24namespace analytics {
25
26ClonedScenarioGenerator::ClonedScenarioGenerator(const QuantLib::ext::shared_ptr<ScenarioGenerator>& scenarioGenerator,
27 const std::vector<Date>& dates, const Size nSamples) {
28 DLOG("Build cloned scenario generator for " << dates.size() << " dates and " << nSamples << " samples.");
29 for (size_t i = 0; i < dates.size(); ++i) {
30 dates_[dates[i]] = i;
31 }
32 firstDate_ = dates.front();
33 scenarioGenerator->reset();
34 scenarios_.resize(nSamples * dates_.size());
35 for (Size i = 0; i < nSamples; ++i) {
36 for (Size j = 0; j < dates.size(); ++j) {
37 scenarios_[i * dates.size() + j] = scenarioGenerator->next(dates[j])->clone();
38 }
39 }
40}
41
42QuantLib::ext::shared_ptr<Scenario> ClonedScenarioGenerator::next(const Date& d) {
43 if (d == firstDate_) { // new path
44 ++nSim_;
45 }
46 auto stepIdx = dates_.find(d);
47 QL_REQUIRE(stepIdx != dates_.end(), "ClonedScenarioGenerator::next(" << d << "): invalid date " << d);
48 size_t timePos = stepIdx->second;
49 size_t currentStep = (nSim_ - 1) * dates_.size() + timePos;
50 QL_REQUIRE(currentStep < scenarios_.size(),
51 "ClonedScenarioGenerator::next(" << d << "): no more scenarios stored.");
52 return scenarios_[currentStep];
53}
54
56 nSim_ = 0;
57}
58
59} // namespace analytics
60} // namespace ore
ClonedScenarioGenerator(const QuantLib::ext::shared_ptr< ScenarioGenerator > &scenarioGenerator, const std::vector< Date > &dates, const Size nSamples)
std::vector< QuantLib::ext::shared_ptr< Scenario > > scenarios_
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.
#define DLOG(text)