1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/math/matrix.hpp>
#include <ql/math/factorial.hpp>
#include <ql/methods/finitedifferences/operators/numericaldifferentiation.hpp>
#include <cmath>
#include <algorithm>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(NumericalDifferentiationTests)
bool isTheSame(Real a, Real b) {
constexpr double eps = 500 * QL_EPSILON;
if (std::fabs(b) < QL_EPSILON)
return std::fabs(a) < eps;
else
return std::fabs((a - b)/b) < eps;
}
void checkTwoArraysAreTheSame(const Array& calculated,
const Array& expected) {
bool correct = (calculated.size() == expected.size())
&& std::equal(calculated.begin(), calculated.end(),
expected.begin(), isTheSame);
if (!correct) {
BOOST_FAIL("Failed to reproduce expected array"
<< "\n calculated: " << calculated
<< "\n expected: " << expected
<< "\n difference: " << expected - calculated);
}
}
void singleValueTest(const std::string& comment,
Real calculated, Real expected, Real tol) {
if (std::fabs(calculated - expected) > tol)
BOOST_FAIL("Failed to reproduce " << comment
<< " order derivative"
<< "\n calculated: " << calculated
<< "\n expected: " << expected
<< "\n tolerance: " << tol
<< "\n difference: "
<< expected - calculated);
}
BOOST_AUTO_TEST_CASE(testTabulatedCentralScheme) {
BOOST_TEST_MESSAGE("Testing numerical differentiation "
"using the central scheme...");
const std::function<Real(Real)> f;
const NumericalDifferentiation::Scheme central
= NumericalDifferentiation::Central;
// see http://en.wikipedia.org/wiki/Finite_difference_coefficient
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 1.0, 3, central).weights(),
{-0.5, 0.0, 0.5});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 0.5, 3, central).weights(),
{-1.0, 0.0, 1.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 0.25, 7, central).weights(),
{-4/60.0, 12/20.0, -12/4.0, 0.0, 12/4.0, -12/20.0, 4/60.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 4, std::pow(0.5, 0.25), 9, central).weights(),
{14/240.0, -4/5.0, 338/60.0, -244/15.0, 182/8.0, -244/15.0, 338/60.0, -4/5.0, 14/240.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 0.5, 7, central).offsets(),
{-1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5});
}
BOOST_AUTO_TEST_CASE(testTabulatedBackwardScheme) {
BOOST_TEST_MESSAGE("Testing numerical differentiation "
"using the backward scheme...");
const std::function<Real(Real)> f;
const NumericalDifferentiation::Scheme backward
= NumericalDifferentiation::Backward;
// see http://en.wikipedia.org/wiki/Finite_difference_coefficient
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 1.0, 2, backward).weights(),
{1.0, -1.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 2, 2.0, 4, backward).weights(),
{2/4.0, -5/4.0, 4/4.0, -1.0/4.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 4, 1.0, 6, backward).weights(),
{3.0, -14.0, 26.0, -24.0, 11.0, -2.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 2, 0.5, 4, backward).offsets(),
{0.0, -0.5, -1.0, -1.5});
}
BOOST_AUTO_TEST_CASE(testTabulatedForwardScheme) {
BOOST_TEST_MESSAGE("Testing numerical differentiation "
"using the Forward scheme...");
const std::function<Real(Real)> f;
const NumericalDifferentiation::Scheme forward
= NumericalDifferentiation::Forward;
// see http://en.wikipedia.org/wiki/Finite_difference_coefficient
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 1.0, 2, forward).weights(),
{-1.0, 1.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 0.5, 3, forward).weights(),
{-6/2.0, 4.0, -2/2.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, 0.5, 7, forward).weights(),
{-98/20.0, 12.0, -30/2.0, 40/3.0, -30/4.0, 12/5.0, -2/6.0});
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 2, 0.5, 4, forward).offsets(),
{0.0, 0.5, 1.0, 1.5});
}
BOOST_AUTO_TEST_CASE(testIrregularSchemeFirstOrder) {
BOOST_TEST_MESSAGE("Testing numerical differentiation "
"of first order using an irregular scheme...");
const std::function<Real(Real)> f;
const Real h1 = 5e-7;
const Real h2 = 3e-6;
const Real alpha = -h2/(h1*(h1+h2));
const Real gamma = h1/(h2*(h1+h2));
const Real beta = -alpha - gamma;
Array offsets = { -h1, 0.0, h2 };
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 1, offsets).weights(),
{ alpha, beta, gamma });
}
BOOST_AUTO_TEST_CASE(testIrregularSchemeSecondOrder) {
BOOST_TEST_MESSAGE("Testing numerical differentiation "
"of second order using an irregular scheme...");
const std::function<Real(Real)> f;
const Real h1 = 2e-7;
const Real h2 = 8e-8;
const Real alpha = 2/(h1*(h1+h2));
const Real gamma = 2/(h2*(h1+h2));
const Real beta = -alpha - gamma;
Array offsets = { -h1, 0.0, h2 };
checkTwoArraysAreTheSame(
NumericalDifferentiation(f, 2, offsets).weights(),
{alpha, beta, gamma});
}
BOOST_AUTO_TEST_CASE(testDerivativesOfSineFunction) {
BOOST_TEST_MESSAGE("Testing numerical differentiation"
" of sin function...");
const std::function<Real(Real)> f = [](Real x) -> Real { return std::sin(x); };
const std::function<Real(Real)> df_central
= NumericalDifferentiation(f, 1, std::sqrt(QL_EPSILON), 3,
NumericalDifferentiation::Central);
const std::function<Real(Real)> df_backward
= NumericalDifferentiation(f, 1, std::sqrt(QL_EPSILON), 3,
NumericalDifferentiation::Backward);
const std::function<Real(Real)> df_forward
= NumericalDifferentiation(f, 1, std::sqrt(QL_EPSILON), 3,
NumericalDifferentiation::Forward);
for (Real x=0.0; x < 5.0; x+=0.1) {
const Real calculatedCentral = df_central(x);
const Real calculatedBackward = df_backward(x);
const Real calculatedForward = df_forward(x);
const Real expected = std::cos(x);
singleValueTest("central first", calculatedCentral, expected, 1e-8);
singleValueTest("backward first", calculatedBackward, expected, 1e-6);
singleValueTest("forward first", calculatedForward, expected, 1e-6);
}
const std::function<Real(Real)> df4_central
= NumericalDifferentiation(f, 4, 1e-2, 7,
NumericalDifferentiation::Central);
const std::function<Real(Real)> df4_backward
= NumericalDifferentiation(f, 4, 1e-2, 7,
NumericalDifferentiation::Backward);
const std::function<Real(Real)> df4_forward
= NumericalDifferentiation(f, 4, 1e-2, 7,
NumericalDifferentiation::Forward);
for (Real x=0.0; x < 5.0; x+=0.1) {
const Real calculatedCentral = df4_central(x);
const Real calculatedBackward = df4_backward(x);
const Real calculatedForward = df4_forward(x);
const Real expected = std::sin(x);
singleValueTest("central 4th", calculatedCentral, expected, 1e-4);
singleValueTest("backward 4th", calculatedBackward, expected, 1e-4);
singleValueTest("forward 4th", calculatedForward, expected, 1e-4);
}
const Array offsets = {-0.01, -0.02, 0.03, 0.014, 0.041};
NumericalDifferentiation df3_irregular(f, 3, offsets);
checkTwoArraysAreTheSame(df3_irregular.offsets(), offsets);
for (Real x=0.0; x < 5.0; x+=0.1) {
const Real calculatedIrregular = df3_irregular(x);
const Real expected = -std::cos(x);
singleValueTest("irregular 3th", calculatedIrregular, expected, 5e-5);
}
}
Array vandermondeCoefficients(
Size order, Real x, const Array& gridPoints) {
const Array q = gridPoints - x;
const Size n = gridPoints.size();
Matrix m(n, n, 1.0);
for (Size i=1; i < n; ++i) {
const Real fact = Factorial::get(i);
for (Size j=0; j < n; ++j)
m[i][j] = std::pow(q[j], Integer(i)) / fact;
}
Array b(n, 0.0);
b[order] = 1.0;
return inverse(m)*b;
}
BOOST_AUTO_TEST_CASE(testCoefficientBasedOnVandermonde) {
BOOST_TEST_MESSAGE("Testing coefficients from numerical differentiation"
" by comparison with results from"
" Vandermonde matrix inversion...");
const std::function<Real(Real)> f;
for (Natural order=0; order < 5; ++order) {
for (Natural nGridPoints = order + 1;
nGridPoints < order + 3; ++nGridPoints) {
Array gridPoints(nGridPoints);
for (Natural i=0; i < nGridPoints; ++i) {
const Real p = Real(i);
gridPoints[i] = std::sin(p) + std::cos(p); // strange points
}
const Real x = 0.3902842; // strange points
const Array weightsVandermonde
= vandermondeCoefficients(order, x, gridPoints);
const NumericalDifferentiation nd(f, order, gridPoints-x);
checkTwoArraysAreTheSame(gridPoints, nd.offsets() + x);
checkTwoArraysAreTheSame(weightsVandermonde, nd.weights());
}
}
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|