27#include <ql/math/distributions/normaldistribution.hpp>
28#include <ql/math/randomnumbers/inversecumulativerng.hpp>
29#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
31#include <boost/test/unit_test.hpp>
37BOOST_AUTO_TEST_SUITE(AdTest)
41 constexpr Real tol = 1E-14;
45 auto x =
cg_var(g,
"x", ComputationGraph::VarDoesntExist::Create);
46 auto y =
cg_var(g,
"y", ComputationGraph::VarDoesntExist::Create);
47 auto u =
cg_add(g, x, y,
"u");
61 BOOST_CHECK_CLOSE(values[z][0], 10.0, tol);
66 constexpr Real tol = 1E-14;
70 auto x =
cg_var(g,
"x", ComputationGraph::VarDoesntExist::Create);
71 auto y =
cg_var(g,
"y", ComputationGraph::VarDoesntExist::Create);
72 auto u =
cg_add(g, x, y,
"u");
89 BOOST_CHECK_CLOSE(values[z][0], 10.0, tol);
96 std::vector<bool> keep(g.
size(),
false);
105 BOOST_CHECK_CLOSE(derivativesBwd[x][0], 7.0, tol);
107 BOOST_CHECK_CLOSE(derivativesBwd[y][0], 2.0, tol);
109 BOOST_CHECK_CLOSE(derivativesBwd[u][0], 2.0, tol);
111 BOOST_CHECK_CLOSE(derivativesBwd[z][0], 1.0, tol);
116 constexpr Real tol = 1E-14;
120 auto x =
cg_var(g,
"x", ComputationGraph::VarDoesntExist::Create);
121 auto y =
cg_var(g,
"y", ComputationGraph::VarDoesntExist::Create);
122 auto u =
cg_add(g, x, y,
"u");
123 auto z =
cg_mult(g, u, x,
"z");
147 BOOST_CHECK_CLOSE(derivativesFwdX[z][0], 7.0, tol);
149 BOOST_CHECK_CLOSE(derivativesFwdY[z][0], 2.0, tol);
153 BOOST_TEST_MESSAGE(
"Testing indicator derivative...");
159 auto z =
cg_var(g,
"z", ComputationGraph::VarDoesntExist::Create);
162 InverseCumulativeRng<MersenneTwisterUniformRng, InverseCumulativeNormal> normal(MersenneTwisterUniformRng(42));
169 BOOST_TEST_MESSAGE(
"z0=" << z0 <<
":");
170 for (Size i = 0; i < n; ++i) {
171 values[z].set(i, z0 + normal.next().value);
175 Real refAv = 1.0 - CumulativeNormalDistribution()(-z0);
176 BOOST_TEST_MESSAGE(
"E( 1_{z>0} ) = " << av <<
", reference " << refAv <<
", diff " << refAv - av);
177 BOOST_CHECK_SMALL(av - refAv, tol);
183 Real refDav = NormalDistribution()(-z0);
186 BOOST_TEST_MESSAGE(
"E( d/dz 1_{z>0} ) = " << dav <<
", reference " << refDav <<
", diff " << refDav - dav);
187 BOOST_CHECK_SMALL(dav - refDav, tol);
193BOOST_AUTO_TEST_SUITE_END()
195BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(testForwardEvaluation)
backward derivatives computation
forward derivatives computation
std::vector< RandomVariableGrad > getRandomVariableGradients(const Size size, const Size regressionOrder, const QuantLib::LsmBasisSystem::PolynomialType polynomType, const double eps, const Real regressionVarianceCutoff)
std::size_t cg_const(ComputationGraph &g, const double value)
void forwardDerivatives(const ComputationGraph &g, const std::vector< T > &values, std::vector< T > &derivatives, const std::vector< std::function< std::vector< T >(const std::vector< const T * > &, const T *)> > &grad, std::function< void(T &)> deleter={}, const std::vector< bool > &keepNodes={}, const std::size_t conditionalExpectationOpId=0, const std::function< T(const std::vector< const T * > &)> &conditionalExpectation={})
std::vector< RandomVariableOpNodeRequirements > getRandomVariableOpNodeRequirements()
RandomVariable expectation(const RandomVariable &r)
std::size_t cg_mult(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_add(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::vector< std::string > getRandomVariableOpLabels()
std::string ssaForm(const ComputationGraph &g, const std::vector< std::string > &opCodeLabels, const std::vector< T > &values, const std::vector< T > &values2)
void backwardDerivatives(const ComputationGraph &g, std::vector< T > &values, std::vector< T > &derivatives, const std::vector< std::function< std::vector< T >(const std::vector< const T * > &, const T *)> > &grad, std::function< void(T &)> deleter={}, const std::vector< bool > &keepNodes={}, const std::vector< std::function< T(const std::vector< const T * > &)> > &fwdOps={}, const std::vector< std::function< std::pair< std::vector< bool >, bool >(const std::size_t)> > &fwdOpRequiresNodesForDerivatives={}, const std::vector< bool > &fwdKeepNodes={}, const std::size_t conditionalExpectationOpId=0, const std::function< T(const std::vector< const T * > &)> &conditionalExpectation={})
void forwardEvaluation(const ComputationGraph &g, std::vector< T > &values, const std::vector< std::function< T(const std::vector< const T * > &)> > &ops, std::function< void(T &)> deleter={}, bool keepValuesForDerivatives=true, const std::vector< std::function< std::pair< std::vector< bool >, bool >(const std::size_t)> > &opRequiresNodesForDerivatives={}, const std::vector< bool > &keepNodes={}, const std::size_t startNode=0, const std::size_t endNode=ComputationGraph::nan, const bool redBlockReconstruction=false)
std::size_t cg_var(ComputationGraph &g, const std::string &name, const ComputationGraph::VarDoesntExist v)
std::vector< RandomVariableOp > getRandomVariableOps(const Size size, const Size regressionOrder, QuantLib::LsmBasisSystem::PolynomialType polynomType, const double eps, QuantLib::Real regressionVarianceCutoff)
std::size_t cg_indicatorGt(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
ops for type randomvariable
static std::function< void(RandomVariable &)> deleter
Fixture that can be used at top level.