I want to implement an ERF function in PHP. I got its formula from wikipedia
P_Value = 1- ( ERF ( ABS ( Residual –mean ) )/(√2*SD )
I didn't get the idea how to implement it in PHP.
Based on the formula you provided, only the ERF (Error function) part should look like this:
function ERF ($difference) {
return abs($difference);
}
Now call $value = ERF($residual - $mean);
from anywhere inside a php script to store the ERF value in the $value
variable.
Edit: Let's assume you meant this formula:
So, it should be:
function ERF ($ll, $ul, $t, $dt, $dx) {
$val = 0;
for($i = $ll; $i <= $ul; $i+=$dx){
$val += exp(-pow($t,2)) * $dt;
}
return (2/sqrt(pi())) * $val;
}
now call, $value = ERF(0, $x, $t, $dt, $dx);
where $x is the upper limit, $t is the time and $dt is the dt part of integration as in time interval.
Note: I've added another parameter $dx here because it is an integral for continuous domain and $dx, $dt both should be very close to 0 for better approximations. For discrete values, you can provide both $dx and $dt as 1. And that would rather be called summation than integral.
For better approximation on integration, see Numerical integration algorithms and techniques.
There are better ways to approximate the error function than by naive numerical integration. The Wikipedia article about erf has a formula for numerical approximation. You can probably find others in Abramowitz & Stegun "Handbook of Mathematical Functions" or maybe the Digital Library of Mathematical Functions.
I found an implementation in PHP here: http://php.net/manual/en/function.stats-stat-percentile.php (look for the term "error function" in the text). Not sure which formula this implements.