descriptiveStatistics.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_DESCRIPTIVESTATISTICS_HPP
2 #define JULIAN_DESCRIPTIVESTATISTICS_HPP
3 
4 #include <vector>
5 #include <algorithm>
6 #include <boost/assert.hpp>
7 #include <gsl/gsl_statistics.h>
8 
9 namespace julian {
10  namespace stats {
11 
17  void descriptiveStatistics(const std::vector<double>&);
18 
30  inline double mean(const std::vector<double>& data) {
31  return gsl_stats_mean(&data.front(), 1, data.size());
32  }
33 
47  inline double variance(const std::vector<double>& data) {
48  return gsl_stats_variance(&data.front(), 1, data.size());
49  }
50 
63  inline double variance(const std::vector<double>& data,const double mean) {
64  return gsl_stats_variance_with_fixed_mean(&data.front(), 1, data.size(), mean);
65  }
66 
80  inline double stdDev(const std::vector<double>& data) {
81  return gsl_stats_sd(&data.front(), 1, data.size());
82  }
83 
96  inline double stdDev(const std::vector<double>& data,const double mean) {
97  return gsl_stats_sd_with_fixed_mean(&data.front(), 1, data.size(), mean);
98  }
99 
113  inline double absDev(const std::vector<double>& data) {
114  return gsl_stats_absdev(&data.front(), 1, data.size());
115  }
116 
130  inline double absDev(const std::vector<double>& data, const double mean) {
131  return gsl_stats_absdev_m(&data.front(), 1, data.size(), mean);
132  }
133 
145  inline double skew(const std::vector<double>& data) {
146  return gsl_stats_skew(&data.front(), 1, data.size());
147  }
148 
160  inline double kurtosis(const std::vector<double>& data) {
161  return gsl_stats_kurtosis(&data.front(), 1, data.size());
162  }
163 
177  inline double pearsonCorr(const std::vector<double>& data1,const std::vector<double>& data2) {
178  BOOST_ASSERT_MSG(data1.size() == data2.size(),"Data vectors have different sizes.");
179  return gsl_stats_correlation(&data1.front(), 1, &data2.front(), 1, data1.size());
180  }
181 
196  inline double spearmanCorr(const std::vector<double>& data1,const std::vector<double>& data2) {
197  BOOST_ASSERT_MSG(data1.size() == data2.size(),"Data vectors have different sizes.");
198  int n = 2 * data1.size();
199  double* t = new double[n];
200  double corr = gsl_stats_spearman(&data1.front(), 1, &data2.front(), 1, data1.size(),t);
201  delete[] t;
202  return corr;
203  }
204 
213  inline double max(const std::vector<double>& data) {
214  return gsl_stats_max(&data.front(), 1, data.size());
215  }
216 
225  inline double min(const std::vector<double>& data) {
226  return gsl_stats_min(&data.front(), 1, data.size());
227  }
228 
241  inline double median(const std::vector<double> data) {
242  auto data_(data);
243  std::sort(data_.begin(),data_.end());
244  return gsl_stats_median_from_sorted_data(&data_.front(), 1, data_.size());
245  }
246 
261  inline double percentile(const std::vector<double> data,const double& q) {
262  auto data_(data);
263  std::sort(data_.begin(),data_.end());
264  return gsl_stats_quantile_from_sorted_data(&data_.front(), 1, data_.size(), q);
265  }
266 
277  inline double IQR(const std::vector<double>& data) {
278  return percentile(data, 0.75) - percentile(data, 0.25);
279  }
280 
281  } // stats
282 } // namespace julian
283 #endif
double mean(const std::vector< double > &data)
Function calculates mean.
Definition: descriptiveStatistics.hpp:30
double absDev(const std::vector< double > &data, const double mean)
Function calculates absolute deviation using the provided mean.
Definition: descriptiveStatistics.hpp:130
Definition: cadHoliday.cpp:3
double pearsonCorr(const std::vector< double > &data1, const std::vector< double > &data2)
Function calculates Pearson correlation.
Definition: descriptiveStatistics.hpp:177
double max(const std::vector< double > &data)
Function returns the maximum value.
Definition: descriptiveStatistics.hpp:213
double IQR(const std::vector< double > &data)
Returns interquartile range.
Definition: descriptiveStatistics.hpp:277
double stdDev(const std::vector< double > &data, const double mean)
Function calculates standard deviation using the provided mean.
Definition: descriptiveStatistics.hpp:96
double skew(const std::vector< double > &data)
Function calculates skew.
Definition: descriptiveStatistics.hpp:145
double min(const std::vector< double > &data)
Function returns the minimum value.
Definition: descriptiveStatistics.hpp:225
double spearmanCorr(const std::vector< double > &data1, const std::vector< double > &data2)
Function calculates Spearman correlation.
Definition: descriptiveStatistics.hpp:196
double median(const std::vector< double > data)
Function returns median.
Definition: descriptiveStatistics.hpp:241
double kurtosis(const std::vector< double > &data)
Function calculates normalized kurtosis.
Definition: descriptiveStatistics.hpp:160
double variance(const std::vector< double > &data, const double mean)
Function calculates variance using the provided mean.
Definition: descriptiveStatistics.hpp:63
double percentile(const std::vector< double > data, const double &q)
Function returns a quantile.
Definition: descriptiveStatistics.hpp:261
void descriptiveStatistics(const std::vector< double > &data)
Procedure prints the basic statistical measures.
Definition: descriptiveStatistics.cpp:15