import papaya.*; PFont myfont; PFont myboldfont; JakeSlider samplemeanslider, confidenceslider, smallsampleslider, largesampleslider; JakeButton leftarrow, rightarrow; PImage PIeqn; 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 = { "male height", "female height", "male weight", "female weight", "cat length", "male salary", "cat speed", "dog weight" }; float[] lower = { 5.5, 5, 150, 100, 15, 40000, 20, 40 }; float[] upper = { 6.5, 6, 200, 150, 20, 60000, 30, 70 }; String[] units = { "ft", "ft", "pounds", "pounds", "inches", "dollars", "mi/hr", "pounds" }; String[] nouns = { "males", "females", "males", "females", "cats", "males", "cats", "dogs" }; int[] samplesize = {10, 20, 51, 61, 91}; int[] confidence = { 80, 90, 95, 99}; float[][] ttable = { { 1.3830, 1.8331, 2.2622, 3.2498 } , { 1.3277, 1.7291, 2.0930, 2.8609 } , { 1.2987, 1.6759, 2.0086, 2.6778 } , { 1.2958, 1.6707, 2.0003, 2.6603 } , { 1.2910, 1.6620, 1.9867, 2.6316 } }; float[] zvalues; float leftCI = 0, rightCI = 0; int samplesizeslidervalue; int confidenceslidervalue; float samplemeanslidervalue; int[] digits = {1, 1, 3, 3, 2, 5, 2, 2}; int page = 1; int numtemplates = 2; String[] problemText = new String[numtemplates]; int namerand; int objrand; int sizerand; int confidencerand; float numrand; String numrandstr; float sdrand; String sdrandstr; String pagetext; int temprand; void setup() { size(700, 500); smooth(); myfont = loadFont("ArialMT-24.vlw"); myboldfont = loadFont("Arial-BoldMT-24.vlw"); PIeqn = loadImage("predicteqn.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(); float tempfloat = Float.parseFloat(numrandstr); float tempsd = Float.parseFloat(sdrandstr); samplemeanslider = new JakeSlider(390,350,100,30,tempfloat,tempfloat-tempsd/5, tempfloat+tempsd/5,false,color(71, 145, 255),color(2,52,77),myfont,10,digits[objrand],2); samplemeanslider.setVisible(false); confidenceslider = new JakeSlider(220,350,100,30,confidence[confidencerand],80,99,true,color(71, 145, 255),color(2,52,77),myfont,10,2,0); confidenceslider.setVisible(false); smallsampleslider = new JakeSlider(50,350,100,30,samplesize[sizerand],10,25,true,color(71, 145, 255),color(2,52,77),myfont,10,2,0); smallsampleslider.setVisible(false); largesampleslider = new JakeSlider(50,350,100,30,samplesize[sizerand],30,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 = random(lower[objrand], upper[objrand]); numrandstr = nf(numrand, digits[objrand], 2); sdrand = random(lower[objrand]/10, upper[objrand]/10); sdrandstr = nf(sdrand, digits[objrand]-1, 2); temprand = int(random(numtemplates)); samplesizeslidervalue = samplesize[sizerand]; confidenceslidervalue = confidence[confidencerand]; float tempfloat = Float.parseFloat(numrandstr); samplemeanslidervalue = tempfloat; } void draw() { background(255); fill(0); problemText[0] = names[namerand] + " is interested in studying " + obj[objrand] + " in the US. " + gender[namerand] + " sampled " + samplesize[sizerand] + " " + nouns[objrand] + " at random and found a sample mean of " + numrandstr + " " + units[objrand] + " and sample standard deviation of " + sdrandstr + " " + units[objrand] + "." + " Find the " + confidence[confidencerand] + "% prediction interval for a new observation of " + obj[objrand] + "."; problemText[1] = names[namerand] + " is performing a study on " + obj[objrand] + " in France. " + gender[namerand] + " collected a sample of " + samplesize[sizerand] + " " + nouns[objrand] + " at random and found the mean of the sample to be " + numrandstr + " " + units[objrand] + " and found the standard deviation of the sample to be " + sdrandstr + " " + units[objrand] + "." + " What is the " + confidence[confidencerand] + "% prediction interval for a new observation of " + 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, "bold"); pagetext = "The type of problem we have here is to find the prediction " + "interval for a new observation. We use the formula: "; image(PIeqn, 350-72, 295); drawStrings(pagetext, 80, 244, 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, "bold"); pagetext = "We want to identify each variable in our formula:"; drawStrings(pagetext, 80, 244, 25); image(PIeqn, 350-72, 256); pagetext = "Ybar represents our sample mean. In this problem, the sample mean is " + numrandstr + "." + " S is our sample standard deviation, which is " + sdrandstr + "." + " Our sample size is represented by n, which is " + samplesize[sizerand] + "." + " Lastly, we have the confidence level, given by the variable L, as " + confidence[confidencerand] + "%."; drawStrings(pagetext, 80, 319, 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, "bold"); pagetext = "In our formula: "; drawStrings(pagetext, 80, 244, 25); image(PIeqn, 350-72, 248); pagetext = "The t with the subscript n - 1, (1 + L)/2 represents (1 + L)/2*100 percentile of the n - 1 t curve. In this case, " + "we have L = ." + confidence[confidencerand] + " and n = " + samplesize[sizerand] + " so we are looking for the " + (confidence[confidencerand]+100)/2.0 + " percentile of the " + (samplesize[sizerand] - 1) + "th t curve. "; drawStrings(pagetext, 80, 319, 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, "bold"); float tempfloat = ((float)confidence[confidencerand]/100+1)/2.0; pagetext = "Use the table A.4 to the find the percentile of the t curve. Find " + "the " + (samplesize[sizerand] - 1) +"th row (sample size - 1) and the " + tempfloat + " column. This gives us the " + (confidence[confidencerand]+100)/2.0 + " percentile as " + nf(ttable[sizerand][confidencerand], 1, 4) + " (ie " + (confidence[confidencerand]+100)/2.0 + "% of the area is to the left of this point)."; drawStrings(pagetext, 80, 244, 25); pagetext = nf(tempfloat, 1, 4); drawStrings(pagetext, 230, 294+45, 25); image(smalldown, 245, 294+50); if (samplesize[sizerand] == 10) { pagetext = "9"; drawStrings(pagetext, 178, 396, 25); } if (samplesize[sizerand] > 10) { pagetext = nf(samplesize[sizerand]-1, 2); drawStrings(pagetext, 167, 396, 25); } image(smallright, 195, 372); pagetext = nf(ttable[sizerand][confidencerand], 1, 4); drawStrings(pagetext, 230, 395, 25); drawBellcurve(ttable[sizerand][confidencerand], 400, 308, 200, 88); //noFill(); //rect(130,294+31,400,70); 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, "bold"); pagetext = "Now, plug in your values for the variables into: "; drawStrings(pagetext, 80, 244, 25); image(PIeqn, 350-72, 258); pagetext = "Plugging in, we get " + numrandstr + " plus or minus " + sdrandstr + " times the the square root of (1.0 + 1.0 divided by " + samplesize[sizerand] + ") times " + nf(ttable[sizerand][confidencerand], 1, 4)+"."; leftCI = Float.parseFloat(numrandstr) - Float.parseFloat(sdrandstr)*sqrt(1.0+1.0/(float)samplesize[sizerand])*ttable[sizerand][confidencerand]; rightCI = Float.parseFloat(numrandstr) + Float.parseFloat(sdrandstr)*sqrt(1.0+1.0/(float)samplesize[sizerand])*ttable[sizerand][confidencerand]; pagetext += " This gives us the interval (" + nf(leftCI, digits[objrand], 2) + ", " + nf(rightCI, digits[objrand], 2)+ "). " + names[namerand] + " is " + confidence[confidencerand] + "% confident that a new observation of " + obj[objrand] + " will be between " + nf(leftCI, digits[objrand], 2) + " " + units[objrand] + " and " + nf(rightCI, digits[objrand], 2) + " " + units[objrand] + "."; drawStrings(pagetext, 80, 319, 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, "bold"); pagetext = "In creating our prediction interval, we have assumed the C + E model is valid, that the " + "population variance is unknown, and that epsilon is from a normal density. "; drawStrings(pagetext, 80, 244, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); break; case 7: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Exploring the prediction interval: "; drawStrings(pagetext, 30, 194, 25, "bold"); pagetext = "What is the effect of the sample size on the prediction interval? What are the effects of the confidence level and sample mean? "; drawStrings(pagetext, 50, 244, 25); pagetext = "Sample size"; drawStrings(pagetext, 50, 294+25, 25); pagetext = "Confidence Level"; drawStrings(pagetext, 205, 294+25, 25); pagetext = "Sample mean"; drawStrings(pagetext, 385, 294+25, 25); pagetext = "Your interval:"; drawStrings(pagetext, 540, 294+25, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); confidenceslider.setVisible(true); samplemeanslider.setVisible(true); float leftedge, rightedge; tempfloat = Float.parseFloat(numrandstr); float tempsd = Float.parseFloat(sdrandstr); if (samplesize[sizerand] < 30) { smallsampleslider.setVisible(true); float percentile = (float)Probability.tinv((1.0 + (float)confidenceslidervalue/100.0)/2.0, samplesizeslidervalue-1); leftCI = samplemeanslidervalue - tempsd*sqrt(1.0+1.0/(float)samplesizeslidervalue)*percentile; rightCI = samplemeanslidervalue + tempsd*sqrt(1.0+1.0/(float)samplesizeslidervalue)*percentile; leftedge = map(leftCI, tempfloat-tempsd/5-tempsd*sqrt(1.1)*3.25, tempfloat+tempsd/5+tempsd*sqrt(1.1)*3.25, 540, 640); rightedge = map(rightCI, tempfloat-tempsd/5-tempsd*sqrt(1.1)*3.25, tempfloat+tempsd/5+tempsd*sqrt(1.1)*3.25, 540, 640); } else { largesampleslider.setVisible(true); float percentile = (float)Probability.tinv((1.0 + (float)confidenceslidervalue/100.0)/2.0, samplesizeslidervalue-1); leftCI = samplemeanslidervalue - tempsd*sqrt(1.0+1.0/(float)samplesizeslidervalue)*percentile; rightCI = samplemeanslidervalue + tempsd*sqrt(1.0+1.0/(float)samplesizeslidervalue)*percentile; leftedge = map(leftCI, tempfloat-tempsd/5-tempsd*sqrt(1.0333)*2.76, tempfloat+tempsd/5+tempsd*sqrt(1.0333)*2.76, 540, 640); rightedge = map(rightCI, tempfloat-tempsd/5-tempsd*sqrt(1.0333)*2.76, tempfloat+tempsd/5+tempsd*sqrt(1.0333)*2.76, 540, 640); } pagetext = "("+nf(leftCI, digits[objrand], 2)+", "+nf(rightCI, digits[objrand], 2)+")"; drawStrings(pagetext, 540, 294+100-5, 25); fill(71, 145, 255); rect(leftedge, 294+57, rightedge-leftedge, 4); break; case 8: drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Exploring the prediction interval continued: "; drawStrings(pagetext, 30, 194, 25, "bold"); pagetext = "The sample mean is the midpoint/center of the prediction interval. Varying it will move the prediction interval around. The sample size " + "alters the width of the prediction interval. As you increase your sample size, the width gets smaller. Lastly, if you increase your level of confidence, " + "the width of the prediction interval will increase. "; drawStrings(pagetext, 30, 194+50, 25); pagetext = "To try another problem, click the right arrow."; drawStrings(pagetext, 30, 194+200, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); break; } samplemeanslider.updateSlider(); samplemeanslider.drawSlider(); samplemeanslidervalue = samplemeanslider.getValue(); samplemeanslidervalue = round(samplemeanslidervalue*100.0)/100.0; confidenceslider.updateSlider(); confidenceslider.drawSlider(); confidenceslidervalue = (int)confidenceslider.getValue(); smallsampleslider.updateSlider(); smallsampleslider.drawSlider(); largesampleslider.updateSlider(); largesampleslider.drawSlider(); if (samplesize[sizerand] < 30) { samplesizeslidervalue = (int)smallsampleslider.getValue(); } else { samplesizeslidervalue = (int)largesampleslider.getValue(); } leftarrow.updateButton(); leftarrow.drawButton(); if (leftarrow.clicked) { if (samplesize[sizerand] < 30) smallsampleslider.setVisible(false); else largesampleslider.setVisible(false); confidenceslider.setVisible(false); samplemeanslider.setVisible(false); page--; } rightarrow.updateButton(); rightarrow.drawButton(); if(rightarrow.clicked) { if (samplesize[sizerand] < 30) smallsampleslider.setVisible(false); else largesampleslider.setVisible(false); confidenceslider.setVisible(false); samplemeanslider.setVisible(false); if (page == 8) { page = 0; generateRandom(); float tempfloat = Float.parseFloat(numrandstr); float tempsd = Float.parseFloat(sdrandstr); confidenceslider.setValue(confidenceslidervalue); samplemeanslider.setRange(tempfloat-tempsd/5, tempfloat+tempsd/5); samplemeanslider.setValue(tempfloat); samplemeanslider.setDigits(digits[objrand],2); smallsampleslider.setValue(samplesize[sizerand]); largesampleslider.setValue(samplesize[sizerand]); } 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); }