This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch tdigest-cpp
in repository 
https://gitbox.apache.org/repos/asf/datasketches-characterization.git

commit 266c847b44b2b2ce562ab974498f631dd6b0334f
Author: AlexanderSaydakov <[email protected]>
AuthorDate: Mon Jan 20 12:14:37 2025 -0800

    missing file
---
 .../characterization/tdigest/TrueDoubleRanks.java  | 170 +++++++++++++++++++++
 1 file changed, 170 insertions(+)

diff --git 
a/src/main/java/org/apache/datasketches/characterization/tdigest/TrueDoubleRanks.java
 
b/src/main/java/org/apache/datasketches/characterization/tdigest/TrueDoubleRanks.java
new file mode 100644
index 0000000..092c2e7
--- /dev/null
+++ 
b/src/main/java/org/apache/datasketches/characterization/tdigest/TrueDoubleRanks.java
@@ -0,0 +1,170 @@
+/*
+ * 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.
+ */
+
+package org.apache.datasketches.characterization.tdigest;
+
+import java.util.Arrays;
+
+import org.apache.datasketches.characterization.Shuffle;
+import org.apache.datasketches.quantilescommon.BinarySearch;
+import org.testng.annotations.Test;
+
+/**
+ * Given an array of values, these methods compute the true rank (mass) of
+ * each value of the array based on the comparison criterion.
+ * The mass or rank of each value is the fractional number of elements of the 
array that satisfy
+ * the criterion.
+ *
+ * @author Lee Rhodes
+ */
+public class TrueDoubleRanks {
+  private static final String LS = System.getProperty("line.separator");
+  private int length;
+  private double[] stream;
+  private double[] sortedStream;
+  private int[] sortedAbsRanks;
+  private int[] streamAbsRanks;
+
+  TrueDoubleRanks() { } //for TestNG
+
+  public TrueDoubleRanks(final double[] stream) {
+    this.stream = stream;
+    compute();
+  }
+
+  public double getMinValue() { return sortedStream[0]; }
+
+  public double getMaxValue() { return sortedStream[length - 1]; }
+
+  public int getMinAbsRank() { return sortedAbsRanks[0]; }
+
+  public int getMaxAbsRank() { return sortedAbsRanks[length - 1]; }
+
+  public double[] getStream() { return stream; }
+
+  public double[] getSortedStream() { return sortedStream; }
+
+  public int[] getSortedAbsRanks() { return sortedAbsRanks; }
+
+  public int[] getStreamAbsRanks() { return streamAbsRanks; }
+
+  public double[] getSortedRelRanks() {
+    return relativeRank(sortedAbsRanks);
+  }
+
+  public double[] getStreamRelRanks() {
+    return relativeRank(streamAbsRanks);
+  }
+
+  public int getAbsRank(final double v) {
+    final int idx = BinarySearch.find(sortedStream, 0, length - 1, v);
+    return sortedAbsRanks[idx];
+  }
+
+  /**
+   * Sorts the stream, then computes the sortedAbsRanks based on the 
comparison criterion.
+   */
+  private void compute() {
+    length = stream.length;
+    sortedStream = stream.clone();
+    Arrays.sort(sortedStream);
+    sortedAbsRanks = new int[length];
+    if (ltEq) { //LE
+      sortedAbsRanks[length - 1] = length;
+      int i = length - 2;
+      while (i >= 0) { //goes backwards
+        if (sortedStream[i] == sortedStream[i + 1]) { sortedAbsRanks[i] = 
sortedAbsRanks[i + 1]; }
+        else { sortedAbsRanks[i] = i + 1; }
+        i--;
+      }
+    } else { // LT
+      sortedAbsRanks[0] = 0;
+      int i = 1;
+      while (i < length) { //forwards
+        if (sortedStream[i - 1] == sortedStream[i]) { sortedAbsRanks[i] = 
sortedAbsRanks[i - 1]; }
+        else { sortedAbsRanks[i] = i; }
+        i++;
+      }
+    }
+    streamAbsRanks = new int[length]; //put the ranks in original stream order
+    for (int j = 0; j < length; j++) {
+      final int idx = BinarySearch.find(sortedStream, 0, length - 1, 
stream[j]);
+      streamAbsRanks[j] = sortedAbsRanks[idx];
+    }
+  }
+
+  /**
+   * Converts an absolute rank array to a relative rank array.
+   * @param absRankArr the absolute rank array to be converted.
+   * @return the relative rank array.
+   */
+  public static double[] relativeRank(final int[] absRankArr) {
+    final int length = absRankArr.length;
+    final double[] relRank = new double[length];
+    for (int i = 0; i < length; i++) { relRank[i] = (double)absRankArr[i] / 
length; }
+    return relRank;
+  }
+
+  @Test
+  public void checkRanks() {
+    final double[] vArr = { 5, 5, 5, 6, 6, 6, 7, 8, 8, 8 };
+    checkRanksImpl(vArr);
+    println(LS + "SHUFFLED:");
+    Shuffle.shuffle(vArr);
+    checkRanksImpl(vArr);
+  }
+
+  private static void checkRanksImpl(final double[] vArr) {
+    final StringBuilder sb = new StringBuilder();
+    final String ffmt  = "%5.1f ";
+    final String dfmt    = "%5d ";
+    TrueDoubleRanks trueRanks;
+
+    final int N = vArr.length;
+    sb.append("Values:").append(LS);
+    for (int i = 0; i < N; i++) { sb.append(String.format(ffmt, vArr[i])); }
+    sb.append(LS);
+
+    trueRanks = new TrueDoubleRanks(vArr);
+    sb.append("LT Abs Ranks:").append(LS);
+    int[] absArr = trueRanks.getStreamAbsRanks();
+    for (int i = 0; i < N; i++) { sb.append(String.format(dfmt, absArr[i])); }
+    sb.append(LS);
+    sb.append("LT Rel Ranks:").append(LS);
+    double[] relArr = relativeRank(absArr);
+    for (int i = 0; i < N; i++) { sb.append(String.format(ffmt, relArr[i])); }
+    sb.append(LS);
+
+    trueRanks = new TrueDoubleRanks(vArr);
+    sb.append("LE Abs Ranks:").append(LS);
+    absArr = trueRanks.getStreamAbsRanks();
+    for (int i = 0; i < N; i++) { sb.append(String.format(dfmt, absArr[i])); }
+    sb.append(LS);
+    sb.append("LE Rel Ranks:").append(LS);
+    relArr = relativeRank(absArr);
+    for (int i = 0; i < N; i++) { sb.append(String.format(ffmt, relArr[i])); }
+    sb.append(LS);
+    println(sb.toString());
+  }
+
+  private static void println(final Object o) {
+    System.out.println(o.toString());
+  }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to