Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
onedimsolverconfig.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
21
23using namespace QuantLib;
24using std::make_pair;
25using std::pair;
26
27namespace ore {
28namespace data {
29
31 : maxEvaluations_(Null<Size>()),
32 initialGuess_(Null<Real>()),
33 accuracy_(Null<Real>()),
34 minMax_(make_pair(Null<Real>(), Null<Real>())),
35 step_(Null<Real>()),
36 lowerBound_(Null<Real>()),
37 upperBound_(Null<Real>()),
38 empty_(true) {}
39
41 Real initialGuess,
42 Real accuracy,
43 const pair<Real, Real>& minMax,
44 Real lowerBound,
45 Real upperBound)
46 : maxEvaluations_(maxEvaluations),
47 initialGuess_(initialGuess),
48 accuracy_(accuracy),
49 minMax_(minMax),
50 step_(Null<Real>()),
51 lowerBound_(lowerBound),
52 upperBound_(upperBound),
53 empty_(false) {
54 check();
55}
56
58 Real initialGuess,
59 Real accuracy,
60 Real step,
61 Real lowerBound,
62 Real upperBound)
63 : maxEvaluations_(maxEvaluations),
64 initialGuess_(initialGuess),
65 accuracy_(accuracy),
66 minMax_(make_pair(Null<Real>(), Null<Real>())),
67 step_(step),
68 lowerBound_(lowerBound),
69 upperBound_(upperBound),
70 empty_(false) {
71 check();
72}
73
75
76 XMLUtils::checkNode(node, "OneDimSolverConfig");
77
78 maxEvaluations_ = XMLUtils::getChildValueAsInt(node, "MaxEvaluations", true);
79 initialGuess_ = XMLUtils::getChildValueAsDouble(node, "InitialGuess", true);
80 accuracy_ = XMLUtils::getChildValueAsDouble(node, "Accuracy", true);
81
82 // Choice between (min, max) pair or step
83 if (XMLNode* minMaxNode = XMLUtils::getChildNode(node, "MinMax")) {
84 Real min = XMLUtils::getChildValueAsDouble(minMaxNode, "Min", true);
85 Real max = XMLUtils::getChildValueAsDouble(minMaxNode, "Max", true);
86 minMax_ = make_pair(min, max);
87 } else if (XMLNode* stepNode = XMLUtils::getChildNode(node, "Step")) {
89 } else {
90 QL_FAIL("OneDimSolverConfig: expected a MinMax or Step node.");
91 }
92
93 lowerBound_ = Null<Real>();
94 if (XMLNode* n = XMLUtils::getChildNode(node, "LowerBound")) {
96 }
97
98 upperBound_ = Null<Real>();
99 if (XMLNode* n = XMLUtils::getChildNode(node, "UpperBound")) {
101 }
102
103 check();
104
105 empty_ = false;
106}
107
109
110 XMLNode* node = doc.allocNode("OneDimSolverConfig");
111
112 XMLUtils::addChild(doc, node, "MaxEvaluations", static_cast<int>(maxEvaluations_));
113 XMLUtils::addChild(doc, node, "InitialGuess", initialGuess_);
114 XMLUtils::addChild(doc, node, "Accuracy", accuracy_);
115
116 if (step_ != Null<Real>()) {
117 XMLUtils::addChild(doc, node, "Step", accuracy_);
118 } else {
119 XMLNode* minMaxNode = doc.allocNode("MinMax");
120 XMLUtils::addChild(doc, minMaxNode, "Min", minMax_.first);
121 XMLUtils::addChild(doc, minMaxNode, "Max", minMax_.second);
122 XMLUtils::appendNode(node, minMaxNode);
123 }
124
125 if (lowerBound_ != Null<Real>())
126 XMLUtils::addChild(doc, node, "LowerBound", lowerBound_);
127
128 if (upperBound_ != Null<Real>())
129 XMLUtils::addChild(doc, node, "UpperBound", upperBound_);
130
131 return node;
132}
133
134// Leaving QuantExt:: in the method signature satisfies VS intellisense.
135// Without it, it complains about a missing definition.
136OneDimSolverConfig::operator QuantExt::Solver1DOptions() const {
137
138 Solver1DOptions solverOptions;
139
140 if (!empty_) {
141 solverOptions.maxEvaluations = maxEvaluations_;
142 solverOptions.accuracy = accuracy_;
143 solverOptions.initialGuess = initialGuess_;
144 solverOptions.minMax = minMax_;
145 solverOptions.step = step_;
146 solverOptions.lowerBound = lowerBound_;
147 solverOptions.upperBound = upperBound_;
148 }
149
150 return solverOptions;
151}
152
154
155 QL_REQUIRE(maxEvaluations_ > 0, "MaxEvaluations (" << maxEvaluations_ << ") should be positive.");
156 QL_REQUIRE(accuracy_ > 0, "Accuracy (" << accuracy_ << ") should be positive.");
157
158 if (step_ != Null<Real>()) {
159 QL_REQUIRE(step_ > 0, "Step (" << step_ << ") should be positive when given.");
160 } else {
161 const Real& min = minMax_.first;
162 const Real& max = minMax_.second;
163 QL_REQUIRE(min != Null<Real>() && max != Null<Real>(), "When Step is not given" <<
164 " Min and Max should be provided.");
165 QL_REQUIRE(min < max, "When given, Min (" << min << ") should be less than Max (" << max <<").");
166 }
167
168 if (lowerBound_ != Null<Real>() && upperBound_ != Null<Real>()) {
169 QL_REQUIRE(lowerBound_ < upperBound_, "When given, LowerBound (" << lowerBound_ <<
170 ") should be less than UpperBound (" << upperBound_ << ").");
171 }
172}
173
174}
175}
OneDimSolverConfig()
Default constructor with everything QuantLib::Null.
void check() const
Basic checks.
void fromXML(ore::data::XMLNode *node) override
std::pair< QuantLib::Real, QuantLib::Real > minMax_
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
static int getChildValueAsInt(XMLNode *node, const string &name, bool mandatory=false, int defaultValue=0)
Definition: xmlutils.cpp:291
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
@ data
Definition: log.hpp:77
RandomVariable max(RandomVariable x, const RandomVariable &y)
RandomVariable min(RandomVariable x, const RandomVariable &y)
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Class for holding 1-D solver configuration.
Map text representations to QuantLib/QuantExt types.
std::pair< QuantLib::Real, QuantLib::Real > minMax
QuantLib::Real initialGuess
QuantLib::Real upperBound
QuantLib::Real lowerBound
QuantLib::Size maxEvaluations