QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdblackscholesasianengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Ralph Schreyer
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/math/distributions/normaldistribution.hpp>
22#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
23#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
24#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25#include <ql/methods/finitedifferences/solvers/fdmsimple2dbssolver.hpp>
26#include <ql/methods/finitedifferences/stepconditions/fdmarithmeticaveragecondition.hpp>
27#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
28#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
29#include <ql/pricingengines/asian/fdblackscholesasianengine.hpp>
30#include <ql/processes/blackscholesprocess.hpp>
31#include <utility>
32
33namespace QuantLib {
34
35
37 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
38 Size tGrid,
39 Size xGrid,
40 Size aGrid,
41 const FdmSchemeDesc& schemeDesc)
42 : process_(std::move(process)), tGrid_(tGrid), xGrid_(xGrid), aGrid_(aGrid),
43 schemeDesc_(schemeDesc) {}
44
45
47
48 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
49 "European exercise supported only");
51 "Arithmetic averaging supported only");
52 QL_REQUIRE( arguments_.runningAccumulator == 0
54 "Running average requires at least one past fixing");
55
56 // 1. Mesher
57 const ext::shared_ptr<StrikedTypePayoff> payoff =
58 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
59 const Time maturity = process_->time(arguments_.exercise->lastDate());
60 const ext::shared_ptr<Fdm1dMesher> equityMesher(
62 payoff->strike()));
63
64 const Real spot = process_->x0();
65 QL_REQUIRE(spot > 0.0, "negative or null underlying given");
66
67 const Real avg = (arguments_.runningAccumulator == 0)
69
70 const Real normInvEps = InverseCumulativeNormal()(1-0.0001);
71 const Real sigmaSqrtT
72 = process_->blackVolatility()->blackVol(maturity, payoff->strike())
73 *std::sqrt(maturity);
74 const Real r = sigmaSqrtT*normInvEps;
75
76 Real xMin = std::min(std::log(avg) - 0.25*r, std::log(spot) - 1.5*r);
77 Real xMax = std::max(std::log(avg) + 0.25*r, std::log(spot) + 1.5*r);
78
79 const ext::shared_ptr<Fdm1dMesher> averageMesher(
81 payoff->strike(), xMin, xMax));
82
83 const ext::shared_ptr<FdmMesher> mesher (
84 new FdmMesherComposite(equityMesher, averageMesher));
85
86 // 2. Calculator
87 ext::shared_ptr<FdmInnerValueCalculator> calculator(
88 new FdmLogInnerValue(payoff, mesher, 1));
89
90 // 3. Step conditions
91 std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
92 std::list<std::vector<Time> > stoppingTimes;
93
94 // 3.1 Arithmetic average step conditions
95 std::vector<Time> averageTimes;
96 for (auto& fixingDate : arguments_.fixingDates) {
97 Time t = process_->time(fixingDate);
98 QL_REQUIRE(t >= 0, "Fixing dates must not contain past date");
99 averageTimes.push_back(t);
100 }
101 stoppingTimes.emplace_back(averageTimes);
102 stepConditions.push_back(ext::shared_ptr<StepCondition<Array> >(
104 averageTimes, arguments_.runningAccumulator,
105 arguments_.pastFixings, mesher, 0)));
106
107 ext::shared_ptr<FdmStepConditionComposite> conditions(
108 new FdmStepConditionComposite(stoppingTimes, stepConditions));
109
110 // 4. Boundary conditions
111 const FdmBoundaryConditionSet boundaries;
112
113 // 5. Solver
114 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
115 calculator, maturity, tGrid_, 0 };
116 ext::shared_ptr<FdmSimple2dBSSolver> solver(
119 payoff->strike(), solverDesc, schemeDesc_));
120
121 results_.value = solver->valueAt(spot, avg);
122 results_.delta = solver->deltaAt(spot, avg, spot*0.01);
123 results_.gamma = solver->gammaAt(spot, avg, spot*0.01);
124 }
125}
FdBlackScholesAsianEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >, Size tGrid=100, Size xGrid=100, Size aGrid=50, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Douglas())
const ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Shared handle to an observable.
Definition: handle.hpp:41
Inverse cumulative normal distribution function.
condition to be applied at every time step
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.