function SparseVector::correlation in Recommender API 7.6
Same name and namespace in other branches
- 6.2 Matrix.php \SparseVector::correlation()
Compute correlation with $vector. No caching option. Works for RealVector. SparseVector needs additional handling.
Parameters
$vector it has to be the same type (either SparseVector or RealVector) as $this:
Return value
correlation value
Overrides Vector::correlation
File
- classes/
Matrix.php, line 324
Class
- SparseVector
- Sparse Vector takes care of missing data.
Code
function correlation(&$vector) {
$subset = $this
->common_items($vector);
if ($subset === NULL) {
return NAN;
}
$covariance = $subset[0]
->covariance($subset[1]);
$std_a = $subset[0]
->std(TRUE);
$std_b = $subset[1]
->std(TRUE);
return $std_a == 0 || $std_b == 0 ? NAN : $covariance / ($std_a * $std_b);
}