Find harmonic mean of a distribution using Python

Harmonic Mean of a distribution:

Harmonic Mean is the reciprocal of mean of reciprocal values in the distribution.

Harmonic Mean of the distribution is given by the formula

X H = n / (1/Xi)

when Xi > 0 for i = 1,2,3......n

Examples of Harmonic Mean:

- Cost Averaging

- Travelling a constant distance "d" by breaking the distance as

d1, d2,...dn, travelling each distance with a different velocity and finally

finding out a uniform velocity that would have required to travel the entire

distance with the same total time is nothing but finding the harmonic mean

of the velocities.

Method Name:

harmonic_mean(data)

Method Overview:

harmonic_mean() function from the statistics module of python standard library, returns the harmonic mean of the distribution which is also called as the subcontrary mean of the distribution.

Example Python Program to find the Harmonic Mean of a distribution:

 import statistics   # velocities of individual distances velocityData = [50,55,65,60]   # Find harmonic mean harmonicMean = statistics.harmonic_mean(velocityData)   #print the harmonic mean with a precision of upto 2 decimal points print("The harmonic mean of velocities is:%.2f"%(harmonicMean))

Output of the Python Program that finds the Harmonic Mean of a distribution:

 The harmonic mean of velocities is:56.95