-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
156 lines (132 loc) · 3.3 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
class spearman{
function __construct(){
}
function rank($array){
$temp=$array;
rsort($temp);
$rank=array();
$frequency=array_count_values($temp);
foreach($array as $v){
$key=array_search($v, $temp);
$key++;
if($frequency[$v]>1){
$rank[]=array_sum(range($key,$key+$frequency[$v]-1))/$frequency[$v];
}
else{
$rank[]=$key;
}
}
return $rank;
}
function calc($array1,$array2){
$d_square=array_map(
function ($x, $y) { return pow(($y-$x),2); },
$this->rank($array1),
$this->rank($array2)
);
$n=count($array1);
$result=1-(6*(array_sum($d_square))/($n*(pow($n,2)-1)));
return $result;
}
function Median($Array) {
sort($Array);
return $this->Quartile_50($Array);
}
function Quartile_25($Array) {
sort($Array);
return $this->Quartile($Array, 0.25);
}
function Quartile_50($Array) {
sort($Array);
return $this->Quartile($Array, 0.5);
}
function Quartile_75($Array) {
sort($Array);
return $this->Quartile($Array, 0.75);
}
function IQR($Array) {
sort($Array);
$q1 = $this->Quartile($Array, 0.25);
$q3 = $this->Quartile($Array, 0.75);
$iqr = ($q3 - $q1) ;
return $iqr;
}
function LOB($Array) {
sort($Array);
$iqr = $this->IQR($Array);
$q1 = $this->Quartile($Array, 0.25);
$lob = $q1 - (1.5*$iqr);
return $lob;
}
function UOB($Array) {
sort($Array);
$iqr = $this->IQR($Array);
$q3 = $this->Quartile($Array, 0.75);
$uob = $q3 + (1.5*$iqr);
return $uob;
}
function Outlier($Array) {
sort($Array);
$lob = $this->LOB($Array);
$uob = $this->UOB($Array);
$outliers = [];
foreach ($Array as $item) {
if($item < $lob || $item > $uob){
array_push($outliers, $item);
}
}
return $outliers;
}
function Quartile($Array, $Quartile) {
$quantElement = count($Array);
$position = floor($quantElement * $Quartile);
if(($quantElement%2)==0){
if(isset($Array[$position+1]) and $position!=0) {
return ($Array[$position-1] + $Array[$position])/2;
} else {
return $Array[$position];
}
} else {
return $Array[$position];
}
}
function Average($Array) {
return array_sum($Array) / count($Array);
}
function StdDev($Array) {
if( count($Array) < 2 ) {
return;
}
$avg = $this->Average($Array);
$sum = 0;
foreach($Array as $value) {
$sum += pow($value - $avg, 2);
}
return sqrt((1 / (count($Array) - 1)) * $sum);
}
}
$array1 =[56,75,45,71,61,64,58,80,76,61]; //input example
$array2 =[66,70,40,60,65,56,59,77,67,63]; //input example
$array3 =[20, 20, 21, 21, 21, 21, 22, 22, 22, 23, 24, 25, 26, 150, 200]; //input example
$spearmancalc = new spearman();
echo "Spearman: ";
echo $spearmancalc->calc($array1,$array2);
echo "<br/> Quartile 1: ";
echo $spearmancalc->Quartile_25($array3);
echo "<br/> Medium: ";
echo $spearmancalc->Median($array3);
echo "<br/> Quartile 3: ";
echo $spearmancalc->Quartile_75($array3);
echo "<br/> Interquartile range (IQR): ";
echo $spearmancalc->IQR($array3);
echo "<br/> Lower outlier bounds (LOB): ";
echo $spearmancalc->LOB($array3);
echo "<br/> Upper outlier bounds (UOB): ";
echo $spearmancalc->UOB($array3);
echo "<br/> Outliers: ";
print_r($spearmancalc->Outlier($array3));
echo "<br/> Average: ";
print_r($spearmancalc->Average($array3));
echo "<br/> Standard deviation: ";
print_r($spearmancalc->StdDev($array3));