• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

markrogoyski / math-php / 20965361344

13 Jan 2026 04:59PM UTC coverage: 99.457% (-0.2%) from 99.66%
20965361344

Pull #488

github

web-flow
Merge 41c6f440c into c064e80bf
Pull Request #488: Add multilinear regression

66 of 78 new or added lines in 5 files covered. (84.62%)

7 existing lines in 3 files now uncovered.

8614 of 8661 relevant lines covered (99.46%)

223.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/src/Statistics/Regression/Regression.php
1
<?php
2

3
namespace MathPHP\Statistics\Regression;
4

5
/**
6
 * Base class for regressions.
7
 */
8
abstract class Regression
9
{
10
    /**
11
     * Array of x and y points: [ [x₁₁, x₁₂, x₁ₖ, y₁], [x₂₁, x₂₂, x₂ₖ, y₂], ... ]
12
     * @var list<array{float, float, ...<float>}>
13
     */
14
    protected $points;
15

16
    /**
17
     * X values of the original points
18
     * @var list<float>
19
     */
20
    protected $xs;
21

22
    /**
23
     * Y values of the original points
24
     * @var list<float>
25
     */
26
    protected $ys;
27

28
    /**
29
     * X row values of the original points
30
     * @var list<non-empty-list<float>>
31
     */
32
    protected $xss;
33

34
    /**
35
     * Number of points
36
     * @var int
37
     */
38
    protected $n;
39

40
    /**
41
     * Number of columns in xss
42
     * @var int
43
     */
44
    protected $k;
45

46
    /**
47
     * Constructor - Prepares the data arrays for regression analysis
48
     *
49
     * @param list<array{float|non-empty-list<float>, float}> $points [ [ x₁ | [ x₁₁, x₁₂, x₁ₖ ], y₁ ], [ x₂ | [ x₂₁, x₂₂, x₂ₖ ], y₂ ], ... ]
50
     */
51
    public function __construct(array $points)
52
    {
53
        $this->points = $points;
124✔
54
        $this->n      = \count($points);
124✔
55
        $this->k      = empty($points) ? 0 : (\is_array($points[0]) ? \count($points[0]) : 1);
124✔
56

57
        // Get list of x points and y points.
58
        // This will be fine for linear or polynomial regression, where there is only one x.
59
        $this->xs = \array_map(function ($point) {
124✔
60
            return \is_array($point[0]) ? $point[0][0] : $point[0];
123✔
61
        }, $points);
124✔
62
        $this->ys = \array_map(function ($point) {
124✔
63
            return $point[1];
123✔
64
        }, $points);
124✔
65
        // For multilinear, the format is a list of x for each point.
66
        $this->xss = \array_map(function (array $point) {
124✔
67
            return \is_array($point[0]) ? $point[0] : [$point[0]];
123✔
68
        }, $points);
124✔
69
    }
70

71
    /**
72
     * Evaluate the regression equation at x
73
     *
74
     * @param float $x
75
     *
76
     * @return float
77
     */
78
    abstract public function evaluate(float $x): float;
79

80
    /**
81
     * Evaluate the regression equation at x vector
82
     *
83
     * @param non-empty-list<float> $vector
84
     * @return float
85
     */
86
    public function evaluateVector(array $vector): float
87
    {
88
        return $this->evaluate($vector[0]);
20✔
89
    }
90

91
    /**
92
     * Get points
93
     *
94
     * @return array<array{float, float}>
95
     */
96
    public function getPoints(): array
97
    {
98
        return $this->points;
3✔
99
    }
100

101
    /**
102
     * Get Xs (x values of each point)
103
     *
104
     * @return list<float> of x values
105
     */
106
    public function getXs(): array
107
    {
108
        return $this->xs;
3✔
109
    }
110

111
    /**
112
     * Get Ys (y values of each point)
113
     *
114
     * @return list<float> of y values
115
     */
116
    public function getYs(): array
117
    {
118
        return $this->ys;
3✔
119
    }
120

121
    /**
122
     * Get Xss (x vector of each point)
123
     *
124
     * @return list<non-empty-list<float>> of x values
125
     */
126
    public function getXss(): array
127
    {
NEW
128
        return $this->xss;
×
129
    }
130

131
    /**
132
     * Get sample size (number of points)
133
     *
134
     * @return int
135
     */
136
    public function getSampleSize(): int
137
    {
138
        return $this->n;
5✔
139
    }
140

141
    /**
142
     * Ŷ (yhat)
143
     * A list of the predicted values of Y given the regression.
144
     *
145
     * @return list<float>
146
     */
147
    public function yHat(): array
148
    {
149
        return \array_map([$this, 'evaluateVector'], $this->xss);
21✔
150
    }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc