24#ifndef quantlib_auto_covariance_hpp
25#define quantlib_auto_covariance_hpp
40 template <
typename ForwardIterator>
41 std::vector<std::complex<Real> >
double_ft(ForwardIterator begin,
42 ForwardIterator end) {
43 std::size_t nData = std::distance(begin, end);
46 std::vector<std::complex<Real> > ft(fft.
output_size());
48 Array tmp(ft.size(), 0.0);
49 std::complex<Real> z = std::complex<Real>();
50 for (
Size i=0; i<ft.size(); ++i) {
51 tmp[i] = std::norm(ft[i]);
60 template <
typename InputIterator,
typename OutputIterator>
65 for (InputIterator it = begin; it != end; ++it, ++
n)
66 mean = (mean*
Real(
n-1) + *it)/
n;
67 std::transform(begin, end, out, [=](
Real x) ->
Real {
return x - mean; });
80 template <
typename ForwardIterator,
typename OutputIterator>
82 OutputIterator out, std::size_t maxLag) {
83 using namespace detail;
84 std::size_t nData = std::distance(begin, end);
85 QL_REQUIRE(maxLag < nData,
"maxLag must be less than data size");
86 const std::vector<std::complex<Real> >& ft = double_ft(begin, end);
88 for (std::size_t k = 0; k <= maxLag; ++k)
89 *out++ = ft[k].real() * w;
98 template <
typename ForwardIterator,
typename OutputIterator>
100 OutputIterator out, std::size_t maxLag) {
101 using namespace detail;
102 std::size_t nData = std::distance(begin, end);
104 "number of covariances must be less than data size");
105 const std::vector<std::complex<Real> >& ft = double_ft(begin, end);
107 for (std::size_t k = 0; k <= maxLag; ++k, w2 -= 1.0) {
108 *out++ = ft[k].real() * w1 / w2;
121 template <
typename ForwardIterator,
typename OutputIterator>
124 std::size_t maxLag,
bool reuse) {
125 using namespace detail;
128 mean = remove_mean(begin, end, begin);
131 Array tmp(std::distance(begin, end));
132 mean = remove_mean(begin, end, tmp.
begin());
146 template <
typename ForwardIterator,
typename OutputIterator>
148 OutputIterator out, std::size_t maxLag) {
149 using namespace detail;
150 std::size_t nData = std::distance(begin, end);
152 "number of correlations must be less than data size");
153 const std::vector<std::complex<Real> >& ft = double_ft(begin, end);
158 for (std::size_t k = 1; k <= maxLag; ++k, w2 -= 1.0)
159 *out++ = ft[k].real() * w1 / (
variance * w2);
172 template <
typename ForwardIterator,
typename OutputIterator>
175 std::size_t maxLag,
bool reuse) {
176 using namespace detail;
179 mean = remove_mean(begin, end, begin);
182 Array tmp(std::distance(begin, end));
183 mean = remove_mean(begin, end, tmp.
begin());
1-D array used in linear algebra.
1-D array used in linear algebra.
const_iterator end() const
const_iterator begin() const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
LinearInterpolation variance
std::size_t Size
size of a container
std::vector< std::complex< Real > > double_ft(ForwardIterator begin, ForwardIterator end)
Real remove_mean(InputIterator begin, InputIterator end, OutputIterator out)
void autocorrelations(ForwardIterator begin, ForwardIterator end, OutputIterator out, std::size_t maxLag)
Unbiased auto-correlations.
void convolutions(ForwardIterator begin, ForwardIterator end, OutputIterator out, std::size_t maxLag)
Convolutions of the input sequence.
void autocovariances(ForwardIterator begin, ForwardIterator end, OutputIterator out, std::size_t maxLag)
Unbiased auto-covariances.