StatKit

0.8.0

A collection of statistical analysis tools for your Swift programs.
JimmyMAndersson/StatKit

What's New

StatKit beta 8

2026-05-24T09:46:42Z

Welcome to the StatKit beta.
StatKit beta 8 brings new distributions, new association measures, and important correctness fixes across the library.

New in this release:

  • Three new distributions have been added - the Arcsine, Logistic, and Log-Logistic distributions. FiskDistribution is included as a type alias for LogLogisticDistribution.
  • Added the Goodman-Kruskal gamma coefficient as a new measure of ordinal association.
  • The kendallTau method no longer requires the ConvertibleToReal constraint - Comparable and Hashable are now sufficient, matching the ordinal nature of the statistic.
  • The binomial distribution now uses Walker's alias table for O(1) batch sampling, replacing the previous linear CDF search.
  • The normal distribution's Box-Muller sampler now consumes both outputs of each transform pair, halving the number of transcendental function calls.
  • The test suite has been migrated from XCTest to Swift Testing across all distribution types.
  • Updated the documentation, renaming the "Combinatorial Extensions" catalog page to "Mathematical Functions" and expanding doc comments throughout.

Deprecations:

  • QuantileEstimationMethod.closestOrOddIndexed has been renamed to .closest (breaking change).
  • GammaDistribution.init(alpha:beta:) is now formally marked as deprecated.

Fixes:

  • Fixed an off-by-one index error affecting all quantile estimation methods.
  • Fixed the binomial distribution's PMF calculation crashing on overflow for large trial counts by moving to log-space arithmetic. Also added boundary guards for p=0 and p=1.
  • Fixed the exponential distribution's log-PDF branch using the wrong sign, causing the density to grow rather than decay.
  • Fixed the beta distribution's PDF returning 0 at the boundary points x=0 and x=1 when the true density is infinite or finite non-zero.
  • Fixed the normal distribution's Box-Muller sampler producing ±infinity when sampling at the boundary of the unit interval.
  • Fixed sample variance returning 0 instead of NaN for single-element collections.

Full Changelog:

Feel free to help me develop and maintain this library if you like it!
https://github.com/sponsors/JimmyMAndersson

Thank you, and enjoy using StatKit!

StatKit


Platform Support

LinkedIn: Jimmy M Andersson Sponsor this project

Swift PM Compatible


Welcome to StatKit, a collection of statistical analysis tools for Swift developers.

Builtin Statistics

StatKit adds relevant functionality for statistical analysis for the types you use every day. With StatKit, you will be able to calculate a variety of useful statistics such as:

  • Central Tendency
    Calculate modes, means, and medians of your data sets.

  • Variability
    Compute variances and standard deviations.

  • Correlation
    Find linear tendencies and covariance of measurements.

  • Distributions
    Make computations using several common distribution types.

A simple example would be to calculate the modes of an integer array, which can be done easily with the following piece of code:

print([1, 2, 3, 3, 2, 4].mode(variable: \.self))

// Prints [3, 2]

In this case, Collection.mode(variable:) takes a KeyPath argument which specifies the variable inside the array that you are interested in. In the example above, we specify the \.self keypath, which points to the array element itself (in this case, the integers).

The pattern of specifying one or more variables to investigate is common throughout the StatKit library. It allows you to calculate similar statistics for a variety of different types using the same syntax. For example, both of the below examples produce valid results, even though the types under investigation are completely disparate:

Calculating the mode of all characters in a String:

print("StatKit".mode(variable: \.self))

// Prints ["t"]

Calculating the mode of CGPoint y-values in an array:

import CoreGraphics

let points = [CGPoint(x: 0, y: 1), 
              CGPoint(x: 1, y: 3), 
              CGPoint(x: 3, y: 1)]

print(points.mode(variable: \.y))
// Prints [1.0]

Computing statistics for collections of complex custom types

As the examples in the previous section showed, calculating statistics is easy when using collections of types that are readily available. However, most of us work with custom data structures in our projects. Luckily, StatKit provides support for arbitrary custom types thanks to the extensive use of generics.

Let us look at a custom data structure that keeps track of collected data points for a specific brand of cars, and how we can use StatKit to easily calculate the mean and standard deviation of their fuel consumption:

struct FuelConsumption {
  let modelYear: String
  let litersPer10Km: Double
}

let measurements: [FuelConsumption] = [...]

measurements.mean(variable: \.litersPer10Km, strategy: .arithmetic)
measurements.standardDeviation(variable: \.litersPer10Km, from: .sample)

As you can see, using KeyPath's makes the StatKit API easy to use and reusable across completely arbitrary custom structures.

Distributions

StatKit provides multiple discrete and continuous distribution types for you to work with. These allow you to compute probabilities, calculate common moments such as skewness and variance, and sample random numbers from a specific data distribution.

let normal = NormalDistribution(mean: 0, variance: 1)
print(normal.cdf(x: 0))
// Prints 0.5

let normalRandomVariables = normal.sample(10)
// Generates 10 samples from the normal distribution

Documentation

StatKit is documented using Swift-DocC, which means that the documentation pages can be built by Xcode and viewed in the Developer Documentation panel. Build it by clicking Product > Build Documentation or hitting Shift + Ctrl + Cmd + D.

System Requirements

To use StatKit, make sure that your system has Swift 6.0 (or later) installed. If you’re using a Mac, also make sure that xcode-select points at an Xcode installation that includes a valid version of Swift and that you’re running macOS 14 or later.

IMPORTANT
StatKit does not officially support any beta software, including beta versions of Xcode and macOS, or unreleased versions of Swift.

Installation

Swift Package Manager

To install StatKit using the Swift Package Manager, add it as a dependency in your Package.swift file:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/JimmyMAndersson/StatKit.git", from: "0.8.0")
    ],
    ...
)

Then import StatKit where you would like to use it:

import StatKit

Contributions and support

StatKit is a young project that is under active development. Our vision is to create the go-to statistics library for Swift developers, much like SciPy and NumPy are for the Python language.

❤️ Consider becoming a sponsor to support the development of this library.
You could cover an afternoon coffee or a meal to keep my neurons firing.

Thank you for your contribution, and enjoy using StatKit!

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

Last updated: Mon Jun 01 2026 04:31:23 GMT-0900 (Hawaii-Aleutian Daylight Time)