A collection of functions for statistical calculation written in Swift.

Related tags

Math swift statistics
Overview

σ (sigma) - statistics library written in Swift

Carthage compatible CocoaPods Version License Platform

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.

Statistical library for Swift

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 run carthage update.

Setup with CocoaPods (iOS 8+)

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 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:

  • Returns nil for an empty array.
  • Same as AVERAGE in Microsoft Excel and Google Docs Sheets.

Formula

A = Σ(x) / n

Where:

  • n is the number of values.
Sigma.average([1, 3, 8])
// Result: 4

Central moment

Computes central moment of the dataset.

Note:

  • Returns nil for an empty array.
  • Same as in Wolfram Alpha and "moments" R package.

Formula

Σ(x - m)^k / n

Where:

  • m is the sample mean.
  • k is the order of the moment (0, 1, 2, 3, ...).
  • n is the sample size.
Sigma.centralMoment([3, -1, 1, 4.1, 4.1, 0.7], order: 3)
// Result: -1.5999259259

Covariance of a population

Computes covariance of the entire population between two variables: x and y.

Note:

  • Returns nil if arrays x and y have different number of values.
  • Returns nil for empty arrays.
  • Same as COVAR and COVARIANCE.P functions in Microsoft Excel and COVAR in Google Docs Sheets.

Formula

cov(x,y) = Σ(x - mx)(y - my) / n

Where:

  • mx is the population mean of the first variable.
  • my is the population mean of the second variable.
  • n is the total number of values.
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

Covariance of a sample

Computes sample covariance between two variables: x and y.

Note:

  • Returns nil if arrays x and y have different number of values.
  • Returns nil for empty arrays or arrays containing a single element.
  • Same as COVARIANCE.S function in Microsoft Excel.

Formula

cov(x,y) = Σ(x - mx)(y - my) / (n - 1)

Where:

  • mx is the sample mean of the first variable.
  • my is the sample mean of the second variable.
  • n is the total number of values.
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

Coefficient of variation of a sample

Computes coefficient of variation based on a sample.

Note:

  • Returns nil when the array is empty or contains a single value.
  • Returns Double.infinity if the mean is zero.
  • Same as in Wolfram Alfa and in "raster" R package (expressed as a percentage in "raster").

Formula

CV = s / m

Where:

  • s is the sample standard deviation.
  • m is the mean.
Sigma.coefficientOfVariationSample([1, 12, 19.5, -5, 3, 8])
// Result: 1.3518226672

Frequencies

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]

Kurtosis A

Returns the kurtosis of a series of numbers.

Note:

  • Returns nil if the dataset contains less than 4 values.
  • Returns nil if all the values in the dataset are the same.
  • Same as KURT in Microsoft Excel and Google Docs Sheets.

Formula

Kurtosis formula

Sigma.kurtosisA([2, 1, 3, 4.1, 19, 1.5])
// Result: 5.4570693277

Kurtosis B

Returns the kurtosis of a series of numbers.

Note:

  • Returns nil if the dataset contains less than 2 values.
  • Returns nil if all the values in the dataset are the same.
  • Same as in Wolfram Alpha and "moments" R package.

Formula

Kurtosis formula

Sigma.kurtosisB([2, 1, 3, 4.1, 19, 1.5])
// Result: 4.0138523409

Max

Returns the maximum value in the array.

Note: returns nil for an empty array.

Sigma.max([1, 8, 3])
// Result: 8

Median

Returns the median value from the array.

Note:

  • Returns nil when the array is empty.
  • Returns the mean of the two middle values if there is an even number of items in the array.
  • Same as MEDIAN in Microsoft Excel and Google Docs Sheets.
Sigma.median([1, 12, 19.5, 3, -5])
// Result: 3

Median high

Returns the median value from the array.

Note:

  • Returns nil when the array is empty.
  • Returns the higher of the two middle values if there is an even number of items in the array.
Sigma.medianHigh([1, 12, 19.5, 10, 3, -5])
// Result: 10

Median low

Returns the median value from the array.

Note:

  • Returns nil when the array is empty.
  • Returns the lower of the two middle values if there is an even number of items in the array.
Sigma.medianLow([1, 12, 19.5, 10, 3, -5])
// Result: 3

Min

Returns the minimum value in the array.

Note: returns nil for an empty array.

Sigma.min([7, 2, 3])
// Result: 2

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 value x.

Note:

  • Returns nil if σ is zero or negative.
  • Defaults: μ = 0, σ = 1.
  • Same as NORM.S.DIST, NORM.DIST and NORMDIST Excel functions and NORMDIST function in Google Docs sheet with cumulative argument equal to true.
