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    */
ValuationData(const Matrix & path,const std::vector<Handle<YieldTermStructure>> & forwardTermStructures,Array & payments,Array & exercises,std::vector<Array> & states)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     path_(path),
35     forwardTermStructures_(forwardTermStructures),
36     payments_(payments), exercises_(exercises), states_(states),
37     maximumTimeRead_(0)
38   { }
39 
numberOfTimes() const40   Size AdaptedPathPayoff::ValuationData::numberOfTimes() const {
41     return path_.columns();
42   }
43 
numberOfAssets() const44   Size AdaptedPathPayoff::ValuationData::numberOfAssets() const {
45     return path_.rows();
46   }
47 
getAssetValue(Size time,Size asset)48   Real AdaptedPathPayoff::ValuationData::getAssetValue(Size time, Size asset) {
49     maximumTimeRead_ = std::max(maximumTimeRead_, time);
50 
51     return path_[asset][time];
52   }
53 
getYieldTermStructure(Size time)54   const Handle<YieldTermStructure> & AdaptedPathPayoff::ValuationData::getYieldTermStructure(Size time) {
55     maximumTimeRead_ = std::max(maximumTimeRead_, time);
56 
57     return forwardTermStructures_[time];
58   }
59 
setPayoffValue(Size time,Real value)60   void AdaptedPathPayoff::ValuationData::setPayoffValue(Size time, Real value) {
61     /*
62       This is to ensure the payoff is an adapted function.
63       We prevent payments to depend on future fixings.
64      */
65     QL_REQUIRE(time >= maximumTimeRead_,
66                "not adapted payoff: looking into the future");
67 
68     payments_[time] = value;
69   }
70 
setExerciseData(Size time,Real exercise,Array & state)71   void AdaptedPathPayoff::ValuationData::setExerciseData(
72                                      Size time, Real exercise, Array & state) {
73     /*
74       This is to ensure the payoff is an adapted function.
75       We prevent payments to depend on future fixings.
76      */
77     QL_REQUIRE(time >= maximumTimeRead_,
78                "not adapted payoff: looking into the future");
79 
80     if (!exercises_.empty())
81       exercises_[time] = exercise;
82 
83     if (!states_.empty())
84       std::swap(states_[time], state);
85   }
86 
87 
value(const Matrix & path,const std::vector<Handle<YieldTermStructure>> & forwardTermStructures,Array & payments,Array & exercises,std::vector<Array> & states) const88   void AdaptedPathPayoff::value(const Matrix       & path,
89                                 const std::vector<Handle<YieldTermStructure> > & forwardTermStructures,
90                                 Array              & payments,
91                                 Array              & exercises,
92                                 std::vector<Array> & states) const {
93     ValuationData data(path, forwardTermStructures, payments, exercises, states);
94 
95     operator()(data);
96   }
97 }
98