| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 Roland Stamm |
| 5 | Copyright (C) 2009 Jose Aparicio |
| 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/experimental/credit/cdsoption.hpp> |
| 22 | #include <ql/experimental/credit/blackcdsoptionengine.hpp> |
| 23 | #include <ql/exercise.hpp> |
| 24 | #include <ql/quotes/simplequote.hpp> |
| 25 | #include <ql/instruments/payoffs.hpp> |
| 26 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 27 | #include <ql/math/distributions/normaldistribution.hpp> |
| 28 | #include <ql/math/solvers1d/brent.hpp> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class ImpliedVolHelper { |
| 35 | public: |
| 36 | ImpliedVolHelper( |
| 37 | const CdsOption& cdsoption, |
| 38 | const Handle<DefaultProbabilityTermStructure>& probability, |
| 39 | Real recoveryRate, |
| 40 | const Handle<YieldTermStructure>& termStructure, |
| 41 | Real targetValue) |
| 42 | : targetValue_(targetValue), vol_(ext::make_shared<SimpleQuote>(args: 0.0)) { |
| 43 | |
| 44 | Handle<Quote> h(vol_); |
| 45 | engine_ = ext::shared_ptr<PricingEngine>( |
| 46 | new BlackCdsOptionEngine(probability, recoveryRate, |
| 47 | termStructure, h)); |
| 48 | cdsoption.setupArguments(engine_->getArguments()); |
| 49 | |
| 50 | results_ = |
| 51 | dynamic_cast<const Instrument::results*>( |
| 52 | engine_->getResults()); |
| 53 | } |
| 54 | Real operator()(Volatility x) const { |
| 55 | vol_->setValue(x); |
| 56 | engine_->calculate(); |
| 57 | return results_->value-targetValue_; |
| 58 | } |
| 59 | private: |
| 60 | ext::shared_ptr<PricingEngine> engine_; |
| 61 | Real targetValue_; |
| 62 | ext::shared_ptr<SimpleQuote> vol_; |
| 63 | const Instrument::results* results_; |
| 64 | }; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | |
| 69 | CdsOption::CdsOption(const ext::shared_ptr<CreditDefaultSwap>& swap, |
| 70 | const ext::shared_ptr<Exercise>& exercise, |
| 71 | bool knocksOut) |
| 72 | : Option(ext::shared_ptr<Payoff>(new NullPayoff), exercise), |
| 73 | swap_(swap), knocksOut_(knocksOut) { |
| 74 | QL_REQUIRE(swap->side() == Protection::Buyer || knocksOut_, |
| 75 | "receiver CDS options must knock out" ); |
| 76 | QL_REQUIRE(!swap->upfront(), "underlying must be running-spread only" ); |
| 77 | registerWith(h: swap_); |
| 78 | } |
| 79 | |
| 80 | bool CdsOption::isExpired () const { |
| 81 | return detail::simple_event(exercise_->dates().back()).hasOccurred(); |
| 82 | } |
| 83 | |
| 84 | void CdsOption::setupExpired() const { |
| 85 | Instrument::setupExpired(); |
| 86 | riskyAnnuity_ = 0.0; |
| 87 | } |
| 88 | |
| 89 | void CdsOption::setupArguments(PricingEngine::arguments* args) const { |
| 90 | swap_->setupArguments(args); |
| 91 | Option::setupArguments(args); |
| 92 | |
| 93 | auto* arguments = dynamic_cast<CdsOption::arguments*>(args); |
| 94 | |
| 95 | QL_REQUIRE(arguments != nullptr, "wrong argument type" ); |
| 96 | |
| 97 | arguments->swap = swap_; |
| 98 | arguments->knocksOut = knocksOut_; |
| 99 | } |
| 100 | |
| 101 | void CdsOption::fetchResults(const PricingEngine::results* r) const { |
| 102 | Option::fetchResults(r); |
| 103 | const auto* results = dynamic_cast<const CdsOption::results*>(r); |
| 104 | QL_ENSURE(results != nullptr, "wrong results type" ); |
| 105 | riskyAnnuity_ = results->riskyAnnuity; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | Rate CdsOption::atmRate() const{ |
| 111 | return swap_->fairSpread(); |
| 112 | } |
| 113 | |
| 114 | Real CdsOption::riskyAnnuity() const { |
| 115 | calculate(); |
| 116 | QL_REQUIRE(riskyAnnuity_ != Null<Real>(), "risky annuity not provided" ); |
| 117 | return riskyAnnuity_; |
| 118 | } |
| 119 | |
| 120 | Volatility CdsOption::impliedVolatility( |
| 121 | Real targetValue, |
| 122 | const Handle<YieldTermStructure>& termStructure, |
| 123 | const Handle<DefaultProbabilityTermStructure>& probability, |
| 124 | Real recoveryRate, |
| 125 | Real accuracy, |
| 126 | Size maxEvaluations, |
| 127 | Volatility minVol, |
| 128 | Volatility maxVol) const { |
| 129 | calculate(); |
| 130 | QL_REQUIRE(!isExpired(), "instrument expired" ); |
| 131 | |
| 132 | Volatility guess = 0.10; |
| 133 | |
| 134 | ImpliedVolHelper f(*this, probability, recoveryRate, |
| 135 | termStructure, targetValue); |
| 136 | Brent solver; |
| 137 | solver.setMaxEvaluations(maxEvaluations); |
| 138 | return solver.solve(f, accuracy, guess, xMin: minVol, xMax: maxVol); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | void CdsOption::arguments::validate() const { |
| 144 | CreditDefaultSwap::arguments::validate(); |
| 145 | Option::arguments::validate(); |
| 146 | QL_REQUIRE(swap, "CDS not set" ); |
| 147 | QL_REQUIRE(exercise, "exercise not set" ); |
| 148 | } |
| 149 | |
| 150 | void CdsOption::results::reset() { |
| 151 | Option::results::reset(); |
| 152 | riskyAnnuity = Null<Real>(); |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | |