|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017, 2018 Alexey Kopytko <[email protected]> |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Pipeline\Helper; |
| 21 | + |
| 22 | +use function sqrt; |
| 23 | + |
| 24 | +/** |
| 25 | + * Computes statistics (such as standard deviation) in real time. |
| 26 | + * |
| 27 | + * @see https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm |
| 28 | + * |
| 29 | + * @final |
| 30 | + */ |
| 31 | +class RunningVariance |
| 32 | +{ |
| 33 | + /** |
| 34 | + * The number of observed values. |
| 35 | + * |
| 36 | + * @var int<0, max> |
| 37 | + */ |
| 38 | + private int $count = 0; |
| 39 | + |
| 40 | + /** First moment: the mean value. */ |
| 41 | + private float $mean = 0.0; |
| 42 | + |
| 43 | + /** Second moment: the aggregated squared distance from the mean. */ |
| 44 | + private float $m2 = 0.0; |
| 45 | + |
| 46 | + public function __construct(self ...$spiesToMerge) |
| 47 | + { |
| 48 | + foreach ($spiesToMerge as $spy) { |
| 49 | + $this->merge($spy); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function observe(float $value): float |
| 54 | + { |
| 55 | + ++$this->count; |
| 56 | + |
| 57 | + $delta = $value - $this->mean; |
| 58 | + |
| 59 | + $this->mean += $delta / $this->count; |
| 60 | + $this->m2 += $delta * ($value - $this->mean); |
| 61 | + |
| 62 | + return $value; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * The number of observed values. |
| 67 | + */ |
| 68 | + public function getCount(): int |
| 69 | + { |
| 70 | + return $this->count; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get the mean value. |
| 75 | + */ |
| 76 | + public function getMean(): float |
| 77 | + { |
| 78 | + if (0 === $this->count) { |
| 79 | + // For no values the variance is undefined. |
| 80 | + return NAN; |
| 81 | + } |
| 82 | + |
| 83 | + return $this->mean; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get the variance. |
| 88 | + */ |
| 89 | + public function getVariance(): float |
| 90 | + { |
| 91 | + if (0 === $this->count) { |
| 92 | + // For no values the variance is undefined. |
| 93 | + return NAN; |
| 94 | + } |
| 95 | + |
| 96 | + if (1 === $this->count) { |
| 97 | + // Avoiding division by zero: variance for one value is zero. |
| 98 | + return 0.0; |
| 99 | + } |
| 100 | + |
| 101 | + // https://en.wikipedia.org/wiki/Bessel%27s_correction |
| 102 | + return $this->m2 / ($this->count - 1); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Compute the standard deviation. |
| 107 | + */ |
| 108 | + public function getStandardDeviation(): float |
| 109 | + { |
| 110 | + return sqrt($this->getVariance()); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Merge another instance into this instance. |
| 115 | + * |
| 116 | + * @see https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm |
| 117 | + */ |
| 118 | + private function merge(self $other): void |
| 119 | + { |
| 120 | + // Shortcut a no-op |
| 121 | + if (0 === $other->count) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // Avoid division by zero by copying values |
| 126 | + if (0 === $this->count) { |
| 127 | + $this->count = $other->count; |
| 128 | + $this->mean = $other->mean; |
| 129 | + $this->m2 = $other->m2; |
| 130 | + |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + $count = $this->count + $other->count; |
| 135 | + $delta = $other->mean - $this->mean; |
| 136 | + |
| 137 | + $this->mean = ($this->count * $this->mean) / $count + ($other->count * $other->mean) / $count; |
| 138 | + $this->m2 = $this->m2 + $other->m2 + ($delta ** 2 * $this->count * $other->count / $count); |
| 139 | + $this->count = $count; |
| 140 | + } |
| 141 | +} |
0 commit comments