Builtins and Libraries
3.1 Global Utilities
3.2 Numbers
3.3 Strings
3.4 Booleans
3.5 Raw  Array
3.6 Tables
3.7 lists
3.8 sets
3.9 arrays
3.10 string-dict
3.11 option
3.12 pick
3.13 either
3.14 srcloc
3.15 pprint
3.16 s-exp
3.17 s-exp-structs
3.18 image-structs
3.19 image
3.20 world
3.21 gdrive-sheets
3.22 data-source
3.23 reactors
3.24 chart
3.25 plot
3.26 statistics
3.27 math
On this page:
3.26.1 Basic Statistical Values
mean
median
modes
has-mode
mode-smallest
mode-largest
mode-any
stdev
3.26.2 The Stat  Model Type
Stat  Model
simple-linear-model
3.26.3 Stat  Model Methods
.predictor
.apply
.r-squared
3.26.4 Regression and Modeling
lin-reg-2V
6.12

3.26 statistics

Usage:
include statistics
import statistics as ...
The Pyret Statistics library. It consists of functions that calculate relevant statistical values of data sets, and functions for statistical modeling of numerical data.

Every function in this library is available on the statistics module object. For example, if you used import statistics as S, you would write S.median to access median below. If you used include, then you can refer to identifiers without writing S. as a prefix.

3.26.1 Basic Statistical Values

mean :: (l :: List<Number>) -> Number

Calculates the arithmetic mean of the numbers in l.

Examples:

check: mean([list: ]) raises "Empty List" mean([list: 1]) is 1 mean([list: 2, 2, 4.5, 1.5, 1, 1]) is 2 end

median :: (l :: List<Number>) -> Number

Calculates the median of the numbers in l. If the list is of even length, returns the average of the two middle-most values.

Examples:

check: median([list: ]) raises "Empty List" median([list: 2]) is 2 median([list: -1, 0, 1, 2, 5]) is 1 end

modes :: (l :: List<Number>) -> List<Number>

Calculates the modes of the numbers in l. If no number appears more than once, returns the empty list. The modes will be returned in sorted order.

Computing the mode of a list of values is unambiguous when there is a unique “most common” element. Computer scientists and mathematicians agree that when two values are equally “most common”, they are both considered modes of the list. The natural generalization of this is that when all values occur equally often, they are all modes of the list. However, many high-school textbooks assert that when no element appears more than once, no element should be considered a mode. To avoid confusing high-school students, we adopt the definition they will find in their textbooks.

Examples:

check: modes([list: ]) is [list: ] modes([list: 1, 2, 3, 4]) is [list: ] modes([list: 1, 2, 3, 1, 4]) is [list: 1] modes([list: 1, 2, 1, 2, 2, 1]) is [list: 1, 2] modes([list: 1, 2, 2, 1, 2, 1]) is [list: 1, 2] end

has-mode :: (l :: List<Number>) -> Boolean

Determines if a list of numbers has any modes, i.e., any repeated values.

Examples:

check: has-mode([list: ]) is false has-mode([list: 1, 2, 3, 4]) is false has-mode([list: 1, 2, 2, 1, 2, 2]) is true has-mode([list: 1, 2, 3, 2]) is true end

Returns the smallest mode of a list of numbers, if any is present.

Examples:

check: mode-smallest([list: ]) raises "empty" mode-smallest([list: 1]) raises "no duplicate values" mode-smallest([list: 1, 2, 3, 4, 5]) raises "no duplicate values" mode-smallest([list: 1, 1, 2]) is 1 mode-smallest([list: 1, 2, 1, 2]) is 1 end

Returns the largest mode of a list of numbers, if any is present.

Examples:

check: mode-smallest([list: ]) raises "empty" mode-smallest([list: 1]) raises "no duplicate values" mode-smallest([list: 1, 2, 3, 4, 5]) raises "no duplicate values" mode-smallest([list: 1, 1, 2]) is 1 mode-smallest([list: 1, 2, 1, 2]) is 2 end

mode-any :: (l :: List<Number>) -> Number

Returns an arbitrary mode of a list of numbers, if any is present.

Examples:

check: mode-any([list: ]) raises "empty" mode-any([list: 1]) raises "no duplicate values" mode-any([list: 1, 2, 3, 4, 5]) raises "no duplicate values" mode-any([list: 1, 1, 2]) is 1 mode-any([list: 1, 2, 1, 2]) satisfies lam(m): (m == 1) or (m == 2) end end

stdev :: (l :: List<Number>) -> Number

Gives the standard deviation of the data set represented by numbers in l.

Examples:

check: stdev([list: ]) raises "Empty List" stdev([list: 2]) is 0 stdev([list: 2, 4, 4, 4, 5, 5, 7, 9]) is 2 end

3.26.2 The StatModel Type

Each variant of the StatModel type represents a different kind of statistical model. These variants have their own methods that allow prediction of data, and access to meta-data about the model.

Below is the documentation for the variants of the StatModel type, and the members and methods of each. Note that the methods for a particular variant will be prefixed with a dot.

data StatModel:
| simple-linear-model(alpha :: Number, beta :: Number)
end

simple-linear-model :: (alpha :: Number, beta :: Number) -> StatModel

Models the relationship between a single explanatory variable, and a dependent variable using a linear predictor function.

alpha :: Number
The y-intercept of the linear predictor function.

beta :: Number
The slope of the linear predictor function.

3.26.3 StatModel Methods

Below are all of the methods that can be used by variants of the StatModel data type. Some methods are specific to certain variants; these methods will specify which variant uses which.

.predictor :: () -> (Number -> Number)

Returns the linear predictor function for a simple-linear-model variant.

.apply :: (l :: List<Number>) -> List<Number>

Applies the linear predictor for a simple-linear-model to a list of numerical data.

.r-squared :: () -> Number

Gives the coefficient of correlation for a simple-linear-model.

3.26.4 Regression and Modeling

Each of these functions is used to perform a regression by creating a certain variant of StatModel.

lin-reg-2V :: (X :: List<Number>, Y :: List<Number>) -> StatModel

Calculates a linear regression to model simple independent -> dependent variable relationship. Uses Ordinary Least Squares.

Examples:

check: lin-reg-2V([list: 0, 1, 2, 3], [list: 3, 2, 1, 0]) is StatModel(3, -1, 1) end