QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdblackscholesasianengine.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) 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>
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");
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}
Black-Scholes processes.
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
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
Finite-Differences Black Scholes arithmentic asian option engine.
step condition to handle arithmetic average
1-d mesher for the Black-Scholes process (in ln(S))
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
composite of fdm step conditions
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
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.
normal, cumulative and inverse cumulative distributions
ext::shared_ptr< YieldTermStructure > r