guava/src/com/google/common/math/PairedStatsAccumulator.java
public final LinearTransformation leastSquaresFit() {
    checkState(count() > 1);
    if (isNaN(sumOfProductsOfDeltas)) {
      return LinearTransformation.forNaN();
    }
    double xSumOfSquaresOfDeltas = xStats.sumOfSquaresOfDeltas();
    if (xSumOfSquaresOfDeltas > 0.0) {
      if (yStats.sumOfSquaresOfDeltas() > 0.0) {
        return LinearTransformation.mapping(xStats.mean(), yStats.mean())
            .withSlope(sumOfProductsOfDeltas / xSumOfSquaresOfDeltas);
      } else {
        return LinearTransformation.horizontal(yStats.mean());
      }
    } else {
      checkState(yStats.sumOfSquaresOfDeltas() > 0.0);
      return LinearTransformation.vertical(xStats.mean());
    }
  }
public final LinearTransformation leastSquaresFit() {
    checkState(count() > 1);
    if (isNaN(sumOfProductsOfDeltas)) {
      return LinearTransformation.forNaN();
    }
    double xSumOfSquaresOfDeltas = xStats.sumOfSquaresOfDeltas();
    if (xSumOfSquaresOfDeltas > 0.0) {
      if (yStats.sumOfSquaresOfDeltas() > 0.0) {
        return LinearTransformation.mapping(xStats.mean(), yStats.mean())
            .withSlope(sumOfProductsOfDeltas / xSumOfSquaresOfDeltas);
      } else {
        return LinearTransformation.horizontal(yStats.mean());
      }
    } else {
      checkState(yStats.sumOfSquaresOfDeltas() > 0.0);
      return LinearTransformation.vertical(xStats.mean());
    }
  }
public final LinearTransformation leastSquaresFit ( ) { checkState ( count ( ) > 1 ) ; if ( isNaN ( sumOfProductsOfDeltas ) ) { return LinearTransformation . forNaN ( ) ; } double xSumOfSquaresOfDeltas = xStats . sumOfSquaresOfDeltas ( ) ; if ( xSumOfSquaresOfDeltas > 0.0 ) { if ( yStats . sumOfSquaresOfDeltas ( ) > 0.0 ) { return LinearTransformation . mapping ( xStats . mean ( ) , yStats . mean ( ) ) . withSlope ( sumOfProductsOfDeltas / xSumOfSquaresOfDeltas ) ; } else { return LinearTransformation . horizontal ( yStats . mean ( ) ) ; } } else { checkState ( yStats . sumOfSquaresOfDeltas ( ) > 0.0 ) ; return LinearTransformation . vertical ( xStats . mean ( ) ) ; } }
Returns a linear transformation giving the best fit to the data according to <a
href="http://mathworld.wolfram.com/LeastSquaresFitting.html">Ordinary Least Squares linear
regression</a> of {@code y} as a function of {@code x}. The count must be greater than one, and
either the {@code x} or {@code y} data must have a non-zero population variance (i.e. {@code
xStats().populationVariance() > 0.0 || yStats().populationVariance() > 0.0}). The result is
guaranteed to be horizontal if there is variance in the {@code x} data but not the {@code y}
data, and vertical if there is variance in the {@code y} data but not the {@code x} data.

<p>This fit minimizes the root-mean-square error in {@code y} as a function of {@code x}. This
error is defined as the square root of the mean of the squares of the differences between the
actual {@code y} values of the data and the values predicted by the fit for the {@code x}
values (i.e. it is the square root of the mean of the squares of the vertical distances between
the data points and the best fit line). For this fit, this error is a fraction {@code sqrt(1 -
R*R)} of the population standard deviation of {@code y}, where {@code R} is the Pearson's
correlation coefficient (as given by {@link #pearsonsCorrelationCoefficient()}).

<p>The corresponding root-mean-square error in {@code x} as a function of {@code y} is a
fraction {@code sqrt(1/(R*R) - 1)} of the population standard deviation of {@code x}. This fit
does not normally minimize that error: to do that, you should swap the roles of {@code x} and
{@code y}.

<h3>Non-finite values</h3>

<p>If the dataset contains any non-finite values ({@link Double#POSITIVE_INFINITY}, {@link
Double#NEGATIVE_INFINITY}, or {@link Double#NaN}) then the result is {@link
LinearTransformation#forNaN()}.

@throws IllegalStateException if the dataset is empty or contains a single pair of values, or
both the {@code x} and {@code y} dataset have zero population variance
Returns a linear transformation giving the best fit to the data according to <a href = http : // mathworld . wolfram . com / LeastSquaresFitting . html > Ordinary Least Squares linear regression< / a > of { @code y } as a function of { @code x } . The count must be greater than one and either the { @code x } or { @code y } data must have a non - zero population variance ( i . e . { @code xStats () . populationVariance () > 0 . 0 || yStats () . populationVariance () > 0 . 0 } ) . The result is guaranteed to be horizontal if there is variance in the { @code x } data but not the { @code y } data and vertical if there is variance in the { @code y } data but not the { @code x } data .