1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* 4 Copyright (C) 2006, 2007, 2015 Ferdinando Ametrano 5 Copyright (C) 2006 Cristina Duminuco 6 Copyright (C) 2007 Giorgio Facchinetti 7 Copyright (C) 2015 Paolo Mazzocchi 8 9 This file is part of QuantLib, a free-software/open-source library 10 for financial quantitative analysts and developers - http://quantlib.org/ 11 12 QuantLib is free software: you can redistribute it and/or modify it 13 under the terms of the QuantLib license. You should have received a 14 copy of the license along with this program; if not, please email 15 <quantlib-dev@lists.sf.net>. The license is also available online at 16 <http://quantlib.org/license.shtml>. 17 18 This program is distributed in the hope that it will be useful, but WITHOUT 19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 20 FOR A PARTICULAR PURPOSE. See the license for more details. 21 */ 22 23 #ifndef quantlib_abcdcalibration_hpp 24 #define quantlib_abcdcalibration_hpp 25 26 27 #include <ql/math/optimization/endcriteria.hpp> 28 #include <ql/math/optimization/projectedcostfunction.hpp> 29 #include <ql/math/array.hpp> 30 31 #include <ql/shared_ptr.hpp> 32 33 #include <vector> 34 35 36 namespace QuantLib { 37 38 class Quote; 39 class OptimizationMethod; 40 class ParametersTransformation; 41 42 class AbcdCalibration { 43 private: 44 class AbcdError : public CostFunction { 45 public: AbcdError(AbcdCalibration * abcd)46 AbcdError(AbcdCalibration* abcd) : abcd_(abcd) {} 47 value(const Array & x) const48 Real value(const Array& x) const { 49 const Array y = abcd_->transformation_->direct(x); 50 abcd_->a_ = y[0]; 51 abcd_->b_ = y[1]; 52 abcd_->c_ = y[2]; 53 abcd_->d_ = y[3]; 54 return abcd_->error(); 55 } values(const Array & x) const56 Disposable<Array> values(const Array& x) const { 57 const Array y = abcd_->transformation_->direct(x); 58 abcd_->a_ = y[0]; 59 abcd_->b_ = y[1]; 60 abcd_->c_ = y[2]; 61 abcd_->d_ = y[3]; 62 return abcd_->errors(); 63 } 64 private: 65 AbcdCalibration* abcd_; 66 }; 67 68 class AbcdParametersTransformation : public ParametersTransformation { 69 public: AbcdParametersTransformation()70 AbcdParametersTransformation() : y_(Array(4)) {} 71 // to constrained <- from unconstrained 72 Array direct(const Array& x) const; 73 // to unconstrained <- from constrained 74 Array inverse(const Array& x) const; 75 private: 76 mutable Array y_; 77 }; 78 79 public: AbcdCalibration()80 AbcdCalibration() {}; 81 AbcdCalibration( 82 const std::vector<Real>& t, 83 const std::vector<Real>& blackVols, 84 Real aGuess = -0.06, 85 Real bGuess = 0.17, 86 Real cGuess = 0.54, 87 Real dGuess = 0.17, 88 bool aIsFixed = false, 89 bool bIsFixed = false, 90 bool cIsFixed = false, 91 bool dIsFixed = false, 92 bool vegaWeighted = false, 93 const ext::shared_ptr<EndCriteria>& endCriteria 94 = ext::shared_ptr<EndCriteria>(), 95 const ext::shared_ptr<OptimizationMethod>& method 96 = ext::shared_ptr<OptimizationMethod>()); 97 //! adjustment factors needed to match Black vols 98 std::vector<Real> k(const std::vector<Real>& t, 99 const std::vector<Real>& blackVols) const; 100 void compute(); 101 //calibration results 102 Real value(Real x) const; 103 Real error() const; 104 Real maxError() const; 105 Disposable<Array> errors() const; 106 EndCriteria::Type endCriteria() const; a() const107 Real a() const { return a_; } b() const108 Real b() const { return b_; } c() const109 Real c() const { return c_; } d() const110 Real d() const { return d_; } 111 bool aIsFixed_, bIsFixed_, cIsFixed_, dIsFixed_; 112 Real a_, b_, c_, d_; 113 ext::shared_ptr<ParametersTransformation> transformation_; 114 private: 115 // optimization method used for fitting 116 mutable EndCriteria::Type abcdEndCriteria_; 117 ext::shared_ptr<EndCriteria> endCriteria_; 118 ext::shared_ptr<OptimizationMethod> optMethod_; 119 mutable std::vector<Real> weights_; 120 bool vegaWeighted_; 121 //! Parameters 122 std::vector<Real> times_, blackVols_; 123 }; 124 125 } 126 127 #endif 128