This library is a collection of functions that perform statistical calculations in Swift.
There are four ways you can add Sigma to your project.
Simply addSigma.swift file to your project.
Alternatively, add github "evgenyneu/SigmaSwiftStatistics" ~> 2.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', '~> 2.0'
Here is how to use the library in a WatchKit extension with CocoaPods.
use_frameworks! target 'YourWatchApp Extension Target Name' do platform :watchos, '3.0' pod 'SigmaSwiftStatistics', '~> 2.0' end
Add the following text to your Package.swift file and run swift build
.
import PackageDescription let package = Package( name: "YourPackageName", targets: [], dependencies: [ .Package(url: "https://github.com/evgenyneu/SigmaSwiftStatistics.git", versions: Version(2,0,0)..<Version(3,0,0)) ] )
Setup aprevious version of the library if you use an older version of Swift.
Add import SigmaSwiftStatistics
to your source code if you used Carthage or CocoaPods setup methods.
Returns the maximum value in the array.
Note: returns nil for an empty array.
Sigma.max([1, 8, 3]) // Result: 8
Returns the minimum value in the array.
Note: returns nil for an empty array.
Sigma.min([7, 2, 3]) // Result: 2
Computes sum of values from the array.
Sigma.sum([1, 3, 8]) // Result: 12
Computes arithmetic mean of values in the array.
A = Σ(x) / n
Where:
Sigma.average([1, 3, 8]) // Result: 4
Returns the median value from the array.
Sigma.median([1, 12, 19.5, 3, -5]) // Result: 3
Returns the median value from the array.
Sigma.medianLow([1, 12, 19.5, 10, 3, -5]) // Result: 3
Returns the median value from the array.
Sigma.medianHigh([1, 12, 19.5, 10, 3, -5]) // Result: 10
Computes variance based on a sample.
s^2 = Σ( (x - m)^2 ) / (n - 1)
Where:
Sigma.varianceSample([1, 12, 19.5, -5, 3, 8]) // Result: 75.24166667
Computes variance of entire population.
σ^2 = Σ( (x - m)^2 ) / n
Where:
Sigma.variancePopulation([1, 12, 19.5, -5, 3, 8]) // Result: 62.70138889
Computes standard deviation based on a sample.
s = sqrt( Σ( (x - m)^2 ) / (n - 1) )
Where:
Sigma.standardDeviationSample([1, 12, 19.5, -5, 3, 8]) // Result: 8.674195447801869
Computes standard deviation of entire population.
σ = sqrt( Σ( (x - m)^2 ) / n )
Where:
Sigma.standardDeviationPopulation([1, 12, 19.5, -5, 3, 8]) // Result: 7.918420858282849
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 covariance of the entire population between two variables: x and y.
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
Calculates the Pearson product-moment correlation coefficient between two variables: x and y.
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.
values
array is empty. percentile
parameter is negative or greater than 1. See thePercentile 1 method documentation for more information.
// Calculate 40th percentile Sigma.percentile1(values: [35, 20, 50, 40, 15], percentile: 0.4) // Result: 29
You can type a sigma letter σ
instead of Sigma
. For example:
σ.average([1, 2]) σ.standardDeviationSample([1, 12, 19.5, -5, 3, 8])
If you need help or want to extend the library feel free to create an issue or submit a pull request.
Sigma is released under theMIT License.