import papaya.*; PFont myfont; PFont myboldfont; JakeSlider samplepropslider, confidenceslider, largesampleslider; JakeButton leftarrow, rightarrow; PImage CIpeqn; PImage smallleft; PImage smallup; PImage smalldown; PImage smallright; String[] names = { "Neo", "Batman", "Mr. Anderson", "Lisa Simpson", "Bart Simpson", "Homer Simpson", "Ted Mosby", "Robin Scherbatsky", "Spiderman", "Felicia", "Carol", "Samantha", "Nathan", "Morpheus" }; String[] gender = { "He", "He", "He", "She", "He", "He", "He", "She", "He", "She", "She", "She", "He", "He" }; String[] obj = { "smartphone", "tablet", "desktop computer", "laptop", "blu-ray player", "dvd player", "wii", "ps3", "xbox 360", "high definition TV", "Roomba", "quadrocopter", "Roku box", "5.1 surrond sound system", "photo printer", "ipad", "Kindle Fire" }; int[] samplesize = {60, 70, 80, 90, 100}; int[] confidence = {80, 90, 95, 99}; float[] Zvalues = { 0.8997, .9505, .9750, .9951}; float[] Zrow = {1.2, 1.6, 1.6, 2.5}; float[] Zcolumn = { .08, .05, .06, .08}; float[] zvalues; float leftCI = 0, rightCI = 0; int samplesizeslidervalue; int confidenceslidervalue; float samplepropslidervalue; int page = 1; int numtemplates = 2; String[] problemText = new String[numtemplates]; int namerand; int objrand; int sizerand; int confidencerand; int numrand; String numrandstr; String pagetext; String propstr; int temprand; void setup() { size(700, 500); smooth(); myfont = loadFont("ArialMT-24.vlw"); myboldfont = loadFont("Arial-BoldMT-24.vlw"); CIpeqn = loadImage("CIp.gif"); smallleft = loadImage("leftbluesmall.jpg"); smallup = loadImage("bluesmallup.jpg"); smalldown = loadImage("smalldown.jpg"); smallright = loadImage("rightbluesmall.jpg"); PImage[] lefts = {loadImage("leftblue.jpg"), loadImage("leftpurple.jpg")}; leftarrow = new JakeButton(30,415,lefts[1],lefts[0],lefts[0]); PImage[] rights = {loadImage("rightblue.jpg"), loadImage("rightpurple.jpg")}; rightarrow = new JakeButton(615,415,rights[1],rights[0],rights[0]); zvalues = new float[201]; float xloc = -4; for (int i = 0; i < 201; i++) { zvalues[i] = 1.0/sqrt(2.0*PI)*exp(-1.0*xloc*xloc/2.0); xloc += .04; } generateRandom(); samplepropslider = new JakeSlider(390,325,100,30,0.5,0.2,0.8,false,color(71, 145, 255),color(2,52,77),myfont,10,1,2); samplepropslider.setVisible(false); confidenceslider = new JakeSlider(220,325,100,30,confidence[confidencerand],80,99,true,color(71, 145, 255),color(2,52,77),myfont,10,2,0); confidenceslider.setVisible(false); largesampleslider = new JakeSlider(50,325,100,30,samplesize[sizerand],60,100,true,color(71, 145, 255),color(2,52,77),myfont,10,2,0); largesampleslider.setVisible(false); } void generateRandom() { namerand = int(random(names.length)); objrand = int(random(obj.length)); sizerand = int(random(samplesize.length)); confidencerand = int(random(confidence.length)); numrand = int(random(10.0, samplesize[sizerand]-10.0)); numrandstr = nf(numrand, 2); temprand = int(random(numtemplates)); samplesizeslidervalue = samplesize[sizerand]; confidenceslidervalue = confidence[confidencerand]; float tempfloat = (float)numrand/(float)samplesize[sizerand]; samplepropslidervalue = 0.5; propstr = nf(tempfloat, 1, 2); } void draw() { background(255); fill(0); problemText[0] = names[namerand] + " has surveyed " + samplesize[sizerand] + " individuals about their " + obj[objrand] + " ownership. In this group, " + numrandstr + " said they owned a " + obj[objrand] +". Find the " + confidence[confidencerand] + "% confidence interval for the population proportion who own a " + obj[objrand] + "."; problemText[1] = names[namerand] + " is interested in studying the population proportion who own a " + obj[objrand] + ". " + gender[namerand] + " collected a sample of " + samplesize[sizerand] + " people and found " + numrandstr + " of them own a " + obj[objrand] + ". Calculate the " + confidence[confidencerand] + "% confidence interval for the population proportion who own a " + obj[objrand] + "."; switch(page) { case 1: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step one: Identify the problem type and formula to use"; drawStrings(pagetext, 30, 194-25, 25, "bold"); pagetext = "The type of problem we have here is to find the confidence interval " + "for a population proportion. We use the formula: "; image(CIpeqn, 350-72, 290-29); drawStrings(pagetext, 80, 244-25, 25); leftarrow.setVisible(false); rightarrow.setVisible(true); break; case 2: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step two: Identify variables"; drawStrings(pagetext, 30, 194-25, 25, "bold"); pagetext = "We want to identify each variable in our formula:"; drawStrings(pagetext, 80, 244-25, 25); image(CIpeqn, 350-72, 258-25); pagetext = " Our sample size is represented by n, which is " + samplesize[sizerand] + "." + " The p with a hat represents our sample proportion. In this problem, the sample proportion is " + propstr + " ("+ numrandstr + " divided by the sample size). We also have the confidence level, given by the variable L, as " + confidence[confidencerand] + "%."; drawStrings(pagetext, 80, 324-25, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); break; case 3: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step two continued: "; drawStrings(pagetext, 30, 194-25, 25, "bold"); pagetext = "In our formula: "; drawStrings(pagetext, 80, 244-25, 25); image(CIpeqn, 350-72, 258-25); pagetext = "The Z with the subscript (1 + L)/2 represents (1 + L)/2 percentile of the Z curve. In this case, " + "we have L = ." + confidence[confidencerand] + ", so we are looking for the " + (confidence[confidencerand]+100)/2.0 + " percentile of the Z curve. "; drawStrings(pagetext, 80, 330-25, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); break; case 4: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step three: Find a percentile"; drawStrings(pagetext, 30, 194-25, 25, "bold"); float tempfloat = ((float)confidence[confidencerand]/100+1)/2.0; pagetext = "Use the table A.3 to the find the percentile of the Z curve. "; pagetext += "Scan the entries of the table until you see " + tempfloat + ". The corresponding row and column gives you " + "the " + (confidence[confidencerand]+100)/2.0 + " percentile as " + nf(Zrow[confidencerand]+Zcolumn[confidencerand], 1, 4) + " (ie " + (confidence[confidencerand]+100)/2.0 + "% of the area is to the left of this point)."; drawStrings(pagetext, 80, 244-25, 25); pagetext = nf(Zcolumn[confidencerand], 1, 4); drawStrings(pagetext, 230, 294+45-25, 25); image(smallup, 245, 294+50-25); pagetext = nf(Zrow[confidencerand], 1, 4); drawStrings(pagetext, 130, 394-25, 25); image(smallleft, 190, 373-25); pagetext = nf(Zvalues[confidencerand], 1, 4); drawStrings(pagetext, 230, 394-25, 25); drawBellcurve(Zrow[confidencerand]+Zcolumn[confidencerand], 400, 308-25, 200, 88); leftarrow.setVisible(true); rightarrow.setVisible(true); break; case 5: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step four: Plug in values and interpret result"; drawStrings(pagetext, 30, 194-25, 25, "bold"); pagetext = "Now, plug in your values for the variables into: "; drawStrings(pagetext, 80, 244-25, 25); image(CIpeqn, 350-72, 258-27); pagetext = "Plugging in, we get " + propstr + " plus or minus the square root of (" + propstr + "*(1-"+propstr+")/"+samplesize[sizerand] + ") times " + nf(Zrow[confidencerand]+Zcolumn[confidencerand], 1, 4)+"."; tempfloat = Float.parseFloat(propstr); leftCI = tempfloat - sqrt(tempfloat*(1.0-tempfloat)/(float)samplesize[sizerand]) *(Zrow[confidencerand]+Zcolumn[confidencerand]); rightCI = tempfloat + sqrt(tempfloat*(1.0-tempfloat)/(float)samplesize[sizerand]) *(Zrow[confidencerand]+Zcolumn[confidencerand]); pagetext += " This gives us the interval (" + nf(leftCI, 1, 2) + ", " + nf(rightCI, 1, 2)+ "). " + names[namerand] + " is " + confidence[confidencerand] + "% confident that the population proportion owning a " + obj[objrand] + " will be between " + nf(leftCI, 1, 2) + " and " + nf(rightCI, 1, 2) + "."; drawStrings(pagetext, 80, 319-25, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); break; case 6: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step five: State your assumptions"; drawStrings(pagetext, 30, 194-25, 25, "bold"); pagetext = "In creating our confidence interval, we have assumed a Bin(n,p) model and that the " + "sample size is large. You need to check the conditions: "; drawStrings(pagetext, 80, 244-25, 25); pagetext = "Y >= 10 and n - Y >= 10."; drawStrings(pagetext, 80, 244+25*2-25, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); break; case 7: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Exploring the confidence interval: "; drawStrings(pagetext, 30, 194-25, 25, "bold"); pagetext = "What is the effect of the sample proportion on the confidence interval? What are the effects of the confidence level and sample size? "; drawStrings(pagetext, 50, 244-25, 25); pagetext = "Sample size"; drawStrings(pagetext, 50, 294+25-25, 25); pagetext = "Confidence Level"; drawStrings(pagetext, 205, 294+25-25, 25); pagetext = "Sample proportion"; drawStrings(pagetext, 385, 294+25-25, 25); pagetext = "Your interval:"; drawStrings(pagetext, 540, 294+25-25, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); confidenceslider.setVisible(true); samplepropslider.setVisible(true); float leftedge, rightedge; tempfloat = Float.parseFloat(propstr); largesampleslider.setVisible(true); float percentile = (float)Probability.norminv((1.0 + (float)confidenceslidervalue/100.0)/2.0); leftCI = samplepropslidervalue - sqrt(samplepropslidervalue*(1.0-samplepropslidervalue)/(float)samplesizeslidervalue)*percentile; rightCI = samplepropslidervalue + sqrt(samplepropslidervalue*(1.0-samplepropslidervalue)/(float)samplesizeslidervalue)*percentile; leftedge = map(leftCI, 0.2-sqrt(.25/60.0)*2.58, 0.8+sqrt(.25/60.0)*2.58, 540, 640); rightedge = map(rightCI, 0.2-sqrt(.25/60.0)*2.58, 0.8+sqrt(.25/60.0)*2.58, 540, 640); pagetext = "("+nf(leftCI, 1, 2)+", "+nf(rightCI, 1, 2)+")"; drawStrings(pagetext, 540, 294+100-5-25, 25); fill(71, 145, 255); rect(leftedge, 294+57-25, rightedge-leftedge, 4); // fill(255,0,0); // pagetext = "Warning: assumptions violated. Y needs to be "; // drawStrings(pagetext, 150, 425, 25, "bold"); // pagetext = " between 10 and (samplesize-10)."; // drawStrings(pagetext, 150, 450, 25, "bold"); break; case 8: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Exploring the confidence interval continued: "; drawStrings(pagetext, 30, 194-25, 25, "bold"); pagetext = "The sample proportion is the midpoint/center of the confidence interval. Varying it will move the confidence interval left and right. The sample size " + "alters the width of the confidence interval. As you increase your sample size, the width gets smaller. Lastly, if you increase your level of confidence, " + "the width of the confidence interval will increase. "; drawStrings(pagetext, 30, 194+50-25, 25); pagetext = "To try another problem, click the right arrow."; drawStrings(pagetext, 30, 194+200-25, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); break; } samplepropslider.updateSlider(); samplepropslider.drawSlider(); samplepropslidervalue = samplepropslider.getValue(); samplepropslidervalue = round(samplepropslidervalue*100.0)/100.0; confidenceslider.updateSlider(); confidenceslider.drawSlider(); confidenceslidervalue = (int)confidenceslider.getValue(); largesampleslider.updateSlider(); largesampleslider.drawSlider(); samplesizeslidervalue = (int)largesampleslider.getValue(); leftarrow.updateButton(); leftarrow.drawButton(); if (leftarrow.clicked) { largesampleslider.setVisible(false); confidenceslider.setVisible(false); samplepropslider.setVisible(false); page--; } rightarrow.updateButton(); rightarrow.drawButton(); if(rightarrow.clicked) { largesampleslider.setVisible(false); confidenceslider.setVisible(false); samplepropslider.setVisible(false); if (page == 8) { page = 0; generateRandom(); float tempfloat = Float.parseFloat(propstr); confidenceslider.setValue(confidenceslidervalue); samplepropslider.setValue(0.5); } if (page != 8) page++; } } void drawStrings(String astring, int startx, int starty, int yspacing) { drawStrings(astring, startx, starty, yspacing, "plain"); } void drawStrings(String astring, int startx, int starty, int yspacing, String boldtype) { String[] theTokens = splitTokens(astring, " "); int x = startx; int y = starty; if (boldtype.equals("plain")) textFont(myfont, 18); else textFont(myboldfont, 18); for (int words = 0; words < theTokens.length; words++) { String currentword = theTokens[words]; float wordwidth = textWidth(currentword + " "); if (x + wordwidth > 675) { x = startx; y += yspacing; } text(currentword, x, y); x += wordwidth; } } void drawBellcurve(float loc, int xstart, int ystart, int bwidth, int bheight) { int index = round(map(loc, -4, 4, 0, 200)); float xscreen, yscreen; float xloc = -4.0; fill(255, 0, 0); beginShape(); for (int i = 10; i <= index; i++) { xscreen = map(xloc, -4, 4, xstart, xstart+bwidth); yscreen = map(zvalues[i], 0, 0.5, ystart+bheight, ystart); vertex(xscreen, yscreen); xloc += .04; } xloc-=.04; xscreen = map(xloc, -4, 4, xstart, xstart+bwidth); yscreen = map(zvalues[0], 0, 0.5, ystart+bheight, ystart); vertex(xscreen, yscreen); endShape(CLOSE); float saveloc = xloc; fill(255, 255, 255); beginShape(); for (int i = index; i < 191; i++) { xscreen = map(xloc, -4, 4, xstart, xstart+bwidth); yscreen = map(zvalues[i], 0, 0.5, ystart+bheight, ystart); vertex(xscreen, yscreen); xloc += .04; } xscreen = map(saveloc, -4, 4, xstart, xstart+bwidth); yscreen = map(zvalues[0], 0, 0.5, ystart+bheight, ystart); vertex(xscreen, yscreen); endShape(CLOSE); }