PHP - Stats Stat Correlation() Function
Definition and Usage
The stats_stat_correlation() function can return the Pearson correlation coefficient of two data sets.
Syntax
float stats_stat_correlation( array $arr1, array $arr2 )
Parameters
| Sr.No | Parameter | Description |
|---|---|---|
| 1 | arr1 |
The first array |
| 2 | arr2 |
The second array |
Return Values
The stats_stat_correlation() function can return the Pearson correlation coefficient between arr1 and arr2, or false on failure.
Dependencies
This function was first introduced in statistics extension (PHP 4.0.0 and PEAR 1.4.0). We have used latest release of stats-2.0.3 (PHP 7.0.0 or newer and PEAR 1.4.0 or newer) for this tutorial
Example
In the following example, we compute the Pearson correlation coefficient of two data sets.
<?php var_dump(stats_stat_correlation(array(1, 2, 3), array(1, 2, 3))); ?>
Output
This will produce following result −
float(1)
Example
In the following example, we compute the Pearson correlation coefficient of two data sets.
<?php var_dump(stats_stat_correlation(array(1, 2, 3), array(1, 2, 1))); ?>
Output
This will produce following result −
float(0)
Example
Following is an error case. In the following example, we pass two arrays with unequal length. A warning is displayed in logs.
<?php // error cases var_dump(stats_stat_correlation(array(1, 2, 3), array(1, 2, 3, 4))); ?>
Output
This will produce following result and a warning in logs PHP Warning: stats_stat_correlation(): Unequal number of X and Y coordinates
bool(false)