This library is a collection of functions that perform statistical calculations in Swift. It can be used in Swift apps for Apple devices and in open source Swift programs on other platforms.
Returns the quantile function for the normal distribution (the inverse of normal distribution). The p argument is the probability, or the area under the normal curve to the left of the returned value.
Note:
Returns nil if σ is zero or negative.
Returns nil if p is negative or greater than one.
Returns -Double.infinity if p is zero, and Double.infinity if p is one.
Defaults: μ = 0, σ = 1.
Same as NORM.INV, NORM.S.INV and NORMINV Excel functions and NORMINV, NORMSINV Google Docs sheet functions.
Collection of nine functions that calculate sample quantiles corresponding to the given probability. This is an implementation of the nine algorithms described in the Hyndman and Fan paper (1996). The documentation of the functions is based on R and Wikipedia.
Note:
Returns nil if the dataset is empty.
Returns nil if the probability is outside the [0, 1] range.
Same as quantile function in R.
Quantile method 1
This method calculates quantiles using the inverse of the empirical distribution function.
This method is implemented in Microsoft Excel (PERCENTILE.EXC), Minitab and SPSS. It uses linear interpolation of the expectations for the order statistics for the uniform distribution on [0,1].
This method is implemented in S, Microsoft Excel (PERCENTILE or PERCENTILE.INC) and Google Docs Sheets (PERCENTILE). It uses linear interpolation of the modes for the order statistics for the uniform distribution on [0, 1].
Receives an optional ties parameter that determines how the ranks for the equal values (‘ties’) are calculated. Default value is .average. Possible values:
.average: uses the average rank. Same as RANK.AVG in Microsoft Excel and Google Docs Sheets.
.min, .max: uses the minimum/maximum rank. The value .min is the same as RANK and RANK.EQ in Microsoft Excel and Google Docs Sheets.
.first, .last: the ranks are incremented/decremented.
σ (sigma) - statistics library written in Swift
This library is a collection of functions that perform statistical calculations in Swift. It can be used in Swift apps for Apple devices and in open source Swift programs on other platforms.
Setup
There are four ways you can add Sigma to your project.
Add source (iOS 7+)
Simply add SigmaDistrib.swift file to your project.
Setup with Carthage (iOS 8+)
Alternatively, add
github "evgenyneu/SigmaSwiftStatistics" ~> 9.0
to your Cartfile and runcarthage update
.Setup with CocoaPods (iOS 8+)
If you are using CocoaPods add this text to your Podfile and run
pod install
.Setup with Swift Package Manager
Legacy Swift versions
Setup a previous version of the library if you use an older version of Swift.
Usage
Add
import SigmaSwiftStatistics
to your source code unless you used the file setup method.Average / mean
Computes arithmetic mean of values in the array.
Note:
Formula
Where:
Central moment
Computes central moment of the dataset.
Note:
Formula
Where:
Covariance of a population
Computes covariance of the entire population between two variables: x and y.
Note:
Formula
Where:
Covariance of a sample
Computes sample covariance between two variables: x and y.
Note:
Formula
Where:
Coefficient of variation of a sample
Computes coefficient of variation based on a sample.
Note:
Double.infinity
if the mean is zero.Formula
Where:
Frequencies
Returns a dictionary with the keys containing the numbers from the input array and the values corresponding to the frequencies of those numbers.
Kurtosis A
Returns the kurtosis of a series of numbers.
Note:
Formula
Kurtosis B
Returns the kurtosis of a series of numbers.
Note:
Formula
Max
Returns the maximum value in the array.
Note: returns nil for an empty array.
Median
Returns the median value from the array.
Note:
Median high
Returns the median value from the array.
Note:
Median low
Returns the median value from the array.
Note:
Min
Returns the minimum value in the array.
Note: returns nil for an empty array.
Normal distribution
Returns the normal distribution for the given values of
x
,μ
andσ
. The returned value is the area under the normal curve to the left of the valuex
.Note:
μ = 0
,σ = 1
.cumulative
argument equal totrue
.Normal density
Returns density of the normal function for the given values of
x
,μ
andσ
.Note:
μ = 0
,σ = 1
.cumulative
argument equal tofalse
.Formula
Where:
Normal quantile
Returns the quantile function for the normal distribution (the inverse of normal distribution). The
p
argument is the probability, or the area under the normal curve to the left of the returned value.Note:
-Double.infinity
if p is zero, andDouble.infinity
if p is one.μ = 0
,σ = 1
.Pearson correlation coefficient
Calculates the Pearson product-moment correlation coefficient between two variables: x and y.
Note:
Formula
Where:
Percentile
Calculates the Percentile value for the given dataset.
Note:
values
array is empty.percentile
parameter is negative or greater than 1.See the Percentile method documentation for more information.
Quantiles
Collection of nine functions that calculate sample quantiles corresponding to the given probability. This is an implementation of the nine algorithms described in the Hyndman and Fan paper (1996). The documentation of the functions is based on R and Wikipedia.
Note:
quantile
function in R.Quantile method 1
This method calculates quantiles using the inverse of the empirical distribution function.
Quantile method 2
This method uses inverted empirical distribution function with averaging.
Quantile method 3
Quantile method 4
The method uses linear interpolation of the empirical distribution function.
Quantile method 5
This method uses a piecewise linear function where the knots are the values midway through the steps of the empirical distribution function.
Quantile method 6
This method is implemented in Microsoft Excel (PERCENTILE.EXC), Minitab and SPSS. It uses linear interpolation of the expectations for the order statistics for the uniform distribution on [0,1].
Quantile method 7
This method is implemented in S, Microsoft Excel (PERCENTILE or PERCENTILE.INC) and Google Docs Sheets (PERCENTILE). It uses linear interpolation of the modes for the order statistics for the uniform distribution on [0, 1].
Quantile method 8
The quantiles returned by the method are approximately median-unbiased regardless of the distribution of x.
Quantile method 9
The quantiles returned by this method are approximately unbiased for the expected order statistics if x is normally distributed.
Rank
Returns the ranks of the values in the dataset.
Note:
Receives an optional
ties
parameter that determines how the ranks for the equal values (‘ties’) are calculated. Default value is.average
. Possible values:.average
: uses the average rank. Same as RANK.AVG in Microsoft Excel and Google Docs Sheets..min
,.max
: uses the minimum/maximum rank. The value.min
is the same as RANK and RANK.EQ in Microsoft Excel and Google Docs Sheets..first
,.last
: the ranks are incremented/decremented.Same as rank function in R.
Skewness A
Returns the skewness of the dataset.
Note:
Formula
Skewness B
Returns the skewness of the dataset.
Note:
skewness
function in “moments” R package.Formula
Standard deviation of a population
Computes standard deviation of entire population.
Note:
Formula
Where:
Standard deviation of a sample
Computes standard deviation based on a sample.
Note:
Formula
Where:
Standard error of the mean
Computes standard error of the mean.
Note:
Formula
Where:
Sum
Computes sum of values from the array.
Unique values
Returns an unsorted array containing all values that occur within the input array without the duplicates.
Variance of a population
Computes variance of entire population.
Note:
Formula
Where:
Variance of a sample
Computes variance based on a sample.
Note:
Formula
Where:
Feedback is welcome
If you need help or want to extend the library feel free to create an issue or submit a pull request.
– J.K. Rowling, Harry Potter
Contributors
License
Sigma is released under the MIT License.