Skip to content

Instantly share code, notes, and snippets.

@fatihgune
Created December 6, 2020 16:44
Show Gist options
  • Save fatihgune/59b93b7fe44eef25af9dbeff4b529367 to your computer and use it in GitHub Desktop.
Save fatihgune/59b93b7fe44eef25af9dbeff4b529367 to your computer and use it in GitHub Desktop.
Returns the standard normal cumulative distribution
<?
/**
* @param mixed $x
* @return string
*/
public function normDist($x)
{
// Load tabulated values in an array
$values = config('table');
// Discriminate upon the absolute value, then the sign of $x
$x = number_format($x, 8);
if (abs($x) >= 3.09) {
$output = 0;
} elseif ($x == 0) {
$output = 0.5;
} elseif ($x < 0) {
// find higher boundary (next highest value with 2 decimals)
$x2 = number_format(ceil(100 * $x) / 100, 2);
$x2 = (string) $x2;
// find lower boundary
$x1 = number_format($x2 - 0.01, 2);
$x1 = (string) $x1;
// linear interpolate
$output = $values[$x1] + ($values[$x2] - $values[$x1]) / 0.01 * ($x - $x1);
} else {
// if x>0
$output = 1 - $this->normDist(-$x);
}
return number_format($output, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment