Delphi 中的 Z 分数到 P 值 - 单尾到双尾
Z Score to P Value in Delphi - one-tailed to two-tailed
早上好
我在网络中找到了现成的z值p值计算器(有点难Delphi;)。不幸的是,它只给出了左尾的值,而我需要它给出双尾的值。谁能告诉我如何计算或更改代码中的内容?
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;
根据在线计算器,例如这里:
https://www.easycalculation.com/statistics/p-value-for-z-score.php
对于给定的 z 分数:
0.70710678
这个代码给了我
0,76025003
结果,正确的结果是什么,但对于单尾假设。谁能告诉我如何获得双尾假设的正确结果,即 0.47950012?我的猜测是情况可能非常简单,但我不擅长 z 分布。 :(
感谢您的帮助。
您要查找的公式可以在您链接的网页的源代码中找到。
如果 X 是 P 函数的结果,计算 2*(1-X)。
我也从该源代码中翻译了 Z 函数。它根据您提供的功能生成不同的结果。说不出哪个更准确
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;
对不起,我认为这是一个愚蠢的问题 - 从我收到的反对票来看。出色地。 ;)
确实,我没有想到页面代码中可以包含该模式,真丢人。谢谢你大卫杜波依斯。挖掘部分代码:
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);
我只需要计算简单的方程式。函数 NormalP(x) 将给出 mi 左尾 P 值。然后 1-NormalP(x) 会给我右尾 P 值。然后 2*(1-NormalP(x)) 会给我一个答案。
不需要新函数,上面使用的都是有效的函数,兼容SPSS和R(我检查过)。
早上好
我在网络中找到了现成的z值p值计算器(有点难Delphi;)。不幸的是,它只给出了左尾的值,而我需要它给出双尾的值。谁能告诉我如何计算或更改代码中的内容?
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;
根据在线计算器,例如这里: https://www.easycalculation.com/statistics/p-value-for-z-score.php 对于给定的 z 分数: 0.70710678 这个代码给了我 0,76025003 结果,正确的结果是什么,但对于单尾假设。谁能告诉我如何获得双尾假设的正确结果,即 0.47950012?我的猜测是情况可能非常简单,但我不擅长 z 分布。 :(
感谢您的帮助。
您要查找的公式可以在您链接的网页的源代码中找到。
如果 X 是 P 函数的结果,计算 2*(1-X)。
我也从该源代码中翻译了 Z 函数。它根据您提供的功能生成不同的结果。说不出哪个更准确
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;
对不起,我认为这是一个愚蠢的问题 - 从我收到的反对票来看。出色地。 ;)
确实,我没有想到页面代码中可以包含该模式,真丢人。谢谢你大卫杜波依斯。挖掘部分代码:
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);
我只需要计算简单的方程式。函数 NormalP(x) 将给出 mi 左尾 P 值。然后 1-NormalP(x) 会给我右尾 P 值。然后 2*(1-NormalP(x)) 会给我一个答案。
不需要新函数,上面使用的都是有效的函数,兼容SPSS和R(我检查过)。