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 additional linear regression functions #21630

Merged
merged 1 commit into from
Jan 12, 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
35 changes: 35 additions & 0 deletions presto-docs/src/main/sphinx/functions/aggregate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,41 @@ Statistical Aggregate Functions
Returns linear regression slope of input values. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: regr_avgx(y, x) -> double
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: regr_avgx(y, x) -> double
.. function:: regr_avgx(y, x) -> double


Returns the average of the independent value in a group. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: regr_avgy(y, x) -> double
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: regr_avgy(y, x) -> double
.. function:: regr_avgy(y, x) -> double


Returns the average of the dependent value in a group. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: regr_count(y, x) -> double
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: regr_count(y, x) -> double
.. function:: regr_count(y, x) -> double


Returns the number of non-null pairs of input values. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: regr_r2(y, x) -> double
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: regr_r2(y, x) -> double
.. function:: regr_r2(y, x) -> double


Returns the coefficient of determination of the linear regression. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: regr_sxy(y, x) -> double
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: regr_sxy(y, x) -> double
.. function:: regr_sxy(y, x) -> double


Returns the sum of the product of the dependent and independent values in a group. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: regr_syy(y, x) -> double
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: regr_syy(y, x) -> double
.. function:: regr_syy(y, x) -> double


Returns the sum of the squares of the dependent values in a group. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: regr_sxx(y, x) -> double
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. function:: regr_sxx(y, x) -> double
.. function:: regr_sxx(y, x) -> double

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Updated. Here's the new screenshot of what the doc build looks like:
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Updated. Here's the new screenshot of what the doc build looks like:

Thanks! I just now did a new pull and local build of the doc and everything looks good! Updating my review to Approve for the docs.


Returns the sum of the squares of the independent values in a group. ``y`` is the dependent
value. ``x`` is the independent value.

.. function:: skewness(x) -> double

Returns the skewness of all input values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ public static double getCorrelation(CorrelationState state)
public static void updateRegressionState(RegressionState state, double x, double y)
{
double oldMeanX = state.getMeanX();
double oldMeanY = state.getMeanY();
updateCovarianceState(state, x, y);
state.setM2X(state.getM2X() + (x - oldMeanX) * (x - state.getMeanX()));
state.setM2Y(state.getM2Y() + (y - oldMeanY) * (y - state.getMeanY()));
}

public static double getRegressionSlope(RegressionState state)
Expand All @@ -167,6 +169,41 @@ public static double getRegressionIntercept(RegressionState state)
return meanY - slope * meanX;
}

public static double getRegressionAvgy(RegressionState state)
{
return state.getMeanY();
}

public static double getRegressionAvgx(RegressionState state)
{
return state.getMeanX();
}

public static double getRegressionSxx(RegressionState state)
{
return state.getM2X();
}

public static double getRegressionSxy(RegressionState state)
{
return state.getC2();
}

public static double getRegressionSyy(RegressionState state)
{
return state.getM2Y();
}

public static double getRegressionR2(RegressionState state)
{
return Math.pow(state.getC2(), 2) / (state.getM2X() * state.getM2Y());
}

public static double getRegressionCount(RegressionState state)
{
return state.getCount();
}

