SlidingPercentile


@UnstableApi
class SlidingPercentile


Calculate any percentile over a sliding window of weighted values. A maximum weight is configured. Once the total weight of the values reaches the maximum weight, the oldest value is reduced in weight until it reaches zero and is removed. This maintains a constant total weight, equal to the maximum allowed, at the steady state.

This class can be used for bandwidth estimation based on a sliding window of past transfer rate observations. This is an alternative to sliding mean and exponential averaging which suffer from susceptibility to outliers and slow adaptation to step functions.

See the following Wikipedia articles:

Summary

Public constructors

SlidingPercentile(maxWeight: Int)

Public functions

Unit
addSample(weight: Int, value: Float)

Adds a new weighted value.

Float
getPercentile(percentile: Float)

Computes a percentile by integration.

Unit

Resets the sliding percentile.

Public constructors

SlidingPercentile

SlidingPercentile(maxWeight: Int)
Parameters
maxWeight: Int

The maximum weight.

Public functions

addSample

fun addSample(weight: Int, value: Float): Unit

Adds a new weighted value.

Parameters
weight: Int

The weight of the new observation.

value: Float

The value of the new observation.

getPercentile

fun getPercentile(percentile: Float): Float

Computes a percentile by integration.

Parameters
percentile: Float

The desired percentile, expressed as a fraction in the range (0,1].

Returns
Float

The requested percentile value or NaN if no samples have been added.

reset

fun reset(): Unit

Resets the sliding percentile.