Coverage for src/foapy/characteristics/_regularity.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 regularity(intervals, dtype=None):
5 """
6 Calculates regularity of intervals grouped by element of the alphabet.
8 $$r= \\sqrt[n]{\\prod_{j=1}^{m} \\frac{
9 \\prod_{j=1}^{n_j} \\Delta_{ij}}
10 {{\\left(\\frac{1}{n_j}\\sum_{i=1}^{n_j}{\\Delta_{ij}}\\right)^{n_j}}
11 }
12 }$$
14 where \\( m \\) is count of groups (alphabet power), \\( n_j \\) is count of intervals in group \\( j \\),
15 \\( \\Delta_{ij} \\) represents an interval at index \\( i \\) in group \\( j \\) and \\( n \\) is total count of intervals across all groups.
17 $$ n=\\sum_{j=1}^{m}{n_j} $$
19 Parameters
20 ----------
21 intervals_grouped : array_like
22 An array of intervals grouped by element
23 dtype : dtype, optional
24 The dtype of the output
26 Returns
27 -------
28 : float
29 The regularity of the input array of intervals_grouped.
31 Examples
32 --------
34 Calculate the regularity of intervals_grouped of a sequence.
36 ``` py linenums="1"
37 import foapy
38 import numpy as np
40 source = np.array(['a', 'b', 'a', 'c', 'a', 'd'])
41 order = foapy.ma.order(source)
42 print(order)
44 #[[0 -- 0 -- 0 --]
45 # [-- 1 -- -- -- --]
46 # [-- -- -- 2 -- --]
47 # [-- -- -- -- -- 3]]
49 intervals_grouped = foapy.ma.intervals(order, foapy.binding.start, foapy.mode.normal)
51 print(intervals_grouped)
52 # [
53 # array([1, 2, 2]),
54 # array([2]),
55 # array([4]),
56 # array([6])
57 # ]
59 # m = 4
60 # n_0 = 3
61 # n_1 = 1
62 # n_2 = 1
63 # n_3 = 1
64 # n = 6
66 result = foapy.characteristics.regularity(intervals_grouped)
67 print(result)
68 # 0.9759306487558016
70 # Improve precision by specifying a dtype.
71 result = foapy.characteristics.regularity(intervals_grouped, dtype=np.longdouble)
72 print(result)
73 # 0.97593064875580153104
74 ```
75 """ # noqa: E501
76 from foapy.characteristics import descriptive_information, geometric_mean
78 total_elements = np.concatenate(intervals)
79 g = geometric_mean(total_elements, dtype=dtype)
80 D = descriptive_information(intervals, dtype=dtype)
81 return g / D