Home > matlabmk > binary_ci.m

# binary_ci

## PURPOSE

function ci=binary_ci(n_hits,n_obs,prcnt_ci)

## SYNOPSIS

function ci=binary_ci(n_hits,n_obs,prcnt_ci)

## DESCRIPTION

```function ci=binary_ci(n_hits,n_obs,prcnt_ci)

binary_ci-Computes two-tailed confidence intervals for estimated
probability of a "hit" for binary random variables according to the
methods outlined by Zar (1999).

Inputs:
n_hits   - [integer] Number of observed hits.
n_obs    - [integer] Total number of observations.
prcnt_ci - Desired level of confidence (e.g., 95 for a 95% confidence
limits)

Outputs:
ci - ci(1)=lover confidence bound, ci(2)=upper confidence bound

Example:
>>ci=binary_ci(4,200,95)
>>fprintf('Lower bound %f, Upper bound %f\n',ci(1),ci(2));
Lower bound 0.005476, Upper bound 0.050414

Thus with 95% confidence, we can say that the true probability of a hit
is between 0.0055 and .0504 (given that we observed 4 hits out of 200
total observations).

References:
Zar, J. H. (1999). Biostatistical Analysis (Fourth Ed.). Upper Saddle
River, New Jersey: Prentice Hall.```

## CROSS-REFERENCE INFORMATION

This function calls:
This function is called by:

## SOURCE CODE

```0001 function ci=binary_ci(n_hits,n_obs,prcnt_ci)
0002 %function ci=binary_ci(n_hits,n_obs,prcnt_ci)
0003 %
0004 % binary_ci-Computes two-tailed confidence intervals for estimated
0005 % probability of a "hit" for binary random variables according to the
0006 % methods outlined by Zar (1999).
0007 %
0008 % Inputs:
0009 %  n_hits   - [integer] Number of observed hits.
0010 %  n_obs    - [integer] Total number of observations.
0011 %  prcnt_ci - Desired level of confidence (e.g., 95 for a 95% confidence
0012 %             limits)
0013 %
0014 % Outputs:
0015 %  ci - ci(1)=lover confidence bound, ci(2)=upper confidence bound
0016 %
0017 %
0018 % Example:
0019 % >>ci=binary_ci(4,200,95)
0020 % >>fprintf('Lower bound %f, Upper bound %f\n',ci(1),ci(2));
0021 % Lower bound 0.005476, Upper bound 0.050414
0022 %
0023 % Thus with 95% confidence, we can say that the true probability of a hit
0024 % is between 0.0055 and .0504 (given that we observed 4 hits out of 200
0025 % total observations).
0026 %
0027 %
0028 % References:
0029 % Zar, J. H. (1999). Biostatistical Analysis (Fourth Ed.). Upper Saddle
0030 % River, New Jersey: Prentice Hall.
0031 %
0032
0033 ci=zeros(1,2);
0034 prcnt_ci=prcnt_ci/100; %turn percent into proportion
0035 half_alph=(1-prcnt_ci)/2;
0036
0037 %First three are special cases
0038 if ~n_hits,
0039     %note: ci(1)=0
0040     ci(2)=1-(half_alph)^(1/n_obs);
0041 elseif n_hits==n_obs-1,
0042     temp_hits=1;
0043     v1=2*(n_obs-temp_hits+1);
0044     v2=2*temp_hits;
0045     vv1=v2+2;
0046     vv2=v1-2;
0047     FF=finv(1-half_alph,vv1,vv2);
0048     temp_upr=(temp_hits+1)*FF/(n_obs-temp_hits+(temp_hits+1)*FF);
0049     ci(1)=1-temp_upr;
0050
0051     ci(2)=(1-half_alph)^(1/n_obs);
0052 elseif n_hits==n_obs,
0053     ci(1)=half_alph^(1/n_obs);
0054     ci(2)=1;
0055 else
0056     v1=2*(n_obs-n_hits+1);
0057     v2=2*n_hits;
0058     F=finv(1-half_alph,v1,v2);
0059     ci(1)=n_hits/(n_hits+(n_obs-n_hits+1)*F);
0060
0061     vv1=v2+2;
0062     vv2=v1-2;
0063     FF=finv(1-half_alph,vv1,vv2);
0064     ci(2)=(n_hits+1)*FF/(n_obs-n_hits+(n_hits+1)*FF);
0065 end```

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005