PhpBench\Math\Kde::evaluate PHP Method

evaluate() public method

Evaluate the estimated pdf on a set of points.
public evaluate ( array $points ) : array
$points array 1-D array of points on to which we will map the kde
return array
    public function evaluate(array $points)
    {
        $count = count($this->dataset);
        $bigger = count($points) > $count;
        if ($bigger) {
            $range = $count - 1;
        } else {
            $range = count($points) - 1;
        }
        $result = array_fill(0, count($points), 0);
        // loop over points
        foreach (range(0, $range) as $i) {
            if ($bigger) {
                $dataValue = $this->dataset[$i];
                $diff = array_map(function ($point) use($dataValue) {
                    return $dataValue - $point;
                }, $points);
            } else {
                $diff = array_map(function ($v) use($points, $i) {
                    return $v - $points[$i];
                }, $this->dataset);
            }
            // dot product (consider dedicated function)
            $invCov = $this->invCov;
            $tDiff = array_map(function ($v) use($invCov) {
                return $invCov * $v;
            }, $diff);
            // multiply the two arrays
            $multiplied = [];
            foreach ($diff as $index => $value) {
                $multiplied[$index] = $diff[$index] * $tDiff[$index];
            }
            // numpy sum does nothing with our 2d array in PHP
            // $energy = array_sum(diff * tdiff, axis=0) / 2.0
            $energy = array_map(function ($v) {
                return exp(-($v / 2));
            }, $multiplied);
            if ($bigger) {
                $sum = $result;
                foreach ($sum as $index => $value) {
                    $sum[$index] = $sum[$index] + $energy[$index];
                }
                $result = $sum;
            } else {
                $result[$i] = array_sum($energy);
            }
        }
        $result = array_map(function ($v) {
            return $v / $this->normFactor;
        }, $result);
        return $result;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * It should evaluate a kernel distribution estimate over a given space.
  *
  * @dataProvider provideEvaluate
  */
 public function testEvaluate($dataSet, $space, $bwMethod, $expected)
 {
     $kde = new Kde($dataSet, $bwMethod);
     $result = $kde->evaluate($space);
     // round result
     $result = array_map(function ($v) {
         return round($v, 8);
     }, $result);
     $this->assertEquals($expected, $result);
 }
All Usage Examples Of PhpBench\Math\Kde::evaluate