neongm

coefficent_of_correlation_calc

Feb 27th, 2021
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include <vector>
  2. #include <numeric>
  3.  
  4. struct benchmark_result
  5. {
  6. private:
  7.     size_t elements_amount;
  8.     size_t time;
  9.     unsigned long long elements_amount_squared;
  10.     size_t elements_amount_x_time;
  11. public:
  12.     benchmark_result()
  13.     {
  14.         elements_amount = 0;
  15.         time = 0;
  16.         elements_amount_squared = 0;
  17.         elements_amount_x_time = 0;
  18.     }
  19.     benchmark_result(size_t _elements_amount, size_t _time)
  20.     {
  21.         elements_amount = _elements_amount;
  22.         time = _time;
  23.         elements_amount_squared = pow(_elements_amount, 2);
  24.         elements_amount_x_time = _elements_amount * _time;
  25.     }
  26.     size_t get_elements_amount() { return elements_amount; }
  27.     size_t get_time() { return time; }
  28.     unsigned long long get_amount_squared() { return elements_amount_squared; }
  29.     size_t get_amount_x_time() { return elements_amount_x_time; }
  30. };
  31.  
  32. long double get_mid(const std::vector<size_t>& arr_x, const std::vector<size_t>& arr_y)
  33. {
  34.     unsigned long long count = 0;
  35.     for (size_t i = 0; i < arr_x.size(); i++) count += arr_x.at(i) * arr_y.at(i);
  36.     return count / arr_x.size();
  37. }
  38.  
  39. long double get_mid_sq(const std::vector<size_t>& arr)
  40. {
  41.     unsigned long long count = 0;
  42.     for (size_t i = 0; i < arr.size(); i++) count += pow(arr.at(i), 2);
  43.     return count / arr.size();
  44. }
  45.  
  46. namespace analysis
  47. {
  48.     class calculate
  49.     {
  50.     private:
  51.         std::vector<size_t> values_x;
  52.         std::vector<size_t> values_y;
  53.         long double mid_x;
  54.         long double mid_y;
  55.         long double mid_xy;
  56.         long double dispersion_x;
  57.         long double dispersion_y;
  58.         long double mid_quad_deviation_x;
  59.         long double mid_quad_deviation_y;
  60.         long double coefficent_of_correlation;
  61.  
  62.     public:
  63.            
  64.         calculate();
  65.         calculate(const std::vector<size_t>& arr_x, const std::vector<size_t>& arr_y);
  66.         void set_arrays(const std::vector<size_t>& arr_x, const std::vector<size_t>& arr_y);
  67.         void calc_mid();
  68.         void calc_dispersion();
  69.         void calc_mid_quad_deviation();
  70.         void calc_coefficent_of_correlation();
  71.  
  72.         long double get_coefficent_of_correlation() { return coefficent_of_correlation; }
  73.         long double get_mid_quad_deviation_x() { return mid_quad_deviation_x; }
  74.         long double get_mid_quad_deviation_y() { return mid_quad_deviation_y; }
  75.         long double get_dispersion_x() { return dispersion_x; }
  76.         long double get_dispersion_y() { return dispersion_y; }
  77.         long double get_mid_x() { return mid_x; }
  78.         long double get_mid_y() { return mid_y; }
  79.         long double get_mid_xy() { return mid_xy; }
  80.         std::vector<size_t> get_values_x() { return values_x; }
  81.         std::vector<size_t> get_values_y() { return values_y; }
  82.     };
  83.  
  84.     calculate::calculate(const std::vector<size_t>& arr_x, const std::vector<size_t>& arr_y)
  85.     {
  86.         values_x = arr_x;
  87.         values_y = arr_y;
  88.     }
  89.  
  90.     void calculate::set_arrays(const std::vector<size_t>& arr_x, const std::vector<size_t>& arr_y)
  91.     {
  92.         values_x = arr_x;
  93.         values_y = arr_y;
  94.     }
  95.  
  96.     void calculate::calc_mid()
  97.     {
  98.         mid_x = std::accumulate(values_x.begin(), values_x.end(), 0) / values_x.size();
  99.         mid_y = std::accumulate(values_y.begin(), values_y.end(), 0) / values_y.size();
  100.         mid_xy = get_mid(values_x, values_y);
  101.     }
  102.  
  103.     void calculate::calc_dispersion()
  104.     {
  105.         dispersion_x = get_mid_sq(values_x) - pow(mid_x, 2);
  106.         dispersion_y = get_mid_sq(values_y) - pow(mid_y, 2);
  107.     }
  108.  
  109.     void calculate::calc_mid_quad_deviation()
  110.     {
  111.         mid_quad_deviation_x = pow(dispersion_x, 0.5);
  112.         mid_quad_deviation_y = pow(dispersion_y, 0.5);
  113.     }
  114.  
  115.     void calculate::calc_coefficent_of_correlation()
  116.     {
  117.         coefficent_of_correlation = (mid_xy - mid_x * mid_y) / (mid_quad_deviation_x * mid_quad_deviation_y);
  118.     }
  119.  
  120.     calculate call_all_methods (const std::vector<size_t>& arr_x, const std::vector<size_t>& arr_y)
  121.     {
  122.         calculate calculator = calculate(arr_x, arr_y);
  123.         calculator.calc_mid();
  124.         calculator.calc_dispersion();
  125.         calculator.calc_mid_quad_deviation();
  126.         calculator.calc_coefficent_of_correlation();
  127.         return calculator;
  128.     }
  129.  
  130.     calculate do_calculations(const std::vector<benchmark_result>& arr)
  131.     {
  132.         std::vector<size_t> values_x;
  133.         std::vector<size_t> values_y;
  134.         for (auto el : arr) {
  135.             values_x.push_back(el.get_elements_amount());
  136.             values_y.push_back(el.get_time());
  137.         }
  138.         calculate result = call_all_methods(values_x, values_y);
  139.         return result;
  140.     }
  141. }  
  142.  
  143.  
Advertisement
Add Comment
Please, Sign In to add comment