-
Notifications
You must be signed in to change notification settings - Fork 0
/
2011_1_Covariance.php
87 lines (73 loc) · 1.44 KB
/
2011_1_Covariance.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
//Functions
function mean($a) {
return array_sum($a) / count($a);
}
function std_dev($a,$meana) {
$final = array();
for ($i=0; $i<count($a); $i++) {
$tmp = $a[$i] - $meana;
$final[] = $tmp * $tmp;
}
$tmp = array_sum($final) / count($a);
return sqrt($tmp);
}
//Var Init
$X = array();
$Y = array();
$xtmp = 0;
$ytmp = 0;
// GET INPUT
for ($i = 0; $i>-1; $i++) {
//Read from STDIN
fscanf(STDIN, "%s %s\n", $xtmp, $ytmp);
//Check if valid input
if ( is_numeric($xtmp) && is_numeric($ytmp) ) {
$X[] = $xtmp - 0;
$Y[] = $ytmp - 0;
}
//Check if end of input
elseif ($xtmp == '-') {
break;
}
// Invalid input
else {
echo 'invalid input';
exit();
}
}
//------------------------------------------------
//Caclucating
//Means
$meanx = mean($X);
$meany = mean($Y);
//Standard Deviation X
$stdx = std_dev($X,$meanx);
$stdy = std_dev($Y,$meany);
//Covariance X,Y
$C = array();
for ($i=0; $i<count($X); $i++) {
$xtmp = $X[$i] - $meanx;
$ytmp = $Y[$i] - $meany;
$C[] = $xtmp * $ytmp;
}
$covar = mean($C);
//Pearson's (final)
$pears = $covar / ($stdx * $stdy);
/*
echo "\nMean X:$meanx";
echo "\nMean Y:$meany";
echo "\nStd Dev X:$stdx";
echo "\nStd Dev Y:$stdy";
echo "\nCovariance:$covar";
echo "\n\nPearson's Correlation Coefficient:$pears";
echo "\n";
*/
if ( ($pears>1) || ($pears<-1) ) {
echo 'invalid input';
} elseif ($stdx == 0 || $stdy == 0) {
echo 'invalid input';
} else {
echo number_format($pears, 4, '.', '');
}
?>