/src/quantlib/ql/termstructures/volatility/abcd.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2006, 2007 Ferdinando Ametrano |
5 | | Copyright (C) 2006 Cristina Duminuco |
6 | | Copyright (C) 2005, 2006 Klaus Spanderen |
7 | | Copyright (C) 2007 Giorgio Facchinetti |
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 | | <https://www.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 | | #include <ql/termstructures/volatility/abcd.hpp> |
24 | | #include <ql/math/comparison.hpp> |
25 | | #include <algorithm> |
26 | | |
27 | | namespace QuantLib { |
28 | | |
29 | | AbcdFunction::AbcdFunction(Real a, Real b, Real c, Real d) |
30 | 0 | : AbcdMathFunction(a, b, c, d) {} |
31 | | |
32 | 0 | Real AbcdFunction::volatility(Time tMin, Time tMax, Time T) const { |
33 | 0 | if (tMax==tMin) |
34 | 0 | return instantaneousVolatility(tMax, T); |
35 | 0 | QL_REQUIRE(tMax>tMin, "tMax must be > tMin"); |
36 | 0 | return std::sqrt(variance(tMin, tMax, T)/(tMax-tMin)); |
37 | 0 | } |
38 | | |
39 | 0 | Real AbcdFunction::variance(Time tMin, Time tMax, Time T) const { |
40 | 0 | return covariance(tMin, tMax, T, T); |
41 | 0 | } |
42 | | |
43 | 0 | Real AbcdFunction::covariance(Time t, Time T, Time S) const { |
44 | 0 | return (*this)(T-t) * (*this)(S-t); |
45 | 0 | } |
46 | | |
47 | 0 | Real AbcdFunction::covariance(Time t1, Time t2, Time T, Time S) const { |
48 | 0 | QL_REQUIRE(t1<=t2, |
49 | 0 | "integrations bounds (" << t1 << |
50 | 0 | "," << t2 << ") are in reverse order"); |
51 | 0 | Time cutOff = std::min(S,T); |
52 | 0 | if (t1>=cutOff) { |
53 | 0 | return 0.0; |
54 | 0 | } else { |
55 | 0 | cutOff = std::min(t2, cutOff); |
56 | 0 | return primitive(cutOff, T, S) - primitive(t1, T, S); |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | // INSTANTANEOUS |
61 | 0 | Real AbcdFunction::instantaneousVolatility(Time u, Time T) const { |
62 | 0 | return std::sqrt(instantaneousVariance(u, T)); |
63 | 0 | } |
64 | | |
65 | 0 | Real AbcdFunction::instantaneousVariance(Time u, Time T) const { |
66 | 0 | return instantaneousCovariance(u, T, T); |
67 | 0 | } |
68 | 0 | Real AbcdFunction::instantaneousCovariance(Time u, Time T, Time S) const { |
69 | 0 | return (*this)(T-u)*(*this)(S-u); |
70 | 0 | } |
71 | | |
72 | | // PRIMITIVE |
73 | 0 | Real AbcdFunction::primitive(Time t, Time T, Time S) const { |
74 | 0 | if (T<t || S<t) return 0.0; |
75 | | |
76 | 0 | if (close(c_,0.0)) { |
77 | 0 | Real v = a_+d_; |
78 | 0 | return t*(v*v+v*b_*S+v*b_*T-v*b_*t+b_*b_*S*T-0.5*b_*b_*t*(S+T)+b_*b_*t*t/3.0); |
79 | 0 | } |
80 | | |
81 | 0 | Real k1=std::exp(c_*t), k2=std::exp(c_*S), k3=std::exp(c_*T); |
82 | |
|
83 | 0 | return (b_*b_*(-1 - 2*c_*c_*S*T - c_*(S + T) |
84 | 0 | + k1*k1*(1 + c_*(S + T - 2*t) + 2*c_*c_*(S - t)*(T - t))) |
85 | 0 | + 2*c_*c_*(2*d_*a_*(k2 + k3)*(k1 - 1) |
86 | 0 | +a_*a_*(k1*k1 - 1)+2*c_*d_*d_*k2*k3*t) |
87 | 0 | + 2*b_*c_*(a_*(-1 - c_*(S + T) + k1*k1*(1 + c_*(S + T - 2*t))) |
88 | 0 | -2*d_*(k3*(1 + c_*S) + k2*(1 + c_*T) |
89 | 0 | - k1*k3*(1 + c_*(S - t)) |
90 | 0 | - k1*k2*(1 + c_*(T - t))) |
91 | 0 | ) |
92 | 0 | ) / (4*c_*c_*c_*k2*k3); |
93 | 0 | } |
94 | | |
95 | | //===========================================================================// |
96 | | // AbcdSquared // |
97 | | //===========================================================================// |
98 | | |
99 | | AbcdSquared::AbcdSquared(Real a, Real b, Real c, Real d, Time T, Time S) |
100 | 0 | : abcd_(new AbcdFunction(a,b,c,d)), |
101 | 0 | T_(T), S_(S) {} |
102 | | |
103 | 0 | Real AbcdSquared::operator()(Time t) const { |
104 | 0 | return abcd_->covariance(t, T_, S_); |
105 | 0 | } |
106 | | } |