예제 #1
0
 public function testRegresion4()
 {
     $x = new MatrixBase($this->independent2->getNumRows() + 1, $this->independent2->getNumCols(), 50);
     for ($m = 1; $m <= $x->getNumRows(); $m++) {
         for ($n = 1; $n <= $x->getNumCols(); $n++) {
             $x->setPoint($m, $n, $m == 1 ? 1 : $this->independent2->getPoint($m - 1, $n, 50), 50);
         }
     }
     $y = $this->dependent2;
     /**
      * @var MatrixBase $xx
      */
     $xx = $x->multiplicationMatrix($x->transposed());
     /**
      * @var MatrixBase $xy
      */
     $xy = $y->multiplicationMatrix($x->transposed());
     /**
      * @var MatrixBase $b
      */
     $b = $xy->multiplicationMatrix($xx->inversa());
     $this->assertEquals(-94.55202884424345, round($b->getPoint(1, 1), 15));
     $this->assertEquals(2.801551359811446, round($b->getPoint(1, 2), 15));
     $this->assertEquals(1.072682616998037, round($b->getPoint(1, 3), 15));
 }
예제 #2
0
 public function regresionMultiple(MatrixBase $x, MatrixBase $y, $tipo = 'lineal')
 {
     $precision = $this->precision;
     $newX = new MatrixBase($x->getNumRows() + 1, $x->getNumCols(), $precision);
     for ($m = 1; $m <= $newX->getNumRows(); $m++) {
         for ($n = 1; $n <= $newX->getNumCols(); $n++) {
             $newX->setPoint($m, $n, $m == 1 ? 1 : $x->getPoint($m - 1, $n, $precision), $precision);
         }
     }
     $xx = $newX->multiplicationMatrix($newX->transposed());
     $xy = $y->multiplicationMatrix($newX->transposed());
     $b = $xy->multiplicationMatrix($xx->inversa());
     $result = array();
     $result['tipo'] = $tipo;
     for ($m = 1; $m <= $b->getNumCols(); $m++) {
         $result['B' . ($m - 1)] = $b->getPoint(1, $m, $precision);
     }
     $predicted = array();
     $residual = array();
     $SE = 0;
     $ST = 0;
     for ($n = 1; $n <= $x->getNumCols(); $n++) {
         $Y = $b->getPoint(1, 1, $precision);
         for ($m = 1; $m <= $x->getNumRows(); $m++) {
             $Y = bcadd($Y, bcmul($b->getPoint(1, $m + 1), $x->getPoint($m, $n, $precision), $precision), $precision);
         }
         $predicted[$n] = $Y;
         $residual[$n] = bcsub($y->getPoint(1, $n, $precision), $predicted[$n], $precision);
         $SE = bcadd($SE, $residual[$n], $precision);
         $ST = bcadd($ST, $y->getPoint(1, $n, $precision), $precision);
     }
     $MSE = bcdiv($SE, $x->getNumCols(), $precision);
     $MST = bcdiv($ST, $x->getNumCols(), $precision);
     $SSE = bcadd(0, 0, $precision);
     $SST = bcadd(0, 0, $precision);
     for ($n = 1; $n <= $x->getNumCols(); $n++) {
         $SSE = bcadd($SSE, bcmul(bcsub($residual[$n], $MSE, $precision), bcsub($residual[$n], $MSE, $precision), $precision), $precision);
         $SST = bcadd($SST, bcmul(bcsub($y->getPoint(1, $n, $precision), $MST, $precision), bcsub($y->getPoint(1, $n, $precision), $MST, $precision), $precision), $precision);
     }
     $FR = bcdiv(bcmul(bcsub(bcsub($x->getNumCols(), $x->getNumRows(), $precision), 1, $precision), bcsub($SST, $SSE, $precision), $precision), bcmul($x->getNumRows(), $SSE, $precision), $precision);
     $RRSQ = bcsub(1, bcdiv($SSE, $SST, $precision), $precision);
     $result['correlacion'] = bcsqrt($RRSQ, $precision);
     $result['r2'] = $RRSQ;
     $result['estadisticoF'] = $FR;
     return $result;
 }
예제 #3
0
 /**
  * @depends testMultiplica4
  */
 public function testInversa()
 {
     $tmp1 = new MatrixBase(3, 3, 5);
     $tmp1->setPoint(1, 1, 2);
     $tmp1->setPoint(1, 2, -2);
     $tmp1->setPoint(1, 3, 2);
     $tmp1->setPoint(2, 1, 2);
     $tmp1->setPoint(2, 2, 1);
     $tmp1->setPoint(2, 3, 0);
     $tmp1->setPoint(3, 1, 3);
     $tmp1->setPoint(3, 2, -2);
     $tmp1->setPoint(3, 3, 2);
     $inversa = $tmp1->inversa();
     $tmp2 = new MatrixBase(3, 3, 5);
     $tmp2->setPoint(1, 1, -1);
     $tmp2->setPoint(1, 2, 0);
     $tmp2->setPoint(1, 3, 1);
     $tmp2->setPoint(2, 1, 2);
     $tmp2->setPoint(2, 2, 1);
     $tmp2->setPoint(2, 3, -2);
     $tmp2->setPoint(3, 1, 7 / 2);
     $tmp2->setPoint(3, 2, 1);
     $tmp2->setPoint(3, 3, -3);
     $this->assertTrue($tmp2->isMatrixEquals($inversa));
     $tmp3 = new MatrixBase(3, 3, 5);
     $tmp3->setPoint(1, 1, 1);
     $tmp3->setPoint(1, 2, 0);
     $tmp3->setPoint(1, 3, 0);
     $tmp3->setPoint(2, 1, 0);
     $tmp3->setPoint(2, 2, 1);
     $tmp3->setPoint(2, 3, 0);
     $tmp3->setPoint(3, 1, 0);
     $tmp3->setPoint(3, 2, 0);
     $tmp3->setPoint(3, 3, 1);
     $multiplicada = $tmp1->multiplicationMatrix($inversa);
     $this->assertTrue($tmp3->isMatrixEquals($multiplicada));
 }