Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions tutorial/sample-problems/Statistics/BarGraphStatPlot.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
## DESCRIPTION
## Find the mean and standard deviation of a list of numbers.
## ENDDESCRIPTION
## DBsubject(WeBWorK)
## DBchapter(WeBWorK tutorial)
## DBsection(WeBWorK tutorial)
## Institution(Fitchburg State University)
## Author(Peter Staab)
## Date(06/25/2026)
## KEYWORDS('statistics', 'bar graph')

#:% name = Bar Graph (Stat Plot)
#:% subject = [statistics]
#:% type = sample
#:% categories = [graph, statistics]

#:% section = preamble
#: This is a updated version of PROBLINK('BarGraph.pg') to use the
#: PODLINK('StatisticalPlots.pl') macro, which provides some convenience methods
#: for creating Statistical plots. This also uses the
#: PODLINK('contextPercent.pl') to provide the Percent context.
DOCUMENT();

loadMacros(
'PGstandard.pl', 'PGML.pl',
'StatisticalPlots.pl', 'contextPercent.pl',
'PGcourse.pl'
);

#:% section = setup
#: The 'Percent' context allow students to enter percents.
#:
#: To create a graph, start with a `StatPlot`. The `min` and `max` options in
#: the two directions give the bounding box of the plot. The `tick_delta` for
#: each direction gives the distance between tick marks and the `minor` option is
#: the number of minor ticks between each tick.
#:
#: Use `xtick_labels` to customize the tick labels and the resulting hashref
#: is a mapping from the old tick labels to the new labels.
#:
#: The bars are added with the `add_barplot` method with the xdata and ydata.
#: The `bar_width` give a relative width of the bar. If this option is 1
#: there is no gap between bars.
#: See PODLINK('StatisticalPlots.pl') for other options to this method.
#:
#: Make sure that an alternate text is added to all graphs for accessibility. Be
#: detailed.

Context('Percent');

@grades = map { random(1, 5) } (0 .. 3);

