statistics#


class MovAvg(size: int = 100)[source]#

Bases: object

Class for moving average.

It will automatically exclude the infinity and NaN. Usage:

>>> stat = MovAvg(size=66)
>>> stat.add(torch.tensor(5))
5.0
>>> stat.add(float('inf'))  # which will not add to stat
5.0
>>> stat.add([6, 7, 8])
6.5
>>> stat.get()
6.5
>>> print(f'{stat.mean():.2f}±{stat.std():.2f}')
6.50±1.12
add(data_array: Number | float | number | list | ndarray | Tensor) float[source]#

Add a scalar into MovAvg.

You can add torch.Tensor with only one element, a python scalar, or a list of python scalar.

get() float[source]#

Get the average.

mean() float[source]#

Get the average. Same as get().

std() float[source]#

Get the standard deviation.

class RunningMeanStd(mean: float | ndarray = 0.0, std: float | ndarray = 1.0, clip_max: float | None = 10.0, epsilon: float = 1.1920928955078125e-07)[source]#

Bases: object

Calculates the running mean and std of a data stream.

https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm

Parameters:
  • mean – the initial mean estimation for data array. Default to 0.

  • std – the initial standard error estimation for data array. Default to 1.

  • clip_max – the maximum absolute value for data array. Default to 10.0.

  • epsilon – To avoid division by zero.

norm(data_array: float | ndarray) float | ndarray[source]#
update(data_array: ndarray) None[source]#

Add a batch of item into RMS with the same shape, modify mean/var/count.