Stan Math Library  2.20.0
reverse mode automatic differentiation
autocovariance.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_AUTOCOVARIANCE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_AUTOCOVARIANCE_HPP
3 
7 #include <vector>
8 
9 namespace stan {
10 namespace math {
11 
32 template <typename T>
33 void 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 
63 template <typename T, typename DerivedA, typename DerivedB>
64 void autocovariance(const Eigen::MatrixBase<DerivedA>& y,
65  Eigen::MatrixBase<DerivedB>& acov, Eigen::FFT<T>& fft) {
66  autocorrelation(y, acov, fft);
67  acov = acov.array() * (y.array() - y.mean()).square().sum() / y.size();
68 }
69 
86 template <typename T>
87 void autocovariance(const std::vector<T>& y, std::vector<T>& acov) {
88  Eigen::FFT<T> fft;
89  size_t N = y.size();
90  acov.resize(N);
91 
92  const Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, 1> > y_map(&y[0], N);
93  Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> > acov_map(&acov[0], N);
94  autocovariance(y_map, acov_map, fft);
95 }
96 
113 template <typename T, typename DerivedA, typename DerivedB>
114 void autocovariance(const Eigen::MatrixBase<DerivedA>& y,
115  Eigen::MatrixBase<DerivedB>& acov) {
116  Eigen::FFT<T> fft;
117  autocovariance(y, acov, fft);
118 }
119 
120 } // namespace math
121 } // namespace stan
122 #endif
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:12
boost::math::tools::promote_args< T >::type variance(const std::vector< T > &v)
Returns the sample variance (divide by length - 1) of the coefficients in the specified standard vect...
Definition: variance.hpp:22
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...
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...

     [ Stan Home Page ] © 2011–2018, Stan Development Team.