5 #ifndef COSAN_STATISTICS_H
6 #define COSAN_STATISTICS_H
7 #include <cosan/evaluation.h>
20 template<Numeric NumericType>
26 fmt::print(
"Input must have more than 1 row!")
33 if (X.rows()>1 && X.cols()>1){
34 fmt::print(
"Mean is not defined in matrix! Calculate the mean of all entries!");
40 if (X.rows()>1 && X.cols()>1){
41 fmt::print(
"Variance is not defined in matrix! Calculate the variance of all entries!");
43 variance = std::sqrt((X.array() - X.mean()).square().sum()/(X.size()-1));
48 if (X.rows()>1 && X.cols()>1){
49 fmt::print(
"Median is not defined in matrix! Calculate the median of all entries!");
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;
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);
65 if (X.rows()>1 && X.cols()>1){
66 fmt::print(
"Variance is not defined in matrix! Calculate the minimum of all entries!");
72 if (X.rows()>1 && X.cols()>1){
73 fmt::print(
"Variance is not defined in matrix! Calculate the minimum of all entries!");
95 template<Numeric NumericType>
97 return std::accumulate(v.begin(), v.end(), 0)/v.size();
113 #endif //COSAN_STATISTICS_H