| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2014 Peter Caspers |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <ql/experimental/volatility/sviinterpolation.hpp> |
| 21 | #include <ql/experimental/volatility/svismilesection.hpp> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | SviSmileSection::SviSmileSection(Time timeToExpiry, Rate forward, std::vector<Real> sviParams) |
| 27 | : SmileSection(timeToExpiry, DayCounter()), forward_(forward), params_(std::move(sviParams)) { |
| 28 | init(); |
| 29 | } |
| 30 | |
| 31 | SviSmileSection::SviSmileSection(const Date& d, |
| 32 | Rate forward, |
| 33 | std::vector<Real> sviParams, |
| 34 | const DayCounter& dc) |
| 35 | : SmileSection(d, dc, Date()), forward_(forward), params_(std::move(sviParams)) { |
| 36 | init(); |
| 37 | } |
| 38 | |
| 39 | void SviSmileSection::init() { |
| 40 | QL_REQUIRE(exerciseTime() > 0.0, "svi expects a strictly positive expiry time" ); |
| 41 | QL_REQUIRE(params_.size() == 5, |
| 42 | "svi expects 5 parameters (a,b,sigma,rho,m) but (" |
| 43 | << params_.size() << ") given" ); |
| 44 | detail::checkSviParameters(a: params_[0], b: params_[1], sigma: params_[2], rho: params_[3], m: params_[4], |
| 45 | tte: exerciseTime()); |
| 46 | } |
| 47 | |
| 48 | Volatility SviSmileSection::volatilityImpl(Rate strike) const { |
| 49 | |
| 50 | Real k = std::log(x: std::max(a: strike, b: 1E-6) / forward_); |
| 51 | Real totalVariance = detail::sviTotalVariance(a: params_[0], b: params_[1], sigma: params_[2], |
| 52 | rho: params_[3], m: params_[4],k); |
| 53 | return std::sqrt(x: std::max(a: 0.0, b: totalVariance / exerciseTime())); |
| 54 | |
| 55 | } |
| 56 | } // namespace QuantLib |
| 57 | |