Cosan  1.0
Data Analytics Library
statistics.h
Go to the documentation of this file.
1 //
2 // Created by Xinyu Zhang on 4/6/21.
3 //
4 
5 #ifndef COSAN_STATISTICS_H
6 #define COSAN_STATISTICS_H
7 #include <cosan/evaluation.h>
8 //template<Numeric NumericType>
9 namespace Cosan{
10  /**
11  * @brief Class for statistics results
12  * @details calculate SampleCovariance, mean, variance, median, maxNum, minNum;
13  * @code CosanMatrix<NumericType> & GetSampleCovariance(const CosanMatrix<NumericType> & X) @endcode
14  * @code NumericType GetMean(const CosanMatrix<NumericType> & X) @endcode
15  * @code NumericType GetVar(const CosanMatrix<NumericType> & X) @endcode
16  * @code NumericType GetMedian(const CosanMatrix<NumericType> & X) @endcode
17  * @code NumericType GetMax(const CosanMatrix<NumericType> & X) @endcode
18  * @code NumericType GetMin(const CosanMatrix<NumericType> & X) @endcode
19  **/
20  template<Numeric NumericType>
21  class SummaryStatistics: public Evaluation {
22  public:
25  if (X.rows()<=1){
26  fmt::print("Input must have more than 1 row!")
27  throw SmallRows;
28  }
29  CosanMatrix<NumericType> centered = X.rowwise() - X.colwise().mean();
30  SampleCovariance = (centered.adjoint() * centered) / double(X.rows() - 1);
31  return SampleCovariance}
33  if (X.rows()>1 && X.cols()>1){
34  fmt::print("Mean is not defined in matrix! Calculate the mean of all entries!");
35  }
36  mean = X.mean();
37  return mean;
38  }
40  if (X.rows()>1 && X.cols()>1){
41  fmt::print("Variance is not defined in matrix! Calculate the variance of all entries!");
42  }
43  variance = std::sqrt((X.array() - X.mean()).square().sum()/(X.size()-1));
44  return variance;
45  }
46 
48  if (X.rows()>1 && X.cols()>1){
49  fmt::print("Median is not defined in matrix! Calculate the median of all entries!");
50  }
51  std::vector<NumericType> X_copy(X.data(),X.data()+X.size());
52  if(X_copy.size()%2==0){
53  std::nth_element(X_copy.data(), X_copy.data() + X_copy.size()/2, X_copy.data()+X_copy.size());
54  std::nth_element(X_copy.data(), X_copy.data() + X_copy.size()/2-1, X_copy.data()+X_copy.size());
55  median = (*(X_copy.data()+X_copy.size()/2)+*(X_copy.data()+X_copy.size()/2-1))/2;
56  }
57  else{
58  std::nth_element(X_copy.data(), X_copy.data() + (X_copy.size()-1)/2, X_copy.data()+X_copy.size());
59  median = *(X_copy.data() + (X_copy.size()-1)/2);
60  }
61  return median;
62  }
63 
65  if (X.rows()>1 && X.cols()>1){
66  fmt::print("Variance is not defined in matrix! Calculate the minimum of all entries!");
67  }
68  maxNum = X.maxCoeff();
69  return maxNum;
70  }
72  if (X.rows()>1 && X.cols()>1){
73  fmt::print("Variance is not defined in matrix! Calculate the minimum of all entries!");
74  }
75  minNum = X.minCoeff();
76  return minNum;
77  }
78 
85  };
86 
87 // template<typename NumericType,
88 // typename = typename std::enable_if<std::is_arithmetic<NumericType>::value,NumericType>::type>
89  /**
90  * @brief Calculate the mean of std::vector<NumericType>
91  * @tparam NumericType
92  * @param v
93  * @return NumericType
94  */
95  template<Numeric NumericType>
96  NumericType getVMean(const std::vector<NumericType>& v){
97  return std::accumulate(v.begin(), v.end(), 0)/v.size();
98 // NumericType total = 0;
99 // int size = v.size();
100 // std::accumulate(v.begin(), v.end(), 0)/v.size();
101 //
102 // for (vector<double>::iterator it = v.begin(); it != v.end(); ++it)
103 // {
104 // total += *it;
105 // }
106 //
107 // return total/size;
108  }
109 }
110 
111 }
112 
113 #endif //COSAN_STATISTICS_H
Cosan
Definition: CosanBO.h:29
Cosan::SmallRows
Cosan::TooSmallSizeException SmallRows
Cosan::SummaryStatistics::GetMean
NumericType GetMean(const CosanMatrix< NumericType > &X)
Definition: statistics.h:32
Cosan::SummaryStatistics::GetVar
NumericType GetVar(const CosanMatrix< NumericType > &X)
Definition: statistics.h:39
Cosan::SummaryStatistics::variance
NumericType variance
Definition: statistics.h:81
NumericType
double NumericType
Definition: onehotencodingTest.cpp:20
Cosan::SummaryStatistics::median
NumericType median
Definition: statistics.h:82
Cosan::SummaryStatistics::GetMin
NumericType GetMin(const CosanMatrix< NumericType > &X)
Definition: statistics.h:71
Cosan::Evaluation
Definition: evaluation.h:11
Cosan::getVMean
NumericType getVMean(const std::vector< NumericType > &v)
Calculate the mean of std::vector<NumericType>
Definition: statistics.h:96
Cosan::CosanMatrix
Eigen::Matrix< NumericType, Eigen::Dynamic, Eigen::Dynamic > CosanMatrix
Definition: CosanBO.h:37
Cosan::SummaryStatistics::mean
NumericType mean
Definition: statistics.h:80
Cosan::SummaryStatistics::SummaryStatistics
SummaryStatistics()
Definition: statistics.h:23
Cosan::SummaryStatistics::GetMedian
NumericType GetMedian(const CosanMatrix< NumericType > &X)
Definition: statistics.h:47
Cosan::SummaryStatistics::SampleCovariance
CosanMatrix< NumericType > SampleCovariance
Definition: statistics.h:79
Cosan::SummaryStatistics
Class for statistics results.
Definition: statistics.h:21
Cosan::SummaryStatistics::GetMax
NumericType GetMax(const CosanMatrix< NumericType > &X)
Definition: statistics.h:64
Cosan::SummaryStatistics::GetSampleCovariance
CosanMatrix< NumericType > & GetSampleCovariance(const CosanMatrix< NumericType > &X)
Definition: statistics.h:24
Cosan::SummaryStatistics::maxNum
NumericType maxNum
Definition: statistics.h:83
Cosan::SummaryStatistics::minNum
NumericType minNum
Definition: statistics.h:84