package com.google.common.math; import com.google.common.base.MoreObjects; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import java.io.Serializable; import java.nio.ByteBuffer; import java.nio.ByteOrder; /* loaded from: classes2.dex */ public final class PairedStats implements Serializable { private static final int BYTES = 88; private static final long serialVersionUID = 0; private final double sumOfProductsOfDeltas; private final Stats xStats; private final Stats yStats; private static double ensureInUnitRange(double d) { if (d >= 1.0d) { return 1.0d; } if (d <= -1.0d) { return -1.0d; } return d; } private static double ensurePositive(double d) { if (d > 0.0d) { return d; } return Double.MIN_VALUE; } /* JADX INFO: Access modifiers changed from: package-private */ public PairedStats(Stats stats, Stats stats2, double d) { this.xStats = stats; this.yStats = stats2; this.sumOfProductsOfDeltas = d; } public final long count() { return this.xStats.count(); } 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 = xStats().sumOfSquaresOfDeltas(); double sumOfSquaresOfDeltas2 = 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()); } public final boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) { return false; } PairedStats pairedStats = (PairedStats) obj; return this.xStats.equals(pairedStats.xStats) && this.yStats.equals(pairedStats.yStats) && Double.doubleToLongBits(this.sumOfProductsOfDeltas) == Double.doubleToLongBits(pairedStats.sumOfProductsOfDeltas); } public final int hashCode() { return Objects.hashCode(this.xStats, this.yStats, Double.valueOf(this.sumOfProductsOfDeltas)); } public final String toString() { if (count() > 0) { return MoreObjects.toStringHelper(this).add("xStats", this.xStats).add("yStats", this.yStats).add("populationCovariance", populationCovariance()).toString(); } return MoreObjects.toStringHelper(this).add("xStats", this.xStats).add("yStats", this.yStats).toString(); } public final byte[] toByteArray() { ByteBuffer order = ByteBuffer.allocate(88).order(ByteOrder.LITTLE_ENDIAN); this.xStats.writeTo(order); this.yStats.writeTo(order); order.putDouble(this.sumOfProductsOfDeltas); return order.array(); } public static PairedStats fromByteArray(byte[] bArr) { Preconditions.checkNotNull(bArr); Preconditions.checkArgument(bArr.length == 88, "Expected PairedStats.BYTES = %s, got %s", 88, bArr.length); ByteBuffer order = ByteBuffer.wrap(bArr).order(ByteOrder.LITTLE_ENDIAN); return new PairedStats(Stats.readFrom(order), Stats.readFrom(order), order.getDouble()); } public final Stats yStats() { return this.yStats; } public final Stats xStats() { return this.xStats; } /* JADX INFO: Access modifiers changed from: package-private */ public final double sumOfProductsOfDeltas() { return this.sumOfProductsOfDeltas; } }