QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdsimplebsswingengine.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
24#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
25#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
26#include <ql/methods/finitedifferences/meshers/uniform1dmesher.hpp>
27#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
28#include <ql/methods/finitedifferences/solvers/fdmsimple2dbssolver.hpp>
29#include <ql/methods/finitedifferences/stepconditions/fdmsimpleswingcondition.hpp>
30#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
31#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
32#include <ql/pricingengines/vanilla/fdsimplebsswingengine.hpp>
33#include <ql/processes/blackscholesprocess.hpp>
34#include <utility>
35
36namespace QuantLib {
37
39 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
40 Size tGrid,
41 Size xGrid,
42 const FdmSchemeDesc& schemeDesc)
43 : process_(std::move(process)), tGrid_(tGrid), xGrid_(xGrid), schemeDesc_(schemeDesc) {}
44
46 QL_REQUIRE(arguments_.exercise->type() == Exercise::Bermudan,
47 "Bermudan exercise supported only");
48
49 // 1. Mesher
50 const ext::shared_ptr<StrikedTypePayoff> payoff =
51 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
52 QL_REQUIRE(payoff, "Strike type payoff expected");
53
54 const Time maturity = process_->time(arguments_.exercise->lastDate());
55 const ext::shared_ptr<Fdm1dMesher> equityMesher(
57 maturity, payoff->strike()));
58
59 const ext::shared_ptr<Fdm1dMesher> exerciseMesher(
61 0, static_cast<Real>(arguments_.maxExerciseRights),
63
64 const ext::shared_ptr<FdmMesher> mesher (
65 new FdmMesherComposite(equityMesher, exerciseMesher));
66
67 // 2. Calculator
68 ext::shared_ptr<FdmInnerValueCalculator> calculator(
69 new FdmZeroInnerValue());
70
71 // 3. Step conditions
72 std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
73 std::list<std::vector<Time> > stoppingTimes;
74
75 // 3.1 Bermudan step conditions
76 std::vector<Time> exerciseTimes;
77 for (auto i : arguments_.exercise->dates()) {
78 Time t = process_->time(i);
79 QL_REQUIRE(t >= 0, "exercise dates must not contain past date");
80 exerciseTimes.push_back(t);
81 }
82 stoppingTimes.push_back(exerciseTimes);
83
84 ext::shared_ptr<FdmInnerValueCalculator> exerciseCalculator(
85 new FdmLogInnerValue(payoff, mesher, 0));
86
87 stepConditions.push_back(ext::shared_ptr<StepCondition<Array> >(
89 exerciseTimes, mesher, exerciseCalculator,
91
92 ext::shared_ptr<FdmStepConditionComposite> conditions(
93 new FdmStepConditionComposite(stoppingTimes, stepConditions));
94
95 // 4. Boundary conditions
96 const FdmBoundaryConditionSet boundaries;
97
98 // 5. Solver
99 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
100 calculator, maturity, tGrid_, 0 };
101 ext::shared_ptr<FdmSimple2dBSSolver> solver(
104 payoff->strike(), solverDesc, schemeDesc_));
105
106 const Real spot = process_->x0();
107
108 results_.value = solver->valueAt(spot, 1);
109 results_.delta = solver->deltaAt(spot, 1, spot*0.01);
110 results_.gamma = solver->gammaAt(spot, 1, spot*0.01);
111 results_.theta = solver->thetaAt(spot, 1);
112 }
113}
FdSimpleBSSwingEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > p, Size tGrid=50, Size xGrid=100, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Douglas())
const ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Shared handle to an observable.
Definition: handle.hpp:41
condition to be applied at every time step
ext::shared_ptr< SwingExercise > exercise
ext::shared_ptr< StrikedTypePayoff > payoff
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
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.