我需要根据 Z 值、均值 (0)、标准差 (1) 计算 P 值(用于显着性检查),正态分布是累积的。
PHP 中是否有函数可以做到这一点?
这是我当前的代码:
$P_value = (1/sqrt(2*pi()))*exp(-0.5*pow($this->ZValue,2));
ZValue = -4,688072309 确认正确。
期望值:1,37895E-06
值:6.9681241978373E-6
可能是公式有问题,但查不出错误。
编辑:
如果有人感兴趣,这里是有效的代码:
function CalculateP_Value(){
$this->CalculateZ();
$this->PValue = (1/sqrt(2*pi()))*exp(-0.5*pow($this->ZValue,2));
}
function SetConstant(){
$this->CalculateZ();
$this->A1 = 0.319381530;
$this->A2 = -0.356563782;
$this->A3 = 1.781477937;
$this->A4 = -1.8221255978;
$this->A5 = 1.330274429;
if($this->ZValue>0){
$this->K = 1.0/(1.0+0.2316419*$this->ZValue);
}
else{
$this->ZValue*=-1;
$this->K = 1.0/(1.0+0.2316419*$this->ZValue);
}
}
function CalculateCumulative(){
$this-> SetConstant();
$this-> CalculateP_Value();
$this->Cumulative = $this->GetCalculateCumulative($this->ZValue);
}
function GetCalculateCumulative($z) {
if($z >= 0) {
$val = 1.0 - $this->PValue*($this->A1*$this->K + $this->A2*pow($this->K,2) + $this->A3*pow($this->K,3) + $this->A4*pow($this->K,4) + $this->A5*pow($this->K,5));
return $val;
}
else {
$val = 1.0 - $this->GetCalculateCumulative(-1 * $z);
return $val;
}
}
最佳答案
我进一步研究了它,似乎从 z 分数计算 p 值并不是那么容易。 This gives a formula ,这似乎是一个级数计算。您的公式看起来很接近它,但奇怪的是它给出的值比预期的要大。如果有的话,它应该比预期的要小。
至于解决方案,我认为最好只使用转换表,因为您要处理标准分布和设定的显着性水平(或几个)。
关于php - 在 PHP 中创建 P 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22150348/