lanczos_smoothing.qbk 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. [/
  2. Copyright (c) 2019 Nick Thompson
  3. Use, modification and distribution are subject to the
  4. Boost Software License, Version 1.0. (See accompanying file
  5. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:diff Lanczos Smoothing Derivatives]
  8. [heading Synopsis]
  9. ``
  10. #include <boost/math/differentiation/lanczos_smoothing.hpp>
  11. namespace boost::math::differentiation {
  12. template <class Real, size_t order=1>
  13. class discrete_lanczos_derivative {
  14. public:
  15. discrete_lanczos_derivative(Real spacing,
  16. size_t n = 18,
  17. size_t approximation_order = 3);
  18. template<class RandomAccessContainer>
  19. Real operator()(RandomAccessContainer const & v, size_t i) const;
  20. template<class RandomAccessContainer>
  21. void operator()(RandomAccessContainer const & v, RandomAccessContainer & dvdt) const;
  22. template<class RandomAccessContainer>
  23. RandomAccessContainer operator()(RandomAccessContainer const & v) const;
  24. Real get_spacing() const;
  25. };
  26. } // namespaces
  27. ``
  28. [heading Description]
  29. The `discrete_lanczos_derivative` class calculates a finite-difference approximation to the derivative of a noisy sequence of equally-spaced values /v/.
  30. A basic usage is
  31. std::vector<double> v(500);
  32. // fill v with noisy data.
  33. double spacing = 0.001;
  34. using boost::math::differentiation::discrete_lanczos_derivative;
  35. auto lanczos = discrete_lanczos_derivative(spacing);
  36. // Compute dvdt at index 30:
  37. double dvdt30 = lanczos(v, 30);
  38. // Compute derivative of entire vector:
  39. std::vector<double> dvdt = lanczos(v);
  40. Noise-suppressing second derivatives can also be computed.
  41. The syntax is as follows:
  42. std::vector<double> v(500);
  43. // fill v with noisy data.
  44. auto lanczos = lanczos_derivative<double, 2>(spacing);
  45. // evaluate second derivative at a point:
  46. double d2vdt2 = lanczos(v, 18);
  47. // evaluate second derivative of entire vector:
  48. std::vector<double> d2vdt2 = lanczos(v);
  49. For memory conscious programmers, you can reuse the memory space for the derivative as follows:
  50. std::vector<double> v(500);
  51. std::vector<double> dvdt(500);
  52. // . . . define spacing, create and instance of discrete_lanczos_derivative
  53. // populate dvdt, perhaps in a loop:
  54. lanczos(v, dvdt);
  55. If the data has variance \u03C3[super 2],
  56. then the variance of the computed derivative is roughly \u03C3[super 2]/p/[super 3] /n/[super -3] \u0394 /t/[super -2],
  57. i.e., it increases cubically with the approximation order /p/, linearly with the data variance,
  58. and decreases at the cube of the filter length /n/.
  59. In addition, we must not forget the discretization error which is /O/(\u0394 /t/[super /p/]).
  60. You can play around with the approximation order /p/ and the filter length /n/:
  61. size_t n = 12;
  62. size_t p = 2;
  63. auto lanczos = lanczos_derivative(spacing, n, p);
  64. double dvdt = lanczos(v, i);
  65. If /p=2n/, then the discrete Lanczos derivative is not smoothing:
  66. It reduces to the standard /2n+1/-point finite-difference formula.
  67. For /p>2n/, an assertion is hit as the filter is undefined.
  68. In our tests with AWGN, we have found the error decreases monotonically with /n/,
  69. as is expected from the theory discussed above.
  70. So the choice of /n/ is simple:
  71. As high as possible given your speed requirements (larger /n/ implies a longer filter and hence more compute),
  72. balanced against the danger of overfitting and averaging over non-stationarity.
  73. The choice of approximation order /p/ for a given /n/ is more difficult.
  74. If your signal is believed to be a polynomial,
  75. it does not make sense to set /p/ to larger than the polynomial degree-
  76. though it may be sensible to take /p/ less than this.
  77. For a sinusoidal signal contaminated with AWGN, we ran a few tests showing that for SNR = 1,
  78. p = n/8 gave the best results,
  79. for SNR = 10, p = n/7 was the best, and for SNR = 100, p = n/6 was the most reasonable choice.
  80. For SNR = 0.1, the method appears to be useless.
  81. The user is urged to use these results with caution: they have no theoretical backing and are extrapolated from a single case.
  82. The filters are (regrettably) computed at runtime-the vast number of combinations of approximation order and filter length makes the number of filters that must be stored excessive for compile-time data.
  83. The constructor call computes the filters.
  84. Since each filter has length /2n+1/ and there are /n/ filters, whose element each consist of /p/ summands,
  85. the complexity of the constructor call is O(/n/[super 2]/p/).
  86. This is not cheap-though for most cases small /p/ and /n/ not too large (< 20) is desired.
  87. However, for concreteness, on the author's 2.7GHz Intel laptop CPU, the /n=16/, /p=3/ filter takes 9 microseconds to compute.
  88. This is far from negligible, and as such the filters may be used with multiple data, and even shared between threads:
  89. std::vector<double> v1(500);
  90. std::vector<double> v2(500);
  91. // fill v1, v2 with noisy data.
  92. auto lanczos = lanczos_derivative(spacing);
  93. std::vector<double> dv1dt = lanczos(v1); // threadsafe
  94. std::vector<double> dv2dt = lanczos(v2); // threadsafe
  95. // need to use a different spacing?
  96. lanczos.reset_spacing(0.02); // not threadsafe
  97. The implementation follows [@https://doi.org/10.1080/00207160.2012.666348 McDevitt, 2012],
  98. who vastly expanded the ideas of Lanczos to create a very general framework for numerically differentiating noisy equispaced data.
  99. [heading Example]
  100. We have extracted some data from the [@https://www.gw-openscience.org/data/ LIGO signal] and differentiated it
  101. using the (/n/, /p/) = (60, 4) Lanczos smoothing derivative, as well as using the (/n/, /p/) = (4, 8) (nonsmoothing) derivative.
  102. [graph ligo_derivative]
  103. The original data is in orange, the smoothing derivative in blue, and the non-smoothing standard finite difference formula is in gray.
  104. (Each time series has been rescaled to fit in the same graph.)
  105. We can see that the smoothing derivative tracks the increase and decrease in the trend well, whereas the standard finite difference formula produces nonsense and amplifies noise.
  106. [heading Caveats]
  107. The computation of the filters is ill-conditioned for large /p/.
  108. In order to mitigate this problem, we have computed the filters in higher precision and cast the results to the desired type.
  109. However, this simply pushes the problem to larger /p/.
  110. In practice, this is not a problem, as large /p/ corresponds to less powerful denoising, but keep it in mind.
  111. In addition, the `-ffast-math` flag has a very large effect on the speed of these functions.
  112. In our benchmarks, they were 50% faster with the flag enabled, which is much larger than the usual performance increases we see by turning on this flag.
  113. Hence, if the default performance is not sufficient, this flag is available, though it comes with its own problems.
  114. This requires C++17 `if constexpr`.
  115. [heading References]
  116. * Corless, Robert M., and Nicolas Fillion. ['A graduate introduction to numerical methods.] AMC 10 (2013): 12.
  117. * Lanczos, Cornelius. ['Applied analysis.] Courier Corporation, 1988.
  118. * Timothy J. McDevitt (2012): ['Discrete Lanczos derivatives of noisy data], International Journal of Computer Mathematics, 89:7, 916-931
  119. [endsect]