Kotlin/JVM compensated summation of Double sequences to calculate sum, mean, standard deviation

Overview

Generic badge Generic badge

precise

Implements compensated summation for sequences of Double. Reduces rounding errors associated with limited precision of floating-point numbers.

val numbers = List(420) { 0.1 }  // 420 x 0.01

numbers.preciseSumOf { it } // 42.0 (compensated sum)
numbers.sumOf { it }        // 42.00000000000033 (naive sum)

The table shows the total error when summing the same sequence of random numbers. All the terms were rounded to 0.0001 before addition. In the % column, the error of preciseSumOf compared to sumOf.

Terms err( sum ) err( preciseSum ) %
10 0.00000000003 0.00000000003 100.0%
100 0.0000000008 0.00000000002 3.03%
1,000 0.000000001 0.0000000001 9.57%
10,000 0.00000002 0.0000000007 3.57%
100,000 0.0000005 0.000000004 0.77%
1,000,000 0.000009 0.000000003 0.03%

% is err(preciseSum) / err(sum)

Most of the functions use "second-order iterative Kahanโ€“Babuลกka algorithm" by Klein (2005) .

Install

settings.gradle.kts

sourceControl {
    gitRepository(java.net.URI("https://github.com/rtmigo/precise_kt.git")) {
        producesModule("io.github.rtmigo:precise")
    }
}

build.gradle.kts

dependencies {
    implementation("io.github.rtmigo:precise") {
        version { branch = "staging" }
    }
}

Import in Kotlin code:

import io.github.rtmigo.precise.*  // Kotlin

Lambda functions

val sequence = listOf(1, 2, 3)

// sum
sequence.preciseSumOf { it * 0.1 }  // equals 0.6

// arithmetic mean
sequence.preciseMeanOf { it * 0.1 }  // equals 0.2

// standard deviation and mean
val (stdev, mean) = sequence.preciseStdevMean { it * 0.1 }

Running sum

Running sum, immutable version:

var sum = PreciseSum(5.0)  // 5.0 is optional starting value

sum += 0.1
sum += listOf(0.2, 0.3)
println(sum.value)  // 5.6

sum -= 0.2
println(sum.value)  // 5.4

Running sum, mutable version (faster):

val sum = MutablePreciseSum(5.0)  // 5.0 is optional starting value

sum.add(0.1)
sum.add(listOf(0.2, 0.3))
println(sum.value)  // 5.6

sum.add(-0.2)
println(sum.value)  // 5.4

Benchmarks

An alternative to compensated summation is to use BigDecimal: there is no error when summing them. However, even in the case of a pre-generated array, BigDecimals are 5-10 times slower.

Method Time
List<Double>.sumOf (naive) 17 ms
List<Double>.preciseSumOf 48 ms
MutablePreciseSum 50 ms
PreciseSum (immutable) 75 ms
List<BigDecimal>.sumOf 501 ms
List<Double>.sumOf { it.toBigDecimal() } 3192 ms

Other functions

kahanSumOf implements Kahan compensated summation algorithm in its traditional form. The accuracy is worse than preciseSumOf, but better than the naive sum.

val sequence = listOf(1, 2, 3)
sequence.kahanSumOf { it * 0.1 }  // 0.6

cascadeSumOf performs pairwise summation. The accuracy is worse than preciseSumOf, but better than the naive sum.

val sequence = listOf(1, 2, 3)
sequence.cascadeSumOf { it * 0.1 }  // 0.6

welfordMeanOf calculates the arithmetic mean, avoiding overflow when summing too large values.

val sequence = listOf(1, 2, 3)
println(sequence.welfordMeanOf { it * 0.1 })  // 0.3
You might also like...
Port immer for kotlin/jvm
Port immer for kotlin/jvm

1. kimmer Port https://github.com/immerjs/immer for kotlin/jvm (requires kotlin 1.5+). Immer is the winner of the "Breakthrough of the year" React ope

Blog implemented via the Storyblok Kotlin Multiplatform SDK (Android, JVM)
Blog implemented via the Storyblok Kotlin Multiplatform SDK (Android, JVM)

storyblok-mp-SDK-blog ... a showcase of using the Storyblok Kotlin Multiplatform Client to build a blog application (Android, JVM) What's included ๐Ÿš€

A property/method accessor library for the JVM, written in Kotlin

unlok - unlock your JVM a property/method accessor library for the JVM, written in Kotlin. how to import you can import unlok from maven central just

 A Kotlin library providing a simple, high-performance way to use off-heap native memory in JVM applications.
A Kotlin library providing a simple, high-performance way to use off-heap native memory in JVM applications.

