Hi, I believe that the change below should be made to the Brent solver routine. In cases where std::fabs(fxMax_) < std::fabs(froot), then root_ is set equal to xMax_. If the convergence check is then met, root_ is returned but the funtion/funtor f is not updated with this new route. For example, when the routine is used in the iterative bootstrap, the discount factor vector is not updated with the new root_. Would there be any objections to this change?
Thanks and regards, Francis. Index: QuantLib/ql/math/solvers1d/brent.hpp =================================================================== --- QuantLib/ql/math/solvers1d/brent.hpp (revision 18356) +++ QuantLib/ql/math/solvers1d/brent.hpp (working copy) @@ -82,8 +82,11 @@ // Convergence check xAcc1=2.0*QL_EPSILON*std::fabs(root_)+0.5*xAccuracy; xMid=(xMax_-root_)/2.0; - if (std::fabs(xMid) <= xAcc1 || (close(froot, 0.0))) + if (std::fabs(xMid) <= xAcc1 || (close(froot, 0.0))) { + f(root_); + ++evaluationNumber_; return root_; + } if (std::fabs(e) >= xAcc1 && std::fabs(fxMin_) > std::fabs(froot)) { ------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: INSIGHTS What's next for parallel hardware, programming and related areas? Interviews and blogs by thought leaders keep you ahead of the curve. http://goparallel.sourceforge.net _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
This post was updated on .
additional issue is that Brent method doesn't take in to account initial
guess. when you call Brent solveImpl method through solver1d.solve with guess as one of the arguments, root_ is set to this value in solve() but just after entering solveImpl() it is changed back to root_=xMax_ without any checking whether guess is correct. in this scenario I think there is also need to call f(root_) before returning solution. regards, Piotr ------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: VERIFY Test and improve your parallel project with help from experts and peers. http://goparallel.sourceforge.net _______________________________________________ QuantLib-users mailing list QuantLib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/quantlib-users
why always me?
|
Hi, You are right. This change was made in the HEAD in response to a query to the quantlib-dev group on 26/07/2012. The change is shown below.
Regards, Francis. >svn diff -r {2012-07-26}:HEAD QuantLib\ql\math\solvers1d\brent.hpp Index: QuantLib/ql/math/solvers1d/brent.hpp =================================================================== --- QuantLib/ql/math/solvers1d/brent.hpp (revision 18300) +++ QuantLib/ql/math/solvers1d/brent.hpp (revision 18376) @@ -46,11 +46,22 @@ Real min1, min2; Real froot, p, q, r, s, xAcc1, xMid; - // dummy assignements to avoid compiler warning - Real d = 0.0, e = 0.0; - root_ = xMax_; - froot = fxMax_; + // we want to start with root_ (which equals the guess) on + // one side of the bracket and both xMin_ and xMax_ on the + // other. + froot = f(root_); + ++evaluationNumber_; + if (froot * fxMin_ < 0) { + xMax_ = xMin_; + fxMax_ = fxMin_; + } else { + xMin_ = xMax_; + fxMin_ = fxMax_; + } + Real d = root_- xMax_; + Real e = d; + while (evaluationNumber_<=maxEvaluations_) { if ((froot > 0.0 && fxMax_ > 0.0) || (froot < 0.0 && fxMax_ < 0.0)) { On Thu, Nov 29, 2012 at 7:11 PM, cf16r <[hidden email]> wrote: additional issue is that Brent method doesn't take in to account initial guess. ------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: VERIFY Test and improve your parallel project with help from experts and peers. http://goparallel.sourceforge.net _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Hi Francis,
is your patch still needed after the changes? Luigi On Thu, Nov 29, 2012 at 10:44 PM, Francis Duffy <[hidden email]> wrote: > Hi, > > You are right. This change was made in the HEAD in response to a query to > the quantlib-dev group on 26/07/2012. The change is shown below. > > Regards, > Francis. > >>svn diff -r {2012-07-26}:HEAD QuantLib\ql\math\solvers1d\brent.hpp > Index: QuantLib/ql/math/solvers1d/brent.hpp > =================================================================== > --- QuantLib/ql/math/solvers1d/brent.hpp (revision 18300) > +++ QuantLib/ql/math/solvers1d/brent.hpp (revision 18376) > @@ -46,11 +46,22 @@ > Real min1, min2; > Real froot, p, q, r, s, xAcc1, xMid; > - // dummy assignements to avoid compiler warning > - Real d = 0.0, e = 0.0; > - root_ = xMax_; > - froot = fxMax_; > + // we want to start with root_ (which equals the guess) on > + // one side of the bracket and both xMin_ and xMax_ on the > + // other. > + froot = f(root_); > + ++evaluationNumber_; > + if (froot * fxMin_ < 0) { > + xMax_ = xMin_; > + fxMax_ = fxMin_; > + } else { > + xMin_ = xMax_; > + fxMin_ = fxMax_; > + } > + Real d = root_- xMax_; > + Real e = d; > + > while (evaluationNumber_<=maxEvaluations_) { > if ((froot > 0.0 && fxMax_ > 0.0) || > (froot < 0.0 && fxMax_ < 0.0)) { > > > On Thu, Nov 29, 2012 at 7:11 PM, cf16r <[hidden email]> wrote: >> >> additional issue is that Brent method doesn't take in to account initial >> guess. >> when you call Brent solveImpl method through solver1d.solve with guess as >> one of the arguments, root_ is set to this value in solve() but just after >> entering solveImpl() it is changed back to root_=xMax_ without any checking >> whether guess is correct. >> in this scenario I think there is also need to call f(root_) before >> returning solution. >> >> regards, >> cf16 >> > > > ------------------------------------------------------------------------------ > Keep yourself connected to Go Parallel: > VERIFY Test and improve your parallel project with help from experts > and peers. http://goparallel.sourceforge.net > _______________________________________________ > QuantLib-users mailing list > [hidden email] > https://lists.sourceforge.net/lists/listinfo/quantlib-users > ------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: TUNE You got it built. Now make it sing. Tune shows you how. http://goparallel.sourceforge.net _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Hi Luigi,
Yes, I think so. Regards, Francis.
On Fri, Nov 30, 2012 at 2:25 PM, Luigi Ballabio <[hidden email]> wrote: Hi Francis, ------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: TUNE You got it built. Now make it sing. Tune shows you how. http://goparallel.sourceforge.net _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Ok. If you have time, could you also write a test case to exercise the fix?
Thanks, Luigi On Fri, Nov 30, 2012 at 4:37 PM, Francis Duffy <[hidden email]> wrote: > Hi Luigi, > > Yes, I think so. > > Regards, > Francis. > > > On Fri, Nov 30, 2012 at 2:25 PM, Luigi Ballabio <[hidden email]> > wrote: >> >> Hi Francis, >> is your patch still needed after the changes? >> >> Luigi >> >> On Thu, Nov 29, 2012 at 10:44 PM, Francis Duffy >> <[hidden email]> wrote: >> > Hi, >> > >> > You are right. This change was made in the HEAD in response to a query >> > to >> > the quantlib-dev group on 26/07/2012. The change is shown below. >> > >> > Regards, >> > Francis. >> > >> >>svn diff -r {2012-07-26}:HEAD QuantLib\ql\math\solvers1d\brent.hpp >> > Index: QuantLib/ql/math/solvers1d/brent.hpp >> > =================================================================== >> > --- QuantLib/ql/math/solvers1d/brent.hpp (revision 18300) >> > +++ QuantLib/ql/math/solvers1d/brent.hpp (revision 18376) >> > @@ -46,11 +46,22 @@ >> > Real min1, min2; >> > Real froot, p, q, r, s, xAcc1, xMid; >> > - // dummy assignements to avoid compiler warning >> > - Real d = 0.0, e = 0.0; >> > - root_ = xMax_; >> > - froot = fxMax_; >> > + // we want to start with root_ (which equals the guess) on >> > + // one side of the bracket and both xMin_ and xMax_ on the >> > + // other. >> > + froot = f(root_); >> > + ++evaluationNumber_; >> > + if (froot * fxMin_ < 0) { >> > + xMax_ = xMin_; >> > + fxMax_ = fxMin_; >> > + } else { >> > + xMin_ = xMax_; >> > + fxMin_ = fxMax_; >> > + } >> > + Real d = root_- xMax_; >> > + Real e = d; >> > + >> > while (evaluationNumber_<=maxEvaluations_) { >> > if ((froot > 0.0 && fxMax_ > 0.0) || >> > (froot < 0.0 && fxMax_ < 0.0)) { >> > >> > >> > On Thu, Nov 29, 2012 at 7:11 PM, cf16r <[hidden email]> wrote: >> >> >> >> additional issue is that Brent method doesn't take in to account >> >> initial >> >> guess. >> >> when you call Brent solveImpl method through solver1d.solve with guess >> >> as >> >> one of the arguments, root_ is set to this value in solve() but just >> >> after >> >> entering solveImpl() it is changed back to root_=xMax_ without any >> >> checking >> >> whether guess is correct. >> >> in this scenario I think there is also need to call f(root_) before >> >> returning solution. >> >> >> >> regards, >> >> cf16 >> >> >> > >> > >> > >> > ------------------------------------------------------------------------------ >> > Keep yourself connected to Go Parallel: >> > VERIFY Test and improve your parallel project with help from experts >> > and peers. http://goparallel.sourceforge.net >> > _______________________________________________ >> > QuantLib-users mailing list >> > [hidden email] >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >> > > > ------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: TUNE You got it built. Now make it sing. Tune shows you how. http://goparallel.sourceforge.net _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Hi Luigi, No problem, I will do that over the coming days and send it to you. Thanks, Francis. On Fri, Nov 30, 2012 at 4:27 PM, Luigi Ballabio <[hidden email]> wrote: Ok. If you have time, could you also write a test case to exercise the fix? ------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: TUNE You got it built. Now make it sing. Tune shows you how. http://goparallel.sourceforge.net _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Hi Luigi, I noticed that a number of the other 1D solvers had the same issue, i.e., the calculation of root_ is fine but the final calculation of the funtion f may have been performed with a value different from the return value of root_. Hence, if you are using the solver in an iterative routine, the underlying results may not be updated with the return value of root_.
The attached file, solvers1d.diff, shows the changes that I made to fix this. The files iterativesolvers.xpp give a unit test that exercises the fix for the solvers Brent, Newton, NewtonSafe and Ridder. The Bisection method passes the test with and without the fix. No changes were made to the Secant or False Position method. Thanks and regards, Francis. On Fri, Nov 30, 2012 at 10:48 PM, Francis Duffy <[hidden email]> wrote:
------------------------------------------------------------------------------ Keep yourself connected to Go Parallel: DESIGN Expert tips on starting your parallel project right. http://goparallel.sourceforge.net/ _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users iterativesolvers.cpp (10K) Download Attachment iterativesolvers.hpp (1K) Download Attachment solvers1d.diff (6K) Download Attachment |
Thanks, I've committed your patch to the trunk.
Luigi On Mon, Dec 3, 2012 at 12:14 AM, Francis Duffy <[hidden email]> wrote: > Hi Luigi, > > I noticed that a number of the other 1D solvers had the same issue, i.e., > the calculation of root_ is fine but the final calculation of the funtion f > may have been performed with a value different from the return value of > root_. Hence, if you are using the solver in an iterative routine, the > underlying results may not be updated with the return value of root_. > > The attached file, solvers1d.diff, shows the changes that I made to fix > this. The files iterativesolvers.xpp give a unit test that exercises the fix > for the solvers Brent, Newton, NewtonSafe and Ridder. The Bisection method > passes the test with and without the fix. No changes were made to the Secant > or False Position method. > Thanks and regards, > Francis. > On Fri, Nov 30, 2012 at 10:48 PM, Francis Duffy <[hidden email]> > wrote: >> >> Hi Luigi, >> >> No problem, I will do that over the coming days and send it to you. >> >> Thanks, >> Francis. >> On Fri, Nov 30, 2012 at 4:27 PM, Luigi Ballabio <[hidden email]> >> wrote: >>> >>> Ok. If you have time, could you also write a test case to exercise the >>> fix? >>> >>> Thanks, >>> Luigi >>> >>> >>> On Fri, Nov 30, 2012 at 4:37 PM, Francis Duffy <[hidden email]> >>> wrote: >>> > Hi Luigi, >>> > >>> > Yes, I think so. >>> > >>> > Regards, >>> > Francis. >>> > >>> > >>> > On Fri, Nov 30, 2012 at 2:25 PM, Luigi Ballabio >>> > <[hidden email]> >>> > wrote: >>> >> >>> >> Hi Francis, >>> >> is your patch still needed after the changes? >>> >> >>> >> Luigi >>> >> >>> >> On Thu, Nov 29, 2012 at 10:44 PM, Francis Duffy >>> >> <[hidden email]> wrote: >>> >> > Hi, >>> >> > >>> >> > You are right. This change was made in the HEAD in response to a >>> >> > query >>> >> > to >>> >> > the quantlib-dev group on 26/07/2012. The change is shown below. >>> >> > >>> >> > Regards, >>> >> > Francis. >>> >> > >>> >> >>svn diff -r {2012-07-26}:HEAD QuantLib\ql\math\solvers1d\brent.hpp >>> >> > Index: QuantLib/ql/math/solvers1d/brent.hpp >>> >> > =================================================================== >>> >> > --- QuantLib/ql/math/solvers1d/brent.hpp (revision 18300) >>> >> > +++ QuantLib/ql/math/solvers1d/brent.hpp (revision 18376) >>> >> > @@ -46,11 +46,22 @@ >>> >> > Real min1, min2; >>> >> > Real froot, p, q, r, s, xAcc1, xMid; >>> >> > - // dummy assignements to avoid compiler warning >>> >> > - Real d = 0.0, e = 0.0; >>> >> > - root_ = xMax_; >>> >> > - froot = fxMax_; >>> >> > + // we want to start with root_ (which equals the guess) >>> >> > on >>> >> > + // one side of the bracket and both xMin_ and xMax_ on >>> >> > the >>> >> > + // other. >>> >> > + froot = f(root_); >>> >> > + ++evaluationNumber_; >>> >> > + if (froot * fxMin_ < 0) { >>> >> > + xMax_ = xMin_; >>> >> > + fxMax_ = fxMin_; >>> >> > + } else { >>> >> > + xMin_ = xMax_; >>> >> > + fxMin_ = fxMax_; >>> >> > + } >>> >> > + Real d = root_- xMax_; >>> >> > + Real e = d; >>> >> > + >>> >> > while (evaluationNumber_<=maxEvaluations_) { >>> >> > if ((froot > 0.0 && fxMax_ > 0.0) || >>> >> > (froot < 0.0 && fxMax_ < 0.0)) { >>> >> > >>> >> > >>> >> > On Thu, Nov 29, 2012 at 7:11 PM, cf16r <[hidden email]> wrote: >>> >> >> >>> >> >> additional issue is that Brent method doesn't take in to account >>> >> >> initial >>> >> >> guess. >>> >> >> when you call Brent solveImpl method through solver1d.solve with >>> >> >> guess >>> >> >> as >>> >> >> one of the arguments, root_ is set to this value in solve() but >>> >> >> just >>> >> >> after >>> >> >> entering solveImpl() it is changed back to root_=xMax_ without any >>> >> >> checking >>> >> >> whether guess is correct. >>> >> >> in this scenario I think there is also need to call f(root_) before >>> >> >> returning solution. >>> >> >> >>> >> >> regards, >>> >> >> cf16 >>> >> >> >>> >> > >>> >> > >>> >> > >>> >> > >>> >> > ------------------------------------------------------------------------------ >>> >> > Keep yourself connected to Go Parallel: >>> >> > VERIFY Test and improve your parallel project with help from experts >>> >> > and peers. http://goparallel.sourceforge.net >>> >> > _______________________________________________ >>> >> > QuantLib-users mailing list >>> >> > [hidden email] >>> >> > https://lists.sourceforge.net/lists/listinfo/quantlib-users >>> >> > >>> > >>> > >> >> > ------------------------------------------------------------------------------ LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial Remotely access PCs and mobile devices and provide instant support Improve your efficiency, and focus on delivering more value-add services Discover what IT Professionals Know. Rescue delivers http://p.sf.net/sfu/logmein_12329d2d _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Free forum by Nabble | Edit this page |