C++:实现量化Longstaff-Schwartz模型测试实例
#include "mclongstaffschwartzengine.hpp"
#include "utilities.hpp"
#include <ql/instruments/vanillaoption.hpp>
#include <ql/methods/montecarlo/lsmbasissystem.hpp>
#include <ql/pricingengines/mclongstaffschwartzengine.hpp>
#include <ql/pricingengines/vanilla/fdblackscholesvanillaengine.hpp>
#include <ql/pricingengines/vanilla/mcamericanengine.hpp>
#include <ql/processes/stochasticprocessarray.hpp>
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <utility>
using namespace QuantLib;
using namespace boost::unit_test_framework;
namespace {
class AmericanMaxPathPricer : public EarlyExercisePathPricer<MultiPath> {
public:
explicit AmericanMaxPathPricer(ext::shared_ptr<Payoff> payoff)
: payoff_(std::move(payoff)) {
}
StateType state(const MultiPath& path, Size t) const override {
Array tmp(path.assetNumber());
for (Size i=0; i<path.assetNumber(); ++i) {
tmp[i]=path[i][t];
}
return tmp;
}
Real operator()(const MultiPath& path, Size t) const override {
const Array tmp = state(path, t);
return (*payoff_)(*std::max_element(tmp.begin(), tmp.end()));
}
std::vector<ext::function<Real(StateType)> > basisSystem() const override {
return LsmBasisSystem::multiPathBasisSystem(2, 2,
LsmBasisSystem::Monomial);
}
protected:
const ext::shared_ptr<Payoff> payoff_;
};
template <class RNG>
class MCAmericanMaxEngine
: public MCLongstaffSchwartzEngine<VanillaOption::engine,
MultiVariate,RNG>{
public:
MCAmericanMaxEngine(
const ext::shared_ptr<StochasticProcessArray>& processes,
Size timeSteps,
Size timeStepsPerYear,
bool brownianbridge,
bool antitheticVariate,
bool controlVariate,
Size requiredSamples,
Real requiredTolerance,
Size maxSamples,
BigNatural seed,
Size nCalibrationSamples = Null<Size>())
: MCLongstaffSchwartzEngine<VanillaOption::engine