Code:
export F_var_test:={"",0,"",0};
export F_var_pvalue:=0;
export F_var_dfreedom:={0,0};
export F_var_intconf:={"", 0, "", 0};
EXPORT F_variance()
BEGIN
// F test for variances of two samples (H₀: σ₁²=σ₂² / H₁: σ₁² ≠ σ₂²)
local a:=0.05 ,n1:=2, n2:=2, s1:=1, s2:=1;
local mesg, icl, icu, control:=1, FT, F1, F2, p;
REPEAT
INPUT({a, n1, n2, s1, s2}, "F Variance test", {"Signif. level α=", "Sample1 n=", "Sample2 n=", "S1 var s₁²=", "S2 var s₂²="},
{"Significance level α (default 0.05)", "Sample1 size", "Sample2 size", "Observed Sample1 Variance s²", "Observed Sample2 Variance s²"},{0.01, 2, 2, 1, 1}, {a, n1, n2, s1, s2});
CASE
IF (a <=0 OR a>= 1) THEN MSGBOX("Significance level must be > 0 AND <1"); control:=1; a:=0.05; END;
IF (n1<2) THEN MSGBOX("Sample numerosity must be >=2"); control:=1; n1:=2; END;
IF (n2<2) THEN MSGBOX("Sample numerosity must be >=2"); control:=1; n2:=2; END;
IF (s1<0) THEN MSGBOX("Sample Variance must be >= 0"); control:=1; s1:=1; END;
IF (s2<=0) THEN MSGBOX("Sample Variance must be > 0"); control:=1; s2:=1; END;
DEFAULT control:= 0;
END; // case
UNTIL control < 1;
FT:= s1/s2; // F test=σ₁²/σ₂²
F1:=fisher_icdf(n1-1, n2-1,a/2); // lower Fisher percent
F2:=fisher_icdf(n1-1, n2-1,1-(a/2)); // upper Fisher percent
p:= 2* fisher_cdf(n1-1, n2-1, FT); // pvalue
icl:= (s1/s2)*fisher_icdf(n2-1, n1-1,a/2); // lower confidence interval
icu:= (s1/s2)*fisher_icdf(n2-1, n1-1,1-a/2); // upper confidence interval
MSGBOX( "Test Hypothesis: H₀: σ₁²=σ₂² vs H₁: σ₁² ≠ σ₂²");
IF ( FT>F1 OR FT<F2) THEN MSGBOX("Fail to reject H0 at α=" + eval('a')); END;
IF (FT <= F1 OR FT >=F2) THEN MSGBOX("Reject H0 at α=" + eval('a')); END;
mesg:= "Degrees of freedom: "+ eval( '(n1-1)') + " ; " + eval('(n2-1)');
MSGBOX(mesg);
sto({"FT", FT," F1", F1, "F2", F2}, F_var_test);
sto(p, F_var_pvalue);
sto({n1-1, n2-1}, F_var_dfreedom);
sto({"lower", icl, "upper", icu}, F_var_intconf);
RETURN("Test FT=" + FT+" (F1="+ F1 + " F2=" + F2 + ") p=" + p);
END;