function [upperlim,lowerlim]=agresti_coull_binomial_confidence_interval(num_successes,num_attempts)
%description: based on adding 2 successes and 2 failures before computing the Wald CI;  American Statistician 1998
%called by: analyze_and_plot_mean_responses, analyze_history_dependence, analyze_release_dependence, change_point_data_plotter(deprecated),
 %analyze_mean_responses_using_precisely_measured_charge(deprecated), analyze_mean_responses_before_after(deprecated)
%calls: [no custom functions]
%notes: there seem to be different interpretations as to whether the denominator in the stderr term should be num_attempts+4 or num_attempts+z^2 (3.8416 at 95% CI);
%code confirmed date: oct 8, 2009

z=1.96;     %97.5th %ile for mean=0 std=1 normal distribution (for alpha=5%, alpha/2=2.5%)
%z=abs(qnorm(1-confidence_level/2)) -- this is s code, using confidence level as input
ptilde=(num_successes+2)/(num_attempts+4);
stderr=sqrt( (ptilde*(1-ptilde)) /(num_attempts+4));
upperlim=ptilde+(z*stderr);if(upperlim > 1), upperlim=1;end
lowerlim=ptilde-(z*stderr);if(lowerlim < 0), lowerlim=0;end