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\Shared\Trend;
4
 
5
use Matrix\Matrix;
6
 
7
// Phpstan and Scrutinizer seem to have legitimate complaints.
8
// $this->slope is specified where an array is expected in several places.
9
// But it seems that it should always be float.
10
// This code is probably not exercised at all in unit tests.
11
class PolynomialBestFit extends BestFit
12
{
13
    /**
14
     * Algorithm type to use for best-fit
15
     * (Name of this Trend class).
16
     */
17
    protected string $bestFitType = 'polynomial';
18
 
19
    /**
20
     * Polynomial order.
21
     */
22
    protected int $order = 0;
23
 
24
    /**
25
     * Return the order of this polynomial.
26
     */
27
    public function getOrder(): int
28
    {
29
        return $this->order;
30
    }
31
 
32
    /**
33
     * Return the Y-Value for a specified value of X.
34
     *
35
     * @param float $xValue X-Value
36
     *
37
     * @return float Y-Value
38
     */
39
    public function getValueOfYForX(float $xValue): float
40
    {
41
        $retVal = $this->getIntersect();
42
        $slope = $this->getSlope();
43
        // Phpstan and Scrutinizer are both correct - getSlope returns float, not array.
44
        // @phpstan-ignore-next-line
45
        foreach ($slope as $key => $value) {
46
            if ($value != 0.0) {
47
                $retVal += $value * $xValue ** ($key + 1);
48
            }
49
        }
50
 
51
        return $retVal;
52
    }
53
 
54
    /**
55
     * Return the X-Value for a specified value of Y.
56
     *
57
     * @param float $yValue Y-Value
58
     *
59
     * @return float X-Value
60
     */
61
    public function getValueOfXForY(float $yValue): float
62
    {
63
        return ($yValue - $this->getIntersect()) / $this->getSlope();
64
    }
65
 
66
    /**
67
     * Return the Equation of the best-fit line.
68
     *
69
     * @param int $dp Number of places of decimal precision to display
70
     */
71
    public function getEquation(int $dp = 0): string
72
    {
73
        $slope = $this->getSlope($dp);
74
        $intersect = $this->getIntersect($dp);
75
 
76
        $equation = 'Y = ' . $intersect;
77
        // Phpstan and Scrutinizer are both correct - getSlope returns float, not array.
78
        // @phpstan-ignore-next-line
79
        foreach ($slope as $key => $value) {
80
            if ($value != 0.0) {
81
                $equation .= ' + ' . $value . ' * X';
82
                if ($key > 0) {
83
                    $equation .= '^' . ($key + 1);
84
                }
85
            }
86
        }
87
 
88
        return $equation;
89
    }
90
 
91
    /**
92
     * Return the Slope of the line.
93
     *
94
     * @param int $dp Number of places of decimal precision to display
95
     */
96
    public function getSlope(int $dp = 0): float
97
    {
98
        if ($dp != 0) {
99
            $coefficients = [];
100
            //* @phpstan-ignore-next-line
101
            foreach ($this->slope as $coefficient) {
102
                $coefficients[] = round($coefficient, $dp);
103
            }
104
 
105
            // @phpstan-ignore-next-line
106
            return $coefficients;
107
        }
108
 
109
        return $this->slope;
110
    }
111
 
112
    public function getCoefficients(int $dp = 0): array
113
    {
114
        // Phpstan and Scrutinizer are both correct - getSlope returns float, not array.
115
        // @phpstan-ignore-next-line
116
        return array_merge([$this->getIntersect($dp)], $this->getSlope($dp));
117
    }
118
 
119
    /**
120
     * Execute the regression and calculate the goodness of fit for a set of X and Y data values.
121
     *
122
     * @param int $order Order of Polynomial for this regression
123
     * @param float[] $yValues The set of Y-values for this regression
124
     * @param float[] $xValues The set of X-values for this regression
125
     */
126
    private function polynomialRegression(int $order, array $yValues, array $xValues): void
127
    {
128
        // calculate sums
129
        $x_sum = array_sum($xValues);
130
        $y_sum = array_sum($yValues);
131
        $xx_sum = $xy_sum = $yy_sum = 0;
132
        for ($i = 0; $i < $this->valueCount; ++$i) {
133
            $xy_sum += $xValues[$i] * $yValues[$i];
134
            $xx_sum += $xValues[$i] * $xValues[$i];
135
            $yy_sum += $yValues[$i] * $yValues[$i];
136
        }
137
        /*
138
         *    This routine uses logic from the PHP port of polyfit version 0.1
139
         *    written by Michael Bommarito and Paul Meagher
140
         *
141
         *    The function fits a polynomial function of order $order through
142
         *    a series of x-y data points using least squares.
143
         *
144
         */
145
        $A = [];
146
        $B = [];
147
        for ($i = 0; $i < $this->valueCount; ++$i) {
148
            for ($j = 0; $j <= $order; ++$j) {
149
                $A[$i][$j] = $xValues[$i] ** $j;
150
            }
151
        }
152
        for ($i = 0; $i < $this->valueCount; ++$i) {
153
            $B[$i] = [$yValues[$i]];
154
        }
155
        $matrixA = new Matrix($A);
156
        $matrixB = new Matrix($B);
157
        $C = $matrixA->solve($matrixB);
158
 
159
        $coefficients = [];
160
        for ($i = 0; $i < $C->rows; ++$i) {
161
            $r = $C->getValue($i + 1, 1); // row and column are origin-1
162
            if (!is_numeric($r) || abs($r + 0) <= 10 ** (-9)) {
163
                $r = 0;
164
            } else {
165
                $r += 0;
166
            }
167
            $coefficients[] = $r;
168
        }
169
 
170
        $this->intersect = (float) array_shift($coefficients);
171
        // Phpstan is correct
172
        //* @phpstan-ignore-next-line
173
        $this->slope = $coefficients;
174
 
175
        $this->calculateGoodnessOfFit($x_sum, $y_sum, $xx_sum, $yy_sum, $xy_sum, 0, 0, 0);
176
        foreach ($this->xValues as $xKey => $xValue) {
177
            $this->yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
178
        }
179
    }
180
 
181
    /**
182
     * Define the regression and calculate the goodness of fit for a set of X and Y data values.
183
     *
184
     * @param int $order Order of Polynomial for this regression
185
     * @param float[] $yValues The set of Y-values for this regression
186
     * @param float[] $xValues The set of X-values for this regression
187
     */
188
    public function __construct(int $order, array $yValues, array $xValues = [])
189
    {
190
        parent::__construct($yValues, $xValues);
191
 
192
        if (!$this->error) {
193
            if ($order < $this->valueCount) {
194
                $this->bestFitType .= '_' . $order;
195
                $this->order = $order;
196
                $this->polynomialRegression($order, $yValues, $xValues);
197
                if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) {
198
                    $this->error = true;
199
                }
200
            } else {
201
                $this->error = true;
202
            }
203
        }
204
    }
205
}