// // power.java // power // // Created by Chris Thiel on 2/8/05. // Copyright (c) 2005 Chris Thiel. All rights reserved. // A simple Java applet that allows statistics student // see the relationship of the null hypothesis to different // alternatives, and the likelyhood of making a Type I or II error // (how alpha beta sigma relate to the power of a hypothesis test // import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.EventListener; import java.lang.Math; public class power extends Applet implements ActionListener,MouseListener,AdjustmentListener,KeyListener, ItemListener { Label mu0L, muAL, sigmaL, nL; Label[] blank = new Label[28]; TextField mu0T, muAT, sigmaT, nT; Panel wp= new Panel(); Scrollbar horiz=new Scrollbar(Scrollbar.HORIZONTAL,1,20,-200,143); Scrollbar vert=new Scrollbar(Scrollbar.VERTICAL,1,20,1,220); double h = 0; int v = 1; int sdGap=50; int center=350; double Alpha, Beta; double mu0=0; double sigma=1; double muA=0; private boolean OneTailed = false; private boolean ShowFormula = true; double zsig=1.96; String message = "Power of a Hypothesis Test"; private Font font1 = new Font("serif", Font.ITALIC + Font.BOLD, 36); private Font font2 = new Font("sansserif",0, 9); private Font font3 = new Font("serif",Font.BOLD, 18); private CheckboxGroup OneOrTwoTailed, AlphaLevel; private Checkbox HaGreater,HaLess, HaDiff, a05, a01; private Checkbox ShowForm; public void init() { setBackground(new Color(0xF0F0D0)); OneOrTwoTailed = new CheckboxGroup(); HaDiff = new Checkbox("\u03bca \u2260 \u03bc", OneOrTwoTailed, true); HaGreater = new Checkbox("\u03bca > \u03bc", OneOrTwoTailed,false); HaLess = new Checkbox("\u03bca < \u03bc", OneOrTwoTailed,false); AlphaLevel = new CheckboxGroup(); a05=new Checkbox("\u03b1 = .05",AlphaLevel,true); ShowForm= new Checkbox (); a01=new Checkbox("\u03b1 = .01",AlphaLevel,false); ShowFormula=false; ShowForm.setState(false); setLayout (null); mu0L= new Label ("\u03bc =",Label.RIGHT); muAL= new Label ("\u03bc ="); sigmaL= new Label ("\u03c3 =",Label.RIGHT); horiz.addAdjustmentListener(this); vert.addAdjustmentListener(this); nL= new Label (" n ="); // alpha is 03b1, beta is 03b2 mu0T = new TextField ( "0",5); muAT = new TextField ( "1",5); sigmaT = new TextField ( "1",5); nT = new TextField ( "1",5); setLayout(new BorderLayout()); wp.setLayout(new GridLayout(16,2)); for(int i=0;i<4;i++){ blank[i]=new Label(); wp.add(blank[i]); } wp.add(mu0L);wp.add(mu0T); wp.add(sigmaL); wp.add(sigmaT); mu0T.addActionListener(this); mu0T.addKeyListener(this); mu0T.addMouseListener(this); sigmaT.addActionListener(this); sigmaT.addKeyListener(this); for(int i=4;i<10;i++){ blank[i]=new Label(); wp.add(blank[i]); } blank[8].setText("n ="); blank[8].setAlignment(Label.RIGHT); wp.add(HaDiff); wp.add(HaGreater); blank[23]=new Label();wp.add(blank[23]); wp.add(HaLess); blank[24]=new Label(""); blank[26]=new Label(" "); blank[25]=new Label("\u03b1 level"); wp.add(blank[25]); wp.add(blank[24]); wp.add(blank[26]); wp.add(blank[26]); wp.add(a05); blank[22]=new Label();wp.add(blank[22]); wp.add(a01); for(int i=10;i<16;i++){ blank[i]=new Label(); wp.add(blank[i]); } blank[21]=new Label("Formulas");wp.add(blank[21]); wp.add(ShowForm); HaGreater.addItemListener(this); HaLess.addItemListener(this); HaDiff.addItemListener(this); a05.addItemListener(this); a01.addItemListener(this); ShowForm.addItemListener(this); add ("West", wp); //g.addActionListener(this); add("South",horiz); add("East",vert); vert.setValue(200); repaint(); } private double round(double x){ return Math.round(x*1000)/1000.0; } private double round4(double x){ return Math.round(x*10000)/10000.0; } public int Nullpdf(int x){ // takes a screen x (which is always N[300,sdGap/sqrt[n]], //and returns the screen y pdf based on the null hypothesis double s = 1.0/Math.sqrt(v);//sample sd double z = (x-center)/(sdGap*1.0); double p=Math.exp(-.5*z*z/(s*s))/(s*2.506628275); int amp=150; if (v<10) {amp=300-6*v;} else {amp=100;} return (int)(365-p*amp); } public int Altpdf(int x){ // takes a screen x (which is always N[300,sdGap/sqrt[n]], //and returns the screen y pdf based on the alternative hypothesis double s = 1.0/Math.sqrt(v);//sample sd double a = sdGap*1.0*h/sigma; double z = (x-center-a)/(sdGap*1.0); double p=Math.exp(-.5*z*z/(s*s))/(s*2.506628275); int amp=150; if (v<10) {amp=300-6*v;} else {amp=100;} return (int)(365-p*amp); } private double computeBeta(){ double b=0.0; double s=sigma/Math.sqrt(v); double me=zsig*s; double deltaX=sigma/100.0; if (OneTailed && zsig<0){ for (double i=mu0+me;i0) { for (double i=mu0-9*sigma;i0) { g.drawString("We reject the null hypothesis if x\u0305 greater than "+round4(mu0+me),170,80); }else { g.drawString("We reject the null hypothesis if x\u0305 less than "+round4(mu0-me),170,80); g.drawString("or greater than "+round4(mu0+me),200,100); }} // =/- \u00b1 Beta=computeBeta(); g.setFont(font3); g.setColor(Color.red); g.setColor(Color.magenta); g.drawString("z* = "+zsig,470, 140); g.setColor(Color.orange); g.drawString("\u03b2 = "+round4(Beta),470,160); g.setColor(Color.cyan); g.drawString("Power = "+round4(1.0-Beta),470,180); g.setColor(Color.red); g.drawString("H : \u03bc= "+round(mu0),470,100); g.setColor(Color.blue); g.drawString("H : \u03bc= "+round(muA),470, 120); g.setColor(Color.red); g.setFont(font2); g.drawString( "0", 483,105); g.setColor(Color.blue); g.drawString( "a", 483,125); //Shade rejection area on the H0 distribution g.setColor(Color.magenta); if(OneTailed){ if (zsig<0){//one sided left for (int i=130;i",200,180);//beta g.drawLine(250,175,388,175); g.drawString(round(sigma)+" / \u221a"+v,255,190); //sigma, sqrt g.drawString("("+round(mu0)+round(zsig)+"("+round(sigma)+"/\u221a"+v+") )-("+round(muA)+")" , 255,168); g.drawString("=P(Z> ",205,228); g.drawString("("+round4(mu0+me)+")-("+round4(muA)+")",250,220); g.drawLine(250,223,388,223); g.drawString(round(sigma)+" / \u221a"+v,255,240); g.drawString("=P(Z> "+round4((mu0+me-muA)/(sigma/Math.sqrt(v)))+")", 205 , 260); } }else{//one tailed right g.setColor(Color.cyan); for (int i=screenX(mu0+me)+1;i<601;i+=2){ g.drawLine(i,368,i,Altpdf(i)); } g.setColor(Color.yellow); for(int i=131;i9){sdGap=50+v;}else{sdGap=50;} } repaint(); } }