Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Percentile-type functions from Statistics #1966

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,7 @@ class Calculation
],
'PERCENTILE' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'PERCENTILE'],
'functionCall' => [Statistical\Percentiles::class, 'PERCENTILE'],
'argumentCount' => '2',
],
'PERCENTILE.EXC' => [
Expand All @@ -1913,12 +1913,12 @@ class Calculation
],
'PERCENTILE.INC' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'PERCENTILE'],
'functionCall' => [Statistical\Percentiles::class, 'PERCENTILE'],
'argumentCount' => '2',
],
'PERCENTRANK' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'PERCENTRANK'],
'functionCall' => [Statistical\Percentiles::class, 'PERCENTRANK'],
'argumentCount' => '2,3',
],
'PERCENTRANK.EXC' => [
Expand All @@ -1928,7 +1928,7 @@ class Calculation
],
'PERCENTRANK.INC' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'PERCENTRANK'],
'functionCall' => [Statistical\Percentiles::class, 'PERCENTRANK'],
'argumentCount' => '2,3',
],
'PERMUT' => [
Expand Down Expand Up @@ -2018,7 +2018,7 @@ class Calculation
],
'QUARTILE' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'QUARTILE'],
'functionCall' => [Statistical\Percentiles::class, 'QUARTILE'],
'argumentCount' => '2',
],
'QUARTILE.EXC' => [
Expand All @@ -2028,7 +2028,7 @@ class Calculation
],
'QUARTILE.INC' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'QUARTILE'],
'functionCall' => [Statistical\Percentiles::class, 'QUARTILE'],
'argumentCount' => '2',
],
'QUOTIENT' => [
Expand Down Expand Up @@ -2058,7 +2058,7 @@ class Calculation
],
'RANK' => [
'category' => Category::CATEGORY_STATISTICAL,
'functionCall' => [Statistical::class, 'RANK'],
'functionCall' => [Statistical\Percentiles::class, 'RANK'],
'argumentCount' => '2,3',
],
'RANK.AVG' => [
Expand Down
132 changes: 27 additions & 105 deletions src/PhpSpreadsheet/Calculation/Statistical.php
Original file line number Diff line number Diff line change
Expand Up @@ -1665,45 +1665,18 @@ public static function NORMSINV($value)
* Excel Function:
* PERCENTILE(value1[,value2[, ...]],entry)
*
* @Deprecated 1.18.0
*
* @see Statistical\Percentiles::PERCENTILE()
* Use the PERCENTILE() method in the Statistical\Percentiles class instead
*
* @param mixed $args Data values
*
* @return float|string The result, or a string containing an error
*/
public static function PERCENTILE(...$args)
{
$aArgs = Functions::flattenArray($args);

// Calculate
$entry = array_pop($aArgs);

if ((is_numeric($entry)) && (!is_string($entry))) {
if (($entry < 0) || ($entry > 1)) {
return Functions::NAN();
}
$mArgs = [];
foreach ($aArgs as $arg) {
// Is it a numeric value?
if ((is_numeric($arg)) && (!is_string($arg))) {
$mArgs[] = $arg;
}
}
$mValueCount = count($mArgs);
if ($mValueCount > 0) {
sort($mArgs);
$count = Counts::COUNT($mArgs);
$index = $entry * ($count - 1);
$iBase = floor($index);
if ($index == $iBase) {
return $mArgs[$index];
}
$iNext = $iBase + 1;
$iProportion = $index - $iBase;

return $mArgs[$iBase] + (($mArgs[$iNext] - $mArgs[$iBase]) * $iProportion);
}
}

return Functions::VALUE();
return Statistical\Percentiles::PERCENTILE(...$args);
}

/**
Expand All @@ -1714,6 +1687,11 @@ public static function PERCENTILE(...$args)
* rather than floored (as MS Excel), so value 3 for a value set of 1, 2, 3, 4 will return
* 0.667 rather than 0.666
*
* @Deprecated 1.18.0
*
* @see Statistical\Percentiles::PERCENTRANK()
* Use the PERCENTRANK() method in the Statistical\Percentiles class instead
*
* @param mixed (float[]) $valueSet An array of, or a reference to, a list of numbers
* @param mixed (int) $value the number whose rank you want to find
* @param mixed (int) $significance the number of significant digits for the returned percentage value
Expand All @@ -1722,38 +1700,7 @@ public static function PERCENTILE(...$args)
*/
public static function PERCENTRANK($valueSet, $value, $significance = 3)
{
$valueSet = Functions::flattenArray($valueSet);
$value = Functions::flattenSingleValue($value);
$significance = ($significance === null) ? 3 : (int) Functions::flattenSingleValue($significance);

foreach ($valueSet as $key => $valueEntry) {
if (!is_numeric($valueEntry)) {
unset($valueSet[$key]);
}
}
sort($valueSet, SORT_NUMERIC);
$valueCount = count($valueSet);
if ($valueCount == 0) {
return Functions::NAN();
}

$valueAdjustor = $valueCount - 1;
if (($value < $valueSet[0]) || ($value > $valueSet[$valueAdjustor])) {
return Functions::NA();
}

$pos = array_search($value, $valueSet);
if ($pos === false) {
$pos = 0;
$testValue = $valueSet[0];
while ($testValue < $value) {
$testValue = $valueSet[++$pos];
}
--$pos;
$pos += (($value - $valueSet[$pos]) / ($testValue - $valueSet[$pos]));
}

return round($pos / $valueAdjustor, $significance);
return Statistical\Percentiles::PERCENTRANK($valueSet, $value, $significance);
}

/**
Expand Down Expand Up @@ -1811,64 +1758,39 @@ public static function POISSON($value, $mean, $cumulative)
* Excel Function:
* QUARTILE(value1[,value2[, ...]],entry)
*
* @Deprecated 1.18.0
*
* @see Statistical\Percentiles::QUARTILE()
* Use the QUARTILE() method in the Statistical\Percentiles class instead
*
* @param mixed $args Data values
*
* @return float|string The result, or a string containing an error
*/
public static function QUARTILE(...$args)
{
$aArgs = Functions::flattenArray($args);
$entry = array_pop($aArgs);

// Calculate
if ((is_numeric($entry)) && (!is_string($entry))) {
$entry = floor($entry);
$entry /= 4;
if (($entry < 0) || ($entry > 1)) {
return Functions::NAN();
}

return self::PERCENTILE($aArgs, $entry);
}

return Functions::VALUE();
return Statistical\Percentiles::QUARTILE(...$args);
}

/**
* RANK.
*
* Returns the rank of a number in a list of numbers.
*
* @param int $value the number whose rank you want to find
* @param float[] $valueSet An array of, or a reference to, a list of numbers
* @param int $order Order to sort the values in the value set
* @Deprecated 1.18.0
*
* @see Statistical\Percentiles::RANK()
* Use the RANK() method in the Statistical\Percentiles class instead
*
* @param mixed (float) $value the number whose rank you want to find
* @param mixed (float[]) $valueSet An array of, or a reference to, a list of numbers
* @param mixed (int) $order Order to sort the values in the value set
*
* @return float|string The result, or a string containing an error
*/
public static function RANK($value, $valueSet, $order = 0)
{
$value = Functions::flattenSingleValue($value);
$valueSet = Functions::flattenArray($valueSet);
$order = ($order === null) ? 0 : (int) Functions::flattenSingleValue($order);

foreach ($valueSet as $key => $valueEntry) {
if (!is_numeric($valueEntry)) {
unset($valueSet[$key]);
}
}

if ($order == 0) {
sort($valueSet, SORT_NUMERIC);
} else {
rsort($valueSet, SORT_NUMERIC);
}

$pos = array_search($value, $valueSet);
if ($pos === false) {
return Functions::NA();
}

return ++$pos;
return Statistical\Percentiles::RANK($value, $valueSet, $order);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/PhpSpreadsheet/Calculation/Statistical/BaseValidations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;

trait BaseValidations
{
protected static function validateFloat($value): float
{
if (!is_numeric($value)) {
throw new Exception(Functions::VALUE());
}

return (float) $value;
}

protected static function validateInt($value): int
{
if (!is_numeric($value)) {
throw new Exception(Functions::VALUE());
}

return (int) floor($value);
}
}
23 changes: 13 additions & 10 deletions src/PhpSpreadsheet/Calculation/Statistical/Confidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;

use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;

class Confidence
{
use BaseValidations;

/**
* CONFIDENCE.
*
Expand All @@ -24,18 +27,18 @@ public static function CONFIDENCE($alpha, $stdDev, $size)
$stdDev = Functions::flattenSingleValue($stdDev);
$size = Functions::flattenSingleValue($size);

if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size))) {
$size = floor($size);
if (($alpha <= 0) || ($alpha >= 1)) {
return Functions::NAN();
}
if (($stdDev <= 0) || ($size < 1)) {
return Functions::NAN();
}
try {
$alpha = self::validateFloat($alpha);
$stdDev = self::validateFloat($stdDev);
$size = self::validateInt($size);
} catch (Exception $e) {
return $e->getMessage();
}

return Statistical::NORMSINV(1 - $alpha / 2) * $stdDev / sqrt($size);
if (($alpha <= 0) || ($alpha >= 1) || ($stdDev <= 0) || ($size < 1)) {
return Functions::NAN();
}

return Functions::VALUE();
return Statistical::NORMSINV(1 - $alpha / 2) * $stdDev / sqrt($size);
}
}
Loading