QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
costfunction.hpp
Go to the documentation of this file.
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
31namespace QuantLib {
32
33 //! Cost function abstract class for optimization problem
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(v.begin(), v.end(), v.begin(), [](Real x) -> Real { return x*x; });
41 return std::sqrt(std::accumulate(v.begin(), v.end(), 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(xx);
55 xx[i] -= 2.0*eps;
56 fm = value(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
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 {
74 Array xx(x), fp, fm;
75 for(Size i=0; i<x.size(); ++i) {
76 xx[i] += eps;
77 fp = values(xx);
78 xx[i] -= 2.0*eps;
79 fm = values(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
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
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
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Cost function abstract class for optimization problem.
virtual Array values(const Array &x) const =0
method to overload to compute the cost function values in x
virtual Real value(const Array &x) const
method to overload to compute the cost function value in x
virtual Array valuesAndJacobian(Matrix &jac, const Array &x) const
method to overload to compute J_f, the jacobian of
virtual ~CostFunction()=default
virtual void gradient(Array &grad, const Array &x) const
method to overload to compute grad_f, the first derivative of
virtual void jacobian(Matrix &jac, const Array &x) const
method to overload to compute J_f, the jacobian of
virtual Real valueAndGradient(Array &grad, const Array &x) const
method to overload to compute grad_f, the first derivative of
virtual Real finiteDifferenceEpsilon() const
Default epsilon for finite difference method :
Matrix used in linear algebra.
Definition: matrix.hpp:41
virtual Array direct(const Array &x) const =0
virtual ~ParametersTransformation()=default
virtual Array inverse(const Array &x) const =0
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
matrix used in linear algebra.
Definition: any.hpp:35
ext::shared_ptr< BlackVolTermStructure > v