QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
smilesection.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) 2006 Mario Pucci
5 Copyright (C) 2013, 2015 Peter Caspers
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
23#include <ql/settings.hpp>
25#include <utility>
26
27using std::sqrt;
28
29namespace QuantLib {
30
32 if (isFloating_) {
35 }
36 }
37
40 "expiry date (" << exerciseDate_ <<
41 ") must be greater than reference date (" <<
42 referenceDate_ << ")");
44 }
45
47 DayCounter dc,
48 const Date& referenceDate,
49 const VolatilityType type,
50 const Rate shift)
51 : exerciseDate_(d), dc_(std::move(dc)), volatilityType_(type), shift_(shift) {
53 if (isFloating_) {
54 registerWith(Settings::instance().evaluationDate());
56 } else
59 }
60
62 DayCounter dc,
63 const VolatilityType type,
64 const Rate shift)
65 : isFloating_(false), dc_(std::move(dc)), exerciseTime_(exerciseTime), volatilityType_(type),
66 shift_(shift) {
68 "expiry time must be positive: " <<
69 exerciseTime_ << " not allowed");
70 }
71
73 Option::Type type,
74 Real discount) const {
75 Real atm = atmLevel();
76 QL_REQUIRE(atm != Null<Real>(),
77 "smile section must provide atm level to compute option price");
78 // if lognormal or shifted lognormal,
79 // for strike at -shift, return option price even if outside
80 // minstrike, maxstrike interval
82 return blackFormula(type,strike,atm, std::fabs(strike+shift()) < QL_EPSILON ?
83 0.2 : Real(sqrt(variance(strike))),discount,shift());
84 else
85 return bachelierBlackFormula(type,strike,atm,sqrt(variance(strike)),discount);
86 }
87
89 Option::Type type,
90 Real discount,
91 Real gap) const {
93 Real kl = std::max(strike-gap/2.0,m);
94 Real kr = kl+gap;
95 return (type==Option::Call ? 1.0 : -1.0) *
96 (optionPrice(kl,type,discount)-optionPrice(kr,type,discount)) / gap;
97 }
98
99 Real SmileSection::density(Rate strike, Real discount, Real gap) const {
101 Real kl = std::max(strike-gap/2.0,m);
102 Real kr = kl+gap;
103 return (digitalOptionPrice(kl,Option::Call,discount,gap) -
104 digitalOptionPrice(kr,Option::Call,discount,gap)) / gap;
105 }
106
107 Real SmileSection::vega(Rate strike, Real discount) const {
108 Real atm = atmLevel();
109 QL_REQUIRE(atm != Null<Real>(),
110 "smile section must provide atm level to compute option vega");
112 return blackFormulaVolDerivative(strike,atmLevel(),
113 sqrt(variance(strike)),
114 exerciseTime(),discount,shift())*0.01;
115 else
116 QL_FAIL("vega for normal smilesection not yet implemented");
117 }
118
120 Real shift) const {
122 return volatility(strike);
123 Real atm = atmLevel();
124 QL_REQUIRE(atm != Null<Real>(),
125 "smile section must provide atm level to compute converted volatilties");
126 Option::Type type = strike >= atm ? Option::Call : Option::Put;
127 Real premium = optionPrice(strike,type);
128 Real premiumAtm = optionPrice(atm,type);
130 try {
131 return blackFormulaImpliedStdDev(type, strike, atm, premium,
132 1.0, shift) /
133 std::sqrt(exerciseTime());
134 } catch(...) {
136 type, strike, atm, premium, premiumAtm, 1.0, shift) /
137 std::sqrt(exerciseTime());
138 }
139 } else {
140 return bachelierBlackFormulaImpliedVol(type, strike, atm,
141 exerciseTime(), premium);
142 }
143 }
144}
Black formula.
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
virtual Real digitalOptionPrice(Rate strike, Option::Type type=Option::Call, Real discount=1.0, Real gap=1.0e-5) const
virtual const Date & referenceDate() const
void update() override
virtual Time exerciseTime() const
Volatility volatility(Rate strike) const
virtual Real vega(Rate strike, Real discount=1.0) const
virtual VolatilityType volatilityType() const
virtual Real atmLevel() const =0
virtual Real density(Rate strike, Real discount=1.0, Real gap=1.0E-4) const
virtual void initializeExerciseTime() const
virtual Rate shift() const
virtual Real optionPrice(Rate strike, Option::Type type=Option::Call, Real discount=1.0) const
VolatilityType volatilityType_
floating-point comparisons
#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
Date d
LinearInterpolation variance
#define QL_MAX_REAL
Definition: qldefines.hpp:176
#define QL_EPSILON
Definition: qldefines.hpp:178
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
Real blackFormulaImpliedStdDevChambers(Option::Type optionType, Real strike, Real forward, Real blackPrice, Real blackAtmPrice, Real discount, Real displacement)
Real bachelierBlackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount)
Real blackFormulaImpliedStdDev(Option::Type optionType, Real strike, Real forward, Real blackPrice, Real discount, Real displacement, Real guess, Real accuracy, Natural maxIterations)
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
Real blackFormulaVolDerivative(Rate strike, Rate forward, Real stdDev, Real expiry, Real discount, Real displacement)
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)
Real bachelierBlackFormulaImpliedVol(Option::Type optionType, Real strike, Real forward, Real tte, Real bachelierPrice, Real discount)
STL namespace.
global repository for run-time library settings
Smile section base class.