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]; }
/** * @param self $other * @return self * @throws MatrixException */ public function concatenateRight(self $other) : self { return $this->spliceColumns($this->getColumnCount(), 0, $other->toArray()); }
/** * @param Matrix $matrix * @return boolean */ public function equals(Matrix $matrix) : bool { return $this->toArray() === $matrix->toArray(); }
/** * @return Matrix */ public function upper() : Matrix { return $this->decomposition->upper(false); }
public function testGetLower() { $matrix = new Matrix([[2, 0, 2, 0.6], [3, 3, 4, -2], [5, 5, 4, 2], [-1, -2, 3.4, -1]]); $lower = $matrix->lower(true); static::assertEquals(1, $lower->get(0, 0)); static::assertEquals(0, $lower->get(0, 1)); static::assertEquals(0, $lower->get(0, 2)); static::assertEquals(0, $lower->get(0, 3)); static::assertEquals(3, $lower->get(1, 0)); static::assertEquals(1, $lower->get(1, 1)); static::assertEquals(0, $lower->get(1, 2)); static::assertEquals(0, $lower->get(1, 3)); static::assertEquals(5, $lower->get(2, 0)); static::assertEquals(5, $lower->get(2, 1)); static::assertEquals(1, $lower->get(2, 2)); static::assertEquals(0, $lower->get(2, 3)); static::assertEquals(-1, $lower->get(3, 0)); static::assertEquals(-2, $lower->get(3, 1)); static::assertEquals(3.4, $lower->get(3, 2)); static::assertEquals(1, $lower->get(3, 3)); }
/** * @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]; }