Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <stdio.h>
- #include <vector>
- #include <numeric> // for std::accumulate()
- #include <cmath> // for pow()
- double coefficent_of_correlation(const std::vector<size_t>& values_x, const std::vector<size_t>& values_y)
- {
- size_t mid_x = std::accumulate(values_x.begin(), values_x.end(), 0) / values_x.size();
- size_t mid_y = std::accumulate(values_y.begin(), values_y.end(), 0) / values_y.size();
- std::vector<long long int> xi_minus_mid_x;
- for (auto el : values_x) xi_minus_mid_x.push_back(el - mid_x);
- std::vector<long long int> xi_minus_mid_y;
- for (auto el : values_y) xi_minus_mid_y.push_back(el - mid_y);
- int long long upper_part = 0;
- for (size_t i = 0; i < xi_minus_mid_x.size(); i++) upper_part += xi_minus_mid_x.at(i) * xi_minus_mid_y.at(i);
- unsigned long long x_sum = 0;
- for (auto el : xi_minus_mid_x) x_sum += pow(el, 2);
- unsigned long long y_sum = 0;
- for (auto el : xi_minus_mid_y) y_sum += pow(el, 2);
- int long long lower_part = pow(x_sum * y_sum, 0.5);
- return (double)upper_part / lower_part;
- }
- int main()
- {
- std::vector<size_t> x = {1, 2, 3, 4};
- std::vector<size_t> y = {3, 4, 1, 2};
- std::cout << coefficent_of_correlation(x, y);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement