# SlidingPercentile

# SlidingPercentile

Artifact:[androidx.media3:media3-exoplayer](https://developer.android.com/jetpack/androidx/releases/media3)  
[View Source](https://cs.android.com/search?q=file:androidx/media3/exoplayer/upstream/SlidingPercentile.java+class:androidx.media3.exoplayer.upstream.SlidingPercentile)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/exoplayer/upstream/SlidingPercentile "View this page in Java")

<br />

```
@UnstableApi
class SlidingPercentile
```

<br />

*** ** * ** ***

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:

- [Moving average](http://en.wikipedia.org/wiki/Moving_average)
- [Selection algorithm](http://en.wikipedia.org/wiki/Selection_algorithm)

## Summary

|                                                                                                           ### Public constructors                                                                                                           |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [SlidingPercentile](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/SlidingPercentile#SlidingPercentile(int))`(maxWeight: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` |

|                              ### Public functions                               |
|---------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)   | [addSample](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/SlidingPercentile#addSample(int,float))`(weight: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, value: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Adds a new weighted value. |
| [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | [getPercentile](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/SlidingPercentile#getPercentile(float))`(percentile: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Computes a percentile by integration.                                                                    |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)   | [reset](https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/upstream/SlidingPercentile#reset())`()` Resets the sliding percentile.                                                                                                                                                                                             |

## Public constructors

### SlidingPercentile

```
SlidingPercentile(maxWeight: Int)
```  

|                                        Parameters                                        |
|------------------------------------------------------------------------------------------|---------------------|
| `maxWeight: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The maximum weight. |

## Public functions

### addSample

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

Adds a new weighted value.  

|                                        Parameters                                        |
|------------------------------------------------------------------------------------------|------------------------------------|
| `weight: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)    | The weight of the new observation. |
| `value: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The value of the new observation.  |

### getPercentile

```
fun getPercentile(percentile: Float): Float
```

Computes a percentile by integration.  

|                                          Parameters                                           |
|-----------------------------------------------------------------------------------------------|----------------------------------------------------------------------|
| `percentile: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The desired percentile, expressed as a fraction in the range (0,1\]. |

|                                     Returns                                     |
|---------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The requested percentile value or[NaN](https://developer.android.com/reference/java/lang/Float.html#NaN())if no samples have been added. |

### reset

```
fun reset(): Unit
```

Resets the sliding percentile.