This library is a collection of functions that perform statistical calculations in Swift.

3.0 Setup (Swift)

There are four ways you can add Sigma to your project.

Add source (iOS 7+)

Simply add Sigma.swift file to your project.

Setup with Carthage (iOS 8+)

Alternatively, the add lot “evgenyneu/SigmaSwiftStatistics” ~ > 2.0 to 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', '~> 2.0'
Copy the code

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
Copy the code

Setup with Swift Package Manager

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 (0, 2).Copy the code

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 if you used Carthage or CocoaPods setup methods.

Max

Returns the maximum value in the array.

Note: returns nil for an empty array.

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

Min

Returns the minimum value in the array.

Note: returns nil for an empty array.

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

Sum

Computes sum of values from the array.

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

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 = sigma (x)/n

Where:

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

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: 3Copy the code

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: 3Copy the code

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: 10Copy the code

Sample variance

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

Sigma ^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.24166667Copy the code

Population variance

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

Sigma ^2 = sigma (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.70138889Copy the code

Sample standard deviation

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.674195447801869Copy the code

Population standard deviation

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

Sigma = 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.918420858282849Copy the code

Sample covariance

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.03Copy the code

Population covariance

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) = sigma (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.19166666666667Copy the code

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 PERSON functions in Microsoft Excel and Google Docs Sheets.

Formula

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

Where:

  • cov is the population covariance.
  • sigma 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] sigmat. Pearson (x: x, y: y) // Result: 0.843760859352745Copy the code

Percentile 1

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.

See the Percentile 1 method documentation for more information.

Calculate 40th percentile sigma. percentile1(values: [35, 20, 50, 40, 15], percentile: 0.4) // Result: 29Copy the code

Shorter syntax

You can type a sigma letter instead of sigma. For example:

Average ([1, 2]) σ. StandardDeviationSample ([1, 12, 19.5, -5, 3, 8])Copy the code

Feedback is welcome

If you need help or want to extend the library feel free to create an issue or submit a pull request.

Contributors

License

Sigma is released under the MIT License.