| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl |
| 5 | Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl |
| 6 | Copyright (C) 2003 Ferdinando Ametrano |
| 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 path.hpp |
| 23 | \brief single factor random walk |
| 24 | */ |
| 25 | |
| 26 | #ifndef quantlib_montecarlo_path_hpp |
| 27 | #define quantlib_montecarlo_path_hpp |
| 28 | |
| 29 | #include <ql/math/array.hpp> |
| 30 | #include <ql/timegrid.hpp> |
| 31 | #include <utility> |
| 32 | |
| 33 | namespace QuantLib { |
| 34 | |
| 35 | //! single-factor random walk |
| 36 | /*! \ingroup mcarlo |
| 37 | |
| 38 | \note the path includes the initial asset value as its first point. |
| 39 | */ |
| 40 | class Path { |
| 41 | public: |
| 42 | Path(TimeGrid timeGrid, Array values = Array()); |
| 43 | //! \name inspectors |
| 44 | //@{ |
| 45 | bool empty() const; |
| 46 | Size length() const; |
| 47 | //! asset value at the \f$ i \f$-th point |
| 48 | Real operator[](Size i) const; |
| 49 | Real at(Size i) const; |
| 50 | Real& operator[](Size i); |
| 51 | Real& at(Size i); |
| 52 | Real value(Size i) const; |
| 53 | Real& value(Size i); |
| 54 | //! time at the \f$ i \f$-th point |
| 55 | Time time(Size i) const; |
| 56 | //! initial asset value |
| 57 | Real front() const; |
| 58 | Real& front(); |
| 59 | //! final asset value |
| 60 | Real back() const; |
| 61 | Real& back(); |
| 62 | //! time grid |
| 63 | const TimeGrid& timeGrid() const; |
| 64 | //@} |
| 65 | //! \name iterators |
| 66 | //@{ |
| 67 | typedef Array::const_iterator iterator; |
| 68 | typedef Array::const_reverse_iterator reverse_iterator; |
| 69 | iterator begin() const; |
| 70 | iterator end() const; |
| 71 | reverse_iterator rbegin() const; |
| 72 | reverse_iterator rend() const; |
| 73 | //@} |
| 74 | private: |
| 75 | TimeGrid timeGrid_; |
| 76 | Array values_; |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | // inline definitions |
| 81 | |
| 82 | inline Path::Path(TimeGrid timeGrid, Array values) |
| 83 | : timeGrid_(std::move(timeGrid)), values_(std::move(values)) { |
| 84 | if (values_.empty()) |
| 85 | values_ = Array(timeGrid_.size()); |
| 86 | QL_REQUIRE(values_.size() == timeGrid_.size(), |
| 87 | "different number of times and asset values" ); |
| 88 | } |
| 89 | |
| 90 | inline bool Path::empty() const { |
| 91 | return timeGrid_.empty(); |
| 92 | } |
| 93 | |
| 94 | inline Size Path::length() const { |
| 95 | return timeGrid_.size(); |
| 96 | } |
| 97 | |
| 98 | inline Real Path::operator[](Size i) const { |
| 99 | return values_[i]; |
| 100 | } |
| 101 | |
| 102 | inline Real Path::at(Size i) const { |
| 103 | return values_.at(i); |
| 104 | } |
| 105 | |
| 106 | inline Real& Path::operator[](Size i) { |
| 107 | return values_[i]; |
| 108 | } |
| 109 | |
| 110 | inline Real& Path::at(Size i) { |
| 111 | return values_.at(i); |
| 112 | } |
| 113 | |
| 114 | inline Real Path::value(Size i) const { |
| 115 | return values_[i]; |
| 116 | } |
| 117 | |
| 118 | inline Real& Path::value(Size i) { |
| 119 | return values_[i]; |
| 120 | } |
| 121 | |
| 122 | inline Real Path::front() const { |
| 123 | return values_[0]; |
| 124 | } |
| 125 | |
| 126 | inline Real& Path::front() { |
| 127 | return values_[0]; |
| 128 | } |
| 129 | |
| 130 | inline Real Path::back() const { |
| 131 | return values_[values_.size()-1]; |
| 132 | } |
| 133 | |
| 134 | inline Real& Path::back() { |
| 135 | return values_[values_.size()-1]; |
| 136 | } |
| 137 | |
| 138 | inline Time Path::time(Size i) const { |
| 139 | return timeGrid_[i]; |
| 140 | } |
| 141 | |
| 142 | inline const TimeGrid& Path::timeGrid() const { |
| 143 | return timeGrid_; |
| 144 | } |
| 145 | |
| 146 | inline Path::iterator Path::begin() const { |
| 147 | return values_.begin(); |
| 148 | } |
| 149 | |
| 150 | inline Path::iterator Path::end() const { |
| 151 | return values_.end(); |
| 152 | } |
| 153 | |
| 154 | inline Path::reverse_iterator Path::rbegin() const { |
| 155 | return values_.rbegin(); |
| 156 | } |
| 157 | |
| 158 | inline Path::reverse_iterator Path::rend() const { |
| 159 | return values_.rend(); |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | |
| 164 | |
| 165 | #endif |
| 166 | |