import guicomponents.*; import papaya.*; PFont myfont; PFont myboldfont; JakeSlider samplemeanslider, confidenceslider, smallsampleslider, largesampleslider; JakeButton leftarrow, rightarrow, ans; GCombo cb1; GTextField tf3, tf5; PImage zeqn; PImage teqn; PImage smallleft; PImage smallup; PImage smalldown; PImage smallright; String[] names = { "Neo", "Jake", "Mr. Anderson", "Lisa Simpson", "Bart Simpson", "Homer Simpson", "Ted Mosby", "Robin Scherbatsky", "Roger", "Rachel", "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, 50, 60, 90}; int[] confidence = {80, 90, 95, 99}; float[] Zvalues = {0.8997, .9505, .9750, .9951}; float[] Zrow = {1.2, 1.6, 1.9, 2.5}; float[] Zcolumn = { .08, .05, .06, .08}; float[][] ttable = { { 1.3830, 1.8331, 2.2622, 3.2498 } , { 1.3277, 1.7291, 2.0930, 2.8609 } }; 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; String feedback = ""; String[] str1 = { "z equation", "t equation" }; int score; boolean[] answered = { false, true, false, false, false, true, true, true }; String[] answers = { "", "", "", "", "", "", "", "" }; void setup() { size(700, 500); smooth(); myfont = loadFont("ArialMT-24.vlw"); myboldfont = loadFont("Arial-BoldMT-24.vlw"); zeqn = loadImage("zeqn.gif"); teqn = loadImage("teqn.gif"); smallleft = loadImage("leftbluesmall.jpg"); smallup = loadImage("bluesmallup.jpg"); smalldown = loadImage("smalldown.jpg"); smallright = loadImage("rightbluesmall.jpg"); cb1 = new GCombo(this, str1, 2, 100, 330, 100); tf3 = new GTextField(this, "", 330, 350, 80, 20, false); tf5 = new GTextField(this, "", 480, 350, 80, 20, false); PImage[] answerset ={loadImage("answerdark.jpg"),loadImage("answerlight.jpg")}; ans = new JakeButton(200,430,answerset[1],answerset[0],answerset[0]); 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 the average " + 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] + "% confidence interval for the population's mean " + obj[objrand] + "."; problemText[1] = names[namerand] + " is performing a study on the average " + obj[objrand] + " in Canada. " + gender[namerand] + " collected a sample of " + samplesize[sizerand] + " " + nouns[objrand] + " at random and found a sample mean of " + numrandstr + " " + units[objrand] + " and sample standard deviation of " + sdrandstr + " " + units[objrand] + "." + " What is the " + confidence[confidencerand] + "% confidence interval for the population average " + obj[objrand] + "?"; if (page == 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 confidence " + "interval for the population mean, mu. The formula to use depends upon the sample size in the problem. " + "In this case, we have a "; if (samplesize[sizerand] < 30) { pagetext += "small sample size, "; } else { pagetext += "large sample size, "; } pagetext += "so which equation should be used? Choose your response then click Answer: "; drawStrings(pagetext, 30, 230, 25); if (cb1.selectedIndex()==0) { image(zeqn, 300, 325); } else { image(teqn, 300, 325); } fill(0, 0, 255); drawStrings(feedback, 80, 400, 25); leftarrow.setVisible(false); rightarrow.setVisible(true); cb1.setVisible(true); ans.setVisible(true); tf3.setVisible(false); tf5.setVisible(false); } else if (page == 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); if (samplesize[sizerand] < 30) image(teqn, 350-72, 258); else image(zeqn, 350-72, 258); pagetext = "Xbar 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); cb1.setVisible(false); ans.setVisible(false); tf3.setVisible(false); } else if (page == 3) { drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step two continued: "; drawStrings(pagetext, 30, 194, 25, "bold"); pagetext = "In our formula: "; drawStrings(pagetext, 80, 220, 25); if (samplesize[sizerand] < 30) image(teqn, 310, 200); else image(zeqn, 315, 200); if (samplesize[sizerand] < 30) 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 what percentile of the " + (samplesize[sizerand] - 1) + "th t curve "; else pagetext = "The z with the subscript (1 + L)/2 represents (1 + L)/2 * 100 percentile of the Z curve. In this case, " + "we have L = ." + confidence[confidencerand] + " so what percentile of the Z curve "; pagetext += "are we looking for? Type in your response then click Answer:"; drawStrings(pagetext, 80, 265, 25); fill(0, 0, 255); drawStrings(feedback, 80, 400, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); ans.setVisible(true); tf3.setVisible(true); } else if (page == 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; if (samplesize[sizerand] < 30) { 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 (ie " + (confidence[confidencerand]+100)/2.0 + "% of the area is to the left of this point). Type in the value (with at least 3 decimal places) then click Answer:"; drawStrings(pagetext, 30, 220, 25); pagetext = nf(tempfloat, 1, 4); drawStrings(pagetext, 330, 305, 25); image(smalldown, 345, 315); if (samplesize[sizerand] == 10) { pagetext = "9"; drawStrings(pagetext, 270, 365, 25); } else if (samplesize[sizerand] == 20) { pagetext = "19"; drawStrings(pagetext, 270, 365, 25); } image(smallright, 295, 345); // pagetext = nf(ttable[sizerand][confidencerand], 1, 4); // drawStrings(pagetext, 230, 395, 25); } else { 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 (ie " + (confidence[confidencerand]+100)/2.0 + "% of the area is to the left of this point). Type in the value, row + col, then click Answer:"; drawStrings(pagetext, 30, 220, 25); //pagetext = nf(Zcolumn[confidencerand], 1, 4); drawStrings("???", 240, 310, 25); image(smallup, 245, 315); //pagetext = nf(Zrow[confidencerand], 1, 4); drawStrings("???", 150, 370, 25); image(smallleft, 190, 350); pagetext = nf(Zvalues[confidencerand], 1, 4); drawStrings(pagetext, 230, 370, 25); } drawBellcurve(Zrow[confidencerand]+Zcolumn[confidencerand], 500, 280, 200, 88); fill(0, 0, 255); drawStrings(feedback, 80, 400, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); tf5.setVisible(false); } else if (page == 5) { float tz; 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, 30, 220, 25); if (samplesize[sizerand] < 30) { image(teqn, 300, 235); pagetext = "Plugging in, we get " + numrandstr + " plus or minus " + sdrandstr + " divided by the square root of " + samplesize[sizerand] + " times " + nf(ttable[sizerand][confidencerand], 1, 4)+"."; leftCI = Float.parseFloat(numrandstr) - Float.parseFloat(sdrandstr)/sqrt(samplesize[sizerand])*(ttable[sizerand][confidencerand]); rightCI = Float.parseFloat(numrandstr) + Float.parseFloat(sdrandstr)/sqrt(samplesize[sizerand])*(ttable[sizerand][confidencerand]); tz = ttable[sizerand][confidencerand]; pagetext += " Calculate this interval, type in the values (with at least 3 decimal places) then click Answer: "; } else { image(zeqn, 300, 235); pagetext = "Plugging in, we get " + numrandstr + " plus or minus " + sdrandstr + " divided by the square root of " + samplesize[sizerand] + " times " + nf(Zrow[confidencerand]+Zcolumn[confidencerand], 1, 2)+"."; leftCI = Float.parseFloat(numrandstr) - Float.parseFloat(sdrandstr)/sqrt(samplesize[sizerand])*(Zrow[confidencerand]+Zcolumn[confidencerand]); rightCI = Float.parseFloat(numrandstr) + Float.parseFloat(sdrandstr)/sqrt(samplesize[sizerand])*(Zrow[confidencerand]+Zcolumn[confidencerand]); tz = Zrow[confidencerand]+Zcolumn[confidencerand]; pagetext += " Calculate this interval, type in the values (with at least 3 decimal places) then click Answer: "; } // println(Float.parseFloat(numrandstr)); // println(Float.parseFloat(sdrandstr)); // println(samplesize[sizerand]); // println(tz); // println(sqrt(samplesize[sizerand])); // println(leftCI); // println(rightCI); drawStrings(pagetext, 30, 300, 25); text("From", 270, 370); text("To", 440, 370); fill(0, 0, 255); drawStrings(feedback, 110, 400, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); tf5.setVisible(true); tf3.setVisible(true); ans.setVisible(true); } else if (page == 6) { drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Step five: State your assumptions"; drawStrings(pagetext, 30, 194, 25, "bold"); if (samplesize[sizerand] < 30) pagetext = "In creating our confidence interval, we have assumed the C + E model is valid, that the sample size of " + samplesize[sizerand] + " is small, that the " + "population variance is unknown, and that epsilon is from a normal density. "; else pagetext = "In creating our confidence interval, we have assumed the C + E model is valid, that the sample size of " + samplesize[sizerand] + " is large, and " + "the population variance is unknown."; drawStrings(pagetext, 80, 244, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); tf5.setVisible(false); tf3.setVisible(false); ans.setVisible(false); } else if (page == 7) { drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Exploring the confidence interval: "; drawStrings(pagetext, 30, 194, 25, "bold"); pagetext = "What is the effect of the sample size on the confidence 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; float 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(samplesizeslidervalue)*percentile; rightCI = samplemeanslidervalue + tempsd/sqrt(samplesizeslidervalue)*percentile; leftedge = map(leftCI, tempfloat-tempsd/5-tempsd/sqrt(10)*3.25, tempfloat+tempsd/5+tempsd/sqrt(10)*3.25, 540, 640); rightedge = map(rightCI, tempfloat-tempsd/5-tempsd/sqrt(10)*3.25, tempfloat+tempsd/5+tempsd/sqrt(10)*3.25, 540, 640); } else { largesampleslider.setVisible(true); float percentile = (float)Probability.norminv((1.0 + (float)confidenceslidervalue/100.0)/2.0); leftCI = samplemeanslidervalue - tempsd/sqrt(samplesizeslidervalue)*percentile; rightCI = samplemeanslidervalue + tempsd/sqrt(samplesizeslidervalue)*percentile; leftedge = map(leftCI, tempfloat-tempsd/5-tempsd/sqrt(30)*2.58, tempfloat+tempsd/5+tempsd/sqrt(30)*2.58, 540, 640); rightedge = map(rightCI, tempfloat-tempsd/5-tempsd/sqrt(30)*2.58, tempfloat+tempsd/5+tempsd/sqrt(30)*2.58, 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); } else if (page == 8) { drawStrings(problemText[temprand], 30, 44, 25); pagetext = "Exploring the confidence interval continued: "; drawStrings(pagetext, 30, 194, 25, "bold"); pagetext = "The sample mean is the midpoint/center of the confidence interval. Varying it will move the confidence interval around. 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); pagetext = "To try another problem, click the right arrow."; drawStrings(pagetext, 30, 194+200, 25); leftarrow.setVisible(true); rightarrow.setVisible(true); } fill(255, 0, 0); text("Score: " + score + " / 40", 450, 460); if (answered[4]) { fill(255, 0, 0); text("Final", 405, 460); } 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) { feedback = ""; tf3.setText(""); tf5.setText(""); if (samplesize[sizerand] < 30) smallsampleslider.setVisible(false); else largesampleslider.setVisible(false); confidenceslider.setVisible(false); samplemeanslider.setVisible(false); page--; if (answered[page-1]) feedback = "You answered this question with " + answers[page-1]; } rightarrow.updateButton(); rightarrow.drawButton(); if (rightarrow.clicked) { feedback = ""; tf3.setText(""); tf5.setText(""); if (samplesize[sizerand] < 30) smallsampleslider.setVisible(false); else largesampleslider.setVisible(false); confidenceslider.setVisible(false); samplemeanslider.setVisible(false); if (answered[page-1]) { page++; } else { feedback = "You have to answer this question before continuing."; } if (page>8) { score = 0; answered = new boolean[] { false, true, false, false, false, true, true, true }; page = 1; 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 (answered[page-1]) feedback = "You answered this question with " + answers[page-1]; } ans.updateButton(); ans.drawButton(); if(ans.clicked) { if (!answered[page-1]) { switch(page) { case 1: if (samplesize[sizerand] < 30) { if (cb1.selectedIndex()==1) { answers[0] = "t equation, and the correct answer is t equation"; feedback = "Correct"; score += 10; } else { answers[0] = "z equation, and the correct answer is t equation"; feedback = "Incorrect. The correct answer is t equation."; } } else { if (cb1.selectedIndex()==0) { answers[0] = "z equation, and the correct answer is z equation"; feedback = "Correct"; score += 10; } else { answers[0] = "t equation, and the correct answer is z equation"; feedback = "Incorrect. The correct answer is z equation."; } } break; case 3: answers[2]= tf3.getText() + ". The correct answer is " + nf((confidence[confidencerand]+100.0)/2.0, 1, 1); if (abs(Float.parseFloat(tf3.getText())-(confidence[confidencerand]+100.0)/2.0)<0.1) { feedback = "Correct."; score += 10; } else { feedback = "Incorrect. The correct answer is " + nf((confidence[confidencerand]+100.0)/2.0, 1, 1); } break; case 4: if (samplesize[sizerand] < 30) { answers[3] = tf3.getText() + ". The correct answer is " + nf(ttable[sizerand][confidencerand], 1, 4); if (abs(Float.parseFloat(tf3.getText())-ttable[sizerand][confidencerand])<0.001) { feedback = "Correct."; score += 10; } else { feedback = "Incorrect. The correct answer is " + nf(ttable[sizerand][confidencerand], 1, 4); } } else { answers[3] = tf3.getText() + ". The correct answer is " + nf(Zrow[confidencerand]+Zcolumn[confidencerand], 1, 2); if (abs(Float.parseFloat(tf3.getText())-Zrow[confidencerand]-Zcolumn[confidencerand])<0.01) { feedback = "Correct."; score += 10; } else { feedback = "Incorrect. The correct answer is " + nf(Zrow[confidencerand]+Zcolumn[confidencerand], 1, 2); } } break; case 5: answers[4] = tf3.getText() + " and " + tf5.getText() + ". The correct answers are " + nf(leftCI, 1, 4) + " and " + nf(rightCI, 1, 4); float tol=.001; if (digits[objrand] ==5) tol = .1; if ((abs(Float.parseFloat(tf3.getText())-leftCI) 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); }