QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
irregularswaption.cpp
Go to the documentation of this file.
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) 2006 Cristina Duminuco
6 Copyright (C) 2006 Marco Bianchetti
7 Copyright (C) 2007 StatPro Italia srl
8 Copyright (C) 2010 Andre Miemiec
9
10 This file is part of QuantLib, a free-software/open-source library
11 for financial quantitative analysts and developers - http://quantlib.org/
12
13 QuantLib is free software: you can redistribute it and/or modify it
14 under the terms of the QuantLib license. You should have received a
15 copy of the license along with this program; if not, please email
16 <quantlib-dev@lists.sf.net>. The license is also available online at
17 <http://quantlib.org/license.shtml>.
18
19 This program is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 FOR A PARTICULAR PURPOSE. See the license for more details.
22*/
23
24#include <ql/any.hpp>
25#include <ql/exercise.hpp>
30#include <utility>
31
32namespace QuantLib {
33
34 namespace {
35
36 class IrregularImpliedVolHelper {
37 public:
38 IrregularImpliedVolHelper(const IrregularSwaption&,
39 Handle<YieldTermStructure> discountCurve,
40 Real targetValue);
41 Real operator()(Volatility x) const;
42 Real derivative(Volatility x) const;
43 private:
44 ext::shared_ptr<PricingEngine> engine_;
45 Handle<YieldTermStructure> discountCurve_;
47 ext::shared_ptr<SimpleQuote> vol_;
48 const Instrument::results* results_;
49 };
50
51 IrregularImpliedVolHelper::IrregularImpliedVolHelper(
52 const IrregularSwaption& swaption,
53 Handle<YieldTermStructure> discountCurve,
54 Real targetValue)
55 : discountCurve_(std::move(discountCurve)), targetValue_(targetValue),
56 vol_(ext::make_shared<SimpleQuote>(-1.0)) {
57
58 Handle<Quote> h(vol_);
59 engine_ = ext::shared_ptr<PricingEngine>(new
60 BlackSwaptionEngine(discountCurve_, h));
61 swaption.setupArguments(engine_->getArguments());
62
63 results_ =
64 dynamic_cast<const Instrument::results*>(engine_->getResults());
65 }
66
67 Real IrregularImpliedVolHelper::operator()(Volatility x) const {
68 if (x!=vol_->value()) {
69 vol_->setValue(x);
70 engine_->calculate();
71 }
72 return results_->value-targetValue_;
73 }
74
75 Real IrregularImpliedVolHelper::derivative(Volatility x) const {
76 if (x!=vol_->value()) {
77 vol_->setValue(x);
78 engine_->calculate();
79 }
80 auto vega_ = results_->additionalResults.find("vega");
81 QL_REQUIRE(vega_ != results_->additionalResults.end(),
82 "vega not provided");
83 return ext::any_cast<Real>(vega_->second);
84 }
85 }
86
87 std::ostream& operator<<(std::ostream& out,
89 switch (t) {
90 case IrregularSettlement::Physical:
91 return out << "Delivery";
92 case IrregularSettlement::Cash:
93 return out << "Cash";
94 default:
95 QL_FAIL("unknown IrregularSettlement::Type(" << Integer(t) << ")");
96 }
97 }
98
99 IrregularSwaption::IrregularSwaption(ext::shared_ptr<IrregularSwap> swap,
100 const ext::shared_ptr<Exercise>& exercise,
102 : Option(ext::shared_ptr<Payoff>(), exercise), swap_(std::move(swap)),
103 settlementType_(delivery) {
105 }
106
108 return detail::simple_event(exercise_->dates().back()).hasOccurred();
109 }
110
112
113 swap_->setupArguments(args);
114
115 auto* arguments = dynamic_cast<IrregularSwaption::arguments*>(args);
116
117 QL_REQUIRE(arguments != nullptr, "wrong argument type");
118
122 }
123
126 QL_REQUIRE(swap, "Irregular swap not set");
127 QL_REQUIRE(exercise, "exercise not set");
128 }
129
131 Real targetValue,
132 const Handle<YieldTermStructure>& discountCurve,
133 Volatility guess,
134 Real accuracy,
135 Natural maxEvaluations,
136 Volatility minVol,
137 Volatility maxVol) const {
138 calculate();
139 QL_REQUIRE(!isExpired(), "instrument expired");
140
141 IrregularImpliedVolHelper f(*this, discountCurve, targetValue);
142 //Brent solver;
143 NewtonSafe solver;
144 solver.setMaxEvaluations(maxEvaluations);
145 return solver.solve(f, accuracy, guess, minVol, maxVol);
146 }
147
148}
Maps any to either the boost or std implementation.
Black-formula swaption engine.
Real targetValue_
const YieldTermStructure & discountCurve_
Definition: cashflows.cpp:418
ext::shared_ptr< SimpleQuote > vol_
Definition: cdsoption.cpp:62
ext::shared_ptr< PricingEngine > engine_
Definition: cdsoption.cpp:60
const Instrument::results * results_
Definition: cdsoption.cpp:63
virtual bool hasOccurred(const Date &refDate=Date(), ext::optional< bool > includeRefDate=ext::nullopt) const
returns true if an event has already occurred before a date
Definition: event.cpp:28
Shared handle to an observable.
Definition: handle.hpp:41
void calculate() const override
Definition: instrument.hpp:129
Arguments for irregular-swaption calculation
ext::shared_ptr< IrregularSwap > swap
IrregularSettlement::Type settlementType
void setupArguments(PricingEngine::arguments *) const override
bool isExpired() const override
returns whether the instrument might have value greater than zero.
IrregularSettlement::Type settlementType_
ext::shared_ptr< IrregularSwap > swap_
Volatility impliedVolatility(Real price, const Handle< YieldTermStructure > &discountCurve, Volatility guess, Real accuracy=1.0e-4, Natural maxEvaluations=100, Volatility minVol=1.0e-7, Volatility maxVol=4.0) const
implied volatility
safe Newton 1-D solver
Definition: newtonsafe.hpp:40
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
base option class
Definition: option.hpp:36
ext::shared_ptr< Exercise > exercise_
Definition: option.hpp:50
Abstract base class for option payoffs.
Definition: payoff.hpp:36
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Option exercise classes and payoff function.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Volatility
volatility
Definition: types.hpp:78
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Handle< YieldTermStructure > discountCurve_
ext::shared_ptr< SimpleQuote > vol_
ext::shared_ptr< PricingEngine > engine_
Real targetValue_
const Instrument::results * results_
Irregular swaption class.
Definition: any.hpp:35
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
STL namespace.
Safe (bracketed) Newton 1-D solver.
simple quote class