Github user viirya commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8587#discussion_r43395797
  
    --- Diff: 
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/AggregationQuerySuite.scala
 ---
    @@ -556,6 +556,33 @@ abstract class AggregationQuerySuite extends QueryTest 
with SQLTestUtils with Te
             Row(0, null, 1, 1, null, 0) :: Nil)
       }
     
    +  test("pearson correlation") {
    +    val df = Seq.tabulate(10)(i => (1.0 * i, 2.0 * i, i * -1.0)).toDF("a", 
"b", "c")
    +    val corr1 = df.repartition(2).groupBy().agg(corr("a", 
"b")).collect()(0).getDouble(0)
    +    assert(math.abs(corr1 - 1.0) < 1e-12)
    +    val corr2 = df.groupBy().agg(corr("a", "c")).collect()(0).getDouble(0)
    +    assert(math.abs(corr2 + 1.0) < 1e-12)
    +    // non-trivial example. To reproduce in python, use:
    +    // >>> from scipy.stats import pearsonr
    +    // >>> import numpy as np
    +    // >>> a = np.array(range(20))
    +    // >>> b = np.array([x * x - 2 * x + 3.5 for x in range(20)])
    +    // >>> pearsonr(a, b)
    +    // (0.95723391394758572, 3.8902121417802199e-11)
    +    // In R, use:
    +    // > a <- 0:19
    +    // > b <- mapply(function(x) x * x - 2 * x + 3.5, a)
    +    // > cor(a, b)
    +    // [1] 0.957233913947585835
    +    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)
    +  }
    --- End diff --
    
    I will add ImplicitCastInputTypes to case class Corr. So the other 
NumericType can be automatically casting to double.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to