Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Statistical;
4
 
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
 
8
class Deviations
9
{
10
    /**
11
     * DEVSQ.
12
     *
13
     * Returns the sum of squares of deviations of data points from their sample mean.
14
     *
15
     * Excel Function:
16
     *        DEVSQ(value1[,value2[, ...]])
17
     *
18
     * @param mixed ...$args Data values
19
     */
20
    public static function sumSquares(mixed ...$args): string|float
21
    {
22
        $aArgs = Functions::flattenArrayIndexed($args);
23
 
24
        $aMean = Averages::average($aArgs);
25
        if (!is_numeric($aMean)) {
26
            return ExcelError::NAN();
27
        }
28
 
29
        // Return value
30
        $returnValue = 0.0;
31
        $aCount = -1;
32
        foreach ($aArgs as $k => $arg) {
33
            // Is it a numeric value?
34
            if (
35
                (is_bool($arg))
36
                && ((!Functions::isCellValue($k))
37
                    || (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE))
38
            ) {
39
                $arg = (int) $arg;
40
            }
41
            if ((is_numeric($arg)) && (!is_string($arg))) {
42
                $returnValue += ($arg - $aMean) ** 2;
43
                ++$aCount;
44
            }
45
        }
46
 
47
        return $aCount === 0 ? ExcelError::VALUE() : $returnValue;
48
    }
49
 
50
    /**
51
     * KURT.
52
     *
53
     * Returns the kurtosis of a data set. Kurtosis characterizes the relative peakedness
54
     * or flatness of a distribution compared with the normal distribution. Positive
55
     * kurtosis indicates a relatively peaked distribution. Negative kurtosis indicates a
56
     * relatively flat distribution.
57
     *
58
     * @param array ...$args Data Series
59
     */
60
    public static function kurtosis(...$args): string|int|float
61
    {
62
        $aArgs = Functions::flattenArrayIndexed($args);
63
        $mean = Averages::average($aArgs);
64
        if (!is_numeric($mean)) {
65
            return ExcelError::DIV0();
66
        }
67
        $stdDev = (float) StandardDeviations::STDEV($aArgs);
68
 
69
        if ($stdDev > 0) {
70
            $count = $summer = 0;
71
 
72
            foreach ($aArgs as $k => $arg) {
73
                if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
74
                } else {
75
                    // Is it a numeric value?
76
                    if ((is_numeric($arg)) && (!is_string($arg))) {
77
                        $summer += (($arg - $mean) / $stdDev) ** 4;
78
                        ++$count;
79
                    }
80
                }
81
            }
82
 
83
            if ($count > 3) {
84
                return $summer * ($count * ($count + 1)
85
                        / (($count - 1) * ($count - 2) * ($count - 3))) - (3 * ($count - 1) ** 2
86
                        / (($count - 2) * ($count - 3)));
87
            }
88
        }
89
 
90
        return ExcelError::DIV0();
91
    }
92
 
93
    /**
94
     * SKEW.
95
     *
96
     * Returns the skewness of a distribution. Skewness characterizes the degree of asymmetry
97
     * of a distribution around its mean. Positive skewness indicates a distribution with an
98
     * asymmetric tail extending toward more positive values. Negative skewness indicates a
99
     * distribution with an asymmetric tail extending toward more negative values.
100
     *
101
     * @param array ...$args Data Series
102
     *
103
     * @return float|int|string The result, or a string containing an error
104
     */
105
    public static function skew(...$args): string|int|float
106
    {
107
        $aArgs = Functions::flattenArrayIndexed($args);
108
        $mean = Averages::average($aArgs);
109
        if (!is_numeric($mean)) {
110
            return ExcelError::DIV0();
111
        }
112
        $stdDev = StandardDeviations::STDEV($aArgs);
113
        if ($stdDev === 0.0 || is_string($stdDev)) {
114
            return ExcelError::DIV0();
115
        }
116
 
117
        $count = $summer = 0;
118
        // Loop through arguments
119
        foreach ($aArgs as $k => $arg) {
120
            if ((is_bool($arg)) && (!Functions::isMatrixValue($k))) {
121
            } elseif (!is_numeric($arg)) {
122
                return ExcelError::VALUE();
123
            } else {
124
                // Is it a numeric value?
125
                if (!is_string($arg)) {
126
                    $summer += (($arg - $mean) / $stdDev) ** 3;
127
                    ++$count;
128
                }
129
            }
130
        }
131
 
132
        if ($count > 2) {
133
            return $summer * ($count / (($count - 1) * ($count - 2)));
134
        }
135
 
136
        return ExcelError::DIV0();
137
    }
138
}