用 Swift 编写的用于统计计算的函数集合
σ (sigma) - 用 Swift 编写的统计库
该库是在 Swift 中执行统计计算的函数集合。它可以用于苹果设备的 Swift 应用程序以及其他平台上的开源 Swift 程序。
设置
有四种方法可以将 Sigma 添加到项目中。
添加源 (iOS 7+)
只需将 SigmaDistrib.swift 文件添加到您的项目中即可。
使用迦太基 (iOS 8+) 进行设置
或者,添加到您的购物车文件并运行 .github "evgenyneu/SigmaSwiftStatistics" ~> 9.0
carthage update
使用 CocoaPods (iOS 8+) 进行设置
如果您使用的是 CocoaPods,请将此文本添加到 Podfile 并运行 .pod install
use_frameworks!
target 'Your target name'
pod 'SigmaSwiftStatistics', '~> 9.0'
使用 Swift 包管理器进行设置
- 在 Xcode 11+ 中,选择“文件>包”>“添加包依赖项...”。
- 输入此项目的网址:https://github.com/evgenyneu/SigmaSwiftStatistics.git
Legacy Swift versions
Setup a previous version of the library if you use an older version of Swift.
Usage
Add to your source code unless you used the file setup method.import SigmaSwiftStatistics
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.
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.
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.
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.
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 if the mean is zero.
Double.infinity
- 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.
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:
- 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 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
Max
Returns the maximum value in the array.
Note: returns nil for an empty array.
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.
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.
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.
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 , and . The returned value is the area under the normal curve to the left of the value .x
μ
σ
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 argument equal to .
cumulative
true
Normal density
Returns density of the normal function for the given values of , and .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 argument equal to .
cumulative
false
Formula
Where:
- x is the input value of the normal density function.
- μ is the mean.
- σ is the standard deviation.
Normal quantile
Returns the quantile function for the normal distribution (the inverse of normal distribution). The argument is the probability, or the area under the normal curve to the left of the returned value.p
Note:
- Returns nil if σ is zero or negative.
- Returns nil if p is negative or greater than one.
- Returns if p is zero, and if p is one.
-Double.infinity
Double.infinity
- Defaults: , .
μ = 0
σ = 1
- Same as NORM.INV, NORM.S.INV and NORMINV Excel functions and NORMINV, NORMSINV Google Docs sheet functions.
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.
Percentile
Calculates the Percentile value for the given dataset.
Note:
- Returns nil when the array is empty.
values
- Returns nil when supplied parameter is negative or greater than 1.
percentile
- 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.
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 function in R.
quantile
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 parameter that determines how the ranks for the equal values ('ties') are calculated. Default value is . Possible values:
ties
.average
.average
: uses the average rank. Same as RANK.AVG in Microsoft Excel and Google Docs Sheets..min
, : uses the minimum/maximum rank. The value is the same as RANK and RANK.EQ in Microsoft Excel and Google Docs Sheets..max
.min
.first
, : the ranks are incremented/decremented..last
-
Same as rank function in R.
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 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 function in "moments" R package.
skewness
Formula
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.
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.
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.
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:
- 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.
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.
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.