QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdvanillaengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2002, 2003, 2004 Ferdinando Ametrano
5 Copyright (C) 2002, 2003 RiskMap srl
6 Copyright (C) 2003, 2004, 2005, 2007 StatPro Italia srl
7 Copyright (C) 2005 Joseph Wang
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23#include <ql/pricingengines/vanilla/fdvanillaengine.hpp>
24#include <ql/instruments/payoffs.hpp>
25#include <ql/exercise.hpp>
26#include <ql/grid.hpp>
27#include <ql/instruments/oneassetoption.hpp>
28#include <ql/methods/finitedifferences/bsmoperator.hpp>
29#include <ql/methods/finitedifferences/bsmtermoperator.hpp>
30
31namespace QuantLib {
32
34
36 setGridLimits(process_->stateVariable()->value(),
39 }
40
42 const PricingEngine::arguments* a) const {
43 const auto* args = dynamic_cast<const OneAssetOption::arguments*>(a);
44 QL_REQUIRE(args, "incorrect argument type");
45 exerciseDate_ = args->exercise->lastDate();
46 payoff_ = args->payoff;
47 }
48
50 QL_REQUIRE(center > 0.0, "negative or null underlying given");
51 QL_REQUIRE(t > 0.0, "negative or zero residual time");
52 center_ = center;
53 Size newGridPoints = safeGridPoints(gridPoints_, t);
54 QL_DEPRECATED_DISABLE_WARNING
55 if (newGridPoints > intrinsicValues_.size()) {
56 intrinsicValues_ = SampledCurve(newGridPoints);
57 }
58 QL_DEPRECATED_ENABLE_WARNING
59
60 Real volSqrtTime = std::sqrt(process_->blackVolatility()
61 ->blackVariance(t, center_));
62
63 // the prefactor fine tunes performance at small volatilities
64 Real prefactor = 1.0 + 0.02/volSqrtTime;
65 Real minMaxFactor = std::exp(4.0 * prefactor * volSqrtTime);
66 sMin_ = center_/minMaxFactor; // underlying grid min value
67 sMax_ = center_*minMaxFactor; // underlying grid max value
68 }
69
71 // ensure strike is included in the grid
72 ext::shared_ptr<StrikedTypePayoff> striked_payoff =
73 ext::dynamic_pointer_cast<StrikedTypePayoff>(payoff_);
74 if (!striked_payoff)
75 return;
76 Real requiredGridValue = striked_payoff->strike();
77
78 if(sMin_ > requiredGridValue/safetyZoneFactor_){
79 sMin_ = requiredGridValue/safetyZoneFactor_;
80 // enforce central placement of the underlying
82 }
83 if(sMax_ < requiredGridValue*safetyZoneFactor_){
84 sMax_ = requiredGridValue*safetyZoneFactor_;
85 // enforce central placement of the underlying
87 }
88 }
89
91 QL_DEPRECATED_DISABLE_WARNING
94 QL_DEPRECATED_ENABLE_WARNING
95 }
96
98 QL_DEPRECATED_DISABLE_WARNING
99 if (timeDependent_) {
102 } else {
103 const YieldTermStructure& R = **(process_->riskFreeRate());
105 const YieldTermStructure& Q = **(process_->dividendYield());
107
108 ext::shared_ptr<StrikedTypePayoff> striked_payoff =
109 ext::dynamic_pointer_cast<StrikedTypePayoff>(payoff_);
110 Real K = striked_payoff != nullptr ? striked_payoff->strike() : process_->x0();
111 Volatility sigma =
112 process_->blackVolatility()->blackVol(exerciseDate_, K);
113
115 r, q, sigma);
116 }
117 QL_DEPRECATED_ENABLE_WARNING
118 }
119
121 QL_DEPRECATED_DISABLE_WARNING
122 BCs_[0] = ext::shared_ptr<bc_type>(new NeumannBC(
126 BCs_[1] = ext::shared_ptr<bc_type>(new NeumannBC(
130 QL_DEPRECATED_ENABLE_WARNING
131 }
132
134 return process_->time(exerciseDate_);
135 }
136
137 // safety check to be sure we have enough grid points.
139 Time residualTime) const {
140 static const Size minGridPoints = 10;
141 static const Size minGridPointsPerYear = 2;
142 return std::max(gridPoints,
143 residualTime > 1.0 ?
144 static_cast<Size>((minGridPoints +
145 (residualTime-1.0) *
146 minGridPointsPerYear))
147 : minGridPoints);
148 }
149
150}
Black-Scholes-Merton differential operator.
Definition: bsmoperator.hpp:35
virtual void initializeOperator() const
virtual void setGridLimits() const
TridiagonalOperator finiteDifferenceOperator_
virtual void initializeInitialCondition() const
QL_DEPRECATED_DISABLE_WARNING SampledCurve intrinsicValues_
std::vector< ext::shared_ptr< bc_type > > BCs_
ext::shared_ptr< Payoff > payoff_
virtual Time getResidualTime() const
virtual void setupArguments(const PricingEngine::arguments *) const
static const Real safetyZoneFactor_
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
virtual void initializeBoundaryConditions() const
Size safeGridPoints(Size gridPoints, Time residualTime) const
Neumann boundary condition (i.e., constant derivative)
basic option arguments
Definition: option.hpp:57
void sample(const F &f)
const Array & grid() const
QL_DEPRECATED_ENABLE_WARNING void setLogGrid(Real min, Real max)
Real value(Size i) const
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
Interest-rate term structure.
InterestRate zeroRate(const Date &d, const DayCounter &resultDayCounter, Compounding comp, Frequency freq=Annual, bool extrapolate=false) const
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
PdeOperator< PdeBSM > BSMTermOperator