public static void mergeVarianceState(VarianceState state, VarianceState otherState)
{
long count = otherState.getCount();
Expand Down Expand Up @@ -265,6 +302,7 @@ public static void mergeRegressionState(RegressionState state, RegressionState o
long na = state.getCount();
long nb = otherState.getCount();
state.setM2X(state.getM2X() + otherState.getM2X() + na * nb * Math.pow(state.getMeanX() - otherState.getMeanX(), 2) / (double) (na + nb));
state.setM2Y(state.getM2Y() + otherState.getM2Y() + na * nb * Math.pow(state.getMeanY() - otherState.getMeanY(), 2) / (double) (na + nb));
updateCovarianceState(state, otherState);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
import com.facebook.presto.spi.function.SqlType;

import static com.facebook.presto.common.type.DoubleType.DOUBLE;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionAvgx;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionAvgy;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionCount;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionIntercept;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionR2;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSlope;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSxx;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSxy;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSyy;
import static com.facebook.presto.operator.aggregation.AggregationUtils.mergeRegressionState;
import static com.facebook.presto.operator.aggregation.AggregationUtils.updateRegressionState;

Expand Down Expand Up @@ -71,4 +78,95 @@ public static void regrIntercept(@AggregationState RegressionState state, BlockB
out.appendNull();
}
}

@AggregationFunction("regr_sxy")
@OutputFunction(StandardTypes.DOUBLE)
public static void regrSxy(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionSxy(state);
if (Double.isFinite(result)) {
DOUBLE.writeDouble(out, result);
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_sxx")
@OutputFunction(StandardTypes.DOUBLE)
public static void regrSxx(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionSxx(state);
if (Double.isFinite(result)) {
DOUBLE.writeDouble(out, result);
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_syy")
@OutputFunction(StandardTypes.DOUBLE)
public static void regrSyy(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionSyy(state);
if (Double.isFinite(result)) {
DOUBLE.writeDouble(out, result);
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_r2")
@OutputFunction(StandardTypes.DOUBLE)
public static void regrR2(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionR2(state);
if (Double.isFinite(result)) {
DOUBLE.writeDouble(out, result);
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_count")
@OutputFunction(StandardTypes.DOUBLE)
public static void regrCount(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionCount(state);
if (Double.isFinite(result)) {
DOUBLE.writeDouble(out, result);
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_avgy")
@OutputFunction(StandardTypes.DOUBLE)
public static void regrAvgy(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionAvgy(state);
if (Double.isFinite(result)) {
DOUBLE.writeDouble(out, result);
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_avgx")
@OutputFunction(StandardTypes.DOUBLE)
public static void regrAvgx(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionAvgx(state);
if (Double.isFinite(result)) {
DOUBLE.writeDouble(out, result);
}
else {
out.appendNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@
import com.facebook.presto.spi.function.SqlType;

import static com.facebook.presto.common.type.RealType.REAL;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionAvgx;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionAvgy;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionCount;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionIntercept;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionR2;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSlope;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSxx;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSxy;
import static com.facebook.presto.operator.aggregation.AggregationUtils.getRegressionSyy;
import static java.lang.Float.floatToRawIntBits;
import static java.lang.Float.intBitsToFloat;

Expand Down Expand Up @@ -71,4 +78,95 @@ public static void regrIntercept(@AggregationState RegressionState state, BlockB
out.appendNull();
}
}

@AggregationFunction("regr_sxy")
@OutputFunction(StandardTypes.REAL)
public static void regrSxy(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionSxy(state);
if (Double.isFinite(result)) {
REAL.writeLong(out, floatToRawIntBits((float) result));
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_sxx")
@OutputFunction(StandardTypes.REAL)
public static void regrSxx(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionSxx(state);
if (Double.isFinite(result)) {
REAL.writeLong(out, floatToRawIntBits((float) result));
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_syy")
@OutputFunction(StandardTypes.REAL)
public static void regrSyy(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionSyy(state);
if (Double.isFinite(result)) {
REAL.writeLong(out, floatToRawIntBits((float) result));
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_r2")
@OutputFunction(StandardTypes.REAL)
public static void regrR2(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionR2(state);
if (Double.isFinite(result)) {
REAL.writeLong(out, floatToRawIntBits((float) result));
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_count")
@OutputFunction(StandardTypes.REAL)
public static void regrCount(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionCount(state);
if (Double.isFinite(result)) {
REAL.writeLong(out, floatToRawIntBits((float) result));
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_avgy")
@OutputFunction(StandardTypes.REAL)
public static void regrAvgy(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionAvgy(state);
if (Double.isFinite(result)) {
REAL.writeLong(out, floatToRawIntBits((float) result));
}
else {
out.appendNull();
}
}

@AggregationFunction("regr_avgx")
@OutputFunction(StandardTypes.REAL)
public static void regrAvgx(@AggregationState RegressionState state, BlockBuilder out)
{
double result = getRegressionAvgx(state);
if (Double.isFinite(result)) {
REAL.writeLong(out, floatToRawIntBits((float) result));
}
else {
out.appendNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public interface RegressionState
double getM2X();

void setM2X(double value);

double getM2Y();

void setM2Y(double value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.operator.aggregation;

import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.type.StandardTypes;
import com.google.common.collect.ImmutableList;
import org.testng.annotations.Test;

import java.util.List;

import static com.facebook.presto.block.BlockAssertions.createDoubleSequenceBlock;

public abstract class AbstractTestDoubleRegrAggregationFunction
extends AbstractTestAggregationFunction
{
@Override
public Block[] getSequenceBlocks(int start, int length)
{
return new Block[] {createDoubleSequenceBlock(start, start + length), createDoubleSequenceBlock(start + 2, start + 2 + length)};
}

@Override
protected List<String> getFunctionParameterTypes()
{
return ImmutableList.of(StandardTypes.DOUBLE, StandardTypes.DOUBLE);
}

@Test
public void testSinglePosition()
{
testAggregation(getExpectedValue(0, 2), getSequenceBlocks(0, 2));
}

@Test
public void testNonTrivialResult()
{
testNonTrivialAggregation(new Double[] {1.0, 2.0, 3.0, 4.0, 5.0}, new Double[] {1.0, 4.0, 9.0, 16.0, 25.0});
testNonTrivialAggregation(new Double[] {1.0, 4.0, 9.0, 16.0, 25.0}, new Double[] {1.0, 2.0, 3.0, 4.0, 5.0});
}

protected abstract void testNonTrivialAggregation(Double[] y, Double[] x);
}
Loading
Loading