Common
public struct Common
Common Statistical Functions
This struct
is used as a namespace to separate the stats functions in
SwiftStats.
In general, the functions here do not throw exceptions but nil
values can be
returned. For example, to calculate the standard deviation of a sample
requires at least two data points. If the array that is passed to sd()
does
not have at least two values then nil
is returned.
Functions here are often generic in that they work for a range of types (for example, the same function is called to calculate the mean of Int8, Int32, Int64, or Int arrays). Separate implementations can be found for BinaryInteger and BinaryFloatingPoint protocols.
-
Calculate
n!
for values ofn
that conform to the BinaryInteger protocol. Returnsnil
ifn
is less than zero.Declaration
Swift
public static func factorial<T>(_ n: T) -> Int? where T : BinaryInteger
-
Calculate
n!
for values ofn
that conform to the BinaryFloatingPoint protocol. Uses the gamma function tofill in
values ofn
that are not integers. Returnsnil
ifn
is less than zero.Declaration
Swift
public static func factorial<T>(_ n: T) -> Double? where T : BinaryFloatingPoint
-
Calculate n-choose-k for values of
n
andk
that conform to the BinaryInteger protocol.Declaration
Swift
public static func choose<T>(n: T, k: T) -> Int where T : BinaryInteger
-
Calculate n-choose-k for values of
n
that conform to the BinaryFloatingPoint protocol and values ofk
that conform to the BinaryInteger protocol.Declaration
Swift
public static func choose<N, K>(n: N, k: K) -> Double where N : BinaryFloatingPoint, K : BinaryInteger
-
Calculates the mean of an array of values for types that satisfy the BinaryInteger protocol (e.g Int, Int32).
Declaration
Swift
public static func mean<T>(_ data: [T]) -> Double? where T : BinaryInteger
Parameters
data
Array of values
Return Value
The mean of the array of values or
nil
if the array was empty. -
Calculates the mean of an array of values for types that satisfy the BinaryFloatingPoint protocol (e.g Float, Double).
Declaration
Swift
public static func mean<T>(_ data: [T]) -> Double? where T : BinaryFloatingPoint
Parameters
data
Array of values
Return Value
The mean of the array of values or
nil
if the array was empty. -
Calculates the unbiased sample variance for an array for types that satisfy the BinaryFloatingPoint protocol (e.g Float, Double).
Declaration
Swift
public static func variance<T>(_ data: [T]) -> Double? where T : BinaryFloatingPoint
Parameters
data
Sample of values. Note that this should contain at least two values.
Return Value
The unbiased sample variance or
nil
ifdata
contains fewer than two values. -
Calculates the unbiased sample standard deviation for an array of values for types that satisfy the BinaryFloatingPoint protocol (e.g Float, Double).
Declaration
Swift
public static func sd<T>(_ data: [T]) -> Double? where T : BinaryFloatingPoint
Parameters
data
Sample of values. Note that this should contain at least two values.
Return Value
The sample unbiased standard deviation or
nil
ifdata
contains fewer than two values. -
Calculates the unbiased sample standard deviation for an array of values for types that satisfy the BinaryInteger protocol (e.g Int, Int32).
Declaration
Swift
public static func sd<T>(_ data: [T]) -> Double? where T : BinaryInteger
Parameters
data
Sample of values. Note that this should contain at least two values.
Return Value
The sample unbiased standard deviation or
nil
ifdata
contains fewer than two values. -
Calculates the population variance for an array of values for types that satisfy the BinaryFloatingPoint protocol (e.g Float, Double).
Declaration
Swift
public static func pvariance<T>(_ data: [T]) -> Double? where T : BinaryFloatingPoint
Parameters
data
Values of population. Note that this should contain at least one value.
Return Value
The population variance or
nil
ifdata
contains fewer than one value. -
Calculates the unbiased sample variance for an array of values for types that satisfy the BinaryInteger protocol (e.g Int, Int32).
Declaration
Swift
public static func variance<T>(_ data: [T]) -> Double? where T : BinaryInteger
Parameters
data
Sample of values. Note that this should contain at least two values.
Return Value
The unbiased sample variance or
nil
ifdata
contains fewer than two values. -
Calculates the population variance for an array of values for types that satisfy the BinaryInteger protocol (e.g Int, Int32).
Declaration
Swift
public static func pvariance<T>(_ data: [T]) -> Double? where T : BinaryInteger
Parameters
data
Values of population. Note that this should contain at least one value.
Return Value
The population variance or
nil
ifdata
contains fewer than one value. -
Calculates the median of an array of values for types that satisfy the BinaryFloatingPoint protocol (e.g Float, Double).
Declaration
Swift
public static func median<T>(_ data: [T]) -> Double? where T : BinaryFloatingPoint
Parameters
data
Values of population. Note that this should contain at least one value.
Return Value
The population variance or
nil
ifdata
contains fewer than one value. -
Calculates the median of an array of values for types that satisfy the BinaryInteger protocol (e.g Int, Int32).
Declaration
Swift
public static func median<T>(_ data: [T]) -> Double? where T : BinaryInteger
Parameters
data
Values of population. Note that this should contain at least one value.
Return Value
The population variance or
nil
ifdata
contains fewer than one value. -
Undocumented
Declaration
Swift
public static func erfinv(_ y: Double) -> Double
-
Undocumented
Declaration
Swift
public static func lsr(_ points: [[Double]]) -> [Double]