/src/quantlib/ql/termstructures/volatility/gaussian1dsmilesection.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2015 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 | | <https://www.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/instruments/makecapfloor.hpp> |
21 | | #include <ql/instruments/makeswaption.hpp> |
22 | | #include <ql/pricingengines/blackformula.hpp> |
23 | | #include <ql/termstructures/volatility/gaussian1dsmilesection.hpp> |
24 | | #include <utility> |
25 | | |
26 | | using std::sqrt; |
27 | | |
28 | | namespace QuantLib { |
29 | | |
30 | | Gaussian1dSmileSection::Gaussian1dSmileSection( |
31 | | const Date& fixingDate, |
32 | | ext::shared_ptr<SwapIndex> swapIndex, |
33 | | const ext::shared_ptr<Gaussian1dModel>& model, |
34 | | const DayCounter& dc, |
35 | | const ext::shared_ptr<Gaussian1dSwaptionEngine>& swaptionEngine) |
36 | 0 | : SmileSection(fixingDate, dc, model->termStructure()->referenceDate()), |
37 | 0 | fixingDate_(fixingDate), swapIndex_(std::move(swapIndex)), model_(model), |
38 | 0 | engine_(swaptionEngine) { |
39 | |
|
40 | 0 | atm_ = model_->swapRate(fixingDate_, swapIndex_->tenor(), Date(), 0.0, swapIndex_); |
41 | 0 | annuity_ = |
42 | 0 | model_->swapAnnuity(fixingDate_, swapIndex_->tenor(), Date(), 0.0, swapIndex_); |
43 | |
|
44 | 0 | if (engine_ == nullptr) { |
45 | 0 | engine_ = ext::make_shared<Gaussian1dSwaptionEngine>( |
46 | 0 | model_, 64, 7.0, true, false, swapIndex_->discountingTermStructure()); |
47 | 0 | } |
48 | 0 | } Unexecuted instantiation: QuantLib::Gaussian1dSmileSection::Gaussian1dSmileSection(QuantLib::Date const&, boost::shared_ptr<QuantLib::SwapIndex>, boost::shared_ptr<QuantLib::Gaussian1dModel> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::Gaussian1dSwaptionEngine> const&) Unexecuted instantiation: QuantLib::Gaussian1dSmileSection::Gaussian1dSmileSection(QuantLib::Date const&, boost::shared_ptr<QuantLib::SwapIndex>, boost::shared_ptr<QuantLib::Gaussian1dModel> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::Gaussian1dSwaptionEngine> const&) |
49 | | |
50 | | Gaussian1dSmileSection::Gaussian1dSmileSection( |
51 | | const Date& fixingDate, |
52 | | ext::shared_ptr<IborIndex> iborIndex, |
53 | | const ext::shared_ptr<Gaussian1dModel>& model, |
54 | | const DayCounter& dc, |
55 | | const ext::shared_ptr<Gaussian1dCapFloorEngine>& capEngine) |
56 | 0 | : SmileSection(fixingDate, dc, model->termStructure()->referenceDate()), |
57 | 0 | fixingDate_(fixingDate), iborIndex_(std::move(iborIndex)), model_(model), engine_(capEngine) { |
58 | |
|
59 | 0 | atm_ = model_->forwardRate(fixingDate_, Date(), 0.0, iborIndex_); |
60 | 0 | CapFloor c = |
61 | 0 | MakeCapFloor(CapFloor::Cap, iborIndex_->tenor(), iborIndex_, Null<Real>(), 0 * Days) |
62 | 0 | .withEffectiveDate(fixingDate_, false); |
63 | 0 | annuity_ = iborIndex_->dayCounter().yearFraction(c.startDate(), c.maturityDate()) * |
64 | 0 | model_->zerobond(c.maturityDate()); |
65 | |
|
66 | 0 | if (engine_ == nullptr) { |
67 | 0 | engine_ = ext::make_shared<Gaussian1dCapFloorEngine>( |
68 | 0 | model_, 64, 7.0, true, |
69 | 0 | false); // use model curve as discounting curve |
70 | 0 | } |
71 | 0 | } Unexecuted instantiation: QuantLib::Gaussian1dSmileSection::Gaussian1dSmileSection(QuantLib::Date const&, boost::shared_ptr<QuantLib::IborIndex>, boost::shared_ptr<QuantLib::Gaussian1dModel> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::Gaussian1dCapFloorEngine> const&) Unexecuted instantiation: QuantLib::Gaussian1dSmileSection::Gaussian1dSmileSection(QuantLib::Date const&, boost::shared_ptr<QuantLib::IborIndex>, boost::shared_ptr<QuantLib::Gaussian1dModel> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::Gaussian1dCapFloorEngine> const&) |
72 | | |
73 | 0 | Real Gaussian1dSmileSection::atmLevel() const { return atm_; } |
74 | | |
75 | | Real Gaussian1dSmileSection::optionPrice(Rate strike, Option::Type type, |
76 | 0 | Real discount) const { |
77 | |
|
78 | 0 | if (swapIndex_ != nullptr) { |
79 | 0 | Swaption s = MakeSwaption(swapIndex_, fixingDate_, strike) |
80 | 0 | .withUnderlyingType(type == Option::Call |
81 | 0 | ? Swap::Payer |
82 | 0 | : Swap::Receiver) |
83 | 0 | .withPricingEngine(engine_); |
84 | 0 | Real tmp = s.NPV(); |
85 | 0 | return tmp / annuity_ * discount; |
86 | 0 | } else { |
87 | 0 | CapFloor c = |
88 | 0 | MakeCapFloor(type == Option::Call ? CapFloor::Cap : CapFloor::Floor, |
89 | 0 | iborIndex_->tenor(), iborIndex_, strike, 0 * Days) |
90 | 0 | .withEffectiveDate(fixingDate_, false) |
91 | 0 | .withPricingEngine(engine_); |
92 | 0 | Real tmp = c.NPV(); |
93 | 0 | return tmp / annuity_ * discount; |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | 0 | Real Gaussian1dSmileSection::volatilityImpl(Rate strike) const { |
98 | 0 | Real vol = 0.0; |
99 | 0 | try { |
100 | 0 | Option::Type type = strike >= atm_ ? Option::Call : Option::Put; |
101 | 0 | Real o = optionPrice(strike, type); |
102 | 0 | vol = blackFormulaImpliedStdDev(type, strike, atm_, o) / |
103 | 0 | sqrt(exerciseTime()); |
104 | 0 | } catch (...) { |
105 | 0 | } |
106 | 0 | return vol; |
107 | 0 | } |
108 | | } |