# DESCRIPTION # Interpreting a histogram - three examples # Output controlled by contents of @case # WeBWorK problem written by Hedley Pinsent, # # ENDDESCRIPTION ## DBsubject('WeBWorK') ## DBchapter('Statistics') ## DBsection('Grouped Data') ## KEYWORDS('') ## TitleText1('') ## EditionText1('') ## AuthorText1('') ## Section1('') ## Problem1('') ## Author('Hedley Pinsent') ## Institution('College of the North Atlantic') DOCUMENT(); loadMacros( "PGstandard.pl", # Standard macros for PG language "MathObjects.pl", "PGchoicemacros.pl", "PGgraphmacros.pl" ); $showPartialCorrectAnswers = 1; $refreshCachedImages = 1; $trial =0; #@caselist = (0,1,2); @caselist = list_random(0,1,2); foreach $case (@caselist){ $trial += 1; if ($case ==0){ #Starting Parameters VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV $ClassInterval = list_random(2,5,10); $MidLowest = random(10,30,5); $NumberClasses = list_random(4,5,6); $n = random(int(2**($NumberClasses-1)),int(2**$NumberClasses-1), 1); @probabilityPattern = (); push @probabilityPattern , 0..$NumberClasses-1; push @probabilityPattern , 1..$NumberClasses-2; #for a central preference } #end of case 0 # case = 1 (student marks) if ($case ==1){ #Starting Parameters VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV $ClassInterval = list_random(5,10); #$MidLowest = random(10,30,5); $NumberClasses = list_random(4,5,6); $MidLowest = list_random(80,90,100) - ($NumberClasses -0.5)*$ClassInterval; $n = random(int(2**($NumberClasses-1)),int(2**$NumberClasses-1), 1); @probabilityPattern = (); push @probabilityPattern , 0..$NumberClasses-1; push @probabilityPattern , 1..$NumberClasses-2; #for a central preference } #end of case 1 # case = 2 (active ingredients - decomals) if ($case ==2){ #Starting Parameters VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV $ClassInterval = list_random(0.001,0.002); $MidLowest = list_random(0.05,0.06,0.07,0.08,0.09); $MidLowest += $ClassInterval /2; #band aid for cramped numbers on graph $NumberClasses = list_random(4,5,6); $n = random(int(2**($NumberClasses-1)),int(2**$NumberClasses-1), 1); @probabilityPattern = (); push @probabilityPattern , 0..$NumberClasses-1; push @probabilityPattern , 1..$NumberClasses-2; #for a central preference } #end of case 2 #VVassigning values to classes VVVVVVVVVVVVVVVVVVVVVVVVVVVVV $MAXfrequency = 0; foreach $i (0.. $NumberClasses -1){$f[$i]=1} # frequency at least 1 for each foreach $i (0.. $n -1-$NumberClasses) #one for each class already assigned { $tmpIndex = list_random(@probabilityPattern); $f[$tmpIndex] += 1 ; if ($f[$tmpIndex] > $MAXfrequency){$MAXfrequency = $f[$tmpIndex]} } #^^assigning values to classes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #VV finding the class(es) with the maximum frequency VVVVVVVVVVVVVVVVVVV @MaxClassMidpoint = (); #clearing history - storing maximum clasc @Rights = (); @Lefts = (); foreach $i (0.. $NumberClasses -1) { $Mids[$i]= $MidLowest + $ClassInterval * $i ; $Lefts[$i]= $MidLowest + $ClassInterval * $i - $ClassInterval/2; $Rights[$i]= $MidLowest + $ClassInterval * $i + $ClassInterval/2; if ($f[$i] == $MAXfrequency){push @MaxClassMidpoint, $MidLowest + $ClassInterval * $i} # max class midpoint } $MAXfrequencyLISTOBJECT = List(@MaxClassMidpoint); #^^ finding the class(es) with the maximum frequency ^^^^^^^^^^^^^^^^^^^ #Starting Parameters ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # Scaling horizantal positions VVVVVVVVVVVVV $xAxisStart = max( $Lefts[0] - $ClassInterval ,0); #empty cell if not negative $xWindowStart = $xAxisStart - $ClassInterval ; $xAxisFinish = $Rights[$NumberClasses -1] + $ClassInterval ; #ditto $xWindowFinish = $xAxisFinish ; $xnumberGrids = ($xWindowFinish - $xWindowStart)/$ClassInterval ; # Scaling horizantal positions ^^^^^^^^^^^^^^^^ $yAxisStart = 0 ; $yWindowStart = -1 ; $yAxisFinish = $MAXfrequency + 1; $yWindowFinish = $yAxisFinish ; $ynumberGrids = $yWindowFinish - $yWindowStart ; $graph = init_graph($xWindowStart,-1,$xWindowFinish,$yWindowFinish, axes=>[$xAxisStart,0], grid=>[$xnumberGrids,$ynumberGrids], size=>[400,400]); # or pixels=>[400,400] $graph->lb('reset'); foreach my $i ($xAxisStart,$Lefts[0],@Rights){$graph->lb( new Label($i,0,$i, 'black','center','top'))}; foreach my $i (0 .. $MAXfrequency){$graph->lb( new Label($xAxisStart,$i,$i, 'black','right','bottom'))}; ##junk ##graphing the bars vvvvvvvvvvvvvvvvvvvvvvv $total = 0; #$graph->new_color("randomcolor", random(160,240),random(160,240),random(160,240)); foreach $i (0.. $NumberClasses -1) { $graph->moveTo($Lefts[$i],0); $graph->lineTo($Lefts[$i],$f[$i],'black',4); $graph->lineTo($Rights[$i],$f[$i],'black',4); $graph->lineTo($Rights[$i],0,'black',4); $graph->lineTo($Lefts[$i],0); $graph->new_color("randomcolor"."$i", random(160,240),random(160,240),random(160,240)); $graph->fillRegion([$Mids[$i],0.5,"randomcolor"."$i"]); } ## graphing the bars ^^^^^^^^^^^^^^^^^^^^^ # questions vvvvvvvvvvvvvvvvvvvv # Greater than question VVVVVVVVVVVVVVVVv $RandomPoint = random (1,$NumberClasses -3); #ensuring two classes counted, one left out $q4 = $Lefts[$RandomPoint]; $numberGreater = 0; foreach $i ($RandomPoint .. $NumberClasses -1){$numberGreater +=$f[$i]} # Greater than question ^^^^^^^^^^^^^^^^^^ # Less than question VVVVVVVVVVVVVVVVv $RandomPoint = random (1,$NumberClasses -2); #ensuring two classes counted,one left out $q5 = $Rights[$RandomPoint]; $numberLessThan = 0; foreach $i (0 ..$RandomPoint ){$numberLessThan +=$f[$i]} # Less than question ^^^^^^^^^^^^^^^ #questions ^^^^^^^^^^^^^^^^^^^^^ # make sure we're in the context we want Context("Numeric"); #TEXT(beginproblem()); Context()->texStrings; if ($case ==0){ #Labels on graphs VVVVVVVVVVVVVVVVVVV $graph->lb( new Label(($xAxisStart+$xAxisFinish)/2,-0.5,'hourly salary (dollars)','black','center','top')); $graph->lb( new Label($xAxisStart,$MAXfrequency +0.5,'number of employees','black','left','middle')); #Labels on graphs ^^^^^^^^^^^^^^^^^^^^^ BEGIN_TEXT $HR A sample of the hourly wages of a number of employees at Coleman's Supermarkets is shown in the histogram below. $BCENTER $BR \{ image( insertGraph($graph), width=>400,height=>400,tex_size=>800 ) \} $ECENTER $PAR How many employees were surveyed? \{ labeled_ans_rule("$trial"."n",15) \} $PAR What is the Class Interval? \{ labeled_ans_rule("$trial"."CI",15) \} $PAR The midpoint of the class with the highest frequency is \{ labeled_ans_rule("$trial"."MP",15) \} $BR - separate by commas if there is more than one. $PAR How many employees earn $DOLLAR \{sprintf("%.2f", $q4)\} or more \{ labeled_ans_rule("$trial"."MT",15) \} $PAR $PAR How many employees earn less than $DOLLAR \{sprintf("%.2f",$q5)\}\{labeled_ans_rule("$trial"."LT",15) \} $PAR END_TEXT } # end case if ($case ==1){ #Labels on graphs VVVVVVVVVVVVVVVVVVV $graph->lb( new Label(($xAxisStart+$xAxisFinish)/2,-0.5,'Quiz Grade (%)','black','center','top')); $graph->lb( new Label($xAxisStart,$MAXfrequency +0.5,'number of students','black','left','middle')); #Labels on graphs ^^^^^^^^^^^^^^^^^^^^^ BEGIN_TEXT $HR The Professor gives his class a quiz. $BR The results are organized into the histogram shown below. $PAR $BCENTER $BR \{ image( insertGraph($graph), width=>400,height=>400,tex_size=>800 ) \} $ECENTER $PAR How many students are in the class? \{ labeled_ans_rule("$trial"."n",15) \} $PAR What is the Class Interval? \{ labeled_ans_rule("$trial"."CI",15) \} $PAR The midpoint of the class with the highest frequency is \{ labeled_ans_rule("$trial"."MP",15) \} $BR - separate by commas if there is more than one. $PAR How many students scored $q4 $PERCENT or more \{ labeled_ans_rule("$trial"."MT",15) \} $PAR $PAR How many students scored less than $q5 $PERCENT\{labeled_ans_rule("$trial"."LT",15) \} $PAR END_TEXT } # end case if ($case ==2){ #Labels on graphs VVVVVVVVVVVVVVVVVVV $graph->lb( new Label(($xAxisStart+$xAxisFinish)/2,-0.5,'Active Intredient (grams)','black','center','top')); $graph->lb( new Label($xAxisStart,$MAXfrequency +0.5,'number of pills','black','left','middle')); #Labels on graphs ^^^^^^^^^^^^^^^^^^^^^ BEGIN_TEXT $HR A study is done to examine the amount of active ingredient in a certain pill $BR The results are organized into the histogram shown below. $PAR $BCENTER $BR \{ image( insertGraph($graph), width=>400,height=>400,tex_size=>800 ) \} $ECENTER $PAR How may pills were analyzed? \{ labeled_ans_rule("$trial"."n",15) \} $PAR What is the Class Interval? \{ labeled_ans_rule("$trial"."CI",15) \} $PAR The midpoint of the class with the highest frequency is \{ labeled_ans_rule("$trial"."MP",15) \} $BR - separate by commas if there is more than one. $PAR How many pills had $q4 grams or more \{ labeled_ans_rule("$trial"."MT",15) \} $PAR $PAR How many pills had less than $q5 grams\{labeled_ans_rule("$trial"."LT",15) \} $PAR END_TEXT } # end case #LABELED_ANS("numerical_answer" => num_cmp(12),"string_answer" => str_cmp("World") ); LABELED_ANS("$trial"."n"=> num_cmp($n) ); LABELED_ANS("$trial"."CI"=> num_cmp($ClassInterval) ); LABELED_ANS("$trial"."MP"=> $MAXfrequencyLISTOBJECT->cmp() ); LABELED_ANS("$trial"."MT"=> num_cmp($numberGreater) ); LABELED_ANS("$trial"."LT"=> num_cmp($numberLessThan) ); } ENDDOCUMENT();