Virtual Testbed
Ship dynamics simulator for extreme conditions
yule_walker_solver.hh
1 #ifndef VTESTBED_CORE_YULE_WALKER_SOLVER_HH
2 #define VTESTBED_CORE_YULE_WALKER_SOLVER_HH
3 
4 #include <cmath>
5 
6 #include <vtestbed/base/blitz.hh>
7 #include <vtestbed/linalg/types.hh>
8 
9 namespace vtb {
10 
11  namespace core {
12 
13  template <class T>
14  blitz::TinyVector<int,3>
15  chop_right(const blitz::Array<T,3>& rhs, T eps);
16  template <class T>
17  blitz::TinyVector<int,4>
18  chop_right(const blitz::Array<T,4>& rhs, T eps);
19 
20  template <class T, int N>
21  inline T
22  autoregression_white_noise_variance(
23  const blitz::Array<T,N>& acf,
24  const blitz::Array<T,N>& phi
25  ) {
26  using blitz::RectDomain;
27  using blitz::sum;
28  RectDomain<N> rect{{0}, phi.shape() - 1};
29  const T& variance = *acf.data();
30  return variance - sum(phi*acf(rect));
31  }
32 
45  template <class T, int N>
46  T
48  const blitz::Array<T,N>& acf,
49  const blitz::Array<T,N>& theta
50  ) {
51  using blitz::sum;
52  using blitz::pow2;
53  const T& variance = *acf.data();
54  return variance / sum(pow2(theta));
55  }
56 
57  template <class T, int N>
58  inline T
59  acf_variance(const blitz::Array<T,N>& acf) {
60  return *acf.data();
61  }
62 
63  template <class T, int N>
65 
66  public:
68  typedef T value_type;
70  typedef blitz::Array<T,N> array_type;
72 
73  private:
75  T _varwn = T(0);
76 
77  public:
78 
79  Yule_walker_solver() = default;
80 
81  Yule_walker_solver(const Yule_walker_solver&) = default;
82 
84  operator=(const Yule_walker_solver&) = default;
85 
86  virtual ~Yule_walker_solver() = default;
87 
93  virtual array_type
94  solve(array_type acf) = 0;
95 
96  virtual pointer
97  copy() const = 0;
98 
100  inline array_type
102  return this->solve();
103  }
104 
106  inline value_type
107  white_noise_variance() const noexcept {
108  return this->_varwn;
109  }
110 
111  protected:
112 
113  inline void
115  this->_varwn = rhs;
116  }
117 
118  };
119 
131  template <class T, int N>
133 
134  private:
136 
137  public:
138  using typename base_type::value_type;
139  using typename base_type::array_type;
140  using typename base_type::pointer;
141 
142  private:
149  T _varepsilon = T(1e-5);
155  bool _determinetheorder = true;
161  T _chopepsilon = T(1e-10);
168  bool _chop = true;
170  int _maxorder = 0;
172  T _minvariance{0};
173 
174  public:
175 
176  Choi_yule_walker_solver() = default;
177 
178  ~Choi_yule_walker_solver() = default;
179 
181 
183  operator=(const Choi_yule_walker_solver&) = default;
184 
189  array_type
190  solve(array_type acf) override;
191 
192  inline pointer
193  copy() const override {
194  return pointer(new Choi_yule_walker_solver<T,N>(*this));
195  }
196 
198  inline bool
199  determine_the_order() const noexcept {
200  return this->_determinetheorder;
201  }
202 
203  inline void
204  determine_the_order(bool rhs) noexcept {
205  this->_determinetheorder = rhs;
206  }
207 
208  inline void
209  do_not_determine_the_order() noexcept {
210  this->_determinetheorder = false;
211  }
212 
213  inline void
214  chop(bool rhs) noexcept {
215  this->_chop = rhs;
216  }
217 
219  inline bool
220  chop() const noexcept {
221  return this->_chop;
222  }
223 
224  inline void
225  do_not_chop() noexcept {
226  this->_chop = false;
227  }
228 
230  inline value_type
231  chop_epsilon() const noexcept {
232  return this->_chopepsilon;
233  }
234 
235  inline void
236  chop_epsilon(value_type rhs) noexcept {
237  this->_chopepsilon = rhs;
238  }
239 
241  inline value_type
242  var_epsilon() const noexcept {
243  return this->_varepsilon;
244  }
245 
246  inline void
247  var_epsilon(value_type rhs) noexcept {
248  this->_varepsilon = rhs;
249  }
250 
251  inline void
252  max_order(int rhs) {
253  this->_maxorder = rhs;
254  }
255 
257  inline int
258  max_order() const noexcept {
259  return this->_maxorder;
260  }
261 
262  inline void
263  min_variance(T rhs) noexcept {
264  this->_minvariance = rhs;
265  }
266 
267  inline T
268  min_variance() const noexcept {
269  return this->_minvariance;
270  }
271 
272  private:
273 
274  inline bool
275  variance_has_not_changed_much(T var0, T var1) noexcept {
276  return (this->_determinetheorder &&
277  std::abs(var1-var0) < this->_varepsilon) ||
278  (var1 > 0 && !(var1 > this->_minvariance));
279  }
280 
281  };
282 
287  template <class T, int N>
289 
290  template <class T>
292  private:
293  typedef blitz::Array<T,3> array_type;
294  typedef blitz::TinyVector<int,3> shape_type;
296 
297  private:
298  const array_type& _acf;
299  const shape_type& _arorder;
300 
301  public:
303  const array_type& acf,
304  const shape_type& ar_order
305  ): _acf(acf), _arorder(ar_order) {}
306 
308  AC_matrix_block(int i0, int j0);
309 
311  AC_matrix_block(int i0);
312 
314  operator()();
315  };
316 
317  template <class T>
319 
320  private:
321  typedef blitz::Array<T,4> array_type;
322  typedef blitz::TinyVector<int,4> shape_type;
324 
325  private:
326  const array_type& _acf;
327  const shape_type& _arorder;
328 
329  public:
331  const array_type& acf,
332  const shape_type& ar_order
333  ): _acf(acf), _arorder(ar_order) {}
334 
336  AC_matrix_block(int i0, int j0, int k0);
337 
339  AC_matrix_block(int i0, int j0);
340 
342  AC_matrix_block(int i0);
343 
345  operator()();
346  };
347 
348  template <class T, int N>
350 
351  private:
353  typedef blitz::TinyVector<int,N> shape_type;
354 
355  public:
356  using typename base_type::value_type;
357  using typename base_type::array_type;
358  using typename base_type::pointer;
359 
360  private:
361  shape_type _order{0};
362  T _chopepsilon = T(1e-10);
363  bool _chop = true;
364 
365  public:
366 
367  Gauss_yule_walker_solver() = default;
368 
370 
372  operator=(const Gauss_yule_walker_solver&) = default;
373 
374  ~Gauss_yule_walker_solver() = default;
375 
376  array_type
377  solve(array_type acf) override;
378 
379  inline pointer
380  copy() const override {
381  return pointer(new Gauss_yule_walker_solver<T,N>(*this));
382  }
383 
384  inline void order(const shape_type& rhs) { this->_order = rhs; }
385  inline const shape_type& order() const { return this->_order; }
386  inline void chop(bool rhs) { this->_chop = rhs; }
387  inline value_type chop_epsilon() const { return this->_chopepsilon; }
388  inline void chop_epsilon(value_type rhs) { this->_chopepsilon = rhs; }
389 
390  };
391 
401  template <class T, int N>
403  public Yule_walker_solver<T,N> {
404 
405  private:
407 
408  public:
409  using typename base_type::value_type;
410  using typename base_type::array_type;
411  using typename base_type::pointer;
412  typedef blitz::TinyVector<int,N> shape_type;
413 
414  private:
415  shape_type _order;
416  int _maxiterations = 1000;
417  T _maxresidual = T(1e-5);
418  T _minvarwn = T(1e-6);
419  T _epsvarwn = T(1e-5);
420 
421  public:
422 
423  array_type
424  solve(array_type acf) override;
425 
426  inline pointer
427  copy() const override {
428  return pointer(
430  );
431  }
432 
433  inline void
434  order(const shape_type& rhs) {
435  this->_order = rhs;
436  }
437 
439  inline const shape_type&
440  order() const noexcept {
441  return this->_order;
442  }
443 
445  inline int
446  max_iterations() const noexcept {
447  return this->_maxiterations;
448  }
449 
450  inline void
451  max_iterations(int rhs) noexcept {
452  this->_maxiterations = rhs;
453  }
454 
455  inline T
456  max_residual() const noexcept {
457  return this->_maxresidual;
458  }
459 
460  inline void
461  max_residual(T rhs) noexcept {
462  this->_maxresidual = rhs;
463  }
464 
471  inline void
472  min_white_noise_variance(T rhs) noexcept {
473  this->_minvarwn = rhs;
474  }
475 
476  inline T
477  min_white_noise_variance() const noexcept {
478  return this->_minvarwn;
479  }
480 
481  inline void
482  min_white_noise_variance_delta(T rhs) noexcept {
483  this->_epsvarwn = rhs;
484  }
485 
487  inline T
489  return this->_epsvarwn;
490  }
491 
492  };
493 
494  }
495 
496 }
497 
498 #endif // vim:filetype=cpp
value_type chop_epsilon() const noexcept
void min_white_noise_variance(T rhs) noexcept
Minimal white noise variance.
value_type white_noise_variance() const noexcept
T variance(const Array< T, N > &rhs)
Sample variance.
Definition: blitz.hh:458
int max_iterations() const noexcept
Maximum number of iterations.
array_type operator()()
Solve Yule—Walker system of equations.
const shape_type & order() const noexcept
Moving average model order.
Multi-dimensional solver that determines MA model coefficients from ACF.
array_type solve(array_type acf) override
Solve Yule—Walker system of equations.
Main namespace.
Definition: convert.hh:9
virtual array_type solve(array_type acf)=0
Solve Yule—Walker system of equations.
Autocovariate matrix generator that reduces the size of ACF to match AR model order.
value_type var_epsilon() const noexcept
array_type solve(array_type acf) override
Solve Yule—Walker system of equations.
blitz::Array< T, N > array_type
Three-dimensional array type.
Computes AR model coefficients using an order-recursvive method from .
array_type solve(array_type acf) override
Solve Yule—Walker system of equations.
T min_white_noise_variance_delta() const noexcept
Minimal white noise variance difference between iterations.
T moving_average_white_noise_variance(const blitz::Array< T, N > &acf, const blitz::Array< T, N > &theta)