Passed
Push — master ( 10b137...2bffcf )
by Adrien
10:25
created

ExponentialBestFit   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 112
ccs 25
cts 27
cp 0.9259
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValueOfXForY() 0 3 1
A getValueOfYForX() 0 3 1
A getIntersect() 0 7 2
A getEquation() 0 6 1
A getSlope() 0 7 2
A __construct() 0 6 2
A exponentialRegression() 0 10 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared\Trend;
4
5
class ExponentialBestFit extends BestFit
6
{
7
    /**
8
     * Algorithm type to use for best-fit
9
     * (Name of this Trend class).
10
     *
11
     * @var string
12
     */
13
    protected $bestFitType = 'exponential';
14
15
    /**
16
     * Return the Y-Value for a specified value of X.
17
     *
18
     * @param float $xValue X-Value
19
     *
20
     * @return float Y-Value
21
     */
22 8
    public function getValueOfYForX($xValue)
23
    {
24 8
        return $this->getIntersect() * $this->getSlope() ** ($xValue - $this->xOffset);
25
    }
26
27
    /**
28
     * Return the X-Value for a specified value of Y.
29
     *
30
     * @param float $yValue Y-Value
31
     *
32
     * @return float X-Value
33
     */
34
    public function getValueOfXForY($yValue)
35
    {
36
        return log(($yValue + $this->yOffset) / $this->getIntersect()) / log($this->getSlope());
37
    }
38
39
    /**
40
     * Return the Equation of the best-fit line.
41
     *
42
     * @param int $dp Number of places of decimal precision to display
43
     *
44
     * @return string
45
     */
46 1
    public function getEquation($dp = 0)
47
    {
48 1
        $slope = $this->getSlope($dp);
49 1
        $intersect = $this->getIntersect($dp);
50
51 1
        return 'Y = ' . $intersect . ' * ' . $slope . '^X';
52
    }
53
54
    /**
55
     * Return the Slope of the line.
56
     *
57
     * @param int $dp Number of places of decimal precision to display
58
     *
59
     * @return float
60
     */
61 9
    public function getSlope($dp = 0)
62
    {
63 9
        if ($dp != 0) {
64 1
            return round(exp($this->slope), $dp);
65
        }
66
67 9
        return exp($this->slope);
68
    }
69
70
    /**
71
     * Return the Value of X where it intersects Y = 0.
72
     *
73
     * @param int $dp Number of places of decimal precision to display
74
     *
75
     * @return float
76
     */
77 9
    public function getIntersect($dp = 0)
78
    {
79 9
        if ($dp != 0) {
80 1
            return round(exp($this->intersect), $dp);
81
        }
82
83 9
        return exp($this->intersect);
84
    }
85
86
    /**
87
     * Execute the regression and calculate the goodness of fit for a set of X and Y data values.
88
     *
89
     * @param float[] $yValues The set of Y-values for this regression
90
     * @param float[] $xValues The set of X-values for this regression
91
     */
92 7
    private function exponentialRegression(array $yValues, array $xValues, bool $const): void
93
    {
94 7
        $adjustedYValues = array_map(
95 7
            function ($value) {
96 7
                return ($value < 0.0) ? 0 - log(abs($value)) : log($value);
97 7
            },
98
            $yValues
99
        );
100
101 7
        $this->leastSquareFit($adjustedYValues, $xValues, $const);
102 7
    }
103
104
    /**
105
     * Define the regression and calculate the goodness of fit for a set of X and Y data values.
106
     *
107
     * @param float[] $yValues The set of Y-values for this regression
108
     * @param float[] $xValues The set of X-values for this regression
109
     * @param bool $const
110
     */
111 7
    public function __construct($yValues, $xValues = [], $const = true)
112
    {
113 7
        parent::__construct($yValues, $xValues);
114
115 7
        if (!$this->error) {
116 7
            $this->exponentialRegression($yValues, $xValues, (bool) $const);
117
        }
118 7
    }
119
}
120