import java.lang.*; import corejava.Console; /** * *This class permits one to obtain *a two-sided confidence interval *on a coefficient of variation *for a normal distribution. * *@author Steve Verrill *@version .5 --- April 25, 2002 * */ public class COV_norm extends Object implements Fzero_methods { int id_f_to_zero; double sqrtn,df,ratio,alphad2,omad2; public static void main (String args[]) { double b1[] = new double[2]; double b2[] = new double[2]; double c[] = new double[2]; double r,re,ae; int iflag1[] = new int[2]; int iflag2[] = new int[2]; double mean,sd,estcv,xn; double conflev,tlim,ncp; double negb; int another,n; COV_norm zero = new COV_norm(); another = 1; while (another == 1) { /* Console is a public domain class described in Cornell and Horstmann's Core Java (SunSoft Press, Prentice-Hall). */ mean = Console.readDouble("\nWhat is the sample mean?\n"); if (mean == 0.0) { System.out.print("\nThis program does not handle the" + " case in which the sample mean equals 0.\n"); another = Console.readInt("\nDo you want to perform" + " another calculation? 0 - no 1 - yes\n"); } else { sd = Console.readDouble("\nWhat is the sample" + " standard deviation?\n"); if (sd == 0.0) { System.out.print("\nThe confidence interval on the" + " coefficient of variation is the single point 0.\n"); } else { estcv = sd/mean; System.out.print("\nThe estimated coefficient of" + " variation is " + estcv +".\n"); if (mean < 0.0) estcv = -estcv; n = Console.readInt("\nWhat is n?\n"); xn = n; zero.df = xn - 1.0; zero.sqrtn = Math.sqrt(xn); zero.ratio = zero.sqrtn/estcv; conflev = Console.readDouble("\nWhat confidence level" + " do you want? (.95 for a 95% confidence level)\n"); zero.alphad2 = (1.0 - conflev)/2.0; zero.omad2 = 1.0 - zero.alphad2; // Use CDF_nct_Amos.fnct_inv() to obtain a value for // t_{n-1}^{-1}(1 - alphad2) (Note that we want the central t // here so ncp is set to 0.) ncp = 0.0; tlim = CDF_nct_Amos.fnct_inv(zero.omad2,ncp,zero.df); // Obtain a lower confidence bound on cv (upper confidence bound // for a negative cv) zero.id_f_to_zero = 1; b1[1] = .001; c[1] = 10.0; r = estcv; re = .000001; ae = .00001; Fzero.fzero(zero,b1,c,r,re,ae,iflag1); if (iflag1[1] == 4 && b1[1] < 5.0) { b2[1] = .0001; c[1] = .001; r = estcv; re = .000001; ae = .000001; Fzero.fzero(zero,b2,c,r,re,ae,iflag2); if (iflag2[1] == 4 && b2[1] < .0005) { if (mean > 0.0) { System.out.print("\nThe lower confidence bound" + " on the COV lies between 0 and .0001.\n"); } else { System.out.print("\nThe upper confidence bound" + " on the COV lies between -.0001 and 0.\n"); } } else { if (mean > 0.0) { System.out.print("\nThe lower confidence bound" + " on the COV is " + b2[1] + ".\n"); } else { negb = -b2[1]; System.out.print("\nThe upper confidence bound" + " on the COV is " + negb + ".\n"); } } } else if (iflag1[1] == 4 && b1[1] > 5.0) { b2[1] = 10.0; c[1] = 100.0; r = estcv; re = .000001; ae = .01; Fzero.fzero(zero,b2,c,r,re,ae,iflag2); if (iflag2[1] == 4 && b2[1] > 50.0) { if (mean > 0.0) { System.out.print("\nThe lower confidence bound" + " on the COV is greater than 100.\n"); } else { System.out.print("\nThe upper confidence bound" + " on the COV is less than -100.\n"); } } else { if (mean > 0.0) { System.out.print("\nThe lower confidence bound" + " on the COV is " + b2[1] + ".\n"); } else { negb = -b2[1]; System.out.print("\nThe upper confidence bound" + " on the COV is " + negb + ".\n"); } } } else { // iflag != 4 if (mean > 0.0) { System.out.print("\nThe lower confidence bound" + " on the COV is " + b1[1] + ".\n"); } else { negb = -b1[1]; System.out.print("\nThe upper confidence bound" + " on the COV is " + negb + ".\n"); } } // Obtain an upper confidence bound on cv (lower confidence bound // for a negative cv) zero.id_f_to_zero = 2; // If tlim (obtained via the call to fnct_inv above) is greater than // or equal to sqrt(n)/estcv, then the upper confidence // bound is positive infinity if (tlim >= zero.ratio) { if (mean > 0.0) { System.out.print("\nThe upper confidence bound" + " on the COV is positive infinity.\n"); } else { System.out.print("\nThe lower confidence bound" + " on the COV is negative infinity.\n"); } } else { b1[1] = .001; c[1] = 10.0; r = estcv; re = .000001; ae = .00001; Fzero.fzero(zero,b1,c,r,re,ae,iflag1); if (iflag1[1] == 4 && b1[1] < 5.0) { b2[1] = .0001; c[1] = .001; r = estcv; re = .000001; ae = .000001; Fzero.fzero(zero,b2,c,r,re,ae,iflag2); if (iflag2[1] == 4 && b2[1] < .0005) { if (mean > 0.0) { System.out.print("\nThe upper confidence bound" + " on the COV lies between 0 and .0001.\n"); } else { System.out.print("\nThe lower confidence bound" + " on the COV lies between -.0001 and 0.\n"); } } else { if (mean > 0.0) { System.out.print("\nThe upper confidence bound" + " on the COV is " + b2[1] + ".\n"); } else { negb = -b2[1]; System.out.print("\nThe lower confidence bound" + " on the COV is " + negb + ".\n"); } } } else if (iflag1[1] == 4 && b1[1] > 5.0) { b2[1] = 10.0; c[1] = 100.0; r = estcv; re = .000001; ae = .01; Fzero.fzero(zero,b2,c,r,re,ae,iflag2); if (iflag2[1] == 4 && b2[1] > 50.0) { if (mean > 0.0) { System.out.print("\nThe upper confidence bound" + " on the COV is greater than 100.\n"); } else { System.out.print("\nThe lower confidence bound" + " on the COV is less than -100.\n"); } } else { if (mean > 0.0) { System.out.print("\nThe upper confidence bound" + " on the COV is " + b2[1] + ".\n"); } else { negb = -b2[1]; System.out.print("\nThe lower confidence bound" + " on the COV is " + negb + ".\n"); } } } else { // iflag != 4 if (mean > 0.0) { System.out.print("\nThe upper confidence bound" + " on the COV is " + b1[1] + ".\n"); } else { negb = -b1[1]; System.out.print("\nThe lower confidence bound" + " on the COV is " + negb + ".\n"); } } } } another = Console.readInt("\nDo you want to perform" + " another calculation? 0 - no 1 - yes\n"); } } } public double f_to_zero(double cv) { double ncp,diff,p; ncp = sqrtn/cv; if (id_f_to_zero == 1) { // for the lower confidence bound p = CDF_nct_Amos.fnct(ratio,ncp,df); diff = alphad2 - p; return diff; } else { // for the upper confidence bound p = CDF_nct_Amos.fnct(ratio,ncp,df); diff = omad2 - p; return diff; } } }