QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
abcdmathfunction.cpp
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) 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
25#include <utility>
26
27namespace QuantLib {
28
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
55 validate(a_, b_, c_, 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_;
69 }
70
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_;
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];
86 }
87
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
104 Time t2) const {
105 return primitive(t2)-primitive(t1);
106 }
107
108 std::vector<Real>
110 Time t2) const {
111 Time dt = t2 - t;
112 Real expcdt = std::exp(-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>
123 Time t2) const {
124 Time dt = t2 - t;
125 Real expcdt = std::exp(-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}
static void validate(Real a, Real b, Real c, Real d)
Real definiteIntegral(Time t1, Time t2) const
std::vector< Real > definiteIntegralCoefficients(Time t, Time t2) const
Time maximumLocation() const
time at which the function reaches maximum (if any)
AbcdMathFunction(Real a=0.002, Real b=0.001, Real c=0.16, Real d=0.0005)
std::vector< Real > dabcd_
std::vector< Real > definiteDerivativeCoefficients(Time t, Time t2) const
Real a_
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
ext::function< Real(Real)> b
#define QL_MAX_REAL
Definition: qldefines.hpp:176
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
const VF_R b_
Definition: any.hpp:35
STL namespace.