Skip to content

Instantly share code, notes, and snippets.

@fatihgune
Last active December 6, 2020 16:42
Show Gist options
  • Save fatihgune/dc33a209f62a2a72698eb97c52d4be15 to your computer and use it in GitHub Desktop.
Save fatihgune/dc33a209f62a2a72698eb97c52d4be15 to your computer and use it in GitHub Desktop.
Returns the inverse standard normal cumulative distribution ( 0<y<1 ) (PHP)
<?
/**
* @param mixed $y
* @return string
*/
public function normDistInv($y)
{
// Load tabulated values in an array
$values = config('table');
// Discriminate upon whether $y is between 0 and 1, then upon its position relative to 0.5
$y = number_format($y, 2);
if ($y <= 0 || $y >= 1) {
$output = false;
} elseif ($y <= 0.5) {
// find the largest index which value is smaller than $y:
// filter array for values higher than $y
$smaller = [];
while (list($key, $value) = $this->myEach($values)) {
if ($value <= $y) {
$smaller[$key] = $value;
}
}
// order $values in decreasing terms of the $values
krsort($smaller, SORT_NUMERIC);
reset($smaller);
$x1 = (string) key($smaller);
$x2 = (string) ($x1 + 0.01);
// interpolate
$output = $x1 + ($y - $values[$x1]) / ($values[$x2] - $values[$x1]) * 0.01;
} else {
// meaning $x between 0.5 and 1
$output = -$this->normDistInv(1 - $y);
}
return number_format($output, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment