| 1 |
<?php |
| 2 |
/** |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
* http://www.gnu.org/copyleft/gpl.html |
| 17 |
* |
| 18 |
* @file |
| 19 |
* @author Ori Livneh <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Wikimedia; |
| 23 |
|
| 24 |
use Serializable; |
| 25 |
|
| 26 |
/** |
| 27 |
* Represents a running, online estimate of a p-quantile for a series |
| 28 |
* of observations using the P-squared algorithm. |
| 29 |
* |
| 30 |
* The algorithm is from "The P-Square Algorithm for Dynamic Calculation of |
| 31 |
* Percentiles and Histograms without Storing Observations," Communications of |
| 32 |
* the ACM, October 1985 by R. Jain and I. Chlamtac. |
| 33 |
*/ |
| 34 |
class PSquare implements Serializable { |
| 35 |
/** |
| 36 |
* Percentile to estimate. |
| 37 |
* @var float $p |
| 38 |
*/ |
| 39 |
private $p; |
| 40 |
|
| 41 |
/** |
| 42 |
* Position of each marker. |
| 43 |
* @var int[] $positions |
| 44 |
*/ |
| 45 |
private $positions; |
| 46 |
|
| 47 |
/** |
| 48 |
* Desired position of each marker. |
| 49 |
* @var float[] $desired |
| 50 |
*/ |
| 51 |
private $desired; |
| 52 |
|
| 53 |
/** @var float[] $increments */ |
| 54 |
private $increments; |
| 55 |
|
| 56 |
/** |
| 57 |
* Number of observations. |
| 58 |
* @var int $numObservations |
| 59 |
*/ |
| 60 |
private $numObservations = 0; |
| 61 |
|
| 62 |
/** |
| 63 |
* Height of each marker. |
| 64 |
* @var float[] $heights |
| 65 |
*/ |
| 66 |
private $heights = []; |
| 67 |
|
| 68 |
/** |
| 69 |
* @param float $p the percentile (defaults to 0.5, or median). |
| 70 |
*/ |
| 71 |
public function __construct( $p = 0.5 ) { |
| 72 |
$this->p = $p; |
| 73 |
$this->positions = [ 0, 1, 2, 3, 4 ]; |
| 74 |
$this->desired = [ 0, ( 2 * $p ), ( 4 * $p ), 2 + ( 2 * $p ), 4 ]; |
| 75 |
$this->increments = [ 0, ( $p / 2 ), $p, ( ( 1 + $p ) / 2 ), 1 ]; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Export state, e.g. for serializing and caching. |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function __serialize(): array { |
| 84 |
return [ |
| 85 |
'percentile' => $this->p, |
| 86 |
'positions' => $this->positions, |
| 87 |
'desired' => $this->desired, |
| 88 |
'increments' => $this->increments, |
| 89 |
'numObservations' => $this->numObservations, |
| 90 |
'heights' => $this->heights, |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param array $data |
| 96 |
*/ |
| 97 |
public function __unserialize( array $data ): void { |
| 98 |
$this->p = $data['percentile']; |
| 99 |
$this->positions = $data['positions']; |
| 100 |
$this->desired = $data['desired']; |
| 101 |
$this->increments = $data['increments']; |
| 102 |
$this->numObservations = $data['numObservations']; |
| 103 |
$this->heights = $data['heights']; |
| 104 |
} |
| 105 |
|
| 106 |
public function serialize() { |
| 107 |
// TODO: Remove once PHP 7.4+ is required |
| 108 |
return serialize( $this->__serialize() ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @param string $serialized |
| 113 |
*/ |
| 114 |
public function unserialize( $serialized ): void { |
| 115 |
// TODO: Remove once PHP 7.4+ is required |
| 116 |
$this->__unserialize( unserialize( $serialized ) ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get the total number of accumulated observations. |
| 121 |
* |
| 122 |
* @return int |
| 123 |
*/ |
| 124 |
public function getCount() { |
| 125 |
return $this->numObservations; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Add an observation. |
| 130 |
* |
| 131 |
* @param int|float $x Value to add |
| 132 |
*/ |
| 133 |
public function addObservation( $x ) { |
| 134 |
$this->numObservations++; |
| 135 |
|
| 136 |
if ( $this->numObservations <= 5 ) { |
| 137 |
$this->heights[] = $x; |
| 138 |
if ( $this->numObservations === 5 ) { |
| 139 |
sort( $this->heights ); |
| 140 |
} |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if ( $x < $this->heights[0] ) { |
| 145 |
$this->heights[0] = $x; |
| 146 |
$k = 0; |
| 147 |
} elseif ( $x >= $this->heights[4] ) { |
| 148 |
$this->heights[4] = $x; |
| 149 |
$k = 3; |
| 150 |
} else { |
| 151 |
for ( $i = 1; $i < 5; $i++ ) { |
| 152 |
if ( $x < $this->heights[$i] ) { |
| 153 |
$k = $i - 1; |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable |
| 160 |
for ( $i = $k + 1; $i < 5; $i++ ) { |
| 161 |
$this->positions[$i]++; |
| 162 |
} |
| 163 |
|
| 164 |
for ( $i = 0; $i < 5; $i++ ) { |
| 165 |
$this->desired[$i] += $this->increments[$i]; |
| 166 |
} |
| 167 |
|
| 168 |
for ( $i = 1; $i < 4; $i++ ) { |
| 169 |
$n = $this->positions[$i]; |
| 170 |
$nPrev = $this->positions[$i - 1]; |
| 171 |
$nNext = $this->positions[$i + 1]; |
| 172 |
|
| 173 |
$d = $this->desired[$i] - $n; |
| 174 |
|
| 175 |
if ( ( $d >= 1 && $nNext - $n > 1 ) || ( $d <= -1 && $nPrev - $n < -1 ) ) { |
| 176 |
$d = ( $d < 0 ) ? -1 : 1; |
| 177 |
|
| 178 |
$q = $this->computeParabolic( $i, $d ); |
| 179 |
$qPrev = $this->heights[$i - 1]; |
| 180 |
$qNext = $this->heights[$i + 1]; |
| 181 |
|
| 182 |
if ( $qPrev < $q && $q < $qNext ) { |
| 183 |
$this->heights[$i] = $q; |
| 184 |
} else { |
| 185 |
$this->heights[$i] = $this->computeLinear( $i, $d ); |
| 186 |
} |
| 187 |
|
| 188 |
$this->positions[$i] += $d; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Use piecewise parabolic prediction to predict the ideal |
| 195 |
* height of a marker. |
| 196 |
* |
| 197 |
* @param int $i index of marker to adjust |
| 198 |
* @param int $d always -1 or 1 |
| 199 |
* @return float ideal height of marker |
| 200 |
*/ |
| 201 |
private function computeParabolic( $i, $d ) { |
| 202 |
$q = $this->heights[$i]; |
| 203 |
$qPrev = $this->heights[$i - 1]; |
| 204 |
$qNext = $this->heights[$i + 1]; |
| 205 |
|
| 206 |
$n = $this->positions[$i]; |
| 207 |
$nPrev = $this->positions[$i - 1]; |
| 208 |
$nNext = $this->positions[$i + 1]; |
| 209 |
|
| 210 |
return ( $q + |
| 211 |
$d / ( $nNext - $nPrev ) * |
| 212 |
( |
| 213 |
( $n - $nPrev + $d ) * ( $qNext - $q ) / ( $nNext - $n ) + |
| 214 |
( $nNext - $n - $d ) * ( $q - $qPrev ) / ( $n - $nPrev ) |
| 215 |
) |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Linear formula to predict ideal position of a marker. |
| 221 |
* |
| 222 |
* @param int $i index of marker to adjust |
| 223 |
* @param int $d always -1 or 1 |
| 224 |
* @return float ideal height of marker |
| 225 |
*/ |
| 226 |
private function computeLinear( $i, $d ) { |
| 227 |
$q = $this->heights[$i]; |
| 228 |
$n = $this->positions[$i]; |
| 229 |
return ( $q + $d * |
| 230 |
( $this->heights[$i + $d] - $q ) / |
| 231 |
( $this->positions[$i + $d] - $n ) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get the estimated p-quantile value. |
| 237 |
* |
| 238 |
* @return float |
| 239 |
*/ |
| 240 |
public function getValue() { |
| 241 |
// If we have five samples or fewer, fall back to a naive method. |
| 242 |
if ( $this->getCount() <= 5 ) { |
| 243 |
sort( $this->heights ); |
| 244 |
$i = $this->p * count( $this->heights ); |
| 245 |
if ( $i === floor( $i ) ) { |
| 246 |
return ( $this->heights[$i - 1] + $this->heights[$i] ) / 2; |
| 247 |
} else { |
| 248 |
return $this->heights[floor( $i )]; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return $this->heights[2]; |
| 253 |
} |
| 254 |
} |