phpprobabilitydistributionnormal-distribution

Calculate normal distribution probability in php


I need to use this table to get the probability from a given z value:

https://statistics.laerd.com/statistical-guides/img/normal-table-large.png

I'm sure there should be a better way to get those values on php but don't know how to calculate them. Any help would be very appreciated.


Solution

  • Table, I believe, is CDF of the normal distribution. I coded it using expression for error function approximation.

    Code is untested!

    function sgn( $x ) {
      if ( $x < 0 )
        return -1;
      return 1;
    }
    
    function erf( $x ) {
      $e  = exp(-$x*$x);
      $e2 = exp(-$x*$x*2);
    
      $q = sqrt(pi())/2 + 31*$e/200 - 341*$e2/8000;
    
      return 2*sgn($x)*sqrt(1-$e)*$q/sqrt(pi());
    }
    
    function CDF( $x ) {
        return (1 + erf($x / sqrt(2))) / 2;
    }
    
    print_r(CDF(0));
    print_r(CDF(0.1));
    ....    
    

    UPDATE

    Here is quick (untested!) code to compute erf() with up to 4 terms

    function erf( $x ) {
      $e = exp(-$x*$x);
      $t = 1.0 - $e;
    
      $s = 1. + $t*(-1./12. + $t*(-7./480. + $t*( -5./896. +$t * (-787./276480.))));
    
      return 2.*sgn($x)*sqrt($t)*$s/sqrt(pi());
    }