Automatic Differentiation
 
Loading...
Searching...
No Matches
autocovariance.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_AUTOCOVARIANCE_HPP
2#define STAN_MATH_PRIM_FUN_AUTOCOVARIANCE_HPP
3
7#include <vector>
8
9namespace stan {
10namespace math {
11
32template <typename T>
33void autocovariance(const std::vector<T>& y, std::vector<T>& acov,
34 Eigen::FFT<T>& fft) {
35 autocorrelation(y, acov, fft);
36
37 T var = variance(y) * (y.size() - 1) / y.size();
38 for (size_t i = 0; i < y.size(); i++) {
39 acov[i] *= var;
40 }
41}
42
65template <typename T, typename DerivedA, typename DerivedB>
66void autocovariance(const Eigen::MatrixBase<DerivedA>& y,
67 Eigen::MatrixBase<DerivedB>& acov, Eigen::FFT<T>& fft) {
68 autocorrelation(y, acov, fft);
69 acov = acov.array() * (y.array() - y.mean()).square().sum() / y.size();
70}
71
88template <typename T>
89void autocovariance(const std::vector<T>& y, std::vector<T>& acov) {
90 Eigen::FFT<T> fft;
91 size_t N = y.size();
92 acov.resize(N);
93
94 const Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1> > y_map(&y[0], N);
95 Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> > acov_map(&acov[0], N);
96 autocovariance(y_map, acov_map, fft);
97}
98
117template <typename T, typename DerivedA, typename DerivedB>
118void autocovariance(const Eigen::MatrixBase<DerivedA>& y,
119 Eigen::MatrixBase<DerivedB>& acov) {
120 Eigen::FFT<T> fft;
121 autocovariance(y, acov, fft);
122}
123
124} // namespace math
125} // namespace stan
126
127#endif
double variance(const T &a)
Return the sample variance of the var_value matrix Raise domain error if size is not greater than zer...
Definition variance.hpp:25
Eigen::Matrix< scalar_type_t< V >, -1, 1 > fft(const V &x)
Return the discrete Fourier transform of the specified complex vector.
Definition fft.hpp:35
void autocovariance(const std::vector< T > &y, std::vector< T > &acov, Eigen::FFT< T > &fft)
Write autocovariance estimates for every lag for the specified input sequence into the specified resu...
var_value< double > var
Definition var.hpp:1187
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
void autocorrelation(const std::vector< T > &y, std::vector< T > &ac, Eigen::FFT< T > &fft)
Write autocorrelation estimates for every lag for the specified input sequence into the specified res...
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9