public function regress(array $dependentData, array $independentData) { $design = new Matrix($independentData); $observed = (new Matrix([$dependentData]))->transpose(); if ($design->columns >= $design->rows) { throw new InvalidArgumentException('Not enough observations to perform regression. You need to have more observations than explanatory variables.'); } $designTranspose = $design->transpose(); $prediction = $designTranspose->multiply($design)->inverse()->multiply($designTranspose->multiply($observed)); // Extract the vertical vector as a simple array. return $prediction->transpose()->toArray()[0]; }
public function testEntrywise() { $matrix1 = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); $matrix2 = $matrix1->transpose(); static::assertEquals([[1, 8, 21], [8, 25, 48], [21, 48, 81]], $matrix1->entrywise($matrix2)->toArray()); }
/** * @param Observations $observations * @return array * @throws InvalidArgumentException */ 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]; }