Descriptive Statistics#

Numpy offers many statistical functions, but if you want to obtain several statistical variables from the same array, it’s necessary to process the data several times to calculate the various parameters. This example shows how to use the DescriptiveStatistics class to obtain several statistical variables with a single calculation. Also, the calculation algorithm is incremental and is more numerically stable.

Note

Pébay, P., Terriberry, T.B., Kolla, H. et al. Numerically stable, scalable formulas for parallel and online computation of higher-order multivariate central moments with arbitrary weights. Comput Stat 31, 1305–1325, 2016, https://doi.org/10.1007/s00180-015-0637-z

import dask.array
import numpy

import pyinterp

Create a random array

Create a DescriptiveStatistics object.

ds = pyinterp.DescriptiveStatistics(values)

The constructor will calculate the statistical variables on the provided data. The calculated variables are stored in the instance and can be accessed using different methods:

  • mean

  • var

  • std

  • skewness

  • kurtosis

  • min

  • max

  • sum

  • sum_of_weights

  • count

ds.count()
array([384], dtype=uint64)
ds.mean()
array([0.50151422])

It’s possible to get a structured numpy array containing the different statistical variables calculated.

ds.array()
array([(384, -1.17292331, 0.99964469, 0.50151422, 0.00674238, 0.00654089, 384., 192.58146049, 0.07907892)],
      dtype=[('count', '<u8'), ('kurtosis', '<f8'), ('max', '<f8'), ('mean', '<f8'), ('min', '<f8'), ('skewness', '<f8'), ('sum_of_weights', '<f8'), ('sum', '<f8'), ('var', '<f8')])

Like numpy, it’s possible to compute statistics along axis.

ds = pyinterp.DescriptiveStatistics(values, axis=(1, 2))
ds.mean()
array([[0.55284495, 0.50918919, 0.58101145, 0.54275717, 0.54968826,
        0.52358159, 0.47940404, 0.48739702],
       [0.51255507, 0.38284679, 0.42390928, 0.49230764, 0.43535806,
        0.49225299, 0.65549639, 0.40362762]])

The class can also process a dask array. In this case, the call to the constructor triggers the calculation.

ds = pyinterp.DescriptiveStatistics(dask.array.from_array(values,
                                                          chunks=(2, 2, 2, 2)),
                                    axis=(1, 2))
ds.mean()
array([[0.55284495, 0.50918919, 0.58101145, 0.54275717, 0.54968826,
        0.52358159, 0.47940404, 0.48739702],
       [0.51255507, 0.38284679, 0.42390928, 0.49230764, 0.43535806,
        0.49225299, 0.65549639, 0.40362762]])

Finally, it’s possible to calculate weighted statistics.

weights = numpy.random.random_sample((2, 4, 6, 8))
ds = pyinterp.DescriptiveStatistics(values, weights=weights, axis=(1, 2))
ds.mean()
array([[0.55204361, 0.48518751, 0.57168112, 0.59331051, 0.57745313,
        0.52974562, 0.45314877, 0.48891758],
       [0.51717951, 0.4383929 , 0.43142146, 0.46124402, 0.4340834 ,
        0.48024679, 0.64057294, 0.39312551]])

Total running time of the script: (0 minutes 0.193 seconds)

Gallery generated by Sphinx-Gallery