Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/min max #142

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Helper/RunningVariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
*/
private int $count = 0;

/** The smallest observed value */
private float $min = NAN;

/** The largest observed value */
private float $max = NAN;

/** First moment: the mean value. */
private float $mean = 0.0;

Expand All @@ -61,6 +67,19 @@
$this->mean += $delta / $this->count;
$this->m2 += $delta * ($value - $this->mean);

if (1 === $this->count) {
$this->min = $value;
$this->max = $value;
} else {
if ($value < $this->min) {

Check warning on line 74 in src/Helper/RunningVariance.php

View workflow job for this annotation

GitHub Actions / Static Analysis and Validation

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ $this->min = $value; $this->max = $value; } else { - if ($value < $this->min) { + if ($value <= $this->min) { $this->min = $value; } if ($value > $this->max) {
$this->min = $value;
}

if ($value > $this->max) {

Check warning on line 78 in src/Helper/RunningVariance.php

View workflow job for this annotation

GitHub Actions / Static Analysis and Validation

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ if ($value < $this->min) { $this->min = $value; } - if ($value > $this->max) { + if ($value >= $this->max) { $this->max = $value; } }
$this->max = $value;
}
}

return $value;
}

Expand All @@ -72,6 +91,18 @@
return $this->count;
}

/** The smallest observed value */
public function getMin(): float
{
return $this->min;
}

/** The largest observed value */
public function getMax(): float
{
return $this->max;
}

/**
* Get the mean value.
*/
Expand Down Expand Up @@ -129,6 +160,8 @@
$this->count = $other->count;
$this->mean = $other->mean;
$this->m2 = $other->m2;
$this->min = $other->min;
$this->max = $other->max;

return;
}
Expand All @@ -139,5 +172,13 @@
$this->mean = ($this->count * $this->mean) / $count + ($other->count * $other->mean) / $count;
$this->m2 = $this->m2 + $other->m2 + ($delta ** 2 * $this->count * $other->count / $count);
$this->count = $count;

if ($other->min < $this->min) {

Check warning on line 176 in src/Helper/RunningVariance.php

View workflow job for this annotation

GitHub Actions / Static Analysis and Validation

Escaped Mutant for Mutator "LessThan": --- Original +++ New @@ @@ $this->mean = $this->count * $this->mean / $count + $other->count * $other->mean / $count; $this->m2 = $this->m2 + $other->m2 + $delta ** 2 * $this->count * $other->count / $count; $this->count = $count; - if ($other->min < $this->min) { + if ($other->min <= $this->min) { $this->min = $other->min; } if ($other->max > $this->max) {
$this->min = $other->min;
}

if ($other->max > $this->max) {

Check warning on line 180 in src/Helper/RunningVariance.php

View workflow job for this annotation

GitHub Actions / Static Analysis and Validation

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ if ($other->min < $this->min) { $this->min = $other->min; } - if ($other->max > $this->max) { + if ($other->max >= $this->max) { $this->max = $other->max; } } }
$this->max = $other->max;
}
}
}
32 changes: 32 additions & 0 deletions tests/Helper/RunningVarianceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function testEmpty(): void

$this->assertSame(0, $variance->getCount());
$this->assertNan($variance->getMean());
$this->assertNan($variance->getMin());
$this->assertNan($variance->getMax());
$this->assertNan($variance->getVariance());
$this->assertNan($variance->getStandardDeviation());
}
Expand All @@ -72,10 +74,26 @@ public function testOne(): void

$this->assertSame(1, $variance->getCount());
$this->assertSame(M_PI, $variance->getMean());
$this->assertSame(M_PI, $variance->getMin());
$this->assertSame(M_PI, $variance->getMax());
$this->assertSame(0.0, $variance->getVariance());
$this->assertSame(0.0, $variance->getStandardDeviation());
}

public function testOneNegative(): void
{
$variance = new RunningVariance();
$variance->observe(-1.01);

$this->assertSame(1, $variance->getCount());
$this->assertSame(-1.01, $variance->getMean());
$this->assertSame(-1.01, $variance->getMin());
$this->assertSame(-1.01, $variance->getMax());
$this->assertSame(0.0, $variance->getVariance());
$this->assertSame(0.0, $variance->getStandardDeviation());
}


public function testTwo(): void
{
$variance = new RunningVariance();
Expand All @@ -84,6 +102,8 @@ public function testTwo(): void

$this->assertSame(2, $variance->getCount());
$this->assertSame(M_PI, $variance->getMean());
$this->assertSame(M_PI, $variance->getMin());
$this->assertSame(M_PI, $variance->getMax());
$this->assertSame(0.0, $variance->getVariance());
$this->assertSame(0.0, $variance->getStandardDeviation());
}
Expand All @@ -96,6 +116,8 @@ public function testNAN(): void

$this->assertSame(2, $variance->getCount());
$this->assertNan($variance->getMean());
$this->assertSame(M_PI, $variance->getMin());
$this->assertSame(M_PI, $variance->getMax());
$this->assertNan($variance->getVariance());
$this->assertNan($variance->getStandardDeviation());
}
Expand All @@ -111,6 +133,8 @@ public function testFive(): void

$this->assertSame(5, $variance->getCount());
$this->assertSame(5.0, $variance->getMean());
$this->assertSame(2.0, $variance->getMin());
$this->assertSame(8.0, $variance->getMax());
$this->assertEqualsWithDelta(5.0, $variance->getVariance(), 0.0001);
$this->assertEqualsWithDelta(sqrt(5.0), $variance->getStandardDeviation(), 0.0001);
}
Expand All @@ -128,6 +152,8 @@ public function testCopy(): void

$this->assertSame(5, $variance->getCount());
$this->assertSame(5.0, $variance->getMean());
$this->assertSame(2.0, $variance->getMin());
$this->assertSame(8.0, $variance->getMax());
$this->assertEqualsWithDelta(5.0, $variance->getVariance(), 0.0001);
$this->assertEqualsWithDelta(sqrt(5.0), $variance->getStandardDeviation(), 0.0001);
}
Expand All @@ -145,6 +171,8 @@ public function testFiveMerged(): void

$this->assertSame(5, $variance->getCount());
$this->assertSame(5.0, $variance->getMean());
$this->assertSame(2.0, $variance->getMin());
$this->assertSame(8.0, $variance->getMax());
$this->assertEqualsWithDelta(5.0, $variance->getVariance(), 0.0001);
$this->assertEqualsWithDelta(sqrt(5.0), $variance->getStandardDeviation(), 0.0001);
}
Expand All @@ -164,6 +192,8 @@ public function testFiveMergedTwice(): void

$this->assertSame(5, $variance->getCount());
$this->assertSame(5.0, $variance->getMean());
$this->assertSame(2.0, $variance->getMin());
$this->assertSame(8.0, $variance->getMax());
$this->assertEqualsWithDelta(5.0, $variance->getVariance(), 0.0001);
$this->assertEqualsWithDelta(sqrt(5.0), $variance->getStandardDeviation(), 0.0001);
}
Expand All @@ -185,6 +215,8 @@ public function testFiveMergedThrice(): void

$this->assertSame(5, $variance->getCount());
$this->assertSame(5.0, $variance->getMean());
$this->assertSame(2.0, $variance->getMin());
$this->assertSame(8.0, $variance->getMax());
$this->assertEqualsWithDelta(5.0, $variance->getVariance(), 0.0001);
$this->assertEqualsWithDelta(sqrt(5.0), $variance->getStandardDeviation(), 0.0001);
}
Expand Down
Loading