// // This applet is based on code which was kindly provided by Matt Roughan // It uses Matt's PlotCanvas class which extends Canvas // Last updated 07/08/98 // import java.util.*; import java.applet.*; import java.awt.*; import MyUtil.*; public class PlotProbability extends Applet { Panel cardStack = null; CardLayout c1 = null; DistributionCard currentCard; int N = 5; DistributionCard[] theCards = new DistributionCard[N]; Button[] SelectionButtons = new Button[N]; String[] DistributionNames = {"Binomial", "Poisson", "Geometric", "Negative Binomial", "Hypergeometric" }; String startCard; MyDialog helpDialog; static Frame theFrame; //this array of strings is what is displayed by the help button String[] helpText = { "This applet graphs the probability function p(n) for several of", "the standard discrete distributions.", "", "The various parameters are controlled using the scrollbars.", "Pressing h brings up this message.", "" }; public void init(){ BorderLayout new_BorderLayout = new BorderLayout(0,0); setLayout(new_BorderLayout); cardStack = new Panel(); c1 = new CardLayout(); cardStack.setBackground(Color.white); cardStack.setLayout(c1); add("Center", cardStack); //create the cards for plotting each distribution theCards[0] = new BinomialCard(); cardStack.add(DistributionNames[0], theCards[0]); theCards[1] = new PoissonCard(); cardStack.add(DistributionNames[1], theCards[1]); theCards[2] = new GeometricCard(); cardStack.add(DistributionNames[2], theCards[2]); theCards[3] = new NegBinomialCard(); cardStack.add(DistributionNames[3], theCards[3]); theCards[4] = new HypergeometricCard(); cardStack.add(DistributionNames[4], theCards[4]); //put selection buttons on the bottom of the page Panel SelectionBar = new Panel(); SelectionBar.setBackground(Color.white); GridLayout new_GridLayout = new GridLayout(3,2,3,3); SelectionBar.setLayout(new_GridLayout); SelectionBar.add(new Label()); SelectionBar.add(new Label()); SelectionBar.add(new Label()); for (int i=0; i= low) & (par_value <= high)) { ParameterValues[i] = par_value; ParameterLabels[i].setText(ParameterStrings[i]+" = "+ParameterDefaults[i]); recalculate(); } else { System.out.println("Error: Parameter outside acceptible range!"); } } //define a method for plotting the graph public void plot() { PlotWindow.plot(x,BinWidth,p, Color.blue); //plot the probability function PlotWindow.repaint(); } int Factorial(int n) { if (n>1) { return n*Factorial(n-1); } else if ( (n==1) | (n==0) ) { return 1; } else { return 0; } } double P1(double lambda, int n) { double fac=Math.exp(-lambda); for (int i=1; i<=n; i++) { fac=fac*lambda/( (double) i); } return fac; } int Binom1(int n, int r) { // Adapted from my matlab code // Binom1(n,r) returns the binomial coefficient n choose r // This function is used by binom.m // // P.K. Pollett, May 1992. double fac=1.0; if ((n>0) & (r>0)) { fac=( (double) n)/( (double) r); for (int i=1; i<=(r-1); i++) { fac=fac*( (double) n-i)/( (double) r-i); } } else { fac=1.0; } return Math.round(Math.round(fac)); } int Binom(int n, int r) { // Adapted from my matlab code // Binom(n,r) returns the binomial coefficient n choose r // // P.K. Pollett, May 1992. double fac=1.0; if ((n>0) & (r>0)) { double m = Math.round(Math.round( n/2 )); if (r>m) { fac=( (double) Binom1(n,n-r) ); } else { fac=( (double) n)/( (double) r); for (int i=1; i<=(r-1); i++) { fac=fac*( (double) n-i)/( (double) r-i); } } } else { fac=1.0; } return Math.round(Math.round(fac)); } double Bin1(double p, int n, int r) { // Adapted from my matlab code // Bin1(p,n,r) returns the binomial probability binom(n,r)*p^r*(1-p)^{n-r} // P.K. Pollett, April 1992. if (r>n) { return 0.0; } else { double fac=1.0; double q=1-p; double s=p*q; if ((n>0) & (r>0)) { double m = Math.round(Math.round( n/2 )); int mm=n-2*r; fac=s*( (double) n)/( (double) r); for (int i=1; i<=(r-1); i++) { fac=fac*s*( (double) n-i)/( (double) r-i); } for (int i=1; i<=mm; i++) { fac=fac*q; } } else { for (int i=1; i<=n; i++) { fac=fac*q; } } return fac; } } double Bin(double p, int n, int r) { // Adapted from my matlab code // Bin(p,n,r) returns the binomial probability binom(n,r)*p^r*(1-p)^{n-r} // P.K. Pollett, April 1992. if (r>n) { return 0.0; } else { double fac=1.0; double q=1-p; double s=p*q; if ((n>0) & (r>0)) { double m = Math.round(Math.round( n/2 )); if (r>m) { fac=Bin1(q,n,n-r); } else { int mm=n-2*r; fac=s*( (double) n)/( (double) r); for (int i=1; i<=(r-1); i++) { fac=fac*s*( (double) n-i)/( (double) r-i); } for (int i=1; i<=mm; i++) { fac=fac*q; } } } else { for (int i=1; i<=n; i++) { fac=fac*q; } } return fac; } } double NegBin(int r, double p, int n) { // p(n) = ((n+r-1) choose (r-1)) p^r (1-p)^n double q=1-p; if (r==1) { return p*Math.pow(q,n); } else { return p*(( (double) n+r-1.0)/( (double) r-1.0))*NegBin(r-1,p,n); } } //define the function which recalculates the values of the //distribution - it calls the three abstract functions void recalculate() { for (int i=0; ihigh) ) { return 0.0; } else { return ( (double) Binom(a,n))*( (double) Binom(N-a,m-n))/( (double) Binom(N,m)); } } }