C++ main module for mmsd Package  1.0
STAT_Object.h
Go to the documentation of this file.
1 #ifndef STAT_Object_H
2 #define STAT_Object_H
3 
4 #include "CORE_Object.h"
5 #include "CORE_Array.h"
6 
13 class STAT_Object : public CORE_Object {
14  SP_OBJECT(STAT_Object);
15  // ATTRIBUTES
16 
17 private:
18 
19 
20 
21  // ASSOCIATIONS
22 
23 
24 protected:
25  // METHODS
26 
27  // CONSTRUCTORS
28 
30  STAT_Object(void);
31 
32 
33 
34  // DESTRUCTORS
35 
36 
39  virtual ~STAT_Object(void);
40 
41 
42 
43 public:
44  //----------------
45  // NEW constructors
46  // ---------------
47 
48  // --------------
49  // SAVER & LOADER
50  // --------------
51 
52 
53 
54 
57  template<typename T>
58  inline static tReal quantile(const T *d,const int& size,const tReal& q) {
59  if (size == 0) return 0;
60  if (size == 1) return d[0];
61  if (q <= 0) return *std::min_element(d, d + size);
62  if (q >= 1) return *std::max_element(d, d + size);
63 
64  double pos = (size - 1) * q;
65  unsigned int ind = (unsigned int) pos;
66  double delta = pos - ind;
67  vector<tReal> w(size); std::copy(d, d + size, w.begin());
68  std::nth_element(w.begin(), w.begin() + ind, w.end());
69  tReal i1 = *(w.begin() + ind);
70  tReal i2 = *std::min_element(w.begin() + ind + 1, w.end());
71  return i1 * (1.0 - delta) + i2 * delta;
72  }
75  template<typename T>
76  inline static tReal quantile(T *d,const int& size,const tReal& q) {
77  //cout << "sample order is changed...\n";
78  if (size == 0) return 0;
79  if (size == 1) return d[0];
80  if (q <= 0) return *std::min_element(d, d + size);
81  if (q >= 1) return *std::max_element(d, d + size);
82 
83  double pos = (size - 1) * q;
84  unsigned int ind = (unsigned int) pos;
85  double delta = pos - ind;
86 
87  std::nth_element(d, d + ind, d+size);
88  tReal i1 = *(d + ind);
89  tReal i2 = *std::min_element(d + ind + 1, d+size);
90  return i1 * (1.0 - delta) + i2 * delta;
91  };
92 
95  template<typename T>
96  inline static tReal quantile(const CORE_Array<T>& a,const tReal& q) {
97  return quantile(a.getValues(),a.getSize(),q);
98  }
99 };
100 
101 #endif
static tReal quantile(T *d, const int &size, const tReal &q)
compute a quantile of the distribution
Definition: STAT_Object.h:76
This class is the base class of all statistics package.
Definition: STAT_Object.h:13
static tReal quantile(const T *d, const int &size, const tReal &q)
compute a quantile of the distribution
Definition: STAT_Object.h:58
STAT_Object(void)
create an object
Definition: STAT_Object.cpp:4
virtual ~STAT_Object(void)
destroy an object.
Definition: STAT_Object.cpp:10
DEFINE_SPTR(STAT_Object)
this class describes an array
Definition: CORE_Array.h:18
static tReal quantile(const CORE_Array< T > &a, const tReal &q)
compute a quantile of the distribution
Definition: STAT_Object.h:96
abstract base class for most classes.
Definition: CORE_Object.h:30
const T * getValues(tArrayIndex &s) const
get the values of the util vector
Definition: CORE_Array.h:371
const tArrayIndex & getSize() const
return the size of the array
Definition: CORE_Array.h:223
#define tReal
Definition: types.h:18