Test Failed
Branch master (01bb82)
by Arkadiusz
05:58 queued 03:02
created

Gaussian   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A pdf() 0 8 1
A distributionPdf() 0 5 1
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
    /**
20
     * @param float $mean
21
     * @param float $std
22
     */
23
    public function __construct(float $mean, float $std)
24
    {
25
        $this->mean = $mean;
26
        $this->std = $std;
27
    }
28
29
    /**
30
     * Returns probability density of the given <i>$value</i>
31
     *
32
     * @param float $value
33
     *
34
     * @return type
35
     */
36
    public function pdf(float $value)
37
    {
38
        // Calculate the probability density by use of normal/Gaussian distribution
39
        // Ref: https://en.wikipedia.org/wiki/Normal_distribution
40
        $std2 = $this->std ** 2;
41
        $mean = $this->mean;
42
        return exp(- (($value - $mean) ** 2) / (2 * $std2)) / sqrt(2 * $std2 * pi());
43
    }
44
45
    /**
46
     * Returns probability density value of the given <i>$value</i> based on
47
     * given standard deviation and the mean
48
     *
49
     * @param float $mean
50
     * @param float $std
51
     * @param float $value
52
     *
53
     * @return float
54
     */
55
    public static function distributionPdf(float $mean, float $std, float $value)
56
    {
57
        $normal = new self($mean, $std);
58
        return $normal->pdf($value);
59
    }
60
}
61