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 ofnthat conform to the BinaryInteger protocol. Returnsnilifnis less than zero.Declaration
Swift
public static func factorial<T>(_ n: T) -> Int? where T : BinaryInteger -
Calculate
n!for values ofnthat conform to the BinaryFloatingPoint protocol. Uses the gamma function tofill in
values ofnthat are not integers. Returnsnilifnis less than zero.Declaration
Swift
public static func factorial<T>(_ n: T) -> Double? where T : BinaryFloatingPoint -
Calculate n-choose-k for values of
nandkthat 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
nthat conform to the BinaryFloatingPoint protocol and values ofkthat 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 : BinaryIntegerParameters
dataArray of values
Return Value
The mean of the array of values or
nilif 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 : BinaryFloatingPointParameters
dataArray of values
Return Value
The mean of the array of values or
nilif 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 : BinaryFloatingPointParameters
dataSample of values. Note that this should contain at least two values.
Return Value
The unbiased sample variance or
nilifdatacontains 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 : BinaryFloatingPointParameters
dataSample of values. Note that this should contain at least two values.
Return Value
The sample unbiased standard deviation or
nilifdatacontains 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 : BinaryIntegerParameters
dataSample of values. Note that this should contain at least two values.
Return Value
The sample unbiased standard deviation or
nilifdatacontains 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 : BinaryFloatingPointParameters
dataValues of population. Note that this should contain at least one value.
Return Value
The population variance or
nilifdatacontains 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 : BinaryIntegerParameters
dataSample of values. Note that this should contain at least two values.
Return Value
The unbiased sample variance or
nilifdatacontains 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 : BinaryIntegerParameters
dataValues of population. Note that this should contain at least one value.
Return Value
The population variance or
nilifdatacontains 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 : BinaryFloatingPointParameters
dataValues of population. Note that this should contain at least one value.
Return Value
The population variance or
nilifdatacontains 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 : BinaryIntegerParameters
dataValues of population. Note that this should contain at least one value.
Return Value
The population variance or
nilifdatacontains fewer than one value. -
Undocumented
Declaration
Swift
public static func erfinv(_ y: Double) -> Double -
Undocumented
Declaration
Swift
public static func lsr(_ points: [[Double]]) -> [Double]
Common Structure Reference