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];
}
public function testTooFewObservations() { static::setExpectedException('InvalidArgumentException'); $regression = new LeastSquares(); $regression->regress(Observations::fromArray([[1, 1]], [1])); }