746 |
|
|
747 |
|
|
748 |
|
|
749 |
– |
|
750 |
– |
double BinP(int N, double p, int x1, int x2) { |
751 |
– |
double q=p/(1-p); |
752 |
– |
int k=0; |
753 |
– |
double v = 1; |
754 |
– |
double s=0; |
755 |
– |
double tot=0.0; |
756 |
– |
while(k<=N) { |
757 |
– |
tot=tot+v; |
758 |
– |
if(k>=x1 & k<=x2) { s=s+v; } |
759 |
– |
if(tot>1e30){s=s/1e30; tot=tot/1e30; v=v/1e30;} |
760 |
– |
k=k+1; |
761 |
– |
v=v*q*(N+1-k)/k; |
762 |
– |
} |
763 |
– |
return s/tot; |
764 |
– |
} |
765 |
– |
|
766 |
– |
|
767 |
– |
|
768 |
– |
|
769 |
– |
void ClopperPearsonLimits(double numerator, double denominator, double &ratio, |
770 |
– |
double &lowerLimit, double &upperLimit, const double CL_low=1.0, |
771 |
– |
const double CL_high=1.0) |
772 |
– |
{ |
773 |
– |
//Confidence intervals are in the units of \sigma. |
774 |
– |
|
775 |
– |
|
776 |
– |
ratio = numerator/denominator; |
777 |
– |
|
778 |
– |
// first get the lower limit |
779 |
– |
if(numerator==0) lowerLimit = 0.0; |
780 |
– |
else { |
781 |
– |
double v=ratio/2; |
782 |
– |
double vsL=0; |
783 |
– |
double vsH=ratio; |
784 |
– |
double p=CL_low/100; |
785 |
– |
while((vsH-vsL)>1e-5) { |
786 |
– |
if(BinP(int(denominator),v,int(numerator),int(denominator))>p) |
787 |
– |
{ vsH=v; v=(vsL+v)/2; } |
788 |
– |
else { vsL=v; v=(v+vsH)/2; } |
789 |
– |
} |
790 |
– |
lowerLimit = v; |
791 |
– |
} |
792 |
– |
|
793 |
– |
// now get the upper limit |
794 |
– |
if(numerator==denominator) upperLimit = 1.0; |
795 |
– |
else { |
796 |
– |
double v=(1+ratio)/2; |
797 |
– |
double vsL=ratio; |
798 |
– |
double vsH=1; |
799 |
– |
double p=CL_high/100; |
800 |
– |
while((vsH-vsL)>1e-5) { |
801 |
– |
if(BinP(int(denominator),v,0,int(numerator))<p) { vsH=v; v=(vsL+v)/2; } |
802 |
– |
else { vsL=v; v=(v+vsH)/2; } |
803 |
– |
} |
804 |
– |
upperLimit = v; |
805 |
– |
} |
806 |
– |
} |
807 |
– |
|
808 |
– |
|
809 |
– |
|
810 |
– |
|
811 |
– |
|
812 |
– |
|