Reputation: 204
I need to calculate a P-value (for significance checking) out of the Z value, mean(0), standarddeviation(1), normal distrubution being cummulative.
Is there a function in PHP that could do that?
This is my current code:
$P_value = (1/sqrt(2*pi()))*exp(-0.5*pow($this->ZValue,2));
ZValue = -4,688072309 which is confirmed the right value.
Expected value: 1,37895E-06
Value: 6.9681241978373E-6
Probably something wrong in the formula, but can't spot the fault.
Edit:
If anyone is interested, here is the code that does work:
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;
}
}
Upvotes: 4
Views: 1927
Reputation: 1929
I looked into it some more and it seems like it's not that easy to calculate p-value from z-score. This gives a formula, which seems to be a series calculation. Your formula looks like a good enough approximation to it, but it's weird that it gives a larger value than expected. If anything, it should be smaller than expected.
As for a solution, I think it's best you just use a conversion table since you're dealing with a standard distribution and a set significance level (or a few).
Upvotes: 1