| 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) 2005, 2006 Klaus Spanderen |
| 7 | Copyright (C) 2007 Giorgio Facchinetti |
| 8 | Copyright (C) 2015 Paolo Mazzocchi |
| 9 | |
| 10 | This file is part of QuantLib, a free-software/open-source library |
| 11 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 12 | |
| 13 | QuantLib is free software: you can redistribute it and/or modify it |
| 14 | under the terms of the QuantLib license. You should have received a |
| 15 | copy of the license along with this program; if not, please email |
| 16 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 17 | <http://quantlib.org/license.shtml>. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 21 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <ql/math/abcdmathfunction.hpp> |
| 25 | #include <utility> |
| 26 | |
| 27 | namespace QuantLib { |
| 28 | |
| 29 | void AbcdMathFunction::validate(Real a, |
| 30 | Real b, |
| 31 | Real c, |
| 32 | Real d) { |
| 33 | QL_REQUIRE(c>0, "c (" << c << ") must be positive" ); |
| 34 | QL_REQUIRE(d>=0, "d (" << d << ") must be non negative" ); |
| 35 | QL_REQUIRE(a+d>=0, |
| 36 | "a+d (" << a << "+" << d << ") must be non negative" ); |
| 37 | |
| 38 | if (b>=0.0) |
| 39 | return; |
| 40 | |
| 41 | // the one and only stationary point... |
| 42 | Time zeroFirstDerivative = 1.0/c-a/b; |
| 43 | if (zeroFirstDerivative>=0.0) { |
| 44 | // ... is a minimum |
| 45 | // must be abcd(zeroFirstDerivative)>=0 |
| 46 | QL_REQUIRE(b>=-(d*c)/std::exp(c*a/b-1.0), |
| 47 | "b (" << b << ") less than " << |
| 48 | -(d*c)/std::exp(c*a/b-1.0) << ": negative function" |
| 49 | " value at stationary point " << zeroFirstDerivative); |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | void AbcdMathFunction::initialize_() { |
| 55 | validate(a: a_, b: b_, c: c_, d: d_); |
| 56 | da_ = b_ - c_*a_; |
| 57 | db_ = -c_*b_; |
| 58 | dabcd_[0]=da_; |
| 59 | dabcd_[1]=db_; |
| 60 | dabcd_[2]=c_; |
| 61 | dabcd_[3]=0.0; |
| 62 | |
| 63 | pa_ = -(a_ + b_/c_)/c_; |
| 64 | pb_ = -b_/c_; |
| 65 | K_ = 0.0; |
| 66 | |
| 67 | dibc_ = b_/c_; |
| 68 | diacplusbcc_ = a_/c_ + dibc_/c_; |
| 69 | } |
| 70 | |
| 71 | AbcdMathFunction::AbcdMathFunction(Real aa, Real bb, Real cc, Real dd) |
| 72 | : a_(aa), b_(bb), c_(cc), d_(dd), abcd_(4), dabcd_(4) { |
| 73 | abcd_[0]=a_; |
| 74 | abcd_[1]=b_; |
| 75 | abcd_[2]=c_; |
| 76 | abcd_[3]=d_; |
| 77 | initialize_(); |
| 78 | } |
| 79 | |
| 80 | AbcdMathFunction::AbcdMathFunction(std::vector<Real> abcd) : abcd_(std::move(abcd)), dabcd_(4) { |
| 81 | a_=abcd_[0]; |
| 82 | b_=abcd_[1]; |
| 83 | c_=abcd_[2]; |
| 84 | d_=abcd_[3]; |
| 85 | initialize_(); |
| 86 | } |
| 87 | |
| 88 | Time AbcdMathFunction::maximumLocation() const { |
| 89 | if (b_==0.0) { |
| 90 | if (a_>=0.0) |
| 91 | return 0.0; |
| 92 | else |
| 93 | return QL_MAX_REAL; |
| 94 | } |
| 95 | |
| 96 | // stationary point |
| 97 | // TODO check if minimum |
| 98 | // TODO check if maximum at +inf |
| 99 | Real zeroFirstDerivative = 1.0/c_-a_/b_; |
| 100 | return (zeroFirstDerivative>0.0 ? zeroFirstDerivative : 0.0); |
| 101 | } |
| 102 | |
| 103 | Real AbcdMathFunction::definiteIntegral(Time t1, |
| 104 | Time t2) const { |
| 105 | return primitive(t: t2)-primitive(t: t1); |
| 106 | } |
| 107 | |
| 108 | std::vector<Real> |
| 109 | AbcdMathFunction::definiteIntegralCoefficients(Time t, |
| 110 | Time t2) const { |
| 111 | Time dt = t2 - t; |
| 112 | Real expcdt = std::exp(x: -c_*dt); |
| 113 | std::vector<Real> result(4); |
| 114 | result[0] = diacplusbcc_ - (diacplusbcc_ + dibc_*dt)*expcdt; |
| 115 | result[1] = dibc_ * (1.0 - expcdt); |
| 116 | result[2] = c_; |
| 117 | result[3] = d_*dt; |
| 118 | return result; |
| 119 | } |
| 120 | |
| 121 | std::vector<Real> |
| 122 | AbcdMathFunction::definiteDerivativeCoefficients(Time t, |
| 123 | Time t2) const { |
| 124 | Time dt = t2 - t; |
| 125 | Real expcdt = std::exp(x: -c_*dt); |
| 126 | std::vector<Real> result(4); |
| 127 | result[1] = b_*c_/(1.0-expcdt); |
| 128 | result[0] = a_*c_ - b_ + result[1]*dt*expcdt; |
| 129 | result[0] /= 1.0-expcdt; |
| 130 | result[2] = c_; |
| 131 | result[3] = d_/dt; |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |