| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb |
| 5 | Copyright (C) 2007 StatPro Italia srl |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <ql/models/shortrate/onefactormodels/vasicek.hpp> |
| 22 | #include <ql/pricingengines/blackformula.hpp> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | Vasicek::Vasicek(Rate r0, Real a, Real b, Real sigma, Real lambda) |
| 27 | : OneFactorAffineModel(4), r0_(r0), |
| 28 | a_(arguments_[0]), b_(arguments_[1]), sigma_(arguments_[2]), |
| 29 | lambda_(arguments_[3]) { |
| 30 | a_ = ConstantParameter(a, PositiveConstraint()); |
| 31 | b_ = ConstantParameter(b, NoConstraint()); |
| 32 | sigma_ = ConstantParameter(sigma, PositiveConstraint()); |
| 33 | lambda_ = ConstantParameter(lambda, NoConstraint()); |
| 34 | } |
| 35 | |
| 36 | Real Vasicek::A(Time t, Time T) const { |
| 37 | Real _a = a(); |
| 38 | if (_a < std::sqrt(QL_EPSILON)) { |
| 39 | return 0.0; |
| 40 | } else { |
| 41 | Real sigma2 = sigma()*sigma(); |
| 42 | Real bt = B(t, T); |
| 43 | return std::exp(x: (b() + lambda()*sigma()/_a |
| 44 | - 0.5*sigma2/(_a*_a))*(bt - (T - t)) |
| 45 | - 0.25*sigma2*bt*bt/_a); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Real Vasicek::B(Time t, Time T) const { |
| 50 | Real _a = a(); |
| 51 | if (_a < std::sqrt(QL_EPSILON)) |
| 52 | return (T - t); |
| 53 | else |
| 54 | return (1.0 - std::exp(x: -_a*(T - t)))/_a; |
| 55 | } |
| 56 | |
| 57 | Real Vasicek::discountBondOption(Option::Type type, |
| 58 | Real strike, Time maturity, |
| 59 | Time bondMaturity) const { |
| 60 | |
| 61 | Real v; |
| 62 | Real _a = a(); |
| 63 | if (std::fabs(x: maturity) < QL_EPSILON) { |
| 64 | v = 0.0; |
| 65 | } else if (_a < std::sqrt(QL_EPSILON)) { |
| 66 | v = sigma()*B(t: maturity, T: bondMaturity)* std::sqrt(x: maturity); |
| 67 | } else { |
| 68 | v = sigma()*B(t: maturity, T: bondMaturity)* |
| 69 | std::sqrt(x: 0.5*(1.0 - std::exp(x: -2.0*_a*maturity))/_a); |
| 70 | } |
| 71 | Real f = discountBond(now: 0.0, maturity: bondMaturity, rate: r0_); |
| 72 | Real k = discountBond(now: 0.0, maturity, rate: r0_)*strike; |
| 73 | |
| 74 | return blackFormula(optionType: type, strike: k, forward: f, stdDev: v); |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | |
| 79 | |