# Clopper-Pearson mid-P for the Julia programming language
# Syntax : cpmidp(number_of_successes, number_of_trials, confidence_lvel)
# Author : André GILLIBERT 2017
# License : Creative Commons CC0
#
using Roots
using Distributions

function cpmidp(x,n,conf)
	function cpmidq(x,n,target)
		fzero((p -> cdf(Binomial(n,p),x)-0.5*pdf(Binomial(n,p),x) - target), x/n, [0,1], xtol=1e-7/n)
	end
	v = [cpmidq(x,n,0.5+conf/2), cpmidq(x,n,0.5-conf/2)]
	if x == 0
		v[1]=0
	end
	if x == n
		v[2]=n
	end
	v
end
