QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdminnervaluecalculator.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) 2008 Andreas Gaida
5 Copyright (C) 2008, 2009 Ralph Schreyer
6 Copyright (C) 2008, 2009 Klaus Spanderen
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file fdminnervaluecalculator.cpp
23 \brief layer of abstraction to calculate the inner value
24*/
25
31#include <ql/payoff.hpp>
32#include <deque>
33#include <utility>
34
35namespace QuantLib {
36
37 namespace {
38 struct mapped_payoff {
39 explicit mapped_payoff(
40 const Payoff& payoff,
41 const ext::function<Real(Real)>& gridMapping)
42 : payoff(payoff), gridMapping_(gridMapping) {}
43
44 Real operator()(Real x) const { return payoff(gridMapping_(x)); }
45
46 const Payoff& payoff;
47 const ext::function<Real(Real)>& gridMapping_;
48 };
49 }
50
52 ext::shared_ptr<FdmMesher> mesher,
53 Size direction,
54 ext::function<Real(Real)> gridMapping)
55 : payoff_(std::move(payoff)), mesher_(std::move(mesher)), direction_(direction),
56 gridMapping_(std::move(gridMapping)) {}
57
59 const Real loc = mesher_->location(iter, direction_);
60 return (*payoff_)(gridMapping_(loc));
61 }
62
64 if (avgInnerValues_.empty()) {
65 // calculate caching values
66 avgInnerValues_.resize(mesher_->layout()->dim()[direction_]);
67 std::deque<bool> initialized(avgInnerValues_.size(), false);
68
69 for (const auto& i : *mesher_->layout()) {
70 const Size xn = i.coordinates()[direction_];
71 if (!initialized[xn]) {
72 initialized[xn] = true;
74 }
75 }
76 }
77
79 }
80
82 const Size dim = mesher_->layout()->dim()[direction_];
83 const Size coord = iter.coordinates()[direction_];
84
85 if (coord == 0 || coord == dim-1)
86 return innerValue(iter, t);
87
88 const Real loc = mesher_->location(iter,direction_);
89 const Real a = loc - mesher_->dminus(iter, direction_)/2.0;
90 const Real b = loc + mesher_->dplus(iter, direction_)/2.0;
91
92 mapped_payoff f(*payoff_, gridMapping_);
93
94 Real retVal;
95 try {
96 const Real acc
97 = ((f(a) != 0.0 || f(b) != 0.0) ? Real((f(a)+f(b))*5e-5) : 1e-4);
98 retVal = SimpsonIntegral(acc, 8)(f, a, b)/(b-a);
99 }
100 catch (Error&) {
101 // use default value
102 retVal = innerValue(iter, t);
103 }
104
105 return retVal;
106 }
107
109 const ext::shared_ptr<Payoff>& payoff,
110 const ext::shared_ptr<FdmMesher>& mesher,
111 Size direction)
113 payoff, mesher, direction,
114 [](Real x) -> Real { return std::exp(x); }) {}
115
116
117 FdmLogBasketInnerValue::FdmLogBasketInnerValue(ext::shared_ptr<BasketPayoff> payoff,
118 ext::shared_ptr<FdmMesher> mesher)
119 : payoff_(std::move(payoff)), mesher_(std::move(mesher)) {}
120
122 const FdmLinearOpIterator& iter, Time) {
123 Array x(mesher_->layout()->dim().size());
124 for (Size i=0; i < x.size(); ++i) {
125 x[i] = std::exp(mesher_->location(iter, i));
126 }
127
128 return (*payoff_)(x);
129 }
130
131 Real
133 const FdmLinearOpIterator& iter, Time t) {
134 return innerValue(iter, t);
135 }
136}
Basket option on a number of assets.
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Base error class.
Definition: errors.hpp:39
Real avgInnerValue(const FdmLinearOpIterator &iter, Time t) override
const ext::shared_ptr< FdmMesher > mesher_
FdmCellAveragingInnerValue(ext::shared_ptr< Payoff > payoff, ext::shared_ptr< FdmMesher > mesher, Size direction, ext::function< Real(Real)> gridMapping=[](Real x){ return x;})
Real avgInnerValueCalc(const FdmLinearOpIterator &iter, Time t)
Real innerValue(const FdmLinearOpIterator &iter, Time) override
const ext::function< Real(Real)> gridMapping_
const ext::shared_ptr< Payoff > payoff_
const std::vector< Size > & coordinates() const
Real avgInnerValue(const FdmLinearOpIterator &iter, Time) override
const ext::shared_ptr< BasketPayoff > payoff_
const ext::shared_ptr< FdmMesher > mesher_
Real innerValue(const FdmLinearOpIterator &iter, Time) override
FdmLogInnerValue(const ext::shared_ptr< Payoff > &payoff, const ext::shared_ptr< FdmMesher > &mesher, Size direction)
Integral of a one-dimensional function.
const DefaultType & t
ext::function< Real(Real)> b
const ext::function< Real(Real)> & gridMapping_
const Payoff & payoff
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
mesher for a fdm grid
const ext::shared_ptr< FdmMesher > mesher_
const Size direction_
const ext::shared_ptr< Payoff > 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
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
STL namespace.
Option payoff classes.
integral of a one-dimensional function using Simpson formula