[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    branches/work/koffice-ko/kspread
From:       Marijn Kruisselbrink <m.kruisselbrink () student ! tue ! nl>
Date:       2010-04-18 15:18:26
Message-ID: 20100418151826.0C89EAC89C () svn ! kde ! org
[Download RAW message or body]

SVN commit 1116069 by mkruisselbrink:

implement PERCENTILE function


 M  +40 -0     functions/statistical.cpp  
 M  +24 -5     functions/statistical.xml  
 M  +13 -0     tests/TestStatisticalFunctions.cpp  
 M  +1 -0      tests/TestStatisticalFunctions.h  


--- branches/work/koffice-ko/kspread/functions/statistical.cpp #1116068:1116069
@@ -87,6 +87,7 @@
 Value func_normdist(valVector args, ValueCalc *calc, FuncExtra *);
 Value func_norminv(valVector args, ValueCalc *calc, FuncExtra *);
 Value func_normsinv(valVector args, ValueCalc *calc, FuncExtra *);
+Value func_percentile(valVector args, ValueCalc *calc, FuncExtra *);
 Value func_phi(valVector args, ValueCalc *calc, FuncExtra *);
 Value func_poisson(valVector args, ValueCalc *calc, FuncExtra *);
 Value func_rank(valVector args, ValueCalc *calc, FuncExtra *);
@@ -312,6 +313,10 @@
     f->setParamCount(2);
     f->setAcceptArray();
     repo->add(f);
+    f = new Function("PERCENTILE", func_percentile);
+    f->setParamCount(2);
+    f->setAcceptArray();
+    repo->add(f);
     f = new Function("PERMUT", func_arrang);
     f->setParamCount(2);
     repo->add(f);
@@ -2143,6 +2148,41 @@
 }
 
 //
+// Function: percentile
+//
+// PERCENTILE( data set; alpha )
+//
+Value func_percentile(valVector args, ValueCalc *calc, FuncExtra*)
+{
+    double alpha = numToDouble(calc->conv()->toFloat(args[1]));
+
+    // create array - does NOT support anything other than doubles !!!
+    List array;
+    int number = 0;
+
+    func_array_helper(args[0], calc, array, number);
+
+    // check constraints - number of values must be > 0 and flag >0 <=4
+    if (number == 0)
+        return Value::errorNA(); // or VALUE?
+    if (alpha < -1e-9 || alpha > 1 + 1e-9)
+        return Value::errorVALUE();
+
+    // sort values
+    qSort(array);
+
+    if (number == 1)
+        return Value(array[0]); // only one value
+    else {
+        double r = alpha * (number - 1);
+        int index = ::floor(r);
+        double d = r - index;
+        return Value(array[index] + d * (array[index+1] - array[index]));
+    }
+}
+
+
+//
 // Function: phi
 //
 // distribution function for a standard normal distribution
--- branches/work/koffice-ko/kspread/functions/statistical.xml #1116068:1116069
@@ -944,6 +944,25 @@
     </Function>
 
     <Function>
+      <Name>PERCENTILE</Name>
+      <Type>Float</Type>
+      <Parameter>
+        <Comment>Range of values</Comment>
+        <Type range="true">Float</Type>
+      </Parameter>
+      <Parameter>
+        <Comment>The percentile value between 0 and 1, inclusive.</Comment>
+        <Type>Float</Type>
+      </Parameter>
+      <Help>
+        <Text>The PERCENTILE() function returns the x-th sample percentile of data \
values in Data. A percentile returns the scale value for a data series which goes \
from the smallest (alpha=0) to the largest value (alpha=1) of a data series. For \
alpha = 25%, the percentile means the first quartile; alpha = 50% is the MEDIAN. \
Blank cells will be considered as a zero, and cells with text will be ignored.</Text> \
+        <Syntax>PERCENTILE(data;alpha)</Syntax> +        <Related>MEDIAN</Related>
+        <Related>QUARTILE</Related>
+      </Help>
+    </Function>
+
+    <Function>
       <Name>PHI</Name>
       <Type>Float</Type>
       <Parameter>
@@ -1198,7 +1217,7 @@
         <Example>STDEVA(6; 7; A1; 8) equals 1, if A1 is empty</Example>
         <Example>STDEVA(6; 7; A1; 8) equals 3.109, if A1 is TRUE</Example>
         <Related>STDEV</Related>
-        <Related>STDEVP</Related> 
+        <Related>STDEVP</Related>
       </Help>
     </Function>
 
@@ -1229,10 +1248,10 @@
         <Text>The STDEVP() function returns the standard deviation based on an \
entire population</Text>  <Syntax>STDEVP(value;value;...)</Syntax>
         <Example>STDEVP(6;7;8) equals 0.816497...</Example>
-        <Related>STDEV</Related> 
+        <Related>STDEV</Related>
       </Help>
     </Function>
-    
+
     <Function>
       <Name>STDEVPA</Name>
       <Type>Float</Type>
@@ -1262,8 +1281,8 @@
         <Example>STDEVPA(6; 7; A1; 8) equals 0.816497..., if A1 is empty</Example>
         <Example>STDEVPA(6; 7; A1; 8) equals 2.69..., if A1 is TRUE</Example>
         <Example>STDEVPA(6; 7; A1; 8) equals 3.11..., if A1 is FALSE</Example>
-        <Related>STDEV</Related> 
-        <Related>STDEVP</Related> 
+        <Related>STDEV</Related>
+        <Related>STDEVP</Related>
       </Help>
     </Function>
 
--- branches/work/koffice-ko/kspread/tests/TestStatisticalFunctions.cpp \
#1116068:1116069 @@ -911,6 +911,19 @@
     CHECK_EVAL("PEARSON(C51:C57;D51:D56)", Value::errorNUM());     //
 }
 
+void TestStatisticalFunctions::testPERCENTILE()
+{
+    // ODF-tests
+    CHECK_EVAL("PERCENTILE(A19:A31;0.38)",          Value(24.96));        //
+    CHECK_EVAL("PERCENTILE(A19:A31;0.95)",          Value(2867.2));       //
+    CHECK_EVAL("PERCENTILE(A19:A31;0.05)",          Value(1.6));          //
+
+    // my tests
+    CHECK_EVAL("PERCENTILE(A10:A15;-0.1)",          Value::errorVALUE()); //
+    CHECK_EVAL("PERCENTILE(A19:A25;1.1)",           Value::errorVALUE()); //
+
+}
+
 void TestStatisticalFunctions::testPERMUT()
 {
     // ODF-tests
--- branches/work/koffice-ko/kspread/tests/TestStatisticalFunctions.h \
#1116068:1116069 @@ -99,6 +99,7 @@
 //     void testNORMSINV();
 
     void testPEARSON();
+    void testPERCENTILE();
     void testPERMUT();
     void testPHI();
     void testPOISSON();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic