UnaryStatistic.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2020 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/More/General/UnaryStatistic.h
10 
11 #ifndef __UnaryStatistic_h
12 #define __UnaryStatistic_h
13 
14 #include "Identifiable.h"
15 #include <algorithm>
16 
18 class UnaryStatistic : public Identifiable
19 {
20 public:
21 
23  static UnaryStatistic* factory (const std::string& name);
24 
26  static const std::vector<UnaryStatistic*>& children ();
27 
29  UnaryStatistic (const std::string& name, const std::string& description);
30 
32  virtual double get (const std::vector<double>&) = 0;
33 
35  virtual UnaryStatistic* clone () const = 0;
36 
37 private:
38 
40  static void build ();
41 };
42 
44 void central_moments (std::vector<double> data, std::vector<double>& mu);
45 
47 
49 double robust_variance (const std::vector<double>& data,
50  std::vector<float>* psd = 0);
51 
52 template<typename T>
53 T median (std::vector<T> data)
54 {
55  unsigned mid = data.size() / 2;
56  std::nth_element( data.begin(), data.begin()+mid, data.end() );
57  return data[mid];
58 }
59 
60 template<typename T>
61 T mean (const std::vector<T>& data)
62 {
63  T sum (0.0);
64  for (auto element: data)
65  sum += element;
66  return sum / data.size();
67 }
68 
69 template<typename T>
70 void Q1_Q2_Q3 (std::vector<T> data, T& Q1, T& Q2, T& Q3)
71 {
72  std::sort( data.begin(), data.end() );
73  unsigned ndat = data.size();
74  Q1 = data[ndat / 4];
75  Q2 = data[ndat / 2];
76  Q3 = data[(3 * ndat) / 4];
77 }
78 
79 template<typename T>
80 void filtered_Q1_Q2_Q3 (std::vector<T> data, T& Q1, T& Q2, T& Q3, T value)
81 {
82  std::remove( data.begin(), data.end(), value );
83  std::sort( data.begin(), data.end() );
84  unsigned ndat = data.size();
85  Q1 = data[ndat / 4];
86  Q2 = data[ndat / 2];
87  Q3 = data[(3 * ndat) / 4];
88 }
89 
90 #endif
91 
virtual UnaryStatistic * clone() const =0
Derived types must also define clone method.
void frc1d(size_t nfft, float *into, const float *from)
Forward real-to-complex FFT.
Definition: FTransform.C:39
virtual double get(const std::vector< double > &)=0
Derived types define the value returned.
UnaryStatistic(const std::string &name, const std::string &description)
Construct with a name and description.
Definition: UnaryStatistic.C:825
static UnaryStatistic * factory(const std::string &name)
Create a new instance of UnaryStatistic based on name.
Definition: UnaryStatistic.C:897
A convenient exception handling class.
Definition: Error.h:54
Commmon statistics that can be derived from a single array of floats.
Definition: UnaryStatistic.h:18
static const std::vector< UnaryStatistic * > & children()
Returns a list of available UnaryStatistic children.
Definition: UnaryStatistic.C:887
normalization get_norm()
Returns the normalization convention of the currently selected library.
Definition: FTransform.C:102

Generated using doxygen 1.8.17