FRsūtil̲s
A fuzzy-rough reasoning utilities library
 
Loading...
Searching...
No Matches
fuzzy_quantifiers.py
Go to the documentation of this file.
1import numpy as np
2
3def fuzzy_quantifier1(p, alpha, beta, increasing=True):
4 """
5 Compute the degree of membership to a fuzzy quantifier.
6
7 Parameters:
8 - p : float or np.array
9 Proportion(s) in the [0, 1] range.
10 - alpha : float
11 Lower threshold (start of transition).
12 - beta : float
13 Upper threshold (full membership).
14 - increasing : bool
15 True for quantifiers like "most", False for "few".
16
17 Returns:
18 - float or np.array
19 Degree(s) of truth for the fuzzy quantifier.
20 """
21 p = np.asarray(p)
22
23 if increasing:
24 # For quantifiers like "most"
25 return np.where(p <= alpha, 0,
26 np.where(p >= beta, 1,
27 (p - alpha) / (beta - alpha)))
28 else:
29 # For quantifiers like "few"
30 return np.where(p <= alpha, 1,
31 np.where(p >= beta, 0,
32 (beta - p) / (beta - alpha)))
33
34
35def fuzzy_quantifier_quad(x, alpha, beta):
36 """
37 Smooth parameterized fuzzy quantifier using quadratic transition.
38
39 Parameters:
40 - x: float or np.array
41 Input value(s), typically in [0, 1].
42 - alpha: float
43 Start of the transition (0 <= alpha < beta <= 1).
44 - beta: float
45 End of the transition.
46
47 Returns:
48 - float or np.array: Degree of membership.
49 """
50 x = np.asarray(x)
51 mid = (alpha + beta) / 2
52 denom = (beta - alpha) ** 2
53
54 result = np.zeros_like(x)
55
56 # Region 1: x <= alpha → Q = 0
57 mask1 = x <= alpha
58
59 # Region 2: alpha < x <= (alpha + beta)/2
60 mask2 = (x > alpha) & (x <= mid)
61 result[mask2] = 2 * ((x[mask2] - alpha) ** 2) / denom
62
63 # Region 3: (alpha + beta)/2 < x <= beta
64 mask3 = (x > mid) & (x <= beta)
65 result[mask3] = 1 - 2 * ((x[mask3] - beta) ** 2) / denom
66
67 # Region 4: x > beta → Q = 1
68 mask4 = x > beta
69 result[mask4] = 1
70
71 return result
fuzzy_quantifier1(p, alpha, beta, increasing=True)