26#ifndef quantlib_longstaff_schwartz_path_pricer_hpp
27#define quantlib_longstaff_schwartz_path_pricer_hpp
52 template <
class PathType>
59 const ext::shared_ptr<YieldTermStructure>& termStructure);
68 const std::vector<StateType> &state,
69 const std::vector<Real> &price,
70 const std::vector<Real> &exercise) {}
72 const ext::shared_ptr<EarlyExercisePathPricer<PathType> >
78 std::unique_ptr<DiscountFactor[]>
dF_;
80 mutable std::vector<PathType>
paths_;
86 template <
class PathType>
90 const ext::shared_ptr<YieldTermStructure>& termStructure)
91 : pathPricer_(
std::move(pathPricer)), coeff_(new
Array[times.size() - 2]),
92 dF_(new
DiscountFactor[times.size() - 1]), v_(pathPricer_->basisSystem()),
95 for (
Size i=0; i<times.
size()-1; ++i) {
96 dF_[i] = termStructure->discount(times[i+1])
97 / termStructure->discount(times[i]);
101 template <
class PathType>
inline
103 (
const PathType& path)
const {
104 if (calibrationPhase_) {
106 paths_.push_back(path);
111 Real price = (*pathPricer_)(path, len_-1);
114 bool exercised = (price > 0.0);
116 for (
Size i=len_-2; i>0; --i) {
119 const Real exercise = (*pathPricer_)(path, i);
120 if (exercise > 0.0) {
121 const StateType regValue = pathPricer_->state(path, i);
123 Real continuationValue = 0.0;
124 for (
Size l=0; l<v_.size(); ++l) {
125 continuationValue += coeff_[i-1][l] * v_[l](regValue);
128 if (continuationValue < exercise) {
137 exerciseProbability_.add(exercised ? 1.0 : 0.0);
142 template <
class PathType>
inline
144 const Size n = paths_.size();
146 std::vector<StateType> p_state(
n);
147 std::vector<Real> p_price(
n), p_exercise(
n);
149 for (
Size i=0; i<
n; ++i) {
150 p_state[i] = pathPricer_->state(paths_[i],len_-1);
151 prices[i] = p_price[i] = (*pathPricer_)(paths_[i], len_-1);
152 p_exercise[i] = prices[i];
155 post_processing(len_ - 1, p_state, p_price, p_exercise);
158 std::vector<StateType> x;
159 for (
Size i=len_-2; i>0; --i) {
164 for (
Size j=0; j<
n; ++j) {
165 exercise[j]=(*pathPricer_)(paths_[j], i);
166 if (exercise[j]>0.0) {
167 x.push_back(pathPricer_->state(paths_[j], i));
168 y.push_back(dF_[i]*prices[j]);
172 if (v_.size() <= x.size()) {
178 coeff_[i-1] =
Array(v_.size(), 0.0);
181 for (
Size j=0, k=0; j<
n; ++j) {
183 if (exercise[j]>0.0) {
184 Real continuationValue = 0.0;
185 for (
Size l=0; l<v_.size(); ++l) {
186 continuationValue += coeff_[i-1][l] * v_[l](x[k]);
188 if (continuationValue < exercise[j]) {
189 prices[j] = exercise[j];
193 p_state[j] = pathPricer_->state(paths_[j],i);
194 p_price[j] = prices[j];
195 p_exercise[j] = exercise[j];
198 post_processing(i, p_state, p_price, p_exercise);
202 std::vector<PathType> empty;
205 calibrationPhase_ =
false;
208 template <
class PathType>
inline
210 return exerciseProbability_.mean();
1-D array used in linear algebra.
base class for early exercise path pricers
general linear least squares regression
const Array & coefficients() const
Statistics tool based on incremental accumulation.
Longstaff-Schwarz path pricer for early exercise options.
Real exerciseProbability() const
std::unique_ptr< Array[]> coeff_
LongstaffSchwartzPathPricer(const TimeGrid ×, ext::shared_ptr< EarlyExercisePathPricer< PathType > >, const ext::shared_ptr< YieldTermStructure > &termStructure)
Real operator()(const PathType &path) const override
std::vector< PathType > paths_
QuantLib::IncrementalStatistics exerciseProbability_
std::unique_ptr< DiscountFactor[]> dF_
EarlyExerciseTraits< PathType >::StateType StateType
const ext::shared_ptr< EarlyExercisePathPricer< PathType > > pathPricer_
const std::vector< ext::function< Real(StateType)> > v_
virtual void post_processing(const Size i, const std::vector< StateType > &state, const std::vector< Real > &price, const std::vector< Real > &exercise)
base class for path pricers
base class for early exercise single-path pricers
Maps function, bind and cref to either the boost or std implementation.
general linear least square regression
Real DiscountFactor
discount factor between dates
std::size_t Size
size of a container
statistics tool based on incremental accumulation in the meantime, this is just a wrapper to the boos...
base class for single-path pricers
Interest-rate term structure.