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.
There are four ways you can add Sigma to your project.
Add source (iOS 7+)
Simply add SigmaDistrib.swift file to your project.
Alternatively, add github "evgenyneu/SigmaSwiftStatistics" ~> 9.0
to your Cartfile and run carthage update
.
If you are using CocoaPods add this text to your Podfile and run pod install
.
use_frameworks!
target 'Your target name'
pod 'SigmaSwiftStatistics', '~> 9.0'
Setup a previous version of the library if you use an older version of Swift.
Add import SigmaSwiftStatistics
to your source code unless you used the file setup method.
Computes arithmetic mean of values in the array.
Note:
A = Σ(x) / n
Where:
Sigma.average([1, 3, 8])
// Result: 4
Computes central moment of the dataset.
Note:
Σ(x - m)^k / n
Where:
Sigma.centralMoment([3, -1, 1, 4.1, 4.1, 0.7], order: 3)
// Result: -1.5999259259
Computes covariance of the entire population between two variables: x and y.
Note:
cov(x,y) = Σ(x - mx)(y - my) / n
Where:
let x = [1, 2, 3.5, 3.7, 8, 12]
let y = [0.5, 1, 2.1, 3.4, 3.4, 4]
Sigma.covariancePopulation(x: x, y: y)
// Result: 4.19166666666667
Computes sample covariance between two variables: x and y.
Note:
cov(x,y) = Σ(x - mx)(y - my) / (n - 1)
Where:
let x = [1, 2, 3.5, 3.7, 8, 12]
let y = [0.5, 1, 2.1, 3.4, 3.4, 4]
Sigma.covarianceSample(x: x, y: y)
// Result: 5.03
Computes coefficient of variation based on a sample.
Note:
Double.infinity
if the mean is zero.CV = s / m
Where:
Sigma.coefficientOfVariationSample([1, 12, 19.5, -5, 3, 8])
// Result: 1.3518226672
Returns a dictionary with the keys containing the numbers from the input array and the values corresponding to the frequencies of those numbers.
Sigma.frequencies([1, 2, 3, 4, 5, 4, 4, 3, 5])
// Result: [2:1, 3:2, 4:3, 5:2, 1:1]
Returns the kurtosis of a series of numbers.
Note:
Sigma.kurtosisA([2, 1, 3, 4.1, 19, 1.5])
// Result: 5.4570693277
Returns the kurtosis of a series of numbers.
Note:
Sigma.kurtosisB([2, 1, 3, 4.1, 19, 1.5])
// Result: 4.0138523409
Returns the maximum value in the array.
Note: returns nil for an empty array.
Sigma.max([1, 8, 3])
// Result: 8
Returns the median value from the array.
Note:
Sigma.median([1, 12, 19.5, 3, -5])
// Result: 3
Returns the median value from the array.
Note:
Sigma.medianHigh([1, 12, 19.5, 10, 3, -5])
// Result: 10
Returns the median value from the array.
Note:
Sigma.medianLow([1, 12, 19.5, 10, 3, -5])
// Result: 3
Returns the minimum value in the array.
Note: returns nil for an empty array.
Sigma.min([7, 2, 3])
// Result: 2
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 value x
.
Note:
μ = 0
, σ = 1
.cumulative
argument equal to true
.Sigma.normalDistribution(x: -1, μ: 0, σ: 1)
// Result: 0.1586552539314570
Returns density of the normal function for the given values of x
, μ
and σ
.
Note:
μ = 0
, σ = 1
.cumulative
argument equal to false
.Where:
Sigma.normalDensity(x: 0, μ: 0, σ: 1)
// Result: 0.3989422804014327
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, and Double.infinity
if p is one.μ = 0
, σ = 1
.Sigma.normalQuantile(p: 0.025, μ: 0, σ: 1)
// -1.9599639845400538
Calculates the Pearson product-moment correlation coefficient between two variables: x and y.
Note:
p(x,y) = cov(x,y) / (σx * σy)
Where:
let x = [1, 2, 3.5, 3.7, 8, 12]
let y = [0.5, 1, 2.1, 3.4, 3.4, 4]
Sigma.pearson(x: x, y: y)
// Result: 0.843760859352745
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.
// Calculate 40th percentile
Sigma.percentile([35, 20, 50, 40, 15], percentile: 0.4)
// Result: 29
// Same as
Sigma.quantiles.method7([35, 20, 50, 40, 15], probability: 0.4)
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.This method calculates quantiles using the inverse of the empirical distribution function.
Sigma.quantiles.method1([1, 12, 19.5, -5, 3, 8], probability: 0.5)
// Result: 3
This method uses inverted empirical distribution function with averaging.
Sigma.quantiles.method2([1, 12, 19.5, -5, 3, 8], probability: 0.5)
// Result: 5.5
Sigma.quantiles.method3([1, 12, 19.5, -5, 3, 8], probability: 0.5)
// Result: 3
The method uses linear interpolation of the empirical distribution function.
Sigma.quantiles.method4([1, 12, 19.5, -5, 3, 8], probability: 0.17)
// Result: -4.88
This method uses a piecewise linear function where the knots are the values midway through the steps of the empirical distribution function.
Sigma.quantiles.method5([1, 12, 19.5, -5, 3, 8], probability: 0.11)
// Result: -4.04
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].
Sigma.quantiles.method6([1, 12, 19.5, -5, 3, 8], probability: 0.1999)
// Result: -2.6042
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].
Sigma.quantiles.method7([1, 12, 19.5, -5, 3, 8], probability: 0.00001)
// Result: -4.9997
The quantiles returned by the method are approximately median-unbiased regardless of the distribution of x.
Sigma.quantiles.method8([1, 12, 19.5, -5, 3, 8], probability: 0.11)
// Result: -4.82
The quantiles returned by this method are approximately unbiased for the expected order statistics if x is normally distributed.
Sigma.quantiles.method9([1, 12, 19.5, -5, 3, 8], probability: 0.10001)
// Result: -4.999625
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.
Sigma.rank([2, 3, 6, 5, 3], ties: .average)
// Result: [1.0, 2.5, 5.0, 4.0, 2.5]
Returns the skewness of the dataset.
Note:
Sigma.skewnessA([4, 2.1, 8, 21, 1])
// Result: 1.6994131524
Returns the skewness of the dataset.
Note:
skewness
function in "moments" R package.Sigma.skewnessB([4, 2.1, 8, 21, 1])
// Result: 1.1400009992
Computes standard deviation of entire population.
Note:
σ = sqrt( Σ( (x - m)^2 ) / n )
Where:
Sigma.standardDeviationPopulation([1, 12, 19.5, -5, 3, 8])
// Result: 7.918420858282849
Computes standard deviation based on a sample.
Note:
s = sqrt( Σ( (x - m)^2 ) / (n - 1) )
Where:
Sigma.standardDeviationSample([1, 12, 19.5, -5, 3, 8])
// Result: 8.674195447801869
Computes standard error of the mean.
Note:
SE = s / sqrt(n)
Where:
Sigma.standardErrorOfTheMean([1, 12, 19.5, -5, 3, 8])
// Result: 3.5412254627
Computes sum of values from the array.
Sigma.sum([1, 3, 8])
// Result: 12
Returns an unsorted array containing all values that occur within the input array without the duplicates.
Sigma.uniqueValues([2, 1, 3, 4, 5, 4, 3, 5])
// Result: [2, 3, 4, 5, 1]
Computes variance of entire population.
Note:
σ^2 = Σ( (x - m)^2 ) / n
Where:
Sigma.variancePopulation([1, 12, 19.5, -5, 3, 8])
// Result: 62.70138889
Computes variance based on a sample.
Note:
s^2 = Σ( (x - m)^2 ) / (n - 1)
Where:
Sigma.varianceSample([1, 12, 19.5, -5, 3, 8])
// Result: 75.24166667
If you need help or want to extend the library feel free to create an issue or submit a pull request.
Help will always be given at Hogwarts to those who ask for it.
-- J.K. Rowling, Harry Potter
Sigma is released under the MIT License.
link |
Stars: 688 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics