Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Phpml\Math\Statistic;
6
 
7
class Gaussian
8
{
9
    /**
10
     * @var float
11
     */
12
    protected $mean;
13
 
14
    /**
15
     * @var float
16
     */
17
    protected $std;
18
 
19
    public function __construct(float $mean, float $std)
20
    {
21
        $this->mean = $mean;
22
        $this->std = $std;
23
    }
24
 
25
    /**
26
     * Returns probability density of the given <i>$value</i>
27
     *
28
     * @return float|int
29
     */
30
    public function pdf(float $value)
31
    {
32
        // Calculate the probability density by use of normal/Gaussian distribution
33
        // Ref: https://en.wikipedia.org/wiki/Normal_distribution
34
        $std2 = $this->std ** 2;
35
        $mean = $this->mean;
36
 
37
        return exp(-(($value - $mean) ** 2) / (2 * $std2)) / ((2 * $std2 * M_PI) ** .5);
38
    }
39
 
40
    /**
41
     * Returns probability density value of the given <i>$value</i> based on
42
     * given standard deviation and the mean
43
     */
44
    public static function distributionPdf(float $mean, float $std, float $value): float
45
    {
46
        $normal = new self($mean, $std);
47
 
48
        return $normal->pdf($value);
49
    }
50
}