-3

Good morning

I found a ready-made p-value calculator from the z value in the network (it's kinda hard for Delphi;). Unfortunately, it only gives the value for left-tailed, and I need it to give a value for two-tailed. Can anyone tell me how to calculate or what to change in the code?

function NormalZ (const X: Extended): Extended;
{ Returns Z(X) for the Standard Normal Distribution as defined by
  Abramowitz & Stegun. This is the function that defines the Standard
  Normal Distribution Curve.
  Full Accuracy of FPU }
begin
  Result := Exp (- Sqr (X) / 2.0)/Sqrt (2 * Pi);
end;

function NormalP (const A: Extended): Single;
{Returns P(A) for the Standard Normal Distribution as defined by
  Abramowitz & Stegun. This is the Probability that a value is less
  than A, i.e. Area under the curve defined by NormalZ to the left
  of A.
  Only handles values A >= 0 otherwise exception raised.
  Accuracy: Absolute Error < 7.5e-8 }
const
  B1: Extended = 0.319381530;
  B2: Extended = -0.356563782;
  B3: Extended = 1.781477937;
  B4: Extended = -1.821255978;
  B5: Extended = 1.330274429;
var
  T: Extended;
  T2: Extended;
  T4: Extended;
begin
  if (A < 0) then
    raise EMathError.Create ('Value must be Non-Negative')
  else
  begin
    T := 1 / (1 + 0.2316419 * A);
    T2 := Sqr (T);
    T4 := Sqr (T2);
    Result := 1.0 - NormalZ (A) * (B1 * T + B2 * T2
      + B3 * T * T2 + B4 * T4 + B5 * T * T4);
  end;
end;

According to the online calculator, for example here: https://www.easycalculation.com/statistics/p-value-for-z-score.php for given z score: 0.70710678 this code gives me 0,76025003 as result, what is the correct result, but for one-tailed hypothesis. Can anyone tell me how to get the correct result for a two-tailed hypothesis, i.e. 0.47950012? My guess is that the case can be very simple, but I'm not good at z-distibution. :(

Thank You for any help.

kwadratens
  • 187
  • 15

2 Answers2

1

The formula you're looking for can be found in the source code of the web page you've linked.

If X is the result of the P function, compute 2*(1-X).

I've translated the Z function from that source code as well. It generates different results from the function you provided. I can't speak to which is more accurate.

function pzscore(z:extended):extended;
const
  Z_MAX = 6;
var
  x, y, w : extended;
begin
  if z = 0.0 then
    x := 0.0
  else
    begin
      y := 0.5 * abs(z);
      if y > Z_MAX * 0.5 then
        x := 1.0
      else
        if y < 1.0 then
          begin
            w := y * y;
            x := ((((((((0.000124818987 * w
                 - 0.001075204047) * w + 0.005198775019) * w
                 - 0.019198292004) * w + 0.059054035642) * w
                 - 0.151968751364) * w + 0.319152932694) * w
                 - 0.531923007300) * w + 0.797884560593) * y * 2.0;
          end
        else
          begin
            Y := Y - 2.0;
            x := (((((((((((((-0.000045255659 * y
                 + 0.000152529290) * y - 0.000019538132) * y
                 - 0.000676904986) * y + 0.001390604284) * y
                 - 0.000794620820) * y - 0.002034254874) * y
                 + 0.006549791214) * y - 0.010557625006) * y
                 + 0.011630447319) * y - 0.009279453341) * y
                 + 0.005353579108) * y - 0.002141268741) * y
                 + 0.000535310849) * y + 0.999936657524;
          end;
    end;
  if x > 0.0 then
    Result :=  (x + 1.0) * 0.5
  else
    Result :=  (1.0 - x) * 0.5;
end;
David Dubois
  • 3,842
  • 3
  • 18
  • 36
0

I'm sorry, i think it was a dumb question - judging by the downvotes that I received. Well. ;)

Indeed, I did not think that the pattern could be inside the page code, so shame on me. And thank You David Dubois. Excavating part of code:

var lp = pzscore(z);
document.getElementById('lp').value = lp.toFixed(4);
var rp = 1 - lp;
document.getElementById('rp').value = rp.toFixed(4);
 var tp = 2 * rp;
document.getElementById('tp').value = tp.toFixed(4);
 var cl = 1 - tp;
document.getElementById('cl').value = cl.toFixed(4);

i just need to calculate simple equations. Function NormalP(x) will give mi Left-tailed P-value. Then 1-NormalP(x) will give me Right-tailed P-value. Then 2*(1-NormalP(x)) will give me an answer.

There's no need for new function, the above used are valid functions that are compatible with eg SPSS and R (I checked).

kwadratens
  • 187
  • 15