QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fd2dblackscholesvanillaengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2010 Klaus Spanderen
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
20#include <ql/exercise.hpp>
21#include <ql/methods/finitedifferences/solvers/fdm2dblackscholessolver.hpp>
22#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
23#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
24#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
25#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
26#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
27#include <ql/pricingengines/basket/fd2dblackscholesvanillaengine.hpp>
28
29namespace QuantLib {
30
32 const ext::shared_ptr<GeneralizedBlackScholesProcess>& p1,
33 const ext::shared_ptr<GeneralizedBlackScholesProcess>& p2,
34 Real correlation,
35 Size xGrid, Size yGrid,
36 Size tGrid, Size dampingSteps,
37 const FdmSchemeDesc& schemeDesc,
38 bool localVol,
39 Real illegalLocalVolOverwrite)
40 : p1_(p1),
41 p2_(p2),
42 correlation_(correlation),
43 xGrid_(xGrid), yGrid_(yGrid), tGrid_(tGrid),
44 dampingSteps_(dampingSteps),
45 schemeDesc_(schemeDesc),
46 localVol_(localVol),
47 illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
48 registerWith(p1);
49 registerWith(p2);
50 }
51
53 // 1. Payoff
54 const ext::shared_ptr<BasketPayoff> payoff =
55 ext::dynamic_pointer_cast<BasketPayoff>(arguments_.payoff);
56
57 // 2. Mesher
58 const Time maturity = p1_->time(arguments_.exercise->lastDate());
59 const ext::shared_ptr<Fdm1dMesher> em1(
61 xGrid_, p1_, maturity, p1_->x0(),
62 Null<Real>(), Null<Real>(), 0.0001, 1.5,
63 std::pair<Real, Real>(p1_->x0(), 0.1)));
64
65 const ext::shared_ptr<Fdm1dMesher> em2(
67 yGrid_, p2_, maturity, p2_->x0(),
68 Null<Real>(), Null<Real>(), 0.0001, 1.5,
69 std::pair<Real, Real>(p2_->x0(), 0.1)));
70
71 const ext::shared_ptr<FdmMesher> mesher (
72 new FdmMesherComposite(em1, em2));
73
74 // 3. Calculator
75 const ext::shared_ptr<FdmInnerValueCalculator> calculator(
76 new FdmLogBasketInnerValue(payoff, mesher));
77
78 // 4. Step conditions
79 const ext::shared_ptr<FdmStepConditionComposite> conditions =
82 mesher, calculator,
83 p1_->riskFreeRate()->referenceDate(),
84 p1_->riskFreeRate()->dayCounter());
85
86 // 5. Boundary conditions
87 const FdmBoundaryConditionSet boundaries;
88
89 // 6. Solver
90 const FdmSolverDesc solverDesc = { mesher, boundaries,
91 conditions, calculator,
92 maturity, tGrid_, dampingSteps_ };
93
94 ext::shared_ptr<Fdm2dBlackScholesSolver> solver(
98 correlation_, solverDesc, schemeDesc_,
100
101 const Real x = p1_->x0();
102 const Real y = p2_->x0();
103
104 results_.value = solver->valueAt(x, y);
105 results_.delta = solver->deltaXat(x, y) + solver->deltaYat(x, y);
106 results_.gamma = solver->gammaXat(x, y) + solver->gammaYat(x, y)
107 + 2*solver->gammaXYat(x, y);
108 results_.theta = solver->thetaAt(x, y);
109 }
110}
const ext::shared_ptr< GeneralizedBlackScholesProcess > p1_
const ext::shared_ptr< GeneralizedBlackScholesProcess > p2_
Fd2dBlackScholesVanillaEngine(const ext::shared_ptr< GeneralizedBlackScholesProcess > &p1, const ext::shared_ptr< GeneralizedBlackScholesProcess > &p2, Real correlation, Size xGrid=100, Size yGrid=100, Size tGrid=50, Size dampingSteps=0, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Hundsdorfer(), bool localVol=false, Real illegalLocalVolOverwrite=-Null< Real >())
static ext::shared_ptr< FdmStepConditionComposite > vanillaComposite(const DividendSchedule &schedule, const ext::shared_ptr< Exercise > &exercise, const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< FdmInnerValueCalculator > &calculator, const Date &refDate, const DayCounter &dayCounter)
Shared handle to an observable.
Definition: handle.hpp:41
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
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
ext::shared_ptr< Payoff > payoff
Definition: option.hpp:64
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet