Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/methods/finitedifferences/utilities/fdminnervaluecalculator.cpp
Line
Count
Source
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
 <https://www.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
26
#include <ql/instruments/basketoption.hpp>
27
#include <ql/math/integrals/simpsonintegral.hpp>
28
#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
29
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
30
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
31
#include <ql/payoff.hpp>
32
#include <deque>
33
#include <utility>
34
35
namespace QuantLib {
36
37
    namespace {
38
        struct mapped_payoff {
39
            explicit mapped_payoff(
40
                const Payoff& payoff,
41
                const std::function<Real(Real)>& gridMapping)
42
0
            : payoff(payoff), gridMapping_(gridMapping) {}
43
44
0
            Real operator()(Real x) const { return payoff(gridMapping_(x)); }
45
46
            const Payoff& payoff;
47
            const std::function<Real(Real)>& gridMapping_;
48
        };
49
    }
50
51
    FdmCellAveragingInnerValue::FdmCellAveragingInnerValue(ext::shared_ptr<Payoff> payoff,
52
                                                           ext::shared_ptr<FdmMesher> mesher,
53
                                                           Size direction,
54
                                                           std::function<Real(Real)> gridMapping)
55
0
    : payoff_(std::move(payoff)), mesher_(std::move(mesher)), direction_(direction),
56
0
      gridMapping_(std::move(gridMapping)) {}
57
58
0
    Real FdmCellAveragingInnerValue::innerValue(const FdmLinearOpIterator& iter, Time) {
59
0
        const Real loc = mesher_->location(iter, direction_);
60
0
        return (*payoff_)(gridMapping_(loc));
61
0
    }
62
63
0
    Real FdmCellAveragingInnerValue::avgInnerValue(const FdmLinearOpIterator& iter, Time t) {
64
0
        if (avgInnerValues_.empty()) {
65
            // calculate caching values
66
0
            avgInnerValues_.resize(mesher_->layout()->dim()[direction_]);
67
0
            std::deque<bool> initialized(avgInnerValues_.size(), false);
68
69
0
            for (const auto& i : *mesher_->layout()) {
70
0
                const Size xn = i.coordinates()[direction_];
71
0
                if (!initialized[xn]) {
72
0
                    initialized[xn]     = true;
73
0
                    avgInnerValues_[xn] = avgInnerValueCalc(i, t);
74
0
                }
75
0
            }
76
0
        }
77
78
0
        return avgInnerValues_[iter.coordinates()[direction_]];
79
0
    }
80
81
0
    Real FdmCellAveragingInnerValue::avgInnerValueCalc(const FdmLinearOpIterator& iter, Time t) {
82
0
        const Size dim = mesher_->layout()->dim()[direction_];
83
0
        const Size coord = iter.coordinates()[direction_];
84
85
0
        if (coord == 0 || coord == dim-1)
86
0
            return innerValue(iter, t);
87
88
0
        const Real loc = mesher_->location(iter,direction_);
89
0
        const Real a = loc - mesher_->dminus(iter, direction_)/2.0;
90
0
        const Real b = loc + mesher_->dplus(iter, direction_)/2.0;
91
92
0
        mapped_payoff f(*payoff_, gridMapping_);
93
94
0
        Real retVal;
95
0
        try {
96
0
            const Real acc
97
0
                = ((f(a) != 0.0 || f(b) != 0.0) ? Real((f(a)+f(b))*5e-5) : 1e-4);
98
0
            retVal = SimpsonIntegral(acc, 8)(f, a, b)/(b-a);
99
0
        }
100
0
        catch (Error&) {
101
            // use default value
102
0
            retVal = innerValue(iter, t);
103
0
        }
104
105
0
        return retVal;
106
0
    }
107
108
    FdmLogInnerValue::FdmLogInnerValue(
109
        const ext::shared_ptr<Payoff>& payoff,
110
        const ext::shared_ptr<FdmMesher>& mesher,
111
        Size direction)
112
0
    : FdmCellAveragingInnerValue(
113
0
        payoff, mesher, direction,
114
0
        [](Real x) -> Real { return std::exp(x); }) {}
115
116
117
    FdmLogBasketInnerValue::FdmLogBasketInnerValue(ext::shared_ptr<BasketPayoff> payoff,
118
                                                   ext::shared_ptr<FdmMesher> mesher)
119
0
    : payoff_(std::move(payoff)), mesher_(std::move(mesher)) {}
120
121
    Real FdmLogBasketInnerValue::innerValue(
122
0
                                    const FdmLinearOpIterator& iter, Time) {
123
0
        Array x(mesher_->layout()->dim().size());
124
0
        for (Size i=0; i < x.size(); ++i) {
125
0
            x[i] = std::exp(mesher_->location(iter, i));
126
0
        }
127
        
128
0
        return (*payoff_)(x);
129
0
    }
130
    
131
    Real 
132
    FdmLogBasketInnerValue::avgInnerValue(
133
0
                                    const FdmLinearOpIterator& iter, Time t) {
134
0
        return innerValue(iter, t);
135
0
    }
136
}