native_memory_allocator A library which uses sun.misc.Unsafe to allocate off-heap native memory. Motivation The goal of this project is to provide a s

๐ŸŽ‘ Up to date IANA timezone database library for Kotlin (JVM, JS, Native)

๐ŸŽ‘ IANA Timezone Library for Kotlin Multiplatform Up to date IANA timezone database library for Kotlin (JVM, JS, Native) Usage import org.noelware.ian

Lightweight compiler plugin intended for Kotlin/JVM library development and symbol visibility control.

Restrikt A Kotlin/JVM compiler plugin to restrict symbols access, from external project sources. This plugin offers two ways to hide symbols: An autom

BlurHash support for iOS, Android and JVM via Kotlin Multiplatform
BlurHash support for iOS, Android and JVM via Kotlin Multiplatform

blurhash A Kotlin Multiplatform library to use blurhash in your Android App, iOS / Mac App & JVM Backend. Android iOS JVM Why? If you've tried using b

Kotlin/JVM Lambda example

kotlin-lambda-example-jvm Kotlin Lambda example on JVM contains four CDK stacks of Kotlin Lambda on JVM: JVM on x86, JVM on Arm64, JVM with C1 compile

๐Ÿ”ด A non-deterministic finite-state machine for Android & JVM that won't let you down
๐Ÿ”ด A non-deterministic finite-state machine for Android & JVM that won't let you down

HAL is a non-deterministic finite-state machine for Android & JVM built with Coroutines StateFlow and LiveData. Why non-deterministic? Because in a no

Owner
Artั‘m IG
Artั‘m IG
Double Open license classification for OSS Review Toolkit (ORT) and other uses.

Double Open Policy Configuration This repository is used to maintain the license classification (license-classifications.yml) created by Double Open.

Double Open 8 Nov 7, 2022
Android library to calculate sun phases like golden hour, blue hour, sunrise, sunset etc

Solarized This is Android library to calculate sun phases like golden hour, blue hour, sunrise, sunset etc Features first / last light golden hour blu

null 8 Nov 29, 2022
ListUtil.kt ListUtils - Advance list functions from kotlin standard library

ListUtils - Advance list functions from kotlin standard library A set of utility functions to work with lists in a robust way. It is based on a patter

Zain Ul Hassan 1 May 6, 2022
Godot's AdMob Plugin for Android (3.2.2+) using GitHub Actions for CI/CD. Working on Standard and Mono Godot versions.

Godot AdMob Android A Godot's plugin for Android of AdMob. About โ€ข Installation โ€ข Docs โ€ข Downloads About This repository is for a Godot Engine Plugin

Poing Studios 148 Jan 8, 2023
KotlinX Serialization Standard Serializers (KS3)

KotlinX Serialization Standard Serializers (KS3) This project aims to provide a set of serializers for common types. โš ๏ธ Consider this project to be Al

Emil Kantis 3 Nov 5, 2022
Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

Cash App 1.5k Dec 30, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Practical Kotlin Multiplatform on the Web ๋ณธ ์ €์žฅ์†Œ๋Š” ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ๊ธฐ๋ฐ˜ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์›Œํฌ์ˆ(๊ฐ•์ขŒ)์„ ์œ„ํ•ด ์ž‘์„ฑ๋œ ํ…œํ”Œ๋ฆฟ ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์›Œํฌ์ˆ ๊ณผ์ •์—์„œ ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋ก ํŠธ์—”๋“œ(front-end)๋Š” Ko

SpringRunner 14 Nov 5, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Building a Full Stack Web App with Kotlin Multiplatform ๋ณธ ์ €์žฅ์†Œ๋Š” INFCON 2022์—์„œ ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ๊ธฐ๋ฐ˜ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•ธ์ฆˆ์˜จ๋žฉ์„ ์œ„ํ•ด ์ž‘์„ฑ๋œ ํ…œํ”Œ๋ฆฟ ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ํ•ธ์ฆˆ์˜จ ๊ณผ์ •์—์„œ ์ฝ”ํ‹€๋ฆฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ์„

Arawn Park 19 Sep 8, 2022
๐ŸšŸ Lightweight, and simple scheduling library made for Kotlin (JVM)

Haru ?? Lightweight, and simple scheduling library made for Kotlin (JVM) Why did you build this? I built this library as a personal usage library to h

Noel 13 Dec 16, 2022
Yaspeller-kt - Asynchronous Yandex.Speller API wrapper for Kotlin/JVM.

yaspeller-kt Asynchronous Yandex.Speller API wrapper for Kotlin/JVM. Installation repositories { maven { url 'https://jitpack.io' }

Mikhail Koshkin 6 Jun 27, 2022