| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2001, 2002, 2003 Nicolas Di Césaré |
| 5 | Copyright (C) 2015 Peter Caspers |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | /*! \file costfunction.hpp |
| 22 | \brief Optimization cost function class |
| 23 | */ |
| 24 | |
| 25 | #ifndef quantlib_optimization_costfunction_h |
| 26 | #define quantlib_optimization_costfunction_h |
| 27 | |
| 28 | #include <ql/math/array.hpp> |
| 29 | #include <ql/math/matrix.hpp> |
| 30 | |
| 31 | namespace QuantLib { |
| 32 | |
| 33 | //! Cost function abstract class for optimization problem |
| 34 | class CostFunction { |
| 35 | public: |
| 36 | virtual ~CostFunction() = default; |
| 37 | //! method to overload to compute the cost function value in x |
| 38 | virtual Real value(const Array& x) const { |
| 39 | Array v = values(x); |
| 40 | std::transform(first: v.begin(), last: v.end(), result: v.begin(), unary_op: [](Real x) -> Real { return x*x; }); |
| 41 | return std::sqrt(x: std::accumulate(first: v.begin(), last: v.end(), init: Real(0.0)) / |
| 42 | static_cast<Real>(v.size())); |
| 43 | } |
| 44 | //! method to overload to compute the cost function values in x |
| 45 | virtual Array values(const Array& x) const =0; |
| 46 | |
| 47 | //! method to overload to compute grad_f, the first derivative of |
| 48 | // the cost function with respect to x |
| 49 | virtual void gradient(Array& grad, const Array& x) const { |
| 50 | Real eps = finiteDifferenceEpsilon(), fp, fm; |
| 51 | Array xx(x); |
| 52 | for (Size i=0; i<x.size(); i++) { |
| 53 | xx[i] += eps; |
| 54 | fp = value(x: xx); |
| 55 | xx[i] -= 2.0*eps; |
| 56 | fm = value(x: xx); |
| 57 | grad[i] = 0.5*(fp - fm)/eps; |
| 58 | xx[i] = x[i]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | //! method to overload to compute grad_f, the first derivative of |
| 63 | // the cost function with respect to x and also the cost function |
| 64 | virtual Real valueAndGradient(Array& grad, |
| 65 | const Array& x) const { |
| 66 | gradient(grad, x); |
| 67 | return value(x); |
| 68 | } |
| 69 | |
| 70 | //! method to overload to compute J_f, the jacobian of |
| 71 | // the cost function with respect to x |
| 72 | virtual void jacobian(Matrix &jac, const Array &x) const { |
| 73 | Real eps = finiteDifferenceEpsilon(); |
| 74 | Array xx(x), fp, fm; |
| 75 | for(Size i=0; i<x.size(); ++i) { |
| 76 | xx[i] += eps; |
| 77 | fp = values(x: xx); |
| 78 | xx[i] -= 2.0*eps; |
| 79 | fm = values(x: xx); |
| 80 | for(Size j=0; j<fp.size(); ++j) { |
| 81 | jac[j][i] = 0.5*(fp[j]-fm[j])/eps; |
| 82 | } |
| 83 | xx[i] = x[i]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | //! method to overload to compute J_f, the jacobian of |
| 88 | // the cost function with respect to x and also the cost function |
| 89 | virtual Array valuesAndJacobian(Matrix &jac, |
| 90 | const Array &x) const { |
| 91 | jacobian(jac,x); |
| 92 | return values(x); |
| 93 | } |
| 94 | |
| 95 | //! Default epsilon for finite difference method : |
| 96 | virtual Real finiteDifferenceEpsilon() const { return 1e-8; } |
| 97 | }; |
| 98 | |
| 99 | class ParametersTransformation { |
| 100 | public: |
| 101 | virtual ~ParametersTransformation() = default; |
| 102 | virtual Array direct(const Array& x) const = 0; |
| 103 | virtual Array inverse(const Array& x) const = 0; |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | #endif |
| 108 | |