using Distributions
using Roots
using Plots
using StatsFuns
?logistic
ccdf(Binomial(1608, 0.001), 1 - 1)
0.7998732846686536
ccdf(Binomial(1609, 0.001), 1 - 1)
0.8000734113839849
ccdf(Binomial(2000, 0.001), 1 - 1)
0.8648000746025003
n = 16000
p = 0.001
bin = Binomial(n, p)
cdf(bin, 0.5n*p), ccdf(bin, 1.5n*p - 1)
(0.021939318156404288, 0.036610210241716665)
normal = Normal(mean(bin), std(bin))
cdf(normal, n*(p - 0.0005)), ccdf(normal, n*(p + 0.0005))
(0.022696154499666343, 0.022696154499666343)
2*√(0.001(1 - 0.001))/√1600
0.001580348062927911
2*√(0.001(1 - 0.001))/√4000
0.000999499874937461
2*√(0.001(1 - 0.001))/√16000
0.0004997499374687305
x ⪅ y = x < y || x ≈ y
function pval(n, p, k)
bin = Binomial(n, p)
p0 = pdf(bin, k)
min(1, sum(pdf(bin, j) for j in support(bin) if pdf(bin, j) ⪅ p0))
end
function ci(n, k, α = 0.05)
CI = logistic.(find_zeros(t -> pval(n, logistic(t), k) - α, -50, 50))
end
ci (generic function with 2 methods)
CI = ci(16000, 16)
2-element Vector{Float64}: 0.0006001276770968745 0.0016157206168289175
plot(p -> pval(16000, p, 16), 0, 0.003; label="p-value function for n = 16000, k = 16")
plot!(; xtick=0:0.0005:0.003)