#### 3.23` `statistics

include statistics

import statistics as ...

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.23.1` `Basic Statistical Values

Calculates the arithmetic mean of the numbers in l.

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

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

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

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.

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

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

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.

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.

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

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

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

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

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.23.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.

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.23.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.

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

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

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

##### 3.23.4` `Regression and Modeling

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