25#ifndef quantlib_tree_based_lattice_hpp
26#define quantlib_tree_based_lattice_hpp
81 Array& newValues)
const;
99 for (
Size i=statePricesLimit_; i<until; i++) {
100 statePrices_.push_back(
Array(this->impl().size(i+1), 0.0));
101 for (
Size j=0; j<this->impl().size(i); j++) {
103 Real statePrice = statePrices_[i][j];
104 for (
Size l=0; l<n_; l++) {
105 statePrices_[i+1][this->impl().descendant(i,j,l)] +=
106 statePrice*disc*this->impl().probability(i,j,l);
110 statePricesLimit_ = until;
113 template <
class Impl>
115 if (i>statePricesLimit_)
116 computeStatePrices(i);
117 return statePrices_[i];
120 template <
class Impl>
126 template <
class Impl>
130 asset.
reset(this->impl().size(i));
133 template <
class Impl>
135 partialRollback(asset,to);
139 template <
class Impl>
149 "cannot roll the asset back to" << to
150 <<
" (it is already at t = " << from <<
")");
155 for (
Integer i=iFrom-1; i>=iTo; --i) {
156 Array newValues(this->impl().size(i));
157 this->impl().stepback(i, asset.
values(), newValues);
159 asset.
values() = newValues;
166 template <
class Impl>
168 Array& newValues)
const {
169 #pragma omp parallel for
170 for (
long j=0; j<(long)this->impl().size(i); j++) {
172 for (
Size l=0; l<n_; l++) {
173 value += this->impl().probability(i,j,l) *
174 values[this->impl().descendant(i,j,l)];
176 value *= this->impl().discount(i,j);
177 newValues[j] = value;
1-D array used in linear algebra.
Support for the curiously recurring template pattern.
Discretized asset class used by numerical methods.
const Array & values() const
virtual void reset(Size size)=0
Lattice (tree, finite-differences) base class
const TimeGrid & timeGrid() const
Tree-based lattice-method base class.
Real presentValue(DiscretizedAsset &) const override
Computes the present value of an asset using Arrow-Debrew prices.
std::vector< Array > statePrices_
void partialRollback(DiscretizedAsset &, Time to) const override
void rollback(DiscretizedAsset &, Time to) const override
void initialize(DiscretizedAsset &, Time t) const override
initialize an asset at the given time.
const Array & statePrices(Size i) const
void computeStatePrices(Size until) const
void stepback(Size i, const Array &values, Array &newValues) const
TreeLattice(const TimeGrid &timeGrid, Size n)
Curiously recurring template pattern.
Discretized asset classes.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Real Time
continuous quantity with 1-year units
Real DiscountFactor
discount factor between dates
QL_INTEGER Integer
integer number
std::size_t Size
size of a container
bool close(const Quantity &m1, const Quantity &m2, Size n)
Real DotProduct(const Array &v1, const Array &v2)