aggregationslib 0.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.2
2
+ Name: aggregationslib
3
+ Version: 0.0.0
4
+ Summary: Python implementation of Arithmetic, quasi arithmetic and other aggregating functions
5
+ Home-page: https://github.com/wgalka/Means
6
+ Download-URL: https://github.com/wgalka/Means/archive/refs/tags/v0.0.3.tar.gz
7
+ Author: wgalka
8
+ License: TO DOO
9
+ Classifier: Programming Language :: Python :: 3.13
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.13
13
+ Description-Content-Type: text/markdown
14
+ Requires-Dist: numpy>=2.2.2
15
+ Requires-Dist: pynverse
16
+ Provides-Extra: test
17
+ Requires-Dist: pytest>=8.3.4; extra == "test"
18
+ Dynamic: author
19
+ Dynamic: classifier
20
+ Dynamic: description
21
+ Dynamic: description-content-type
22
+ Dynamic: download-url
23
+ Dynamic: home-page
24
+ Dynamic: license
25
+ Dynamic: provides-extra
26
+ Dynamic: requires-dist
27
+ Dynamic: requires-python
28
+ Dynamic: summary
29
+
30
+ # Means
31
+ Means, Aggregation functions...
32
+
33
+ Aggregations functions are:
34
+ - Conjuctive - the final aggregated value will always be influenced by the smallest value among the inputs.
35
+ - Disjnuctive - the aggregated value will always be influenced by the largest value among the inputs.
36
+
37
+ #### Example 1:
38
+
39
+ ```pycon
40
+ # example data
41
+ data = [0.2, 0.6, 0.7]
42
+ # configure function parameters
43
+ func1 = A_amn(p=0.5)
44
+ # use aggregation funciton
45
+ print(func1(data))
46
+
47
+ # Combine two aggregations - arithmetic mean and minimum
48
+ func2 = Combine2Aggregations(A_ar(), min)
49
+ # use combination of aggregation funciton
50
+ print(func2(data))
51
+ ```
52
+
53
+ #### Example2:
54
+ To get information about aggregation function you can use `__str__()` or '__repr__()' methods.
55
+
56
+ ```pycon
57
+ func1 = A_amn(p=0.5)
58
+ print(func1)
59
+ >>>A_amn(0.5)
60
+
61
+ func2 = Combine2Aggregations(A_ar(), A_md())
62
+ print(func2)
63
+ >>>A_armd
64
+
65
+ func3 = Combine2Aggregations(A_ar(), A_pw(r=3))
66
+ print(func3.__repr__()) # function parameters are printed in order: func1, func2
67
+ >>>A_arpw(r=3)
68
+ ```
69
+
70
+ `exponential(y, r=1)` is given by equation
71
+
72
+ $$
73
+ A_6^{(r)}(x_1,...,x_n)= \frac{1}{r}\ln
74
+ \Big(\frac{1}{n} \sum \limits_{k=1}^{n} e^{rx_k}\Big), where
75
+ r \in \mathbb{R}, r \neq 0
76
+ $$
77
+
78
+
79
+ # A_ar - Arithmetic mean
80
+
81
+
82
+ # A_qd - Quadratic mean
83
+
84
+
85
+ # A_gm - Geometric mean
86
+
87
+
88
+ # A_hm - Harmonic mean
89
+
90
+
91
+ # A_pw - Power mean
92
+
93
+
94
+ # A_ex, A_ex2, A_ex3 - Exponential mean
95
+
96
+
97
+ # A_lm - Lehmer mean
98
+
99
+
100
+ # A_amn - Arithmetic minimum mean
101
+
102
+
103
+ # A_amx - Arithmetic maximum mean
104
+
105
+
106
+ # A_md - Median - ordered weighted aggregation
107
+
108
+
109
+ # A_ol - Olimpic aggregation
110
+
111
+ # A_oln - Olimpic aggregation
112
+ We can specify how many greatest and smallest records remove
113
+
114
+ # A_ln - Logaritmic aggregation
115
+
116
+ https://www.researchgate.net/publication/228886844_Logarithmic_mean_for_several_arguments
117
+
118
+ $$
119
+ L\left(x_1, x_2, \ldots, x_n\right)=(n-1) ! \sum_{i=1}^n \frac{x_i}{\prod_{\substack{j=1 \\ j \neq i}}^n \log \left(x_i / x_j\right)}
120
+ $$
121
+
122
+ # Combine2Aggregations - Combine aggregation functions
123
+ Amn, Amx, Aar , Aex , Amd,
124
+ Aow1, Aow1
@@ -0,0 +1,95 @@
1
+ # Means
2
+ Means, Aggregation functions...
3
+
4
+ Aggregations functions are:
5
+ - Conjuctive - the final aggregated value will always be influenced by the smallest value among the inputs.
6
+ - Disjnuctive - the aggregated value will always be influenced by the largest value among the inputs.
7
+
8
+ #### Example 1:
9
+
10
+ ```pycon
11
+ # example data
12
+ data = [0.2, 0.6, 0.7]
13
+ # configure function parameters
14
+ func1 = A_amn(p=0.5)
15
+ # use aggregation funciton
16
+ print(func1(data))
17
+
18
+ # Combine two aggregations - arithmetic mean and minimum
19
+ func2 = Combine2Aggregations(A_ar(), min)
20
+ # use combination of aggregation funciton
21
+ print(func2(data))
22
+ ```
23
+
24
+ #### Example2:
25
+ To get information about aggregation function you can use `__str__()` or '__repr__()' methods.
26
+
27
+ ```pycon
28
+ func1 = A_amn(p=0.5)
29
+ print(func1)
30
+ >>>A_amn(0.5)
31
+
32
+ func2 = Combine2Aggregations(A_ar(), A_md())
33
+ print(func2)
34
+ >>>A_armd
35
+
36
+ func3 = Combine2Aggregations(A_ar(), A_pw(r=3))
37
+ print(func3.__repr__()) # function parameters are printed in order: func1, func2
38
+ >>>A_arpw(r=3)
39
+ ```
40
+
41
+ `exponential(y, r=1)` is given by equation
42
+
43
+ $$
44
+ A_6^{(r)}(x_1,...,x_n)= \frac{1}{r}\ln
45
+ \Big(\frac{1}{n} \sum \limits_{k=1}^{n} e^{rx_k}\Big), where
46
+ r \in \mathbb{R}, r \neq 0
47
+ $$
48
+
49
+
50
+ # A_ar - Arithmetic mean
51
+
52
+
53
+ # A_qd - Quadratic mean
54
+
55
+
56
+ # A_gm - Geometric mean
57
+
58
+
59
+ # A_hm - Harmonic mean
60
+
61
+
62
+ # A_pw - Power mean
63
+
64
+
65
+ # A_ex, A_ex2, A_ex3 - Exponential mean
66
+
67
+
68
+ # A_lm - Lehmer mean
69
+
70
+
71
+ # A_amn - Arithmetic minimum mean
72
+
73
+
74
+ # A_amx - Arithmetic maximum mean
75
+
76
+
77
+ # A_md - Median - ordered weighted aggregation
78
+
79
+
80
+ # A_ol - Olimpic aggregation
81
+
82
+ # A_oln - Olimpic aggregation
83
+ We can specify how many greatest and smallest records remove
84
+
85
+ # A_ln - Logaritmic aggregation
86
+
87
+ https://www.researchgate.net/publication/228886844_Logarithmic_mean_for_several_arguments
88
+
89
+ $$
90
+ L\left(x_1, x_2, \ldots, x_n\right)=(n-1) ! \sum_{i=1}^n \frac{x_i}{\prod_{\substack{j=1 \\ j \neq i}}^n \log \left(x_i / x_j\right)}
91
+ $$
92
+
93
+ # Combine2Aggregations - Combine aggregation functions
94
+ Amn, Amx, Aar , Aex , Amd,
95
+ Aow1, Aow1
File without changes
@@ -0,0 +1,186 @@
1
+ import math
2
+ import warnings
3
+ from decimal import Decimal
4
+
5
+ import numpy as np
6
+ from pynverse.inverse import inversefunc
7
+
8
+
9
+ # 1 Arithmetic aggregation
10
+ def arithmetic(y):
11
+ """
12
+ Arithmetic aggregation
13
+ :param y: 1 dimensional array
14
+ :return: aggregation value
15
+ """
16
+ if np.ndim(y) != 1:
17
+ raise ValueError("y must be 1 dimensional array")
18
+ return np.mean(y)
19
+
20
+
21
+ # 2
22
+ def quadratic(y):
23
+ if np.ndim(y) != 1:
24
+ raise ValueError("y must be 1 dimensional array")
25
+ y = np.array(y)
26
+ size = len(y)
27
+ with np.errstate(divide='ignore'):
28
+ return np.sqrt(np.sum(y ** 2) / size)
29
+
30
+
31
+ # 3
32
+ def geometric(y):
33
+ if np.ndim(y) != 1:
34
+ raise ValueError("y must be 1 dimensional array")
35
+
36
+ size = len(y)
37
+ wk = 1 / size
38
+ with np.errstate(divide='ignore'):
39
+ return np.prod(y ** wk)
40
+
41
+
42
+ # 4
43
+ def harmonic(y):
44
+ if np.ndim(y) != 1:
45
+ raise ValueError("y must be 1 dimensional array")
46
+ if type(y).__module__ != np.__name__:
47
+ y = np.array(y)
48
+ size = len(y)
49
+ return size / (np.sum(1 / y))
50
+
51
+
52
+ # 5
53
+ def power(y, r=1):
54
+ if np.ndim(y) != 1:
55
+ raise ValueError("y must be 1 dimensional array")
56
+ if r == 0:
57
+ raise ValueError("parameter r cannot be equal to 0 ")
58
+ # If one 0 occur in array then mean is 0
59
+ if 0 in y and r < 0:
60
+ return 0
61
+ else:
62
+ result = (np.sum(y ** r) / y.shape[0]) ** (1 / r)
63
+ return result
64
+
65
+
66
+ # 6
67
+ def exponential(y, r=1):
68
+ if np.ndim(y) != 1:
69
+ raise ValueError("y must be 1 dimensional array")
70
+ if r == 0:
71
+ raise ValueError("parameter r should be != 0 ")
72
+ size = len(y)
73
+ y = np.array(y)
74
+ with np.errstate(divide='ignore'):
75
+ return (1 / r) * np.lib.scimath.log(np.sum(np.exp(y * r)) / size)
76
+
77
+
78
+ # 7
79
+ def lehmer(y, r=0):
80
+ if np.ndim(y) != 1:
81
+ raise ValueError("y must be 1 dimensional array")
82
+ # if array and r below 0 return 0. 0 to power of 0.
83
+ if 0 in y and r - 1 <= 0:
84
+ return 0
85
+ else:
86
+ r2 = (r - 1)
87
+ nominator = np.sum(y ** r)
88
+ with np.errstate(divide='ignore'): #
89
+ denominator = np.sum(y ** r2)
90
+ if denominator == 0:
91
+ return 0
92
+ else:
93
+ return nominator / denominator
94
+
95
+
96
+ # 8
97
+ def arithmetic_min(y, p=0):
98
+ if np.ndim(y) != 1:
99
+ raise ValueError("y must be 1 dimensional array")
100
+ min = np.min(y)
101
+ return p * np.mean(y) + (1 - p) * min
102
+
103
+
104
+ # 9
105
+ def arithmetic_max(y, p=0):
106
+ if np.ndim(y) != 1:
107
+ raise ValueError("y must be 1 dimensional array")
108
+ max = np.max(y)
109
+ return p * np.mean(y) + (1 - p) * max
110
+
111
+
112
+ # 10
113
+ def median(y):
114
+ if np.ndim(y) != 1:
115
+ raise ValueError("y must be 1 dimensional array")
116
+ return np.median(y)
117
+
118
+
119
+ # 11
120
+ def olimpic(y):
121
+ if np.ndim(y) != 1:
122
+ raise ValueError("y must be 1 dimensional array")
123
+ # b = np.max(a, 0) # max
124
+ # c = np.min(a, 0) # min
125
+ if len(y) < 3:
126
+ return np.mean(y)
127
+ else:
128
+ d = np.sort(y)[1:-1]
129
+ return np.mean(d)
130
+
131
+
132
+ def exponential2(y, p=0, q=0):
133
+ if np.ndim(y) != 1:
134
+ raise ValueError("y must be 1 dimensional array")
135
+ y = np.array(y)
136
+ mean = np.mean(p ** (q ** y))
137
+ firstlog = math.log(mean, p)
138
+ result = math.log(firstlog, q)
139
+ return result
140
+
141
+
142
+ def exponential3(y, p=0, q=0):
143
+ if np.ndim(y) != 1:
144
+ raise ValueError("y must be 1 dimensional array")
145
+ y = np.array(y)
146
+ result = (1 / q) * math.log(np.mean(p ** (q * y)), p)
147
+ return result
148
+
149
+
150
+ def sin_aggregation(y):
151
+ '''
152
+
153
+ :param y:
154
+ :return:
155
+ '''
156
+ # Check if 1 dimensional array
157
+ if np.ndim(y) != 1:
158
+ raise ValueError("y must be 1 dimensional array")
159
+ n = len(y)
160
+ return math.asin(np.sum(np.sin(y)) / n)
161
+
162
+
163
+ def quasi_arithmeric(y, function):
164
+ """
165
+ Quasi arithmetic mean.
166
+
167
+ If function f(x)=x to then quasi-arithmetic mean is aritmetic mean.
168
+ If function f(x)=x^p (p != 0) then quasi-arithmetic mean is power mean to p power.
169
+ If function f(x)=log x then quasi-arithmetic mean is geometric mean.
170
+
171
+ :param y: 1 dimmensional array
172
+ :param function: function which maps an interval I of the real line to the real numbers, and is both continuous and injective
173
+ :return: aggregation value
174
+ """
175
+ # Check if 1 dimensional array
176
+ if np.ndim(y) != 1:
177
+ raise ValueError("y must be 1 dimensional array")
178
+ n = len(y)
179
+ apply_func = np.array([function(x) for x in y])
180
+ value = np.sum(apply_func) / n
181
+ return inversefunc(function, value)
182
+
183
+
184
+ if __name__ == "__main__":
185
+ print(quasi_arithmeric.__str__())
186
+ print(arithmetic_min([0.2, 0.4, 0.6], 0))