2017-03-05 08:43:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Phpml\Math\Statistic;
|
|
|
|
|
|
|
|
class Gaussian
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $mean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $std;
|
|
|
|
|
|
|
|
public function __construct(float $mean, float $std)
|
|
|
|
{
|
|
|
|
$this->mean = $mean;
|
|
|
|
$this->std = $std;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns probability density of the given <i>$value</i>
|
|
|
|
*
|
2017-05-17 07:03:25 +00:00
|
|
|
* @return float|int
|
2017-03-05 08:43:19 +00:00
|
|
|
*/
|
|
|
|
public function pdf(float $value)
|
|
|
|
{
|
|
|
|
// Calculate the probability density by use of normal/Gaussian distribution
|
|
|
|
// Ref: https://en.wikipedia.org/wiki/Normal_distribution
|
|
|
|
$std2 = $this->std ** 2;
|
|
|
|
$mean = $this->mean;
|
2017-08-17 06:50:37 +00:00
|
|
|
|
|
|
|
return exp(-(($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * pi());
|
2017-03-05 08:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns probability density value of the given <i>$value</i> based on
|
|
|
|
* given standard deviation and the mean
|
|
|
|
*/
|
2017-11-06 07:56:37 +00:00
|
|
|
public static function distributionPdf(float $mean, float $std, float $value) : float
|
2017-03-05 08:43:19 +00:00
|
|
|
{
|
|
|
|
$normal = new self($mean, $std);
|
2017-08-17 06:50:37 +00:00
|
|
|
|
2017-03-05 08:43:19 +00:00
|
|
|
return $normal->pdf($value);
|
|
|
|
}
|
|
|
|
}
|