Coverage for src/foapy/characteristics/_geometric_mean.py: 100%
7 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-17 20:45 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-17 20:45 +0000
1import numpy as np
4def geometric_mean(intervals, dtype=None):
5 """
6 Calculates average geometric value of intervals lengths.
8 $$ \\Delta_g=\\sqrt[n]{\\prod_{i=1}^{n} \\Delta_{i}}$$
10 where \\( \\Delta_{i} \\) represents each interval and \\( n \\)
11 is the total number of intervals.
13 Parameters
14 ----------
15 intervals : array_like
16 An array of intervals
17 dtype : dtype, optional
18 The dtype of the output
20 Returns
21 -------
22 : float
23 The geometric mean of the input array of intervals.
25 Examples
26 --------
28 Calculate the geometric mean of intervals of a sequence.
30 ``` py linenums="1"
31 import foapy
32 import numpy as np
34 source = ['a', 'b', 'a', 'c', 'a', 'd']
35 intervals = foapy.intervals(source, foapy.binding.start, foapy.mode.normal)
36 result = foapy.characteristics.geometric_mean(intervals)
37 print(result)
38 # 2.4018739103520055
40 # Improve precision by specifying a dtype.
41 result = foapy.characteristics.geometric_mean(intervals, dtype=np.longdouble)
42 print(result)
43 # 2.4018739103520053365
44 ```
45 """
46 n = len(intervals)
48 # Check for an empty list or a list with zeros
49 if n == 0 or all(x == 0 for x in intervals):
50 return 0
52 from foapy.characteristics import depth
54 return np.power(2, depth(intervals, dtype=dtype) / n, dtype=dtype)