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

markrogoyski / math-php / 23440034522

23 Mar 2026 01:35PM UTC coverage: 99.266% (-0.4%) from 99.66%
23440034522

Pull #488

github

web-flow
Merge 7244aa3c4 into 1b53720fc
Pull Request #488: Add multilinear regression

142 of 171 new or added lines in 14 files covered. (83.04%)

7 existing lines in 3 files now uncovered.

8657 of 8721 relevant lines covered (99.27%)

222.35 hits per line

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

92.59
/src/Statistics/Regression/Models/PolynomialModel.php
1
<?php
2

3
namespace MathPHP\Statistics\Regression\Models;
4

5
use MathPHP\Exception\BadDataException;
6
use MathPHP\Exception\IncorrectTypeException;
7
use MathPHP\Exception\MathException;
8
use MathPHP\Exception\MatrixException;
9
use MathPHP\LinearAlgebra\MatrixFactory;
10
use MathPHP\Polynomials\MonomialExponentGenerator;
11
use MathPHP\Util\Script;
12

13
trait PolynomialModel
14
{
15
    /**
16
     * Order of the polynomial
17
     * @var int
18
     */
19
    private $order = 1;
20

21
    /**
22
     * Whether the model should fit a constant
23
     * @var int
24
     */
25
    private $fit_constant = 1;
26

27
    /**
28
     * Evaluate the model given all the model parameters
29
     *
30
     * @param list<float> $vector
31
     * @param non-empty-list<float> $params
32
     *
33
     * @return float y evaluated
34
     *
35
     * @throws BadDataException
36
     * @throws IncorrectTypeException
37
     * @throws MathException
38
     * @throws MatrixException
39
     */
40
    public function evaluateModel(array $vector, array $params): float
41
    {
42
        $M = [$vector];
6✔
43
        $X = MatrixFactory::vandermonde($M, $this->order + 1);
6✔
44
        if ($this->fit_constant == 0) {
6✔
NEW
45
            $X = $X->columnExclude(0);
×
46
        }
47

48
        $row = $X->getRow(0);
6✔
49
        $y = 0;
6✔
50
        foreach ($row as $i => $x_val) {
6✔
51
            $y += $x_val * $params[$i];
6✔
52
        }
53
        return $y;
6✔
54
    }
55

56
    /**
57
     * Get regression parameters (coefficients).
58
     *
59
     * Use array_values() on the result to get a list<float> of coefficients.
60
     *
61
     * @param array<int, float> $params
62
     *
63
     * @return array<string, float> [β₀ + β₁ + β₂ + … + βₖ]
64
     */
65
    public function getModelParameters(array $params): array
66
    {
67
        $result = [];
4✔
68
        for ($i = 0, $len = \count($params); $i < $len; $i++) {
4✔
69
            $result['β' . Script::getSubscript($i)] = $params[$i];
4✔
70
        }
71
        return $result;
4✔
72
    }
73

74
    /**
75
     * Get regression equation
76
     *
77
     * @param non-empty-list<float> $params
78
     *
79
     * @return string
80
     */
81
    public function getModelEquation(array $params): string
82
    {
83
        $result = '';
1✔
84
        $exponentTuples = $this->getExponentTuples(\count($params) - 1);
1✔
85
        if ($this->fit_constant == 0) {
1✔
NEW
86
            \array_shift($exponentTuples);
×
87
        }
88
        foreach ($exponentTuples as $i => $exponents) {
1✔
89
            $result .= $result === '' ? 'y = ' : ' + ';
1✔
90
            $result .= \sprintf('%f', $params[$i]);
1✔
91
            foreach ($exponents as $j => $exponent) {
1✔
92
                if ($exponent === 0) {
1✔
93
                    continue;
1✔
94
                }
95
                $superscript = $exponent === 1 ? '' : Script::getSuperscript($exponent);
1✔
96
                $result .= \sprintf(' * x%s%s', Script::getSubscript($j + 1), $superscript);
1✔
97
            }
98
        }
99
        return $result;
1✔
100
    }
101

102
    /**
103
     * @param int $dimension
104
     * @return list<list<int>>
105
     */
106
    public function getExponentTuples(int $dimension): array
107
    {
108
        return MonomialExponentGenerator::all($dimension, $this->order, true);
1✔
109
    }
110
}
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