Calculates the Pearson product for a given set of data.

View versions (1)

Interface

#include <codecogs/statistics/moments/pearson.h>

using namespace Statistics::Moments;

Returns the Pearson product moment correlation coefficient, r, a dimensionless index that ranges from -1.0 to 1.0 inclusive and reflects the extent of a linear relationship between two data sets.

The formula for the Pearson product moment correlation coefficient, r, is:

r=\frac{\sum_{i=1}^N(x-\overline{x})(y-\overline{y})}{\sqrt{\sum_{i=1}^N(x-\overline{x})^2\sum{(y-\overline{y})^2}}}
(1)

where

  • \overline{x} and \overline{y} are the sample mean.

Example 1

#include <codecogs/statistics/moments/pearson.h>
#include <iostream>
int main()
{
  double x[5] = {4 , 5 , 8 , 6 , 3};
  double y[5] = {6 , 7 , 8 , 3 , 2};
  double rez = Stats::Moments::pearson<double>(5,x,y);
  std::cout << "The Pearson product is: " << rez << std::endl;
  return 0;
}

Output:

The Pearson product is: 0.592494

Parameters

N
the size of the array
data
is a set of independent values given as an array
data1
is a set of dependent values given as an array

Returns

returns the Pearson product moment correlation coefficient
GPL Licence — free for non commercial use. See Licence details.