You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/09/29 14:48:06 UTC

[GitHub] [arrow] cyb70289 commented on a change in pull request #8269: ARROW-10070: [C++][Compute] Implement var and std aggregate kernel

cyb70289 commented on a change in pull request #8269:
URL: https://github.com/apache/arrow/pull/8269#discussion_r496780078



##########
File path: cpp/src/arrow/compute/kernels/aggregate_var_std.cc
##########
@@ -0,0 +1,200 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+#include "arrow/compute/kernels/aggregate_basic_internal.h"
+
+namespace arrow {
+namespace compute {
+namespace aggregate {
+
+namespace {
+
+// Based on https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
+template <typename ArrowType>
+struct VarStdState {
+  using ArrayType = typename TypeTraits<ArrowType>::ArrayType;
+  using c_type = typename ArrowType::c_type;
+  using ThisType = VarStdState<ArrowType>;
+
+  // Calculate variance of one chunk with `two pass algorithm`
+  // Always use `double` to calculate variance for any array type
+  void Consume(const ArrayType& array) {
+    int64_t count = array.length() - array.null_count();
+    if (count == 0) {
+      return;
+    }
+
+    double sum = 0;
+    VisitArrayDataInline<ArrowType>(
+        *array.data(), [&sum](c_type value) { sum += static_cast<double>(value); },
+        []() {});
+
+    double mean = sum / count, m2 = 0;
+    VisitArrayDataInline<ArrowType>(
+        *array.data(),
+        [mean, &m2](c_type value) {
+          double v = static_cast<double>(value);
+          m2 += (v - mean) * (v - mean);
+        },
+        []() {});
+
+    this->count = count;
+    this->sum = sum;
+    this->m2 = m2;
+  }
+
+  // Combine variance from two chunks
+  void MergeFrom(const ThisType& state) {
+    if (state.count == 0) {
+      return;
+    }
+    if (this->count == 0) {
+      this->count = state.count;
+      this->sum = state.sum;
+      this->m2 = state.m2;
+      return;
+    }
+    double delta = this->sum / this->count - state.sum / state.count;
+    this->m2 += state.m2 +
+                delta * delta * this->count * state.count / (this->count + state.count);

Review comment:
       The comment `// Combine variance from two chunks` is misleading.
   Actually, this code is combining `m2` (which equals `sum((X-mean)^2)`) from two chunks, not the variance. See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
   Will refine comments.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org