Arithmetic, Geometric, and Harmonic Means are all ‘Pythagorean’.

See the wikipedia page on Pythagorean Means for more.

Arithmetic, Geometric, and Harmonic Mean Examples

Imports

import axle._
import axle.math._

Examples

Arithmetic mean

import spire.implicits.DoubleAlgebra
// import spire.implicits.DoubleAlgebra

arithmeticMean(List(2d, 3d, 4d, 5d))
// res0: Double = 3.5

Geometric mean

import spire.math.Real
// import spire.math.Real

geometricMean[Real, List[Real]](List(1d, 5d, 25d))
// res1: spire.math.Real = 5

Harmonic mean

harmonicMean(List(2d, 3d, 4d, 5d))
// res2: Double = 3.116883116883117

Generalized Mean

See the wikipedia page on Generalized Mean.

When the parameter p is 1, it is the arithmetic mean.

generalizedMean[Double, List[Double]](1d, List(2d, 3d, 4d, 5d))
// res3: Double = 3.5

As p approaches 0, it is the geometric mean.

generalizedMean[Double, List[Double]](0.0001, List(1d, 5d, 25d))
// res4: Double = 5.00043173370165

At -1 it is the harmonic mean.

generalizedMean[Double, List[Double]](-1d, List(2d, 3d, 4d, 5d))
// res5: Double = 3.116883116883117

Moving means

import spire.math._

Moving arithmetic mean

val xs = (1 to 100).toList.map(_.toDouble)
// xs: List[Double] = List(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0)

val window = 5
// window: Int = 5

val moved = movingArithmeticMean[List[Double], Int, Double, List[(Double, Double)]](xs, window)
// moved: List[Double] = List(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0)

Moving geometric mean

val xs: List[Real] = List(1d, 5d, 25d, 125d, 625d)
// xs: List[spire.math.Real] = List(1, 5, 25, 125, 625)

val window = 3
// window: Int = 3

val moved = movingGeometricMean[List[Real], Int, Real, List[(Real, Real)]](xs, window)
// moved: List[spire.math.Real] = List(5, 25, 125)

Moving harmonic mean

val xs: List[Real] = (1 to 5).toList.map(v => Real(v))
// xs: List[spire.math.Real] = List(1, 2, 3, 4, 5)

val window = 3
// window: Int = 3

val moved = movingHarmonicMean[List[Real], Int, Real, List[(Real, Real)]](xs, window)
// moved: List[spire.math.Real] = List(18/11, 36/13, 180/47)