Coverage for src/foapy/characteristics/ma/_geometric_mean.py: 100%
3 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 values of the intervals grouped by congeneric sequence.
8 $$
9 \\left[ \\Delta_{g_j} \\right]_{1 \\le j \\le m} =
10 \\left[
11 \\left( \\prod_{i=1}^{n_j} \\Delta_{ij} \\right)^{1/n_j}
12 \\right]_{1 \\le j \\le m}
13 $$
15 where \\( \\Delta_{ij} \\) represents $i$-th interval of $j$-th
16 congeneric intervals array, \\( n_j \\) is the total
17 number of intervals in $j$-th congeneric intervals array
18 and $m$ is number of congeneric intervals arrays.
20 Parameters
21 ----------
22 intervals : array_like
23 An array of congeneric intervals array
24 dtype : dtype, optional
25 The dtype of the output
27 Returns
28 -------
29 : array
30 An array of the geometric means of congeneric intervals.
32 Examples
33 --------
35 Calculate the geometric means of a sequence.
37 ``` py linenums="1"
38 import foapy
39 import numpy as np
41 source = np.array(['a', 'b', 'a', 'c', 'a', 'd'])
42 order = foapy.ma.order(source)
43 intervals = foapy.ma.intervals(order, foapy.binding.start, foapy.mode.normal)
44 result = foapy.characteristics.ma.geometric_mean(intervals)
45 print(result)
46 # [1.58740105 2. 4. 6. ]
47 ```
49 Calculate the arithmetic means of congeneric intervals of a sequence.
51 ``` py linenums="1"
52 import foapy
54 X = []
55 X.append([1, 1, 4, 4])
56 X.append([3, 1, 3])
57 X.append([5, 3, 1])
59 result = foapy.characteristics.ma.geometric_mean(X)
60 print(result)
61 # [2. 2.08008382 2.46621207]
62 ```
63 """ # noqa: W605
64 return np.asanyarray(
65 [
66 (
67 np.power(
68 2,
69 np.sum(np.log2(line, dtype=dtype), dtype=dtype) / len(line),
70 dtype=dtype,
71 ) # noqa: E501 E261
72 if len(line) != 0
73 else 0
74 )
75 for line in intervals
76 ]
77 )