Sigma.normalDistribution(x: -1, μ: 0, σ: 1)
// Result: 0.1586552539314570

Normal density

Returns density of the normal function for the given values of x, μ and σ.

Note:

  • Returns nil if σ is zero or negative.
  • Defaults: μ = 0, σ = 1.
  • Same as NORM.S.DIST, NORM.DIST and NORMDIST Excel functions and NORMDIST function in Google Docs sheet with cumulative argument equal to false.

Formula

Nodemal density function

Where:

  • x is the input value of the normal density function.
  • μ is the mean.
  • σ is the standard deviation.
Sigma.normalDensity(x: 0, μ: 0, σ: 1)
// Result: 0.3989422804014327

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:

  • 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.
Sigma.normalQuantile(p: 0.025, μ: 0, σ: 1)
// -1.9599639845400538

Pearson correlation coefficient

Calculates the Pearson product-moment correlation coefficient between two variables: x and y.

Note:

  • Returns nil if arrays x and y have different number of values.
  • Returns nil for empty arrays.
  • Same as CORREL and PEARSON functions in Microsoft Excel and Google Docs Sheets.

Formula

p(x,y) = cov(x,y) / (σx * σy)

Where:

  • cov is the population covariance.
  • σ is the population standard deviation.
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

Percentile

Calculates the Percentile value for the given dataset.

Note:

  • Returns nil when the values array is empty.
  • Returns nil when supplied percentile parameter is negative or greater than 1.
  • Same as PERCENTILE or PERCENTILE.INC in Microsoft Excel and PERCENTILE in Google Docs Sheets.
  • Same as the 7th sample quantile method from the Hyndman and Fan paper (1996).

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)

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:

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

Sigma.quantiles.method1([1, 12, 19.5, -5, 3, 8], probability: 0.5)
// Result: 3

Quantile method 2

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

Quantile method 3

Sigma.quantiles.method3([1, 12, 19.5, -5, 3, 8], probability: 0.5)
// Result: 3

Quantile method 4

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

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.

Sigma.quantiles.method5([1, 12, 19.5, -5, 3, 8], probability: 0.11)
// Result: -4.04

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

Sigma.quantiles.method6([1, 12, 19.5, -5, 3, 8], probability: 0.1999)
// Result: -2.6042

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

Sigma.quantiles.method7([1, 12, 19.5, -5, 3, 8], probability: 0.00001)
// Result: -4.9997

Quantile method 8

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

Quantile method 9

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

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.

Sigma.rank([2, 3, 6, 5, 3], ties: .average)
// Result: [1.0, 2.5, 5.0, 4.0, 2.5]

Skewness A

Returns the skewness of the dataset.

Note:

  • Returns nil if the dataset contains less than 3 values.
  • Returns nil if all the values in the dataset are the same.
  • Same as SKEW in Microsoft Excel and Google Docs Sheets.

Formula

Skewness formula

Sigma.skewnessA([4, 2.1, 8, 21, 1])
// Result: 1.6994131524

Skewness B

Returns the skewness of the dataset.

Note:

  • Returns nil if the dataset contains less than 3 values.
  • Returns nil if all the values in the dataset are the same.
  • Same as in Wolfram Alpha, SKEW.P in Microsoft Excel and skewness function in "moments" R package.

Formula

Skewness formula

Sigma.skewnessB([4, 2.1, 8, 21, 1])
// Result: 1.1400009992

Standard deviation of a population

Computes standard deviation of entire population.

Note:

  • Returns nil for an empty array.
  • Same as STDEVP and STDEV.P in Microsoft Excel and STDEVP in Google Docs Sheets.

Formula

σ = sqrt( Σ( (x - m)^2 ) / n )

Where:

  • m is the population mean.
  • n is the population size.
Sigma.standardDeviationPopulation([1, 12, 19.5, -5, 3, 8])
// Result: 7.918420858282849

Standard deviation of a sample

Computes standard deviation based on a sample.

Note:

  • Returns nil when the array is empty or contains a single value.
  • Same as STDEV and STDEV.S in Microsoft Excel and STDEV in Google Docs Sheets.

Formula

s = sqrt( Σ( (x - m)^2 ) / (n - 1) )

Where:

  • m is the sample mean.
  • n is the sample size.
Sigma.standardDeviationSample([1, 12, 19.5, -5, 3, 8])
// Result: 8.674195447801869

Standard error of the mean

Computes standard error of the mean.

