Last active
July 2, 2017 16:33
-
-
Save jimjam88/62f219f0fe0bf1df35d2 to your computer and use it in GitHub Desktop.
Simple PHP Implementation of the Poisson Distribution concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Poisson; | |
class Poisson | |
{ | |
/** | |
* Factorial function | |
* | |
* @param integer $number | |
* @return integer | |
*/ | |
public function factorial($number) | |
{ | |
$sum = 1; | |
for ($i = 1; $i <= floor($number); $i++) { | |
$sum *= $i; | |
} | |
return $sum; | |
} | |
/** | |
* Poisson function. | |
* | |
* @param integer $chance The probability | |
* @param integer $occurrence The number of occurances | |
* @return float | |
*/ | |
public function poisson($chance, $occurrence) | |
{ | |
return exp(-$chance) * pow($chance, $occurrence) / $this->factorial($occurrence); | |
} | |
/** | |
* Poisson function returned as a percentage of 100. | |
* | |
* @param integer $chance The probability | |
* @param integer $occurrence The number of occurances | |
* @return float | |
*/ | |
public function percentage($chance, $occurrence) | |
{ | |
return call_user_func_array([$this, 'poisson'], func_get_args()) * 100; | |
} | |
/** | |
* Poisson function returned as rounded a percentage of 100. | |
* | |
* @param integer $chance The probability | |
* @param integer $occurrence The number of occurances | |
* @param integer $dp The number of decimal places to round to | |
* @return float | |
*/ | |
public function roundedPercentage($chance, $occurrence, $dp = 0) | |
{ | |
return round($this->percentage($chance, $occurrence), $dp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any example please