// Author : Andre GILLIBERT 2017
// License Creative Commons CC0
// Usage : cpmidp(number_of_successes, number_of_trials, confidence_level)
// Computes the Clopper-Pearson mid-P two-sided confidence interval of a binomial proportion
// See article doi:10.2307/2348891

EXPORT cpmidp(x,n,conf)
BEGIN
P:=0;
N:=n;
X:=x;
C:=conf;
local lower := 0;
local upper := 1;
if (x > 0) then
	lower := FNROOT(BINOMIAL_CDF(N,P,X)-0.5*BINOMIAL(N,P,X)-(0.5+C/2),P,0.5);
end;
if (x < n) then
	upper := FNROOT(BINOMIAL_CDF(N,P,X)-0.5*BINOMIAL(N,P,X)-(0.5-C/2),P,0.5);
end;
return {lower, upper};
END;
