Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/methods/montecarlo/path.hpp
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) 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
0
    inline bool Path::empty() const {
91
0
        return timeGrid_.empty();
92
0
    }
93
94
0
    inline Size Path::length() const {
95
0
        return timeGrid_.size();
96
0
    }
97
98
0
    inline Real Path::operator[](Size i) const {
99
0
        return values_[i];
100
0
    }
101
102
0
    inline Real Path::at(Size i) const {
103
0
        return values_.at(i);
104
0
    }
105
106
0
    inline Real& Path::operator[](Size i) {
107
0
        return values_[i];
108
0
    }
109
110
0
    inline Real& Path::at(Size i) {
111
0
        return values_.at(i);
112
0
    }
113
114
0
    inline Real Path::value(Size i) const {
115
0
        return values_[i];
116
0
    }
117
118
0
    inline Real& Path::value(Size i) {
119
0
        return values_[i];
120
0
    }
121
122
0
    inline Real Path::front() const {
123
0
        return values_[0];
124
0
    }
125
126
0
    inline Real& Path::front() {
127
0
        return values_[0];
128
0
    }
129
130
0
    inline Real Path::back() const {
131
0
        return values_[values_.size()-1];
132
0
    }
133
134
0
    inline Real& Path::back() {
135
0
        return values_[values_.size()-1];
136
0
    }
137
138
0
    inline Time Path::time(Size i) const {
139
0
        return timeGrid_[i];
140
0
    }
141
142
0
    inline const TimeGrid& Path::timeGrid() const {
143
0
        return timeGrid_;
144
0
    }
145
146
0
    inline Path::iterator Path::begin() const {
147
0
        return values_.begin();
148
0
    }
149
150
0
    inline Path::iterator Path::end() const {
151
0
        return values_.end();
152
0
    }
153
154
0
    inline Path::reverse_iterator Path::rbegin() const {
155
0
        return values_.rbegin();
156
0
    }
157
158
0
    inline Path::reverse_iterator Path::rend() const {
159
0
        return values_.rend();
160
0
    }
161
162
}
163
164
165
#endif