QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
endcriteria.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006, 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Marco Bianchetti
6 Copyright (C) 2001, 2002, 2003 Nicolas Di Césaré
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/math/optimization/endcriteria.hpp>
23#include <ql/errors.hpp>
24#include <algorithm>
25
26namespace QuantLib {
27
28
30 Size maxStationaryStateIterations,
31 Real rootEpsilon,
32 Real functionEpsilon,
33 Real gradientNormEpsilon)
34 : maxIterations_(maxIterations),
35 maxStationaryStateIterations_(maxStationaryStateIterations),
36 rootEpsilon_(rootEpsilon),
37 functionEpsilon_(functionEpsilon),
38 gradientNormEpsilon_(gradientNormEpsilon) {
39
41 maxStationaryStateIterations_ = std::min(static_cast<Size>(maxIterations/2),
42 static_cast<Size>(100));
44 "maxStationaryStateIterations_ (" <<
46 ") must be greater than one");
48 "maxStationaryStateIterations_ (" <<
50 ") must be less than maxIterations_ (" <<
51 maxIterations_ << ")");
54 }
55
57 EndCriteria::Type& ecType) const{
58 if (iteration < maxIterations_)
59 return false;
60 ecType = MaxIterations;
61 return true;
62 }
63
65 const Real xNew,
66 Size& statStateIterations,
67 EndCriteria::Type& ecType) const {
68 if (std::fabs(xNew-xOld) >= rootEpsilon_) {
69 statStateIterations = 0;
70 return false;
71 }
72 ++statStateIterations;
73 if (statStateIterations <= maxStationaryStateIterations_)
74 return false;
75 ecType = StationaryPoint;
76 return true;
77 }
78
80 const Real fxOld,
81 const Real fxNew,
82 Size& statStateIterations,
83 EndCriteria::Type& ecType) const {
84 if (std::fabs(fxNew-fxOld) >= functionEpsilon_) {
85 statStateIterations = 0;
86 return false;
87 }
88 ++statStateIterations;
89 if (statStateIterations <= maxStationaryStateIterations_)
90 return false;
92 return true;
93 }
94
96 const Real f,
97 const bool positiveOptimization,
98 EndCriteria::Type& ecType) const {
99 if (!positiveOptimization)
100 return false;
101 if (f >= functionEpsilon_)
102 return false;
104 return true;
105 }
106
107 //bool EndCriteria::checkZerGradientNormValue(
108 // const Real gNormOld,
109 // const Real gNormNew,
110 // EndCriteria::Type& ecType) const {
111 // if (std::fabs(gNormNew-gNormOld) >= gradientNormEpsilon_)
112 // return false;
113 // ecType = StationaryGradient;
114 // return true;
115 //}
116
118 EndCriteria::Type& ecType) const {
119 if (gradientNorm >= gradientNormEpsilon_)
120 return false;
121 ecType = ZeroGradientNorm;
122 return true;
123 }
124
125 bool EndCriteria::operator()(const Size iteration,
126 Size& statStateIterations,
127 const bool positiveOptimization,
128 const Real fold,
129 const Real, //normgold,
130 const Real fnew,
131 const Real normgnew,
132 EndCriteria::Type& ecType) const {
133 return
134 checkMaxIterations(iteration, ecType) ||
135 checkStationaryFunctionValue(fold, fnew, statStateIterations, ecType) ||
136 checkStationaryFunctionAccuracy(fnew, positiveOptimization, ecType) ||
137 checkZeroGradientNorm(normgnew, ecType);
138 }
139
140 // Inspectors
142 return maxIterations_;
143 }
144
147 }
148
150 return rootEpsilon_;
151 }
152
154 return functionEpsilon_;
155 }
156
159 }
160
161 std::ostream& operator<<(std::ostream& out, EndCriteria::Type ec) {
162 switch (ec) {
164 return out << "None";
166 return out << "MaxIterations";
168 return out << "StationaryPoint";
170 return out << "StationaryFunctionValue";
172 return out << "StationaryFunctionAccuracy";
174 return out << "ZeroGradientNorm";
176 return out << "Unknown";
177 default:
178 QL_FAIL("unknown EndCriteria::Type (" << Integer(ec) << ")");
179 }
180 }
181
182}
bool operator()(Size iteration, Size &statState, bool positiveOptimization, Real fold, Real normgold, Real fnew, Real normgnew, EndCriteria::Type &ecType) const
bool checkStationaryFunctionAccuracy(Real f, bool positiveOptimization, EndCriteria::Type &ecType) const
Definition: endcriteria.cpp:95
Real gradientNormEpsilon() const
bool checkZeroGradientNorm(Real gNorm, EndCriteria::Type &ecType) const
Real functionEpsilon() const
bool checkStationaryPoint(Real xOld, Real xNew, Size &statStateIterations, EndCriteria::Type &ecType) const
Definition: endcriteria.cpp:64
EndCriteria(Size maxIterations, Size maxStationaryStateIterations, Real rootEpsilon, Real functionEpsilon, Real gradientNormEpsilon)
Initialization constructor.
Definition: endcriteria.cpp:29
Real rootEpsilon() const
Size maxIterations() const
Size maxStationaryStateIterations_
Maximun number of iterations in stationary state.
bool checkStationaryFunctionValue(Real fxOld, Real fxNew, Size &statStateIterations, EndCriteria::Type &ecType) const
Definition: endcriteria.cpp:79
Real rootEpsilon_
root, function and gradient epsilons
Size maxStationaryStateIterations() const
Size maxIterations_
Maximum number of iterations.
bool checkMaxIterations(Size iteration, EndCriteria::Type &ecType) const
Definition: endcriteria.cpp:56
template class providing a null value for a given type.
Definition: null.hpp:76
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)