Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/experimental/mcbasket/adaptedpathpayoff.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2009 Andrea Odetti
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/experimental/mcbasket/adaptedpathpayoff.hpp>
21
22
namespace QuantLib {
23
24
  /*
25
    Initializing maximumTimeRead_ to -1 would make more sense,
26
    but it is unsigned and 0 has exactly the same behaviour.
27
   */
28
  AdaptedPathPayoff::ValuationData::ValuationData(
29
      const Matrix& path,
30
      const std::vector<Handle<YieldTermStructure> >& forwardTermStructures,
31
      Array& payments,
32
      Array& exercises,
33
      std::vector<Array>& states)
34
0
  : path_(path), forwardTermStructures_(forwardTermStructures), payments_(payments),
35
0
    exercises_(exercises), states_(states)
36
37
0
  {}
38
39
0
  Size AdaptedPathPayoff::ValuationData::numberOfTimes() const {
40
0
    return path_.columns();
41
0
  }
42
43
0
  Size AdaptedPathPayoff::ValuationData::numberOfAssets() const {
44
0
    return path_.rows();
45
0
  }
46
47
0
  Real AdaptedPathPayoff::ValuationData::getAssetValue(Size time, Size asset) {
48
0
    maximumTimeRead_ = std::max(maximumTimeRead_, time);
49
50
0
    return path_[asset][time];
51
0
  }
52
53
0
  const Handle<YieldTermStructure> & AdaptedPathPayoff::ValuationData::getYieldTermStructure(Size time) {
54
0
    maximumTimeRead_ = std::max(maximumTimeRead_, time);
55
56
0
    return forwardTermStructures_[time];
57
0
  }
58
59
0
  void AdaptedPathPayoff::ValuationData::setPayoffValue(Size time, Real value) {
60
    /*
61
      This is to ensure the payoff is an adapted function.
62
      We prevent payments to depend on future fixings.
63
     */
64
0
    QL_REQUIRE(time >= maximumTimeRead_,
65
0
               "not adapted payoff: looking into the future");
66
67
0
    payments_[time] = value;
68
0
  }
69
70
  void AdaptedPathPayoff::ValuationData::setExerciseData(
71
0
                                     Size time, Real exercise, Array & state) {
72
    /*
73
      This is to ensure the payoff is an adapted function.
74
      We prevent payments to depend on future fixings.
75
     */
76
0
    QL_REQUIRE(time >= maximumTimeRead_,
77
0
               "not adapted payoff: looking into the future");
78
79
0
    if (!exercises_.empty())
80
0
      exercises_[time] = exercise;
81
82
0
    if (!states_.empty())
83
0
      std::swap(states_[time], state);
84
0
  }
85
86
87
  void AdaptedPathPayoff::value(const Matrix       & path,
88
                                const std::vector<Handle<YieldTermStructure> > & forwardTermStructures,
89
                                Array              & payments,
90
                                Array              & exercises,
91
0
                                std::vector<Array> & states) const {
92
0
    ValuationData data(path, forwardTermStructures, payments, exercises, states);
93
94
0
    operator()(data);
95
0
  }
96
}