what-the-bank/sources/com/google/common/math/PairedStatsAccumulator.java

100 lines
3.6 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.math;
import com.google.common.base.Preconditions;
import com.google.common.primitives.Doubles;
/* loaded from: classes2.dex */
public final class PairedStatsAccumulator {
private final StatsAccumulator xStats = new StatsAccumulator();
private final StatsAccumulator yStats = new StatsAccumulator();
private double sumOfProductsOfDeltas = 0.0d;
private double ensurePositive(double d) {
if (d > 0.0d) {
return d;
}
return Double.MIN_VALUE;
}
public final void add(double d, double d2) {
this.xStats.add(d);
if (!Doubles.isFinite(d) || !Doubles.isFinite(d2)) {
this.sumOfProductsOfDeltas = Double.NaN;
} else if (this.xStats.count() > 1) {
this.sumOfProductsOfDeltas += (d - this.xStats.mean()) * (d2 - this.yStats.mean());
}
this.yStats.add(d2);
}
public final void addAll(PairedStats pairedStats) {
if (pairedStats.count() == 0) {
return;
}
this.xStats.addAll(pairedStats.xStats());
if (this.yStats.count() == 0) {
this.sumOfProductsOfDeltas = pairedStats.sumOfProductsOfDeltas();
} else {
this.sumOfProductsOfDeltas += pairedStats.sumOfProductsOfDeltas() + ((pairedStats.xStats().mean() - this.xStats.mean()) * (pairedStats.yStats().mean() - this.yStats.mean()) * pairedStats.count());
}
this.yStats.addAll(pairedStats.yStats());
}
public final PairedStats snapshot() {
return new PairedStats(this.xStats.snapshot(), this.yStats.snapshot(), this.sumOfProductsOfDeltas);
}
public final long count() {
return this.xStats.count();
}
public final Stats xStats() {
return this.xStats.snapshot();
}
public final Stats yStats() {
return this.yStats.snapshot();
}
public final double populationCovariance() {
Preconditions.checkState(count() != 0);
return this.sumOfProductsOfDeltas / count();
}
public final double sampleCovariance() {
Preconditions.checkState(count() > 1);
return this.sumOfProductsOfDeltas / (count() - 1);
}
public final double pearsonsCorrelationCoefficient() {
Preconditions.checkState(count() > 1);
if (Double.isNaN(this.sumOfProductsOfDeltas)) {
return Double.NaN;
}
double sumOfSquaresOfDeltas = this.xStats.sumOfSquaresOfDeltas();
double sumOfSquaresOfDeltas2 = this.yStats.sumOfSquaresOfDeltas();
Preconditions.checkState(sumOfSquaresOfDeltas > 0.0d);
Preconditions.checkState(sumOfSquaresOfDeltas2 > 0.0d);
return ensureInUnitRange(this.sumOfProductsOfDeltas / Math.sqrt(ensurePositive(sumOfSquaresOfDeltas * sumOfSquaresOfDeltas2)));
}
public final LinearTransformation leastSquaresFit() {
Preconditions.checkState(count() > 1);
if (Double.isNaN(this.sumOfProductsOfDeltas)) {
return LinearTransformation.forNaN();
}
double sumOfSquaresOfDeltas = this.xStats.sumOfSquaresOfDeltas();
if (sumOfSquaresOfDeltas <= 0.0d) {
Preconditions.checkState(this.yStats.sumOfSquaresOfDeltas() > 0.0d);
return LinearTransformation.vertical(this.xStats.mean());
}
if (this.yStats.sumOfSquaresOfDeltas() > 0.0d) {
return LinearTransformation.mapping(this.xStats.mean(), this.yStats.mean()).withSlope(this.sumOfProductsOfDeltas / sumOfSquaresOfDeltas);
}
return LinearTransformation.horizontal(this.yStats.mean());
}
private static double ensureInUnitRange(double d) {
return Doubles.constrainToRange(d, -1.0d, 1.0d);
}
}