lib/Math/Kde.php
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;
    }
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;
    }
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 ; }
Evaluate the estimated pdf on a set of points.

@param array $points 1-D array of points on to which we will map the kde

@return array
Evaluate the estimated pdf on a set of points .