MCordingley\Regression\Algorithm\LeastSquares::regress PHP Метод

regress() публичный Метод

public regress ( Observations $observations ) : array
$observations MCordingley\Regression\Observations
Результат array
    public function regress(Observations $observations) : array
    {
        $design = new Matrix($observations->getFeatures());
        $observed = (new Matrix([$observations->getOutcomes()]))->transpose();
        if ($design->getRowCount() < $design->getColumnCount()) {
            throw new InvalidArgumentException('Not enough observations to perform regression. You need to have more observations than explanatory variables.');
        }
        $designTranspose = $design->transpose();
        $prediction = $designTranspose->multiplyMatrix($design)->inverse()->multiplyMatrix($designTranspose->multiplyMatrix($observed));
        return $prediction->transpose()->toArray()[0];
    }

Usage Example

Пример #1
0
 public function testTooFewObservations()
 {
     static::setExpectedException('InvalidArgumentException');
     $regression = new LeastSquares();
     $regression->regress(Observations::fromArray([[1, 1]], [1]));
 }
LeastSquares