Note:

  • Returns nil when the array is empty or contains a single value.

Formula

SE = s / sqrt(n)

Where:

  • s is the sample standard deviation.
  • n is the sample size.
Sigma.standardErrorOfTheMean([1, 12, 19.5, -5, 3, 8])
// Result: 3.5412254627

Sum

Computes sum of values from the array.

Sigma.sum([1, 3, 8])
// Result: 12

Unique values

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]

Variance of a population

Computes variance of entire population.

Note:

  • Returns nil when the array is empty.
  • Same as VAR.P or VARPA in Microsoft Excel and VARP or VARPA in Google Docs Sheets.

Formula

σ^2 = Σ( (x - m)^2 ) / n

Where:

  • m is the population mean.
  • n is the population size.
Sigma.variancePopulation([1, 12, 19.5, -5, 3, 8])
// Result: 62.70138889

Variance of a sample

Computes variance based on a sample.

Note:

  • Returns nil when the array is empty or contains a single value.
  • Same as VAR, VAR.S or VARA in Microsoft Excel and VAR or VARA in Google Docs Sheets.

Formula

s^2 = Σ( (x - m)^2 ) / (n - 1)

Where:

  • m is the sample mean.
  • n is the sample size.
Sigma.varianceSample([1, 12, 19.5, -5, 3, 8])
// Result: 75.24166667

Feedback is welcome

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

Contributors

License

Sigma is released under the MIT License.

