Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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  }