Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add document. Return NaN when count is zero.
  • Loading branch information
viirya committed Oct 27, 2015
commit 02562f3a9ab9cda941b260a834d505f7cacd46f2
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,16 @@ case class Sum(child: Expression) extends DeclarativeAggregate {
override val evaluateExpression = Cast(currentSum, resultType)
}

/**
* Compute Pearson correlation between two expressions.
* When applied on empty data (i.e., count is zero), it returns NaN.
*
* Definition of Pearson correlation can be found at
* http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
*
* @param left one of the expressions to compute correlation with.
* @param right another expression to compute correlation with.
*/
case class Corr(
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Please add ScalaDoc and document the behavior for null and NaN values.
  • Provide a link to the wikipedia page that contains the update formula.

left: Expression,
right: Expression,
Expand Down Expand Up @@ -591,6 +601,8 @@ case class Corr(
buffer.setLong(mutableAggBufferOffset + 5, count)
}

// Merge counters from other partitions. Formula can be found at:
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
override def merge(buffer1: MutableRow, buffer2: InternalRow): Unit = {
val count2 = buffer2.getLong(inputAggBufferOffset + 5)

Expand Down Expand Up @@ -628,10 +640,15 @@ case class Corr(
}

override def eval(buffer: InternalRow): Any = {
val Ck = buffer.getDouble(mutableAggBufferOffset + 2)
val MkX = buffer.getDouble(mutableAggBufferOffset + 3)
val MkY = buffer.getDouble(mutableAggBufferOffset + 4)
Ck / math.sqrt(MkX * MkY)
val count = buffer.getLong(mutableAggBufferOffset + 5)
if (count > 0) {
val Ck = buffer.getDouble(mutableAggBufferOffset + 2)
val MkX = buffer.getDouble(mutableAggBufferOffset + 3)
val MkY = buffer.getDouble(mutableAggBufferOffset + 4)
Ck / math.sqrt(MkX * MkY)
} else {
Double.NaN
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ abstract class AggregationQuerySuite extends QueryTest with SQLTestUtils with Te
val df2 = Seq.tabulate(20)(x => (1.0 * x, x * x - 2 * x + 3.5)).toDF("a", "b")
val corr3 = df2.groupBy().agg(corr("a", "b")).collect()(0).getDouble(0)
assert(math.abs(corr3 - 0.95723391394758572) < 1e-12)

val df3 = Seq.tabulate(0)(i => (1.0 * i, 2.0 * i)).toDF("a", "b")
val corr4 = df3.groupBy().agg(corr("a", "b")).collect()(0).getDouble(0)
assert(corr4.isNaN)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What will happen if the data type of input parameters are not double?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will add ImplicitCastInputTypes to case class Corr. So the other NumericType can be automatically casting to double.


test("test Last implemented based on AggregateExpression1") {
Expand Down