Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenario.cpp
Go to the documentation of this file.
1/*
2Copyright (C) 2016 Quaternion Risk Management Ltd
3All rights reserved.
4*/
5
6#include <oret/toplevelfixture.hpp>
7#include <boost/make_shared.hpp>
12
13using namespace boost::unit_test_framework;
14using namespace QuantLib;
15using namespace ore::analytics;
16
17class TestScenarioGenerator : public ScenarioGenerator {
18public:
19 TestScenarioGenerator() { current_position_ = 0; }
20 void addScenario(QuantLib::ext::shared_ptr<Scenario> s) {
21 scenarios.push_back(s);
22 current_position_++;
23 }
24
25 QuantLib::ext::shared_ptr<Scenario> next(const Date& d) override { return scenarios[current_position_++]; }
26
27 void reset() override { current_position_ = 0; }
28
29 vector<QuantLib::ext::shared_ptr<Scenario>> scenarios;
30
31private:
32 int current_position_;
33};
34
35BOOST_FIXTURE_TEST_SUITE(OREAnalyticsTestSuite, ore::test::TopLevelFixture)
36
37BOOST_AUTO_TEST_SUITE(CSVScenarioGeneratorTest)
38
39BOOST_AUTO_TEST_CASE(testCSVScenarioGenerator) {
40
41 // Make up some scenarios
42 Date d(21, Dec, 2016);
43 vector<RiskFactorKey> rfks = {{RiskFactorKey::KeyType::DiscountCurve, "CHF", 0},
44 {RiskFactorKey::KeyType::DiscountCurve, "CHF", 1},
45 {RiskFactorKey::KeyType::DiscountCurve, "CHF", 2},
46 {RiskFactorKey::KeyType::YieldCurve, "CHF-LIBOR", 0},
47 {RiskFactorKey::KeyType::YieldCurve, "CHF-LIBOR", 1},
48 {RiskFactorKey::KeyType::IndexCurve, "CHF - LIBOR - 6M", 0},
49 {RiskFactorKey::KeyType::IndexCurve, "CHF - LIBOR - 6M", 1},
50 {RiskFactorKey::KeyType::IndexCurve, "CHF - LIBOR - 6M", 2},
51 {RiskFactorKey::KeyType::SwaptionVolatility, "SwapVol"},
52 {RiskFactorKey::KeyType::FXSpot, "CHF"},
53 {RiskFactorKey::KeyType::FXVolatility, "CHFVol"}};
54
55 QuantLib::ext::shared_ptr<TestScenarioGenerator> tsg = QuantLib::ext::make_shared<TestScenarioGenerator>();
56 tsg->addScenario(QuantLib::ext::make_shared<SimpleScenario>(d));
57 tsg->addScenario(QuantLib::ext::make_shared<SimpleScenario>(d));
58 for (auto scenario : tsg->scenarios) {
59 for (auto rf : rfks) {
60 scenario->add(rf, rand());
61 }
62 }
63
64 // Write scenarios to file
65 string filename = "test_csv_scenario_generator.csv";
66 ScenarioWriter sw(tsg, filename);
67 tsg->reset();
68 for (Size i = 0; i < tsg->scenarios.size(); i++) {
69 sw.next(d);
70 }
71 sw.reset();
72
73 // Read in scenarios from file
74 QuantLib::ext::shared_ptr<SimpleScenarioFactory> ssf = QuantLib::ext::make_shared<SimpleScenarioFactory>(true);
75 CSVScenarioGenerator csvsgen(filename, ssf);
76
77 // Compare scenarios by looping over risk keys
78 for (Size i = 0; i < tsg->scenarios.size(); i++) {
79 QuantLib::ext::shared_ptr<Scenario> s = csvsgen.next(d);
80 BOOST_CHECK_EQUAL_COLLECTIONS(s->keys().begin(), s->keys().end(), tsg->scenarios[i]->keys().begin(),
81 tsg->scenarios[i]->keys().end());
82 for (auto rfk : s->keys()) {
83 BOOST_CHECK_EQUAL(s->get(rfk), tsg->scenarios[i]->get(rfk));
84 }
85 }
86
87 remove("test_csv_scenario_generator.csv");
88}
89
90BOOST_AUTO_TEST_SUITE_END()
91
92BOOST_AUTO_TEST_SUITE_END()
Class for generating scenarios from a csv file assumed to be in a format compatible with ScenarioWrit...
virtual QuantLib::ext::shared_ptr< Scenario > next(const Date &d) override
Return the next scenario for the given date.
Scenario generator base class.
virtual void reset()=0
Reset the generator so calls to next() return the first scenario.
virtual QuantLib::ext::shared_ptr< Scenario > next(const Date &d)=0
Return the next scenario for the given date.
Class for writing scenarios to file.
virtual 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.
ScenarioWriter class.
Simple scenario class.
factory classes for simple scenarios
BOOST_AUTO_TEST_CASE(testCSVScenarioGenerator)
Definition: scenario.cpp:39