22#include <boost/test/unit_test.hpp>
23#include <boost/test/data/test_case.hpp>
28#include <ql/time/date.hpp>
29#include <ql/pricingengines/blackformula.hpp>
31#include <boost/math/distributions/normal.hpp>
39BOOST_AUTO_TEST_SUITE(QuantExtTestSuite)
41BOOST_AUTO_TEST_SUITE(RandomVariableTest)
44 BOOST_TEST_MESSAGE(
"Testing filter...");
51 BOOST_CHECK_EQUAL(f[0],
false);
52 BOOST_CHECK_EQUAL(f[5],
true);
53 BOOST_CHECK_EQUAL(f[10],
false);
58 BOOST_CHECK_EQUAL(r[0],
false);
59 BOOST_CHECK_EQUAL(r[5],
true);
63 BOOST_CHECK_EQUAL(r[0],
false);
64 BOOST_CHECK_EQUAL(r[5],
false);
69 BOOST_CHECK_EQUAL(r[0],
false);
70 BOOST_CHECK_EQUAL(r[5],
true);
74 BOOST_CHECK_EQUAL(r[0],
false);
75 BOOST_CHECK_EQUAL(r[5],
false);
79 BOOST_CHECK_EQUAL(r[0],
false);
80 BOOST_CHECK_EQUAL(r[5],
true);
81 BOOST_CHECK_EQUAL(r[10],
true);
84 BOOST_CHECK_EQUAL(r[0],
true);
85 BOOST_CHECK_EQUAL(r[5],
false);
86 BOOST_CHECK_EQUAL(r[10],
false);
88 Filter x(100,
false), y(100,
true);
89 BOOST_CHECK((x && y).deterministic());
90 BOOST_CHECK((x || y).deterministic());
91 BOOST_CHECK((!x).deterministic());
94 BOOST_CHECK_THROW(x && z, QuantLib::Error);
95 BOOST_CHECK_THROW(x || z, QuantLib::Error);
97 BOOST_CHECK_THROW(r.
at(100), QuantLib::Error);
101 BOOST_TEST_MESSAGE(
"Testing random variable...");
110 BOOST_CHECK_CLOSE(r[0], 1.0, tol);
111 BOOST_CHECK_CLOSE(r[5], 2.0, tol);
112 BOOST_CHECK_CLOSE(r[10], 1.0, tol);
115 BOOST_CHECK_CLOSE(s[0], 2.0, tol);
116 BOOST_CHECK_CLOSE(s[5], 4.0, tol);
117 BOOST_CHECK_CLOSE(s[10], 2.0, tol);
120 BOOST_CHECK((x + y).deterministic());
121 BOOST_CHECK((x - y).deterministic());
122 BOOST_CHECK((x * y).deterministic());
123 BOOST_CHECK((x / y).deterministic());
128 for (Size i = 0; i < c.
size(); ++i)
129 BOOST_CHECK_EQUAL(c[i],
true);
135 BOOST_CHECK_EQUAL(c[0],
true);
136 BOOST_CHECK_EQUAL(c[5],
false);
138 BOOST_CHECK_THROW(r.
at(100), QuantLib::Error);
142 BOOST_TEST_MESSAGE(
"Testing functions...");
145 double x = 2.0, y = -2.0;
147 boost::math::normal_distribution<double> n;
149 BOOST_CHECK_CLOSE((X + Y).at(0), x + y, tol);
150 BOOST_CHECK_CLOSE((X - Y).at(0), x - y, tol);
151 BOOST_CHECK_CLOSE((X * Y).at(0), x * y, tol);
152 BOOST_CHECK_CLOSE((X / Y).at(0), x / y, tol);
154 BOOST_CHECK_CLOSE((-X).at(0), -x, tol);
155 BOOST_CHECK_CLOSE((
QuantExt::abs(Y)).at(0), std::abs(y), tol);
156 BOOST_CHECK_CLOSE((
QuantExt::exp(X)).at(0), std::exp(x), tol);
157 BOOST_CHECK_CLOSE((
QuantExt::log(X)).at(0), std::log(x), tol);
160 BOOST_CHECK_CLOSE((
normalCdf(X)).at(0), boost::math::cdf(n, x), tol);
161 BOOST_CHECK_CLOSE((
normalPdf(X)).at(0), boost::math::pdf(n, x), tol);
165 BOOST_TEST_MESSAGE(
"Testing black formula...");
167 Option::Type type[4] = {Option::Call, Option::Call, Option::Put, Option::Put};
169 RandomVariable omega(4), t(4), strike(4), forward(4), impliedVol(4);
183 forward.setAll(100.0);
188 for (Size i = 0; i < 4; ++i) {
189 BOOST_CHECK_CLOSE(res.
at(i),
190 blackFormula(type[i], strike.at(i), forward.at(i), impliedVol.
at(i) * std::sqrt(t.at(i))),
195BOOST_AUTO_TEST_SUITE_END()
197BOOST_AUTO_TEST_SUITE_END()
RandomVariable sqrt(RandomVariable x)
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
CompiledFormula exp(CompiledFormula x)
bool close_enough_all(const RandomVariable &x, const RandomVariable &y)
RandomVariable black(const RandomVariable &omega, const RandomVariable &t, const RandomVariable &strike, const RandomVariable &forward, const RandomVariable &impliedVol)
RandomVariable normalCdf(RandomVariable x)
CompiledFormula abs(CompiledFormula x)
RandomVariable normalPdf(RandomVariable x)
CompiledFormula log(CompiledFormula x)
void set(const Size i, const bool v)
bool deterministic() const
bool at(const Size i) const
Real at(const Size i) const
bool deterministic() const
void set(const Size i, const Real v)
void setAll(const Real v)
BOOST_AUTO_TEST_CASE(testFilter)
Fixture that can be used at top level.