Coverage Report

Created: 2026-10-31 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/methods/finitedifferences/stepconditions/fdmarithmeticaveragecondition.cpp
Line
Count
Source
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
 <https://www.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
/*! \file fdmarithmeticaveragecondition.cpp
21
    \brief step condition to handle arithmetic average
22
*/
23
24
#include <ql/math/interpolations/cubicinterpolation.hpp>
25
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
26
#include <ql/methods/finitedifferences/stepconditions/fdmarithmeticaveragecondition.hpp>
27
#include <utility>
28
29
namespace QuantLib {
30
31
    FdmArithmeticAverageCondition::FdmArithmeticAverageCondition(
32
        std::vector<Time> averageTimes,
33
        Real,
34
        Size pastFixings,
35
        const ext::shared_ptr<FdmMesher>& mesher,
36
        Size equityDirection)
37
0
    : x_(mesher->layout()->dim()[equityDirection]),
38
0
      a_(mesher->layout()->dim()[equityDirection == 0 ? 1 : 0]),
39
0
      averageTimes_(std::move(averageTimes)), pastFixings_(pastFixings), mesher_(mesher),
40
0
      equityDirection_(equityDirection) {
41
42
0
        QL_REQUIRE(mesher->layout()->dim().size()==2, "2D allowed only");
43
0
        QL_REQUIRE(equityDirection == 0 || equityDirection == 1,
44
0
                   "equityDirection has to be 0 or 1");
45
46
0
        const Size xSpacing = mesher_->layout()->spacing()[equityDirection];
47
0
        Array tmp = mesher_->locations(equityDirection);
48
0
        for (Size i = 0; i < x_.size(); ++i) {
49
0
            x_[i] = std::exp(tmp[i*xSpacing]);
50
0
        }
51
0
        const Size averageDirection = equityDirection == 0 ? 1 : 0;
52
0
        const Size aSpacing = mesher_->layout()->spacing()[averageDirection];
53
0
        tmp = mesher_->locations(averageDirection);
54
0
        for (Size i = 0; i < a_.size(); ++i) {
55
0
            a_[i] = std::exp(tmp[i*aSpacing]);
56
0
        }
57
0
    }
58
59
0
    void FdmArithmeticAverageCondition::applyTo(Array& a, Time t) const {
60
0
        QL_REQUIRE(mesher_->layout()->size() == a.size(),
61
0
                   "inconsistent array dimensions");
62
63
0
        const auto iter
64
0
            = std::find(averageTimes_.begin(), averageTimes_.end(), t);
65
0
        const Size nTimes
66
0
            = std::count(averageTimes_.begin(), averageTimes_.end(), t);
67
0
        if (nTimes > 0) {
68
0
            Array aCopy(a);
69
0
            const Size iT = iter - averageTimes_.begin() + 1 + pastFixings_;
70
0
            const Size averageDirection = equityDirection_ == 0 ? 1 : 0;
71
0
            const Size xSpacing = mesher_->layout()->spacing()[equityDirection_];
72
0
            const Size aSpacing = mesher_->layout()->spacing()[averageDirection];
73
0
            Array tmp(a_.size());
74
75
0
            for (Size i=0; i<x_.size(); ++i) {
76
0
                for (Size j=0; j<a_.size(); ++j) {
77
0
                    Size index = i*xSpacing + j*aSpacing;
78
0
                    tmp[j] = aCopy[index];
79
0
                }
80
0
                MonotonicCubicNaturalSpline interp(a_.begin(), a_.end(),
81
0
                                           tmp.begin());
82
0
                for (Size j=0; j<a_.size(); ++j) {
83
0
                    Size index = i*xSpacing + j*aSpacing;
84
0
                    a[index] = interp((iT-nTimes)/(double)(iT)*a_[j] +
85
0
                                      nTimes/(double)(iT)*x_[i], true);
86
0
                }
87
0
            }
88
0
        }
89
0
    }
90
}