$statPlot = StatPlot(
xmin => 0,
xmax => 5,
xtick_delta => 1,
xminor => 0,
ymin => 0,
ymax => 6,
yminor => 0,
ytick_delta => 1,
xtick_labels => { 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D' },
show_grid => 0,
rounded_corners => 1
);

$statPlot->add_barplot(
[ 1 .. 4 ], ~~@grades,
fill_colors => ['Red,', 'Salmon', 'LightYellow', 'LightGreen'],
stroke_width => 1,
bar_width => 0.9
);

$alt_text =
"A bar graph with vertical bars. The height of the bar labelled A "
. "is $grades[0]. The height of the bar labelled B is $grades[1]. "
. " The height of the bar labelled C is $grades[2] and finally the "
. "bar labelled D is $grades[3].";

# This code adds up all the numbers in the @grades array.
$num_students = 0;
$num_students += $_ for @grades;

$perA = Real($grades[0] / $num_students);

#:% section = statement
#: In this case, we plot the bar graph and ask a question.
#: Since the context is `Percent`, the students can
#: answer the question with either a fraction, decimal or percent.
#: If only percent answers is desired, see the flags for the Percent context
#: in the macro.
BEGIN_PGML
The following is a distribution of grades on a Statistics quiz.
>>[! [$alt_text] !]{$statPlot}{400}<<

What percentage of students earned an A? [_]{$perA}
END_PGML

#:% section = solution
BEGIN_PGML_SOLUTION
The total number of students that took the quiz is [$total] so the percentage is
the number of A students or [`[$grades[0]]/[$num_students] = [$perA] `]
END_PGML_SOLUTION

ENDDOCUMENT();
82 changes: 82 additions & 0 deletions tutorial/sample-problems/Statistics/DonutPlot.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
## DESCRIPTION
## Produce a donut plot.
## ENDDESCRIPTION
## DBsubject(WeBWorK)
## DBchapter(WeBWorK tutorial)
## DBsection(WeBWorK tutorial)
## Institution(Fitchburg State University)
## Author(Peter Staab)
## Date(06/25/2026)
## KEYWORDS('statistics', 'bar graph')

#:% name = Donut Plot
#:% subject = [statistics]
#:% type = sample
#:% categories = [graph, statistics]
#:% see_also = [PieChart.pg]

#:% section = preamble
#: This uses the `add_piechart` method in the PODLINK('StatisticalPlots.pl')
#: macro with the option to make it a donut plot.
#: A pie chart version of this is available at PROBLINK('PieChart.pg').
DOCUMENT();

loadMacros("PGstandard.pl", 'PGML.pl', 'StatisticalPlots.pl', 'PGcourse.pl');

#:% section = setup
#: In this example, the size of each wedge in the donut plot is given as an
#: array.
#:
#: The StatPlot is created with the axes and grid turned off. The
#: `add_piechart` method takes the number values in the `@values` array. The
#: values must be passed in as an arrayref (which is why the `~~` is needed).
#:
#: For the options, the `labels` also need to be passed in as an arrayref.
#: The `radius` and `inner_radius` gives the outer and inner radii of the donut.
#: The `angle_offset` is useful to get the labels in the proper position.
#:
#: The default colors come from the `rainbow` palette, and other options
#: exist.
#:
#: A detailed alt_text should accompany all plots/graphs.

@values = (1, 5, 8, 2, 4);
@animals = ('Anteater', 'Bobcat', 'Camel', 'Dingo', 'Elephant');

$stat_plot = StatPlot(xvisible => 0, yvisible => 0, show_grid => 0);
$stat_plot->add_piechart(
~~@values,
labels => ~~@animals,
radius => 3.25,
inner_radius => 2.5,
angle_offset => 30,
stroke_width => 1,
rounded_corners => 1
);

$total = 0;
$total += $_ for (@values);
$alt_text = 'A pie chart of animals. ';
for (0 .. 4) {
$alt_text .=
"The size of the $animals[$_] wedge is about "
. Round(100 * $values[$_] / $total, 1)
. '% of the total. ';
}

#:% section = statement
#: In this case, only the plot is provided with the alt text.
BEGIN_PGML

>> Donut Plot of the number of animals at the local zoo <<

>>[! [$alt_text] !]{$stat_plot}{400}<<

END_PGML

#:% section = solution
BEGIN_PGML_SOLUTION
Place a detailed solution here.
END_PGML_SOLUTION

ENDDOCUMENT();
96 changes: 96 additions & 0 deletions tutorial/sample-problems/Statistics/Histogram.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
## DESCRIPTION
## Find the mean and standard deviation of a list of numbers.
## ENDDESCRIPTION
## DBsubject(WeBWorK)
## DBchapter(WeBWorK tutorial)
## DBsection(WeBWorK tutorial)
## Institution(Missouri Western)
## Author(Glenn Rice)
## Date(06/25/2026)
## KEYWORDS('statistics', 'histogram')

#:% name = Histogram
#:% subject = [statistics]
#:% type = sample
#:% categories = [graph, statistics]

#:% section = preamble
#: This produces a histogram using the PODLINK('StatisticalPlots.pl') macro, which
#: provides some convenience methods for creating Statistical plots. This also uses
#: PODLINK('PGstatisticsmacros.pl') to generate random values from the Normal
#: distribution.
DOCUMENT();

loadMacros(
"PGstandard.pl", "PGML.pl",
'StatisticalPlots.pl', 'PGstatisticsmacros.pl',
"PGcourse.pl"
);

#:% section = setup
#:
#: The `urand` function produces normally distributed random numbers. This will
#: generate an array of 50 with a mean of 30 and standard deviation of 9. See
#: PODLINK('PGstatisticsmacros.pl') for more information.
#:
#: To get nice bin sizes (in this case of width 5), the min and max are found and then
#: are passed into the `add_histogram` method. The vertical maximum is calculated from the
#: data and then the ymax is set with the `->axes->yaxis` method.
#:
#: See PODLINK('StatisticalPlots.pl') for other options to this method.
#:
#: Make sure that an alternate text is added to all graphs for accessibility. Be
#: detailed.

$dx = 5; # bin size
@data = urand(30, 9, 50, 6);
$min = floor(min(@data) / $dx) * $dx;
$max = ceil(max(@data) / $dx) * $dx;

$histogramVertical = StatPlot(
xmin => $min - $dx/2,
xmax => $max + $dx/2,
xtick_distance => $dx,
xminor => 0,
xmajor => 0,
xlabel => '',
xlocation => 'bottom',
ymin => 0,
ytick_distance => 2,
yminor => 1,
ylabel => '',
ylocation => 'left',
rounded_corners => 1
);
(undef, $vFrequencies) = $histogramVertical->add_histogram(
~~@data,
min => $min,
max => $max,
bins => ($max - $min) / $dx,
fill_color => 'blue',
fill_opacity => 0.4,
stroke_width => 1
);
$histogramVertical->axes->yaxis(max => max(@$vFrequencies) + 1);

# use the $vFrequencies to generate the alt_text.
$alt_text = 'A histogram with ' . scalar(@$vFrequencies) . ' bins. ';
for $i (1 .. scalar(@$vFrequencies)) {
$alt_text .= "Bin $i is from " . ($min + ($i-1)*$dx) . ' to ' . ($min + $i*$dx) .
' and the height is ' . $vFrequencies->[$i-1] . '. ';
}

#:% section = statement
#: In this case, only the histogram is plotted. Make sure to add the alt text.
BEGIN_PGML

>>[! [$alt_text] !]{$histogramVertical}{400}<<

END_PGML

#:% section = solution
BEGIN_PGML_SOLUTION
If a question was asked, provide a solution.
END_PGML_SOLUTION

ENDDOCUMENT();
2 changes: 1 addition & 1 deletion tutorial/sample-problems/Statistics/LinearRegression.pg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#:% name = Linear Regression
#:% subject = [statistics]
#:% type = sample
#:% categories = [graph, statistics]
#:% categories = [statistics]

#:% section = preamble
#: The PODLINK('PGstatisticsmacros.pl') macro provides the `sample_correlation`
Expand Down
Loading