function SparseVector::correlation in Recommender API 6.2
Same name and namespace in other branches
- 7.6 classes/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
- ./
Matrix.php, line 328
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);
}