MediaWiki  1.23.2
RunningStat.php
Go to the documentation of this file.
1 <?php
24 // Needed due to PHP non-bug <https://bugs.php.net/bug.php?id=49828>.
25 define( 'NEGATIVE_INF', -INF );
26 
52 class RunningStat implements Countable {
53 
55  public $n = 0;
56 
58  public $m1 = 0.0;
59 
61  public $m2 = 0.0;
62 
64  public $min = INF;
65 
67  public $max = NEGATIVE_INF;
68 
73  public function count() {
74  return $this->n;
75  }
76 
81  public function push( $x ) {
82  $x = (float) $x;
83 
84  $this->min = min( $this->min, $x );
85  $this->max = max( $this->max, $x );
86 
87  $n1 = $this->n;
88  $this->n += 1;
89  $delta = $x - $this->m1;
90  $delta_n = $delta / $this->n;
91  $this->m1 += $delta_n;
92  $this->m2 += $delta * $delta_n * $n1;
93  }
94 
103  public function getMean() {
104  return $this->m1;
105  }
106 
117  public function getVariance() {
118  if ( $this->n === 0 ) {
119  // The variance of the empty set is undefined.
120  return NAN;
121  } elseif ( $this->n === 1 ) {
122  return 0.0;
123  } else {
124  return $this->m2 / ( $this->n - 1.0 );
125  }
126  }
127 
138  public function getStdDev() {
139  return sqrt( $this->getVariance() );
140  }
141 
150  public function merge( RunningStat $other ) {
151  // If the other RunningStat is empty, there's nothing to do.
152  if ( $other->n === 0 ) {
153  return;
154  }
155 
156  // If this RunningStat is empty, copy values from other RunningStat.
157  if ( $this->n === 0 ) {
158  $this->n = $other->n;
159  $this->m1 = $other->m1;
160  $this->m2 = $other->m2;
161  $this->min = $other->min;
162  $this->max = $other->max;
163  return;
164  }
165 
166  $n = $this->n + $other->n;
167  $delta = $other->m1 - $this->m1;
168  $delta2 = $delta * $delta;
169 
170  $this->m1 = ( ( $this->n * $this->m1 ) + ( $other->n * $other->m1 ) ) / $n;
171  $this->m2 = $this->m2 + $other->m2 + ( $delta2 * $this->n * $other->n / $n );
172  $this->min = min( $this->min, $other->min );
173  $this->max = max( $this->max, $other->max );
174  $this->n = $n;
175  }
176 }
RunningStat\getStdDev
getStdDev()
Get the estimated stanard deviation.
Definition: RunningStat.php:133
RunningStat\$m2
float $m2
The second central moment (or variance).
Definition: RunningStat.php:58
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
NEGATIVE_INF
const NEGATIVE_INF
Definition: RunningStat.php:25
RunningStat\$m1
float $m1
The first moment (or mean, or expected value).
Definition: RunningStat.php:56
RunningStat\count
count()
Count the number of accumulated values.
Definition: RunningStat.php:68
n
if(! $in) print Initializing normalization quick check tables n
Definition: UtfNormalGenerate.php:36
RunningStat\$min
float $min
The least value in the the set.
Definition: RunningStat.php:60
RunningStat\merge
merge(RunningStat $other)
Merge another RunningStat instance into this instance.
Definition: RunningStat.php:145
RunningStat\getMean
getMean()
Get the mean, or expected value.
Definition: RunningStat.php:98
RunningStat
Represents a running summary of a stream of numbers.
Definition: RunningStat.php:52
RunningStat\$n
int $n
Number of samples.
Definition: RunningStat.php:54
RunningStat\$max
float $max
The most value in the set.
Definition: RunningStat.php:62
RunningStat\getVariance
getVariance()
Get the estimated variance.
Definition: RunningStat.php:112
RunningStat\push
push( $x)
Add a number to the data set.
Definition: RunningStat.php:76