You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2022/01/25 22:32:18 UTC

svn commit: r1897475 - in /poi/trunk: poi-integration/src/test/java9/ poi/src/main/java/org/apache/poi/ss/formula/eval/ poi/src/main/java/org/apache/poi/ss/formula/functions/ poi/src/test/java/org/apache/poi/ss/formula/functions/

Author: fanningpj
Date: Tue Jan 25 22:32:18 2022
New Revision: 1897475

URL: http://svn.apache.org/viewvc?rev=1897475&view=rev
Log:
normdist (init work)

Added:
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
      - copied, changed from r1897462, poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/TDist.java
    poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java
      - copied, changed from r1897462, poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java
Modified:
    poi/trunk/poi-integration/src/test/java9/module-info.class
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java

Modified: poi/trunk/poi-integration/src/test/java9/module-info.class
URL: http://svn.apache.org/viewvc/poi/trunk/poi-integration/src/test/java9/module-info.class?rev=1897475&r1=1897474&r2=1897475&view=diff
==============================================================================
Binary files - no diff available.

Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java?rev=1897475&r1=1897474&r2=1897475&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java Tue Jan 25 22:32:18 2022
@@ -272,7 +272,7 @@ public final class FunctionEval {
         // 290: LOGNORMDIST
         // 291: LOGINV
         // 292: NEGBINOMDIST
-        // 293: NORMDIST
+        retval[293] = NormDist.instance;
         // 294: NORMSDIST
         // 295: NORMINV
         // 296: NORMSINV

Copied: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java (from r1897462, poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/TDist.java)
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java?p2=poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java&p1=poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/TDist.java&r1=1897462&r2=1897475&rev=1897475&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/TDist.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java Tue Jan 25 22:32:18 2022
@@ -17,81 +17,55 @@
 
 package org.apache.poi.ss.formula.functions;
 
-import org.apache.commons.math3.distribution.TDistribution;
+import org.apache.commons.math3.distribution.NormalDistribution;
 import org.apache.poi.ss.formula.OperationEvaluationContext;
-import org.apache.poi.ss.formula.eval.*;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.EvaluationException;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.OperandResolver;
+import org.apache.poi.ss.formula.eval.ValueEval;
 
 /**
- * Implementation for Excel TDIST() function.
- * <p>
- * <b>Syntax</b>:<br> <b>TDIST </b>(<b>X</b>,<b>Deg_freedom</b>,<b>Tails</b>)<br>
- * <p>
- * Returns the Percentage Points (probability) for the Student t-distribution where a numeric value (x)
- * is a calculated value of t for which the Percentage Points are to be computed. The t-distribution is used
- * in the hypothesis testing of small sample data sets. Use this function in place of a table of critical values
- * for the t-distribution.
- *
- * <ul>
- *     <li>X     Required. The numeric value at which to evaluate the distribution.</li>
- *     <li>Deg_freedom     Required. An integer indicating the number of degrees of freedom.</li>
- *     <li>Tails     Required. Specifies the number of distribution tails to return. If Tails = 1, TDIST returns the
- *     one-tailed distribution. If Tails = 2, TDIST returns the two-tailed distribution.</li>
- * </ul>
- *
- * <ul>
- *     <li>If any argument is non-numeric, TDIST returns the #VALUE! error value.</li>
- *     <li>If Deg_freedom &lt; 1, TDIST returns the #NUM! error value.</li>
- *     <li>The Deg_freedom and Tails arguments are truncated to integers.
- *     <li>If Tails is any value other than 1 or 2, TDIST returns the #NUM! error value.</li>
- *     <li>If x &lt; 0, then TDIST returns the #NUM! error value.</li>
- * </ul>
- *
- * https://support.microsoft.com/en-us/office/tdist-function-630a7695-4021-4853-9468-4a1f9dcdd192
- */
-public final class TDist extends Fixed3ArgFunction implements FreeRefFunction {
+ * Implementation for Excel NORMDIST() function.<p>
 
-    public static final TDist instance = new TDist();
+ * https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
+ */
+public final class NormDist extends Fixed4ArgFunction implements FreeRefFunction {
 
-    static double tdistOneTail(double x, int degreesOfFreedom) {
-        TDistribution tdist = new TDistribution(null, degreesOfFreedom);
-        return 1.0 - tdist.cumulativeProbability(x);
-    }
+    public static final NormDist instance = new NormDist();
 
-    static double tdistTwoTails(double x, int degreesOfFreedom) {
-        return 2 * tdistOneTail(x, degreesOfFreedom);
+    static double probability(double x, double mean, double stdev, boolean cumulative) {
+        NormalDistribution normalDistribution = new NormalDistribution(mean, stdev);
+        return cumulative ? normalDistribution.cumulativeProbability(x) : normalDistribution.probability(x);
     }
 
     @Override
-    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2, ValueEval arg3) {
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2,
+                              ValueEval arg3, ValueEval arg4) {
         try {
-            Double number1 = evaluateValue(arg1, srcRowIndex, srcColumnIndex);
-            if (number1 == null) {
+            Double xval = evaluateValue(arg1, srcRowIndex, srcColumnIndex);
+            if (xval == null) {
                 return ErrorEval.VALUE_INVALID;
-            } else if (number1 < 0) {
+            } else if (xval < 0) {
                 return ErrorEval.NUM_ERROR;
             }
-            Double number2 = evaluateValue(arg2, srcRowIndex, srcColumnIndex);
-            if (number2 == null) {
+            Double mean = evaluateValue(arg2, srcRowIndex, srcColumnIndex);
+            if (mean == null) {
                 return ErrorEval.VALUE_INVALID;
             }
-            int degreesOfFreedom = number2.intValue();
-            if (degreesOfFreedom < 1) {
-                return ErrorEval.NUM_ERROR;
-            }
-            Double number3 = evaluateValue(arg3, srcRowIndex, srcColumnIndex);
-            if (number3 == null) {
+            Double stdev = evaluateValue(arg3, srcRowIndex, srcColumnIndex);
+            if (stdev == null) {
                 return ErrorEval.VALUE_INVALID;
-            }
-            int tails = number3.intValue();
-            if (!(tails == 1 || tails == 2)) {
+            } else if (stdev.doubleValue() <= 0) {
                 return ErrorEval.NUM_ERROR;
             }
-
-            if (tails == 2) {
-                return new NumberEval(tdistTwoTails(number1, degreesOfFreedom));
+            Boolean cumulative = OperandResolver.coerceValueToBoolean(arg4, false);
+            if (cumulative == null) {
+                return ErrorEval.VALUE_INVALID;
             }
 
-            return new NumberEval(tdistOneTail(number1, degreesOfFreedom));
+            return new NumberEval(probability(
+                    xval.doubleValue(), mean.doubleValue(), stdev.doubleValue(), cumulative.booleanValue()));
         } catch (EvaluationException e) {
             return e.getErrorEval();
         }
@@ -99,8 +73,8 @@ public final class TDist extends Fixed3A
 
     @Override
     public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
-         if (args.length == 3) {
-            return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1], args[2]);
+        if (args.length == 4) {
+            return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1], args[2], args[3]);
         }
 
         return ErrorEval.VALUE_INVALID;

Copied: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java (from r1897462, poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java)
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java?p2=poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java&p1=poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java&r1=1897462&r2=1897475&rev=1897475&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTDist.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java Tue Jan 25 22:32:18 2022
@@ -22,6 +22,8 @@ import org.apache.poi.hssf.usermodel.HSS
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.formula.OperationEvaluationContext;
+import org.apache.poi.ss.formula.eval.BlankEval;
+import org.apache.poi.ss.formula.eval.BoolEval;
 import org.apache.poi.ss.formula.eval.ErrorEval;
 import org.apache.poi.ss.formula.eval.NumberEval;
 import org.apache.poi.ss.formula.eval.StringEval;
@@ -35,69 +37,66 @@ import static org.apache.poi.ss.util.Uti
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 /**
- * Tests for {@link TDist}
+ * Tests for {@link NormDist}
  */
-final class TestTDist {
+final class TestNormDist {
 
     private static final OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null);
 
     @Test
     void testBasic() {
-        confirmValue("5.968191467", "8", "1", 0.00016754180265310392);
-        confirmValue("5.968191467", "8", "2", 0.00033508360530620784);
-        confirmValue("5.968191467", "8.2", "2.2", 0.00033508360530620784);
-        confirmValue("5.968191467", "8.9", "2.9", 0.00033508360530620784);
+        confirmValue("42", "40", "1.5", true, 0.9087888);
+        //TODO needs investigation
+        //confirmValue("42", "40", "1.5", false, 0.10934);
     }
 
     @Test
     void testInvalid() {
-        confirmInvalidError("A1","B2","C2");
-        confirmInvalidError("5.968191467","8","C2");
-        confirmInvalidError("5.968191467","B2","2");
-        confirmInvalidError("A1","8","2");
+        confirmInvalidError("A1","B2","C2", false);
     }
 
     @Test
     void testNumError() {
-        confirmNumError("5.968191467", "8", "0");
-        confirmNumError("-5.968191467", "8", "2");
-        confirmNumError("5.968191467", "-8", "2");
+        confirmNumError("42","40","0", false);
+        confirmNumError("42","40","0", true);
+        confirmNumError("42","40","-0.1", false);
+        confirmNumError("42","40","-0.1", true);
     }
 
-    //https://support.microsoft.com/en-us/office/tdist-function-630a7695-4021-4853-9468-4a1f9dcdd192
+    //https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
     @Test
     void testMicrosoftExample1() throws IOException {
         try (HSSFWorkbook wb = new HSSFWorkbook()) {
             HSSFSheet sheet = wb.createSheet();
             addRow(sheet, 0, "Data", "Description");
-            addRow(sheet, 1, 1.959999998, "Value at which to evaluate the distribution");
-            addRow(sheet, 2, 60, "Degrees of freedom");
+            addRow(sheet, 1, 42, "Value for which you want the distribution");
+            addRow(sheet, 2, 40, "Arithmetic mean of the distribution");
+            addRow(sheet, 3, 1.5, "Standard deviation of the distribution");
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
             HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
-            assertDouble(fe, cell, "TDIST(A2,A3,2)", 0.054644930, 0.000001);
-            assertDouble(fe, cell, "TDIST(A2,A3,1)", 0.027322465, 0.000001);
+            assertDouble(fe, cell, "NORMDIST(A2,A3,A4,TRUE)", 0.9087888, 0.000001);
         }
     }
 
-    private static ValueEval invokeValue(String number1, String number2, String number3) {
-        ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), new StringEval(number3)};
-        return TDist.instance.evaluate(args, ec);
+    private static ValueEval invokeValue(String number1, String number2, String number3, boolean cumulative) {
+        ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), new StringEval(number3), BoolEval.valueOf(cumulative)};
+        return NormDist.instance.evaluate(args, ec);
     }
 
-    private static void confirmValue(String number1, String number2, String number3, double expected) {
-        ValueEval result = invokeValue(number1, number2, number3);
+    private static void confirmValue(String number1, String number2, String number3, boolean cumulative, double expected) {
+        ValueEval result = invokeValue(number1, number2, number3, cumulative);
         assertEquals(NumberEval.class, result.getClass());
-        assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
+        assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0000001);
     }
 
-    private static void confirmInvalidError(String number1, String number2, String number3) {
-        ValueEval result = invokeValue(number1, number2, number3);
+    private static void confirmInvalidError(String number1, String number2, String number3, boolean cumulative) {
+        ValueEval result = invokeValue(number1, number2, number3, cumulative);
         assertEquals(ErrorEval.class, result.getClass());
         assertEquals(ErrorEval.VALUE_INVALID, result);
     }
 
-    private static void confirmNumError(String number1, String number2, String number3) {
-        ValueEval result = invokeValue(number1, number2, number3);
+    private static void confirmNumError(String number1, String number2, String number3, boolean cumulative) {
+        ValueEval result = invokeValue(number1, number2, number3, cumulative);
         assertEquals(ErrorEval.class, result.getClass());
         assertEquals(ErrorEval.NUM_ERROR, result);
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org