Coverage for src/foapy/characteristics/ma/_periodicity.py: 100%
6 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 periodicity(intervals, dtype=None):
5 """
6 Calculates periodicity of the intervals grouped by congeneric sequence.
8 $$
9 \\left[ \\tau_j \\right]_{1 \\le j \\le m} =
10 \\left[
11 \\left( \\prod_{i=1}^{n_j} \\Delta_{ij} \\right)^{1/n_j} *
12 \\frac{ n_j }{ \\sum_{i=1}^{n_j} \\Delta_{ij} }
13 \\right]_{1 \\le j \\le m}
14 $$
16 where \\( \\Delta_{ij} \\) represents $i$-th interval of $j$-th
17 congeneric intervals array, \\( n_j \\) is the total
18 number of intervals in $j$-th congeneric intervals array
19 and $m$ is number of congeneric intervals arrays.
21 Parameters
22 ----------
23 intervals : array_like
24 An array of congeneric intervals array
25 dtype : dtype, optional
26 The dtype of the output
28 Returns
29 -------
30 : array
31 An array of the periodicity of congeneric intervals.
33 Examples
34 --------
36 Calculate the periodicity of a sequence.
38 ``` py linenums="1"
39 import foapy
40 import numpy as np
42 source = np.array(['a', 'b', 'a', 'c', 'a', 'd'])
43 order = foapy.ma.order(source)
44 intervals = foapy.ma.intervals(order, foapy.binding.start, foapy.mode.normal)
45 result = foapy.characteristics.ma.periodicity(intervals)
46 print(result)
47 # [0.95244063 1. 1. 1. ]
48 ```
50 Calculate the periodicity of congeneric intervals of a sequence.
52 ``` py linenums="1"
53 import foapy
55 X = []
56 X.append([1, 1, 4, 4])
57 X.append([3, 1, 3])
58 X.append([5, 3, 1])
60 result = foapy.characteristics.ma.periodicity(X)
61 print(result)
62 # [0.8 0.8914645 0.82207069]
63 ```
64 """ # noqa: W605
66 from foapy.characteristics.ma import arithmetic_mean, geometric_mean
68 geometric_mean_seq = geometric_mean(intervals, dtype=dtype)
69 arithmetic_mean_seq = arithmetic_mean(intervals, dtype=dtype)
70 return np.divide(
71 geometric_mean_seq,
72 arithmetic_mean_seq,
73 where=arithmetic_mean_seq != 0.0,
74 dtype=dtype,
75 )