Pearson Correlation Coefficient calculation in C++

1
2
#include <cmath>
#include <cstdio>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2 Responses

1) Passing vectors by const reference would be more efficient, i.e. use const vector<double>& instead of vector<double> as arguments. You will avoid copying the entire vector that way.
2) push_back() might be expensive because it causes automatic reallocation when vector size surpasses its capacity. You may construct retvect using the input, i.e. vector<double> retvect(a). It will make retvect the same size as a and also will initialize it with the same values.
3) Avoid duplicate function calls. You call sum() both in mean() and stdev() functions, i. e. pearsoncoeff calls sum() twice for both X and Y.
1) Passing vectors by const reference would be more efficient, i.e. use const vector<double>& instead of vector<double> as arguments. You will avoid copying the entire vector that way.
2) push_back() might be expensive because it causes automatic reallocation when vector size surpasses its capacity. You may construct retvect using the input, i.e. vector<double> retvect(a). It will make retvect the same size as a and also will initialize it with the same values.
3) Avoid duplicate function calls. You call sum() both in mean() and stdev() functions, i. e. pearsoncoeff calls sum() twice for both X and Y.

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.