Comments
  • Adding quantiles calculation & returning arrays

    Adding quantiles calculation & returning arrays

    I'm wondering if its a good idea to add quantiles calculation to your library. I'm currently in need of a tool which would return an array, say, of all the elements that are in the first 80% of the population. We'd need to calculate the quantile first and then run the selection algorithm on the initial array. I wonder if this functionality fits your library's philosophy, I'd be glad to contribute.

    opened by drinkius 3
  • Update documentation

    Update documentation

    Documentation for the returned value of qnorm had an extra incorrect statement.

    The fucntion returns -Double.infinity as correctly stated, however it had an extra comment: Returns nil if σ is zero or negative.

    opened by tikipatel 2
  • Added variance function in tests and implementation.

    Added variance function in tests and implementation.

    I added the calculation of the variance of an array as I needed it and would be happy if you want to pull it back in.

    I also updated the standardDeviationSample to use the newly created variance by taking the square root of the variance.

    opened by fanktom 2
  • feat: Added medianlow and medianHigh functions

    feat: Added medianlow and medianHigh functions

    Added median functions that provide low and high functions similar to python statistics ( https://docs.python.org/3/library/statistics.html ). These work as the median function when the number of data points is odd. Though when it is even, the larger of the two middle values is returned. This can be useful when using discrete data sets and you wish to keep them this way, rather than ending up with an interpolated output.

    opened by JohnClema 1
  • First commit of ranks function.

    First commit of ranks function.

    This is the ranks function. It operates similar to R but with a couple of differences. There's no control of NAs and ties can be assigned using the mean / average, minimum, maximum, first or last. There's currently no random option yet.

    Let's see if this pull request gets through. Let me know if this is okay and I'll send pull requests for each of the other files.

    opened by salmoni 0
  • How to Rank Percentile

    How to Rank Percentile

    I read the percentile 1 method, which is to find a value within an array. In excel there is a function called PercentRank.EXC, which ranks the value based on an array of data, the output is the percentile.

    I wonder if there is a way to use you library to reverse the percentile 1 method to get this?

    opened by kkhangv 1
  • Xcode 10.2 warnings

    Xcode 10.2 warnings

    Xcode 10.2 gives multiple warnings in multiple files: 'public' modifier is redundant for static method declared in a public extension, for example in CentralMoment.swift line 36:

    public static func centralMoment(_ values: [Double], order: Int) -> Double? {

    It seems that the access level of the extension is the default access level of each method in the extension. To get rid of the warning, one must remove these public access modifiers, or change the default access level of each extension to something other than public.

    This stack overflow post addresses the issue:

    https://stackoverflow.com/questions/34470589/will-marking-swift-extension-public-change-the-property-in-the-extension-to-be-i

    opened by mannd 2
  • Features/univariate anova

    Features/univariate anova

    Function for univariate ANOVA + unit tests.

    This passes the very limited unit tests (more needed) and the code is very ugly but it works for these unit tests.

    opened by salmoni 0
  • Features/t test one

    Features/t test one

    T-test function + unit tests.

    Not documented but the formula is similar to Excel

    tTest(values1, values2, testTails, testType)

    testTails not implemented yet. I'm unsure how to deal with this - just change 'probability' or should we accept an alpha?

    testType: 1 = paired, 2 = unpaired, equal variances, 3 = unpaired, unequal variances

    It passes the very limited unit tests I've made.

    opened by salmoni 0
Owner
Evgenii Neumerzhitckii
🐋🦔🐢🐝wow
Evgenii Neumerzhitckii
SwiftMath is a Swift framework providing some useful math constructs and functions

SwiftMath is a Swift framework providing some useful math constructs and functions, like complex numbers, vectors, matrices, quaternions, and polynomials.

Matteo Battaglio 175 Dec 2, 2022
VectorMath is a Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions

Purpose VectorMath is a Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions, useful for games or vector-based g

Nick Lockwood 341 Dec 31, 2022
Arbitrary-precision arithmetic in pure Swift

Overview API Documentation License Requirements and Integration Implementation Notes Full-width multiplication and division primitives Why is there no

null 707 Dec 19, 2022
Multi-dimensional Swift math

Upsurge Upsurge implements multi-dimensional data structures and operations. It brings numpy-like operations to Swift. Upsurge no longer supports DSP

Alejandro Isaza 180 Dec 20, 2022
Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

Scott Sievert 591 Sep 5, 2022
Overload +-*/ operator for Swift, make it easier to use (and not so strict)

Easy-Cal-Swift Overview This file is an overloading of +-*/ operator for Swift, to make it easier to use (and not so strict) It can make your life wit

Wei Wang 272 Jun 29, 2022
Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)

Matft Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy. Matft Feature & Usage Declaration MfArray MfType Subscription

null 80 Dec 21, 2022
Swift Custom Operators for Mathematical Notation

Euler Euler uses custom operators in the "Math Symbols" character set to implement functions using traditional mathematical notation. Please keep in m

Mattt 1.1k Jan 4, 2023
A cross-platform Swift library for evaluating mathematical expressions at runtime

Introduction What? Why? How? Usage Installation Integration Symbols Variables Operators Functions Arrays Performance Caching Optimization Standard Lib

Nick Lockwood 738 Jan 7, 2023
Numeric facilities for Swift

NumericAnnex NumericAnnex supplements the numeric facilities provided in the Swift standard library. Features The exponentiation operator ** and the c

Xiaodi Wu 69 Nov 3, 2022
Math expression parser built with Point•Free's swift-parsing package

swift-math-parser Basic math expression parser built with Point•Free's swift-parsing package. NOTE: currently, this uses a fork of that fixes a parsin

Brad Howes 36 Dec 14, 2022
Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

Scott Sievert 591 Sep 5, 2022
MRFoundation - A library to complement the Swift Standard Library

MRFoundation MRFoundation is a library to complement the Swift Standard Library.

Roman Mogutnov 2 Feb 12, 2022
Calculation proccess

Calculator Layout Boss Challenge No knowledge is your own until you put it into practice. Using what you've learnt about auto layout, stack views and

Olha Kostenko 0 Dec 10, 2021
Simple calculation to render cheap water effects.

Water Simple calculation to render cheap water effects. This simple project demonstrates : how to use Metal draw compute shader, or known as 'kernal f

Xue Yu 381 Sep 20, 2022
Demonstration of using Tasks and TaskGroup to thread a calculation.

TasksTest Demonstration of using Tasks and TaskGroup to thread a calculation. The calculation takes place in a separate Swift class that can be reused

null 0 Dec 27, 2021
Chess Timer - Time calculation application developed for chess game

Chess Timer Satranç oyunu için geliştirilmiş süre hesaplama uygulaması

Nurşah ARİ 0 Jan 8, 2022
Tip-Calculation- - A program for calculate the tip. You can easily calculate it and you can split money easily

Tip-Calculation- It is a program for calculate the tip. You can easily calculate

Burak Pala 0 Jan 13, 2022
⏳ Collection of Swift 5.5 async/await utility functions.

⏳ FunAsync Collection of Swift 5.5 async/await utility functions. Throw <-> Result conversion asyncThrowsToAsyncResult asyncResultToAsyncThrows More C

Yasuhiro Inami 23 Oct 14, 2022
A collection of Swift functions, extensions, and SwiftUI and UIKit Views.

J's Helper A collection of Swift functions, extensions, and SwiftUI and UIKit Views. Legend: ?? UIKit ?? SwiftUI ?? Shared Installation In XCode 12 go

Jem Alvarez 3 Oct 1, 2022