Calculating the percentiles of a time series
In this last bonus section of this chapter, we are going to learn how to compute the percentiles of a time series or a list (and if you find the information presented here difficult to understand, feel free to skip it). The main usage of such information is to better understand your time series data.
A percentile is a score where a given percentage of scores in the frequency distribution falls. Therefore, the 20th percentile is the score below which 20% of the scores of the distribution of the values of a dataset falls.
A quartile is one of the following three percentiles – 25%, 50%, or 75%. So, we have the first quartile, the second quartile, and the third quartile, respectively.
Both percentiles and quartiles are calculated in datasets sorted in ascending order. Even if you have not sorted that dataset, the relevant NumPy function, which is called quantile()
, does that behind the scenes.
The Python code of percentiles.py
is as follows:
#!/usr/bin/env python3 import sys import pandas as pd import numpy as np def main(): if len(sys.argv) != 2: print("TS") sys.exit() F = sys.argv[1] ts = pd.read_csv(F, compression='gzip') ta = ts.to_numpy() ta = ta.reshape(len(ta)) per01 = round(np.quantile(ta, .01), 5) per25 = round(np.quantile(ta, .25), 5) per75 = round(np.quantile(ta, .75), 5) print("Percentile 1%:", per01, "Percentile 25%:", per25, "Percentile 75%:", per75) if __name__ == '__main__': main()
All the work is done by the quantile()
function of the NumPy package. Among other things, quantile()
appropriately arranges its elements before performing any calculations. We do not know what happens internally, but most likely, quantile()
sorts its input in ascending order.
The first parameter of quantile()
is the NumPy array, and its second parameter is the percentage (percentile) that interests us. A 25% percentage is equal to the first quantile, a 50% percentage is equal to the second quantile, and a 75% percentage is equal to the third quantile. A 1% percentage is equal to the 1% percentile, and so on.
The output of percentiles.py
is as follows:
$ ./percentiles.py ts1.gz Percentile 1%: 1.57925 Percentile 25%: 7.15484 Percentile 75%: 23.2298