Skip to content

Commit 67f203c

Browse files
committed
Fix types and comments, add unit test
1 parent 41c6f44 commit 67f203c

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

src/Statistics/Regression/Models/MultilinearModel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
trait MultilinearModel
66
{
7+
/**
8+
* @var list<string>
9+
*/
710
private static $subscripts = ['', '', '', '', '', '', '', '', '', ''];
811

912
/**

src/Statistics/Regression/Multilinear.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ public function evaluate(float $x): float
4444
* @param non-empty-list<float> $vector
4545
*
4646
* @return float
47+
*
48+
* @throws BadDataException
4749
*/
4850
public function evaluateVector(array $vector): float
4951
{
52+
if (empty($this->parameters)) {
53+
throw new Exception\BadDataException('Regression parameters are not calculated');
54+
}
5055
return $this->evaluateModel($vector, $this->parameters);
5156
}
5257
}

src/Statistics/Regression/Regression.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
abstract class Regression
99
{
1010
/**
11-
* Array of x and y points: [ [x₁₁, x₁₂, x₁ₖ, y₁], [x₂₁, x₂₂, x₂ₖ, y₂], ... ]
12-
* @var list<array{float, float, ...<float>}>
11+
* Array of x and y points: [ [ x₁ | [ x₁₁, x₁₂, x₁ₖ ], y₁ ], [ x₂ | [ x₂₁, x₂₂, x₂ₖ ], y₂ ], ... ]
12+
* @var list<array{float|non-empty-list<float>, float}>
1313
*/
1414
protected $points;
1515

@@ -91,7 +91,7 @@ public function evaluateVector(array $vector): float
9191
/**
9292
* Get points
9393
*
94-
* @return array<array{float, float}>
94+
* @return list<array{float|non-empty-list<float>, float}>
9595
*/
9696
public function getPoints(): array
9797
{

tests/Statistics/Regression/MultilinearTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public function dataProviderForParameters(): array
5858
// y = 2x₁ - 0.5x₂ + 10
5959
// Order from Vandermonde (d=2, p=1): [0,0], [0,1], [1,0]
6060
// β₀ = ε = 10
61-
// β₁ = x coeff = -0.5
62-
// β₂ = x coeff = 2
61+
// β₁ = x coeff = 2
62+
// β₂ = x coeff = -0.5
6363
[
6464
[[1, 5], 9.5], // 2(1) - 0.5(5) + 10 = 2 - 2.5 + 10 = 9.5
6565
[[2, 8], 10], // 2(2) - 0.5(8) + 10 = 4 - 4 + 10 = 10

tests/Statistics/Regression/RegressionTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,41 @@ public function dataProviderForSumOfSquaresEqualsSumOfSQuaresRegressionPlusSumOf
263263
[ [[61,105], [62,120], [63,120], [65,160], [65,120], [68,145], [69,175], [70,160], [72,185], [75,210]] ],
264264
];
265265
}
266+
267+
/**
268+
* @test getXss
269+
* @dataProvider dataProviderForXss
270+
* @param array $points
271+
* @param array $expectedXss
272+
*/
273+
public function testXss(array $points, array $expectedXss): void
274+
{
275+
// Given
276+
$regression = new Linear($points);
277+
278+
// Then
279+
$this->assertEqualsWithDelta($expectedXss, $regression->getXss(), 0.00001);
280+
}
281+
282+
/**
283+
* @return array
284+
*/
285+
public function dataProviderForXss(): array
286+
{
287+
$y = 0;
288+
return [
289+
[
290+
[[1, $y], [2, $y], [3, $y]],
291+
[[1], [2], [3]],
292+
],
293+
[
294+
[[[1], $y], [[2], $y], [[3], $y]],
295+
[[1], [2], [3]],
296+
],
297+
[
298+
[[[1, 10], $y], [[2, 20], $y], [[3, 30], $y]],
299+
[[1, 10], [2, 20], [3, 30]],
300+
],
301+
];
302+
}
266303
}

0 commit comments

Comments
 (0)