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

1import numpy as np 

2 

3 

4def periodicity(intervals, dtype=None): 

5 """ 

6 Calculates periodicity of the intervals grouped by congeneric sequence. 

7 

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 $$ 

15 

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. 

20 

21 Parameters 

22 ---------- 

23 intervals : array_like 

24 An array of congeneric intervals array 

25 dtype : dtype, optional 

26 The dtype of the output 

27 

28 Returns 

29 ------- 

30 : array 

31 An array of the periodicity of congeneric intervals. 

32 

33 Examples 

34 -------- 

35 

36 Calculate the periodicity of a sequence. 

37 

38 ``` py linenums="1" 

39 import foapy 

40 import numpy as np 

41 

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 ``` 

49 

50 Calculate the periodicity of congeneric intervals of a sequence. 

51 

52 ``` py linenums="1" 

53 import foapy 

54 

55 X = [] 

56 X.append([1, 1, 4, 4]) 

57 X.append([3, 1, 3]) 

58 X.append([5, 3, 1]) 

59 

60 result = foapy.characteristics.ma.periodicity(X) 

61 print(result) 

62 # [0.8 0.8914645 0.82207069] 

63 ``` 

64 """ # noqa: W605 

65 

66 from foapy.characteristics.ma import arithmetic_mean, geometric_mean 

67 

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 )