Pearson Similarity

The Pearson correlation coefficient is a measure of linear correlation between two sets of data. It is the ratio between the covariance of two variables and the product of their standard deviations. The formula for calculating the Pearson correlation coefficient is as follows:

\[\rho _{X,Y}={\frac {\operatorname {cov} (X,Y)}{\sigma _{X}\sigma _{Y}}}\]

The algorithm takes two vectors denoted by ListAccum and returns the overlap coefficient between them.

Notes

This algorithm is implemented as a user-defined function. Follow the steps in Add a User-Defined Function to add the function to GSQL. After adding the function, you can call it in any GSQL query in the same way as a built-in GSQL function.

Specifications

pearson_similarity_accum(A, B)

Parameters

Name Description Data type

A

An n-dimensional vector denoted by a ListAccum of length n

ListAccum<INT/UINT/FLOAT/DOUBLE>

B

An n-dimensional vector denoted by a ListAccum of length n

ListAccum<INT/UINT/FLOAT/DOUBLE>

Output

The Pearson correlation coefficient between the two vectors.

Time complexity

The algorithm has a complexity of \(O(n)\), where \(n\) is the number of dimensions of the vectors.

Example

  • Query

  • Result

CREATE QUERY pearson_example() FOR GRAPH social {
  ListAccum<INT> @@a = [1, 2, 3];
  ListAccum<INT> @@b = [2, 2, 3];
  double pearson_similarity = tg_pearson_similarity_accum(@@a, @@b);
  PRINT pearson_similarity;
}
{
    "pearson_similarity": 0.86603
}