Skip to content

Commit

Permalink
Fix assumeZeroMean issue and some minor spacing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jmh530 committed Oct 13, 2023
1 parent 3128689 commit b5d92ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 86 deletions.
30 changes: 1 addition & 29 deletions source/mir/stat/descriptive/multivariate.d
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ struct CovarianceAccumulator(T, CovarianceAlgo covarianceAlgo, Summation summati

///
void put(U, CovarianceAlgo covAlgo, Summation sumAlgo)(CovarianceAccumulator!(U, covAlgo, sumAlgo) v)
if (!is(covAlgo == CovarianceAlgo.assumeZeroMean))
if (covAlgo != CovarianceAlgo.assumeZeroMean)
{
size_t oldCount = count;
T deltaLeft = v.meanLeft;
Expand Down Expand Up @@ -603,34 +603,6 @@ unittest
v1.covariance(false).shouldApprox == -5.5 / 11;
}

// Check adding CovarianceAccumultors (assumeZeroMean)
version(mir_stat_test)
@safe pure nothrow
unittest
{
import mir.math.sum: sum, Summation;
import mir.ndslice.slice: sliced;
import mir.stat.transform: center;
import mir.test: shouldApprox;

auto a1 = [ 0.0, 1.0, 1.5, 2.0, 3.5, 4.25].sliced;
auto b1 = [-0.75, 6.0, -0.25, 8.25, 5.75, 3.5].sliced;
auto a2 = [ 2.0, 7.5, 5.0, 1.0, 1.5, 0.0].sliced;
auto b2 = [ 9.25, -0.75, 2.5, 1.25, -1, 2.25].sliced;
auto x1 = a1.center;
auto y1 = b1.center;
auto x2 = a2.center;
auto y2 = b2.center;

CovarianceAccumulator!(double, CovarianceAlgo.online, Summation.naive) v1;
v1.put(x1, y1);
auto v2 = CovarianceAccumulator!(double, CovarianceAlgo.assumeZeroMean, Summation.naive)(x2, y2);
v1.put(v2);

v1.covariance(true).shouldApprox == -1.9375 / 12; //note: different from above due to inconsistent centering
v1.covariance(false).shouldApprox == -1.9375 / 11; //note: different from above due to inconsistent centering
}

// Initializing with one point
version(mir_stat_test)
@safe pure nothrow
Expand Down
71 changes: 14 additions & 57 deletions source/mir/stat/descriptive/univariate.d
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ unittest
Output range that applies function `fun` to each input before summing
+/
struct MapSummator(alias fun, T, Summation summation)
if(isMutable!T)
if (isMutable!T)
{
///
Summator!(T, summation) summator;
Expand Down Expand Up @@ -2144,7 +2144,7 @@ struct VarianceAccumulator(T, VarianceAlgo varianceAlgo, Summation summation)

///
void put(U, VarianceAlgo varAlgo, Summation sumAlgo)(VarianceAccumulator!(U, varAlgo, sumAlgo) v)
if(!is(varAlgo == VarianceAlgo.assumeZeroMean))
if (varAlgo != VarianceAlgo.assumeZeroMean)
{
size_t oldCount = count;
T delta = v.mean!T;
Expand Down Expand Up @@ -2722,7 +2722,7 @@ struct VarianceAccumulator(T, VarianceAlgo varianceAlgo, Summation summation)

///
void put(U, VarianceAlgo varAlgo, Summation sumAlgo)(VarianceAccumulator!(U, varAlgo, sumAlgo) v)
if(!is(varAlgo == VarianceAlgo.assumeZeroMean))
if (varAlgo != VarianceAlgo.assumeZeroMean)
{
size_t oldCount = count;
T delta = v.mean!T;
Expand Down Expand Up @@ -2991,7 +2991,7 @@ template variance(
isPopulation = true if population variance, false if sample variance (default)
+/
@fmamath meanType!Range variance(Range)(Range r, bool isPopulation = false)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;

Expand Down Expand Up @@ -3374,7 +3374,7 @@ template standardDeviation(
isPopulation = true if population standard deviation, false if sample standard deviation (default)
+/
@fmamath stdevType!Range standardDeviation(Range)(Range r, bool isPopulation = false)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;

Expand Down Expand Up @@ -5789,7 +5789,7 @@ struct SkewnessAccumulator(T, SkewnessAlgo skewnessAlgo, Summation summation)

///
void put(U, SkewnessAlgo skewAlgo, Summation sumAlgo)(SkewnessAccumulator!(U, skewAlgo, sumAlgo) v)
if(!is(skewAlgo == SkewnessAlgo.assumeZeroMean))
if (skewAlgo != SkewnessAlgo.assumeZeroMean)
{
size_t oldCount = count;
T delta = v.mean;
Expand Down Expand Up @@ -5996,28 +5996,6 @@ unittest
assert(v.centeredSumOfSquares.approxEqual(54.765625));
}

// Can put SkewnessAccumulator (assumeZeroMean)
version(mir_stat_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual, pow;
import mir.ndslice.slice: sliced;
import mir.stat.transform: center;

auto a = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25].sliced;
auto b = [2.0, 7.5, 5.0, 1.0, 1.5, 0.0].sliced;
auto x = a.center;
auto y = b.center;

SkewnessAccumulator!(double, SkewnessAlgo.online, Summation.naive) v;
v.put(x);
auto w = SkewnessAccumulator!(double, SkewnessAlgo.assumeZeroMean, Summation.naive)(y);
v.put(w);
assert(v.centeredSumOfCubes.approxEqual(84.015625)); //note: different from above due to inconsistent centering
assert(v.centeredSumOfSquares.approxEqual(52.885417)); //note: different from above due to inconsistent centering
}

// check variance/scaledSumOfCubes
version(mir_stat_test)
@safe pure nothrow
Expand Down Expand Up @@ -6726,7 +6704,7 @@ struct SkewnessAccumulator(T, SkewnessAlgo skewnessAlgo, Summation summation)

///
void put(U, SkewnessAlgo skewAlgo, Summation sumAlgo)(SkewnessAccumulator!(U, skewAlgo, sumAlgo) v)
if(!is(skewAlgo == SkewnessAlgo.assumeZeroMean))
if (skewAlgo != SkewnessAlgo.assumeZeroMean)
{
size_t oldCount = count;
T delta = v.mean;
Expand Down Expand Up @@ -7004,27 +6982,6 @@ unittest
assert(v.centeredSumOfSquares.approxEqual(54.765625));
}

// Can put SkewnessAccumulator (assumeZeroMean)
version(mir_stat_test)
@safe pure nothrow
unittest
{
import mir.math.common: approxEqual, pow;
import mir.ndslice.slice: sliced;
import mir.stat.transform: center;

auto a = [0.0, 1.0, 1.5, 2.0, 3.5, 4.25].sliced;
auto b = [2.0, 7.5, 5.0, 1.0, 1.5, 0.0].sliced;
auto x = a.center;
auto y = b.center;

auto v = SkewnessAccumulator!(double, SkewnessAlgo.hybrid, Summation.naive)(x);
auto w = SkewnessAccumulator!(double, SkewnessAlgo.assumeZeroMean, Summation.naive)(y);
v.put(w);
assert(v.centeredSumOfCubes.approxEqual(84.015625)); //note: different from above due to inconsistent centering
assert(v.centeredSumOfSquares.approxEqual(52.885417)); //note: different from above due to inconsistent centering
}

// check variance/scaledSumOfCubes
version(mir_stat_test)
@safe pure nothrow
Expand Down Expand Up @@ -7108,7 +7065,7 @@ template skewness(SkewnessAlgo skewnessAlgo = SkewnessAlgo.hybrid,
isPopulation = true if population skewness, false if sample skewness (default)
+/
@fmamath stdevType!Range skewness(Range)(Range r, bool isPopulation = false)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;
alias F = typeof(return);
Expand Down Expand Up @@ -9448,7 +9405,7 @@ template kurtosis(
isRaw = true if raw kurtosis, false if excess kurtosis (default)
+/
@fmamath stdevType!Range kurtosis(Range)(Range r, bool isPopulation = false, bool isRaw = false)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;
alias F = typeof(return);
Expand Down Expand Up @@ -10313,7 +10270,7 @@ template coefficientOfVariation(
isPopulation = true if population variance, false if sample variance (default)
+/
@fmamath stdevType!Range coefficientOfVariation(Range)(Range r, bool isPopulation = false)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;

Expand Down Expand Up @@ -11045,7 +11002,7 @@ template rawMoment(size_t N, Summation summation = Summation.appropriate)
r = range, must be finite iterable
+/
@fmamath meanType!Range rawMoment(Range)(Range r)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;

Expand Down Expand Up @@ -11298,7 +11255,7 @@ template centralMoment(size_t N, Summation summation = Summation.appropriate)
r = range, must be finite iterable
+/
@fmamath meanType!Range centralMoment(Range)(Range r)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;

Expand Down Expand Up @@ -11595,7 +11552,7 @@ template standardizedMoment(size_t N,
r = range, must be finite iterable
+/
@fmamath stdevType!Range standardizedMoment(Range)(Range r)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;

Expand Down Expand Up @@ -11908,7 +11865,7 @@ template moment(size_t N,
r = range, must be finite iterable
+/
@fmamath stdevType!Range moment(Range)(Range r)
if(isIterable!Range)
if (isIterable!Range)
{
import core.lifetime: move;

Expand Down

0 comments on commit b5d92ce

Please sign in to comment.