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

source code of quantlib/ql/termstructures/volatility/abcd.cpp