QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
impliedvolatility.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) 2007, 2009 StatPro Italia srl
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
23
24namespace QuantLib {
25
26 namespace {
27
28 class PriceError {
29 public:
30 PriceError(const PricingEngine& engine,
31 SimpleQuote& vol,
32 Real targetValue);
33 Real operator()(Volatility x) const;
34 private:
35 const PricingEngine& engine_;
36 SimpleQuote& vol_;
38 const Instrument::results* results_;
39 };
40
41 PriceError::PriceError(const PricingEngine& engine,
42 SimpleQuote& vol,
43 Real targetValue)
44 : engine_(engine), vol_(vol), targetValue_(targetValue) {
45 results_ =
46 dynamic_cast<const Instrument::results*>(engine_.getResults());
47 QL_REQUIRE(results_ != nullptr, "pricing engine does not supply needed results");
48 }
49
50 Real PriceError::operator()(Volatility x) const {
51 vol_.setValue(x);
52 engine_.calculate();
53 return results_->value-targetValue_;
54 }
55
56 }
57
58
59 namespace detail {
60
61 Volatility ImpliedVolatilityHelper::calculate(
62 const Instrument& instrument,
63 const PricingEngine& engine,
64 SimpleQuote& volQuote,
65 Real targetValue,
66 Real accuracy,
67 Natural maxEvaluations,
68 Volatility minVol,
69 Volatility maxVol) {
70
71 instrument.setupArguments(engine.getArguments());
72 engine.getArguments()->validate();
73
74 PriceError f(engine, volQuote, targetValue);
75 Brent solver;
76 solver.setMaxEvaluations(maxEvaluations);
77 Volatility guess = (minVol+maxVol)/2.0;
78 Volatility result = solver.solve(f, accuracy, guess,
79 minVol, maxVol);
80 return result;
81 }
82
83 ext::shared_ptr<GeneralizedBlackScholesProcess>
84 ImpliedVolatilityHelper::clone(
85 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
86 const ext::shared_ptr<SimpleQuote>& volQuote) {
87
88 Handle<Quote> stateVariable = process->stateVariable();
89 Handle<YieldTermStructure> dividendYield = process->dividendYield();
90 Handle<YieldTermStructure> riskFreeRate = process->riskFreeRate();
91
92 Handle<BlackVolTermStructure> blackVol = process->blackVolatility();
94 ext::shared_ptr<BlackVolTermStructure>(
95 new BlackConstantVol(blackVol->referenceDate(),
96 blackVol->calendar(),
97 Handle<Quote>(volQuote),
98 blackVol->dayCounter())));
99
100 return ext::make_shared<GeneralizedBlackScholesProcess>(
101 stateVariable, dividendYield,
102 riskFreeRate, volatility);
103 }
104
105 }
106
107}
Black constant volatility, no time dependence, no strike dependence.
Brent 1-D solver.
Real targetValue_
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
Constant Black volatility, no time-strike dependence.
Brent 1-D solver
Definition: brent.hpp:37
Shared handle to an observable.
Definition: handle.hpp:41
Abstract instrument class.
Definition: instrument.hpp:44
virtual void setupArguments(PricingEngine::arguments *) const
Definition: instrument.cpp:45
virtual void validate() const =0
interface for pricing engines
virtual arguments * getArguments() const =0
market element returning a stored value
Definition: simplequote.hpp:33
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
const PricingEngine & engine_
SimpleQuote & vol_
Real targetValue_
const Instrument::results * results_
Utilities for implied-volatility calculation.
Definition: any.hpp:35