php statistics z-score正态分布

时间:2011-08-28 22:36:37

标签: php statistics

如何使用PHP计算数组数组的z得分?我需要计算z得分,然后找到百分位数(CDF)!我可以使用哪些PHP函数?谢谢!

4 个答案:

答案 0 :(得分:2)

以下代码将给出CDF的一个很好的近似值(Abramowitz& Stegun(1964))

function normal_pdf($x) {
    return exp(-$x * $x / 4) / sqrt(2 * M_PI);
}

function normal_cdf($x) {
    $b = array(0.2316419, 0.319381530, -0.356563782, 1.781477937, -1.821255978, 1.330274429);
    $t = 1 / (1 + $b[0] * $x);
    $result = 0;
    for ($i = 1; $i < 6; $i++) {
        $result += $b[$i] * pow($t, $i);
    }
    return 1 - normal_pdf($x) * $result;
}

这假设标准正态分布。回想一下,要标准化,请使用z = (x - mean) / (standard deviation)

答案 1 :(得分:1)

/* Mean */
function mean($input_array)
{
  $total = 0;
  foreach ($input_array as $value)
  {
    $total += $value;
  }
  return ($total / count($input_array));
}

/* Standard Deviation */
function std($arr)
{
  if (!count($arr))
  return 0;
  $mean = mean($arr);
  $sos = 0; // Sum of squares
  for ($i = 0; $i < count($arr); $i++)
  {
    $sos += ($arr[$i] - $mean) * ($arr[$i] - $mean);
  }
  return sqrt($sos / (count($arr) - 1));
}

/* Z Scores */
function z($var, $arr)
{
  return ($var -mean($arr)) / std($arr);
}

答案 2 :(得分:0)

PHP statistics extension中有一些可以帮助您的功能 - 您可能希望stats_standard_deviation开始。

答案 3 :(得分:0)

$control=array(15,7);
$treatment=array(46,8);
$confidence=number_format(cumnormdist(zscore($control, $treatment))*100,0);   

function cr($t) 
{ 
    return $t[1]/$t[0]; 
}

function zscore($c, $t) 
{
    $z = cr($t)-cr($c);
    $s = (cr($t)*(1-cr($t)))/$t[0] + (cr($c)*(1-cr($c)))/$c[0];
    return $z/sqrt($s);
}

function cumnormdist($x)
{
  $b1 =  0.319381530;
  $b2 = -0.356563782;
  $b3 =  1.781477937;
  $b4 = -1.821255978;
  $b5 =  1.330274429;
  $p  =  0.2316419;
  $c  =  0.39894228;

  if($x >= 0.0) {
      $t = 1.0 / ( 1.0 + $p * $x );
      return (1.0 - $c * exp( -$x * $x / 2.0 ) * $t *
      ( $t *( $t * ( $t * ( $t * $b5 + $b4 ) + $b3 ) + $b2 ) + $b1 ));
  }
  else {
      $t = 1.0 / ( 1.0 - $p * $x );
      return ( $c * exp( -$x * $x / 2.0 ) * $t *
      ( $t *( $t * ( $t * ( $t * $b5 + $b4 ) + $b3 ) + $b2 ) + $b1 ));
    }
}