Jpp test-rotations-old-533-g2bdbdb559
the software that should make you happy
Loading...
Searching...
No Matches
JASTRONOMY::JQuantile< T > Struct Template Reference

Auxiliary container for statistical analysis of a large number of values. More...

#include <JAstronomyToolkit.hh>

Inheritance diagram for JASTRONOMY::JQuantile< T >:
std::map< int, std::vector< T > >

Public Types

typedef std::map< int, std::vector< T > > map_type
 
typedef map_type::value_type value_type
 
typedef map_type::const_iterator const_iterator
 
typedef map_type::const_reverse_iterator const_reverse_iterator
 

Public Member Functions

 JQuantile (const double factor)
 Constructor.
 
size_t getN () const
 Get total number of values.
 
getMin () const
 Get minimal value.
 
getMax () const
 Get maximal value.
 
int getKey (const T value) const
 Get key of given value.
 
void put (const T value)
 Add value to container.
 
getValue (const double P) const
 Get minimal value corresponding given maximal probability.
 
double getProbability (const double value) const
 Get maximal probability corresponding given minimal value.
 

Public Attributes

const T factor
 scaling factor for rounding value
 

Detailed Description

template<class T>
struct JASTRONOMY::JQuantile< T >

Auxiliary container for statistical analysis of a large number of values.

The data are organised in groups according to a numerical key so that the computation involving quantiles can be speed up.
The numerical key is obtained by scaling the value with a given factor and then taking the floor of the result.
The quantiles correspond to a distribution with ascending values.

Definition at line 36 of file JAstronomyToolkit.hh.

Member Typedef Documentation

◆ map_type

template<class T >
std::map<int, std::vector<T> > JASTRONOMY::JQuantile< T >::map_type

Definition at line 39 of file JAstronomyToolkit.hh.

◆ value_type

template<class T >
map_type::value_type JASTRONOMY::JQuantile< T >::value_type

Definition at line 40 of file JAstronomyToolkit.hh.

◆ const_iterator

template<class T >
map_type::const_iterator JASTRONOMY::JQuantile< T >::const_iterator

Definition at line 41 of file JAstronomyToolkit.hh.

◆ const_reverse_iterator

template<class T >
map_type::const_reverse_iterator JASTRONOMY::JQuantile< T >::const_reverse_iterator

Definition at line 42 of file JAstronomyToolkit.hh.

Constructor & Destructor Documentation

◆ JQuantile()

template<class T >
JASTRONOMY::JQuantile< T >::JQuantile ( const double factor)
inline

Constructor.

Parameters
factorfactor

Definition at line 50 of file JAstronomyToolkit.hh.

50 :
52 {}
const T factor
scaling factor for rounding value

Member Function Documentation

◆ getN()

template<class T >
size_t JASTRONOMY::JQuantile< T >::getN ( ) const
inline

Get total number of values.

Returns
number of values

Definition at line 60 of file JAstronomyToolkit.hh.

61 {
62 return std::accumulate(this->begin(), this->end(), 0, [](const size_t n, const value_type& i) { return n + i.second.size(); });
63 }
const int n
Definition JPolint.hh:791

◆ getMin()

template<class T >
T JASTRONOMY::JQuantile< T >::getMin ( ) const
inline

Get minimal value.

Returns
value

Definition at line 71 of file JAstronomyToolkit.hh.

72 {
73 for (const_iterator i = this->begin(); i != this->end(); ++i) {
74 if (!i->second.empty()) {
75 return *std::min_element(i->second.begin(), i->second.end());
76 }
77 }
78
79 THROW(JEmptyCollection, "Missing data.");
80 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
map_type::const_iterator const_iterator

◆ getMax()

template<class T >
T JASTRONOMY::JQuantile< T >::getMax ( ) const
inline

Get maximal value.

Returns
value

Definition at line 88 of file JAstronomyToolkit.hh.

89 {
90 for (const_reverse_iterator i = this->rbegin(); i != this->rend(); ++i) {
91 if (!i->second.empty()) {
92 return *std::max_element(i->second.begin(), i->second.end());
93 }
94 }
95
96 THROW(JEmptyCollection, "Missing data.");
97 }
map_type::const_reverse_iterator const_reverse_iterator

◆ getKey()

template<class T >
int JASTRONOMY::JQuantile< T >::getKey ( const T value) const
inline

Get key of given value.

Parameters
valuevalue
Returns
key

Definition at line 106 of file JAstronomyToolkit.hh.

107 {
108 return floor(value * factor);
109 }

◆ put()

template<class T >
void JASTRONOMY::JQuantile< T >::put ( const T value)
inline

Add value to container.

Parameters
valuevalue

Definition at line 117 of file JAstronomyToolkit.hh.

118 {
119 (*this)[getKey(value)].push_back(value);
120 }
int getKey(const T value) const
Get key of given value.

◆ getValue()

template<class T >
T JASTRONOMY::JQuantile< T >::getValue ( const double P) const
inline

Get minimal value corresponding given maximal probability.

Parameters
Pprobability [0,1>
Returns
value

Definition at line 129 of file JAstronomyToolkit.hh.

130 {
131 using namespace std;
132
133 if (P < 0.0 || P > 1.0) {
134 THROW(JValueOutOfRange, "Invalid probability " << P);
135 }
136
137 const size_t N = this->getN();
138 const size_t M = N - (size_t) (P * N);
139
140 if (N == 0) {
141 THROW(JEmptyCollection, "Missing data.");
142 }
143
144 if (M == N) { return this->getMax(); }
145 if (M == 0) { return this->getMin(); }
146
147 size_t n = N;
148
149 for (const_reverse_iterator p = this->rbegin(); p != this->rend(); ++p) {
150
151 n -= p->second.size();
152
153 if (n <= M) {
154
155 vector<T> buffer(p->second.begin(), p->second.end());
156
157 sort(buffer.begin(), buffer.end());
158
159 return buffer[M - n];
160 }
161 }
162
163 THROW(JValueOutOfRange, "Invalid index " << M << ' ' << N);
164 }
T getMax() const
Get maximal value.
T getMin() const
Get minimal value.
size_t getN() const
Get total number of values.

◆ getProbability()

template<class T >
double JASTRONOMY::JQuantile< T >::getProbability ( const double value) const
inline

Get maximal probability corresponding given minimal value.

Parameters
valuevalue
Returns
probability

Definition at line 173 of file JAstronomyToolkit.hh.

174 {
175 const int M = getKey(value);
176 size_t n = 0;
177
178 const_reverse_iterator i = this->rbegin();
179
180 for ( ; i != this->rend() && i->first > M; ++i) {
181 n += i->second.size();
182 }
183
184 if (i != this->rend()) {
185 for (T x : i->second) {
186 if (x >= value) {
187 n += 1;
188 }
189 }
190 }
191
192 return (double) n / (double) this->getN();
193 }

Member Data Documentation

◆ factor

template<class T >
const T JASTRONOMY::JQuantile< T >::factor

scaling factor for rounding value

Definition at line 196 of file JAstronomyToolkit.hh.


The documentation for this struct was generated from the following file: