• 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

87.5
/src/Statistics/Regression/PowerLaw.php
1
<?php
2

3
namespace MathPHP\Statistics\Regression;
4

5
use MathPHP\Exception;
6

7
/**
8
 * Power law regression (power curve) - Least squares fitting
9
 * http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
10
 *
11
 * A functional relationship between two quantities,
12
 * where a relative change in one quantity results in a proportional
13
 * relative change in the other quantity,
14
 * independent of the initial size of those quantities: one quantity
15
 * varies as a power of another.
16
 * https://en.wikipedia.org/wiki/Power_law
17
 *
18
 * y = Axᴮ
19
 *
20
 * Using least squares fitting: y = axᵇ
21
 *
22
 *     n∑⟮ln xᵢ ln yᵢ⟯ − ∑⟮ln xᵢ⟯ ∑⟮ln yᵢ⟯
23
 * b = --------------------------------
24
 *           n∑⟮ln xᵢ⟯² − ⟮∑⟮ln xᵢ⟯⟯²
25
 *         _                    _
26
 *        |  ∑⟮ln yᵢ⟯ − b∑⟮ln xᵢ⟯  |
27
 * a = exp|  ------------------  |
28
 *        |_          n         _|
29
 *
30
 * @phpstan-import-type SimpleLinearResultModel from Methods\LeastSquares
31
 * @phpstan-import-type PolynomialResultModel from Methods\LeastSquares
32
 */
33
class PowerLaw extends ParametricRegression
34
{
35
    use Models\PowerModel;
36
    /** @use Methods\LeastSquares<SimpleLinearResultModel> */
37
    use Methods\LeastSquares;
38

39
    /** @var float */
40
    protected $a;
41

42
    /** @var float */
43
    protected $b;
44

45
    /**
46
     * @param list<float> $array
47
     * @return SimpleLinearResultModel
48
     */
49
    protected function createResultModel(array $array): array
50
    {
NEW
51
        return $this->createSimpleLinearResultModel($array);
×
52
    }
53

54
    /**
55
     * Calculate the regression parameters by least squares on linearized data
56
     * ln(y) = ln(A) + B*ln(x)
57
     *
58
     * @throws Exception\BadDataException
59
     * @throws Exception\IncorrectTypeException
60
     * @throws Exception\MatrixException
61
     * @throws Exception\MathException
62
     */
63
    public function calculate(): void
64
    {
65
        // Linearize the relationship by taking the log of both sides.
66
        $x’ = \array_map('\log', $this->xs);
8✔
67
        $y’ = \array_map('\log', $this->ys);
8✔
68

69
        // Perform Least Squares Fit
70
        $linearized_parameters = $this->leastSquares($y’, $x’)->getColumn(0);
8✔
71

72
        // Translate the linearized parameters back.
73
        $this->a = \exp($linearized_parameters[0]);
8✔
74
        $this->b = $linearized_parameters[1];
8✔
75

76
        $this->parameters = [$this->a, $this->b];
8✔
77
    }
78

79
    /**
80
     * Evaluate the regression equation at x
81
     * Uses the instance model's evaluateModel method.
82
     *
83
     * @param  float $x
84
     *
85
     * @return float
86
     */
87
    public function evaluate(float $x): float
88
    {
89
        return $this->evaluateModel($x, $this->parameters);
5✔
90
    }
91
}
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