Giter Club home page Giter Club logo

sigmaswiftstatistics's Introduction

σ (sigma) - statistics library written in Swift

[Carthage compatible][carthage] [CocoaPods Version][cocoadocs] [License][cocoadocs] [Platform][cocoadocs] [cocoadocs]: http://cocoadocs.org/docsets/SigmaSwiftStatistics [carthage]: https://github.com/Carthage/Carthage

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 (Swift 3.0)

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" ~> 4.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', '~> 4.0'

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(4,0,0)..<Version(5,0,0))
    ]
)

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

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

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

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

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

Sum

Computes sum of values from the array.

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

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

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

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.

sigmaswiftstatistics's People

Contributors

evgenyneu avatar johnclema avatar

Watchers

Faridh Mendoza avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.