import java.awt.*; import java.applet.Applet; import java.util.Random; import java.lang.Double; /** Applet to demonstrate that sampling distributions converge to the population. Uses a histogram plot of the sample and overlays it with the expected probability density function (PDF). Also shows the expected and sample means. @Author: Suresh Srinivasan (suresh@thomtech.com) @Date: Oct 1995 @Version: 0.1 */ public class SamplingApplet extends java.applet.Applet { static final java.awt.Color BACKGROUNDCOLOR = Color.lightGray; String distributionNames[] = { "Gaussian", "Exponential", "Uniform" }; Label statusLabel; ControlPanel controlPanel; GraphPanel graphPanel; StatusPanel statusPanel; DataPanel dataPanel; Distribution distribution; public void init() { setLayout(new BorderLayout()); /* Panel to attach the control and data panels */ Panel p = new Panel(); p.setLayout(new GridLayout(1, 2)); controlPanel = new ControlPanel(this); dataPanel = new DataPanel(this); p.add(controlPanel); p.add(dataPanel); add("North", p); graphPanel = new GraphPanel(this); add("Center", graphPanel); statusPanel = new StatusPanel(this); add("South", statusPanel); /* this should cause each component in this layout to compute their sizes */ layout(); /* start the thread for the graph */ graphPanel.start(); } public static void main(String args[]) { Frame f = new Frame("Sampling Distributions"); SamplingApplet applet = new SamplingApplet(); applet.init(); f.add("Center", applet); f.resize(500, 400); f.pack(); f.show(); applet.start(); } } class ControlPanel extends Panel { public final static String STARTSTRING = "Start"; public final static String STOPSTRING = "Stop"; public final static String PAUSESTRING = "Pause"; public final static String RESUMESTRING = "Resume"; public final static Font BUTTONFONT = new Font("Dialog", Font.BOLD, 12); public final static Font CHOICEFONT = new Font("Dialog", Font.PLAIN, 12); public final static Font ERRORFONT = new Font("TimesRoman", Font.BOLD, 20); SamplingApplet applet; Button startButton; Button pauseButton = new Button(PAUSESTRING); Choice distributionChoice; public ControlPanel(SamplingApplet applet) { this.applet = applet; setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10)); startButton = new Button(); startButton.setFont(BUTTONFONT); startButton.setLabel(STARTSTRING); add(startButton); pauseButton = new Button(); pauseButton.setFont(BUTTONFONT); pauseButton.setLabel(PAUSESTRING); add(pauseButton); distributionChoice = new Choice(); distributionChoice.setFont(CHOICEFONT); for (int i=0; i 0) variance = ((double)sampleSize*sum2 - sum*sum)/(double)(sampleSize*(sampleSize-1)); offG.setColor(SamplingApplet.BACKGROUNDCOLOR); offG.fill3DRect(0, 0, d.width-1, d.height-1, true); offG.setColor(Color.blue); offG.drawString(SAMPLESIZESTRING + sampleSize, xOfs, yOfs); offG.drawString(MEANSTRING + sum/(double)sampleSize, xOfs, yOfs+fontHeight+gutter); offG.drawString(VARIANCESTRING + variance, xOfs, yOfs+2*(fontHeight+gutter)); g.drawImage(offI, 0, 0, this); } } class GraphPanel extends Panel implements Runnable { int current = 0; SamplingApplet applet; Thread thread; Distribution dist[]; boolean running = false; boolean readyToPaint = true; public GraphPanel(SamplingApplet applet) { this.applet = applet; } public void start() { if (thread == null) { dist = new Distribution[applet.distributionNames.length]; for (int i=0; i= maxFreq) return(true); } return(false); } /* generates a sample and adds to the histogram */ public void sample() { double d = rand.nextGaussian(); int i = (int)((d-low)/intervalLength); if (i>0 && i= maxFreq) return(true); } return(false); } /* generates a sample and adds to the histogram */ public void sample() { double d = invpdf(rand.nextDouble()); int i = (int)((d-low)/intervalLength); if (i>=0 && i= maxFreq) return(true); } return(false); } /* generates a sample and adds to the histogram */ public void sample() { double d = rand.nextDouble(); int i = (int)((d-low)/intervalLength); if (i>=0 && i