Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenariogeneratordata.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
24
25#include <boost/algorithm/string.hpp>
26#include <boost/algorithm/string/case_conv.hpp>
27#include <boost/algorithm/string/trim.hpp>
28#include <boost/lexical_cast.hpp>
29#include <boost/tokenizer.hpp>
30
31#include <map>
32
33using namespace ore::data;
34using namespace std;
35
36namespace ore {
37namespace analytics {
38
40 if (grid_)
41 grid_->truncate(0);
42}
43
44void ScenarioGeneratorData::setGrid(QuantLib::ext::shared_ptr<DateGrid> grid) {
45 grid_ = grid;
46
47 std::ostringstream oss;
48 if (grid->tenors().size() == 0) {
49 oss << "";
50 } else {
51 oss << grid->tenors()[0];
52 for (Size i = 1; i < grid->tenors().size(); i++) {
53 oss << ", " << grid->tenors()[i];
54 }
55 }
56
57 gridString_ = oss.str();
58}
59
61 XMLNode* sim = XMLUtils::locateNode(root, "Simulation");
62 XMLNode* node = XMLUtils::getChildNode(sim, "Parameters");
63 XMLUtils::checkNode(node, "Parameters");
64
65 std::string calString = XMLUtils::getChildValue(node, "Calendar", true);
66 Calendar cal = parseCalendar(calString);
67
68 std::string dcString = XMLUtils::getChildValue(node, "DayCounter", false);
69 DayCounter dc = dcString.empty() ? ActualActual(ActualActual::ISDA) : parseDayCounter(dcString);
70
71 gridString_ = XMLUtils::getChildValue(node, "Grid", true);
72 std::vector<std::string> tokens;
73 boost::split(tokens, gridString_, boost::is_any_of(","));
74 if (tokens.size() <= 2) {
75 grid_ = QuantLib::ext::make_shared<DateGrid>(gridString_, cal, dc);
76 } else {
77 std::vector<Period> gridTenors = XMLUtils::getChildrenValuesAsPeriods(node, "Grid", true);
78 grid_ = QuantLib::ext::make_shared<DateGrid>(gridTenors, cal, dc);
79 }
80 LOG("ScenarioGeneratorData grid points size = " << grid_->size());
81
82 std::string sequenceTypeString = XMLUtils::getChildValue(node, "Sequence", true);
83 sequenceType_ = parseSequenceType(sequenceTypeString);
84 LOG("ScenarioGeneratorData sequence type = " << sequenceTypeString);
85
86 seed_ = XMLUtils::getChildValueAsInt(node, "Seed", true);
87 LOG("ScenarioGeneratorData seed = " << seed_);
88
89 samples_ = XMLUtils::getChildValueAsInt(node, "Samples", true);
90 LOG("ScenarioGeneratorData samples = " << samples_);
91
92 // overwrite samples with environment variable OVERWRITE_SCENARIOGENERATOR_SAMPLES
93 if (auto c = getenv("OVERWRITE_SCENARIOGENERATOR_SAMPLES")) {
94 try {
95 samples_ = std::stol(c);
96 } catch (const std::exception& e) {
97 WLOG("enviroment variable OVERWRITE_SCENARIOGENERATOR_SAMPLES is set ("
98 << c << ") but can not be parsed to a number - ignoring.");
99 }
100 LOG("Overwrite samples with " << samples_ << " from environment variable OVERWRITE_SCENARIOGENERATOR_SAMPLES")
101 }
102
103 if (auto n = XMLUtils::getChildNode(node, "Ordering"))
105 else
106 ordering_ = SobolBrownianGenerator::Steps;
107
108 if (auto n = XMLUtils::getChildNode(node, "DirectionIntegers"))
110 else
111 directionIntegers_ = SobolRsg::JoeKuoD7;
112
113 withCloseOutLag_ = false;
114 if (XMLUtils::getChildNode(node, "CloseOutLag") != NULL) {
115 withCloseOutLag_ = true;
116 closeOutLag_ = parsePeriod(XMLUtils::getChildValue(node, "CloseOutLag", true));
117 grid_->addCloseOutDates(closeOutLag_);
118 LOG("Use lagged close out grid, lag period is " << closeOutLag_);
119 }
120 withMporStickyDate_ = false;
121 if (XMLUtils::getChildNode(node, "MporMode") != NULL) {
122 string mporMode = XMLUtils::getChildValue(node, "MporMode", true);
123 if (mporMode == "StickyDate") {
124 withMporStickyDate_ = true;
125 LOG("Use Mpor sticky date mode");
126 } else if (mporMode == "ActualDate") {
127 withMporStickyDate_ = false;
128 LOG("Use Mpor actual date mode");
129 } else {
130 QL_FAIL("MporMode " << mporMode << " not recognised");
131 }
132 }
133
134 LOG("ScenarioGeneratorData done.");
135}
136
138 XMLNode* node = doc.allocNode("Simulation");
139 XMLNode* pNode = XMLUtils::addChild(doc, node, "Parameters");
140
141 if (grid_) {
142 XMLUtils::addChild(doc, pNode, "Calendar", grid_->calendar().name());
143 XMLUtils::addChild(doc, pNode, "DayCounter", grid_->dayCounter().name());
144 if (!gridString_.empty()) {
145 XMLUtils::addChild(doc, pNode, "Grid", gridString_);
146 } else {
147 XMLUtils::addGenericChildAsList(doc, pNode, "Grid", grid_->tenors());
148 }
149 }
150
151 XMLUtils::addChild(doc, pNode, "Sequence", ore::data::to_string( sequenceType_));
152 XMLUtils::addChild(doc, pNode, "Seed", to_string(seed_));
153 XMLUtils::addChild(doc, pNode, "Samples", to_string(samples_));
154
155 XMLUtils::addChild(doc, pNode, "Ordering", ore::data::to_string((SobolBrownianGenerator::Ordering) ordering_) );
156 XMLUtils::addChild(doc, pNode, "DirectionIntegers", ore::data::to_string(directionIntegers_));
157
158 if (withCloseOutLag_) {
159 XMLUtils::addChild(doc, pNode, "CloseOutLag", closeOutLag_);
160 }
162 XMLUtils::addChild(doc, pNode, "MporMode", "StickyDate");
163 } else {
164 XMLUtils::addChild(doc, pNode, "MporMode", "ActualDate");
165 }
166
167 return node;
168}
169
170} // namespace analytics
171} // namespace ore
SobolBrownianGenerator::Ordering ordering_
SobolRsg::DirectionIntegers directionIntegers_
QuantLib::ext::shared_ptr< DateGrid > grid_
virtual void fromXML(XMLNode *node) override
Load members from XML.
virtual XMLNode * toXML(XMLDocument &doc) const override
Write members to XML.
void setGrid(QuantLib::ext::shared_ptr< DateGrid > grid)
XMLNode * allocNode(const string &nodeName)
static void addGenericChildAsList(XMLDocument &doc, XMLNode *n, const string &name, const vector< T > &values, const string &attrName="", const string &attr="")
static void checkNode(XMLNode *n, const string &expectedName)
static XMLNode * locateNode(XMLNode *n, const string &name="")
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static string getNodeValue(XMLNode *node)
static int getChildValueAsInt(XMLNode *node, const string &name, bool mandatory=false, int defaultValue=0)
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static vector< Period > getChildrenValuesAsPeriods(XMLNode *node, const string &name, bool mandatory=false)
Calendar parseCalendar(const string &s)
SequenceType parseSequenceType(const std::string &s)
Period parsePeriod(const string &s)
SobolRsg::DirectionIntegers parseSobolRsgDirectionIntegers(const std::string &s)
SobolBrownianGenerator::Ordering parseSobolBrownianGeneratorOrdering(const std::string &s)
DayCounter parseDayCounter(const string &s)
#define LOG(text)
#define WLOG(text)
Size size(const ValueType &v)
std::string to_string(const LocationInfo &l)
Build a scenariogenerator.
factory classes for simple scenarios