Coverage for src/foapy/characteristics/ma/_arithmetic_mean.py: 100%

3 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-17 20:45 +0000

1import numpy as np 

2 

3 

4def arithmetic_mean(intervals, dtype=None): 

5 """ 

6 Calculates average arithmetic value of intervals lengths 

7 grouped by congeneric sequence. 

8 

9 $$ 

10 \\left[ \\Delta_{a_j} \\right]_{1 \\le j \\le m} = 

11 \\left[ \\frac{1}{n_j} * \\sum_{i=1}^{n_j} \\Delta_{ij} \\right]_{1 \\le j \\le m} 

12 $$ 

13 

14 where \\( \\Delta_{ij} \\) represents $i$-th interval of $j$-th 

15 congeneric intervals array, \\( n_j \\) is the total 

16 number of intervals in $j$-th congeneric intervals array 

17 and $m$ is number of congeneric intervals arrays. 

18 

19 Parameters 

20 ---------- 

21 intervals : array_like 

22 An array of congeneric intervals array 

23 dtype : dtype, optional 

24 The dtype of the output 

25 

26 Returns 

27 ------- 

28 : array 

29 An array of the arithmetic means of congeneric intervals. 

30 

31 Examples 

32 -------- 

33 

34 Calculate the arithmetic means of a sequence. 

35 

36 ``` py linenums="1" 

37 import foapy 

38 import numpy as np 

39 

40 source = np.array(['a', 'b', 'a', 'c', 'a', 'd']) 

41 order = foapy.ma.order(source) 

42 intervals = foapy.ma.intervals(order, foapy.binding.start, foapy.mode.normal) 

43 result = foapy.characteristics.ma.arithmetic_mean(intervals) 

44 print(result) 

45 # [1.66666667 2 4 6] 

46 ``` 

47 

48 Calculate the arithmetic means of congeneric intervals of a sequence. 

49 

50 ``` py linenums="1" 

51 import foapy 

52 

53 X = [] 

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

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

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

57 

58 result = foapy.characteristics.ma.arithmetic_mean(X) 

59 print(result) 

60 # [2.5 2.333 3] 

61 ``` 

62 """ # noqa: W605 

63 

64 return np.asanyarray( 

65 [ 

66 np.sum(line, dtype=dtype) / len(line) if len(line) != 0 else 0 

67 for line in intervals 

68 ] 

69 )