Advertisement
Guest User

Untitled

a guest
Apr 1st, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. using Distributions, HypothesisTests, StatsBase, StatsPlots
  2. theme(:solarized)
  3.  
  4.  
  5. function calc_coverage(n, p, nrep; method = :wald)
  6. x = rand(Binomial(n, p), nrep);
  7. bt = BinomialTest.(x, n);
  8. CIs = confint.(bt; level = 0.95, tail = :both, method = method);
  9.  
  10. res = Bool[]
  11. for i in eachindex(CIs)
  12. res = push!(res, CIs[i][1] .<= p && CIs[i][2] .>= p)
  13. end
  14.  
  15. return(mean(res))
  16. end
  17.  
  18.  
  19. p = 0.2;
  20. n = Int(1e4) .+ collect(0:100);
  21. nrep = Int(1e7);
  22.  
  23. cov = zeros(length(n));
  24. @time Threads.@threads for i in eachindex(n)
  25. cov[i] = calc_coverage(n[i], p, nrep; method = :clopper_pearson)
  26. if n[i] % 10 == 0
  27. println([n[i], cov[i]])
  28. end
  29. end
  30.  
  31. plot(n, cov, label = "p = 0.2", xlabel = "n", ylabel = " Coverage")
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement