1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared\Trend; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Trend\Trend; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class BestFitTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
private const LBF_PRECISION = 1.0E-4; |
14
|
|
|
|
15
|
|
|
public function testBestFit(): void |
16
|
|
|
{ |
17
|
|
|
$xValues = [45, 55, 47, 75, 90, 100, 100, 95, 88, 50, 45, 58]; |
18
|
|
|
$yValues = [15, 25, 17, 30, 41, 47, 50, 46, 37, 22, 20, 26]; |
19
|
|
|
$maxGoodness = -1000.0; |
20
|
|
|
$maxType = ''; |
21
|
|
|
|
22
|
|
|
$type = Trend::TREND_LINEAR; |
23
|
|
|
$result = Trend::calculate($type, $yValues, $xValues); |
24
|
|
|
$goodness = $result->getGoodnessOfFit(); |
25
|
|
|
if ($maxGoodness < $goodness) { |
26
|
|
|
$maxGoodness = $goodness; |
27
|
|
|
$maxType = $type; |
28
|
|
|
} |
29
|
|
|
self::assertEqualsWithDelta(0.9628, $goodness, self::LBF_PRECISION); |
30
|
|
|
|
31
|
|
|
$type = Trend::TREND_EXPONENTIAL; |
32
|
|
|
$result = Trend::calculate($type, $yValues, $xValues); |
33
|
|
|
$goodness = $result->getGoodnessOfFit(); |
34
|
|
|
if ($maxGoodness < $goodness) { |
35
|
|
|
$maxGoodness = $goodness; |
36
|
|
|
$maxType = $type; |
37
|
|
|
} |
38
|
|
|
self::assertEqualsWithDelta(0.9952, $goodness, self::LBF_PRECISION); |
39
|
|
|
|
40
|
|
|
$type = Trend::TREND_LOGARITHMIC; |
41
|
|
|
$result = Trend::calculate($type, $yValues, $xValues); |
42
|
|
|
$goodness = $result->getGoodnessOfFit(); |
43
|
|
|
if ($maxGoodness < $goodness) { |
44
|
|
|
$maxGoodness = $goodness; |
45
|
|
|
$maxType = $type; |
46
|
|
|
} |
47
|
|
|
self::assertEqualsWithDelta(-0.0724, $goodness, self::LBF_PRECISION); |
48
|
|
|
|
49
|
|
|
$type = Trend::TREND_POWER; |
50
|
|
|
$result = Trend::calculate($type, $yValues, $xValues); |
51
|
|
|
$goodness = $result->getGoodnessOfFit(); |
52
|
|
|
if ($maxGoodness < $goodness) { |
53
|
|
|
$maxGoodness = $goodness; |
54
|
|
|
$maxType = $type; |
55
|
|
|
} |
56
|
|
|
self::assertEqualsWithDelta(0.9946, $goodness, self::LBF_PRECISION); |
57
|
|
|
|
58
|
|
|
$type = Trend::TREND_BEST_FIT_NO_POLY; |
59
|
|
|
$result = Trend::calculate($type, $yValues, $xValues); |
60
|
|
|
$goodness = $result->getGoodnessOfFit(); |
61
|
|
|
self::assertSame($maxGoodness, $goodness); |
62
|
|
|
self::assertSame(lcfirst($maxType), $result->getBestFitType()); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$type = Trend::TREND_BEST_FIT; |
66
|
|
|
Trend::calculate($type, $yValues, [0, 1, 2]); |
67
|
|
|
self::fail('should have failed - mismatched number of elements'); |
68
|
|
|
} catch (SpreadsheetException $e) { |
69
|
|
|
self::assertStringContainsString('Number of elements', $e->getMessage()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$type = Trend::TREND_BEST_FIT; |
74
|
|
|
Trend::calculate($type, $yValues, $xValues); |
75
|
|
|
self::fail('should have failed - TREND_BEST_FIT includes polynomials which are not implemented yet'); |
76
|
|
|
} catch (SpreadsheetException $e) { |
77
|
|
|
self::assertStringContainsString('not yet implemented', $e->getMessage()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$type = 'unknown'; |
82
|
|
|
Trend::calculate($type, $yValues, $xValues); |
83
|
|
|
self::fail('should have failed - invalid trend type'); |
84
|
|
|
} catch (SpreadsheetException $e) { |
85
|
|
|
self::assertStringContainsString('Unknown trend type', $e->getMessage()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|