Skip to content

Statistics

concrete_fhe_toolkit.ml.stats

Deprecated location: these helpers moved to concrete_fhe_toolkit.stats.

This module re-exports them for backwards compatibility.

array_count_greater

array_count_greater(array: Union[ndarray, List[Any]], threshold: Any) -> Any

Count how many elements are strictly greater than a threshold.

Parameters:

Name Type Description Default
array Union[ndarray, List[Any]]

The encrypted array or list of elements.

required
threshold Any

The value to compare each element against.

required

Returns:

Name Type Description
Any Any

The count of elements that are strictly greater than the threshold.

Example
from concrete_fhe_toolkit.stats import array_count_greater

print(array_count_greater([1, 5, 3], threshold=2))  # 2
Source code in src/concrete_fhe_toolkit/stats.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def array_count_greater(array: Union[np.ndarray, List[Any]], threshold: Any) -> Any:
    """Count how many elements are strictly greater than a threshold.

    Args:
        array (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.
        threshold (Any): The value to compare each element against.

    Returns:
        Any: The count of elements that are strictly greater than the threshold.

    Example:
        ```python
        from concrete_fhe_toolkit.stats import array_count_greater

        print(array_count_greater([1, 5, 3], threshold=2))  # 2
        ```
    """
    return np.sum(_ensure_tensor(array) > threshold)

array_max

array_max(elements: Union[ndarray, List[Any]]) -> Any

Find the maximum value in an encrypted array using a tournament reduction.

Parameters:

Name Type Description Default
elements Union[ndarray, List[Any]]

The encrypted array or list of elements.

required

Returns:

Name Type Description
Any Any

The maximum value found in the array.

Example
from concrete_fhe_toolkit.stats import array_max

print(array_max([1, 5, 3]))  # 5
Source code in src/concrete_fhe_toolkit/stats.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def array_max(elements: Union[np.ndarray, List[Any]]) -> Any:
    """Find the maximum value in an encrypted array using a tournament reduction.

    Args:
        elements (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.

    Returns:
        Any: The maximum value found in the array.

    Example:
        ```python
        from concrete_fhe_toolkit.stats import array_max

        print(array_max([1, 5, 3]))  # 5
        ```
    """
    max_item = np.max(_ensure_tensor(elements))
    return max_item

array_mean

array_mean(array: Union[ndarray, List[Any]]) -> Any

Calculate the floor mean of an encrypted array.

Parameters:

Name Type Description Default
array Union[ndarray, List[Any]]

The encrypted array or list of elements.

required

Returns:

Name Type Description
Any Any

The integer floor mean of the elements.

Example
from concrete_fhe_toolkit.stats import array_mean

print(array_mean([2, 4, 6]))  # 4
Source code in src/concrete_fhe_toolkit/stats.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def array_mean(array: Union[np.ndarray, List[Any]]) -> Any:
    """Calculate the floor mean of an encrypted array.

    Args:
        array (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.

    Returns:
        Any: The integer floor mean of the elements.

    Example:
        ```python
        from concrete_fhe_toolkit.stats import array_mean

        print(array_mean([2, 4, 6]))  # 4
        ```
    """
    return array_sum(array) // len(array)

array_min

array_min(elements: Union[ndarray, List[Any]]) -> Any

Find the minimum value in an encrypted array using a tournament reduction.

Parameters:

Name Type Description Default
elements Union[ndarray, List[Any]]

The encrypted array or list of elements.

required

Returns:

Name Type Description
Any Any

The minimum value found in the array.

Example
from concrete_fhe_toolkit.stats import array_min

print(array_min([1, 5, 3]))  # 1
Source code in src/concrete_fhe_toolkit/stats.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def array_min(elements: Union[np.ndarray, List[Any]]) -> Any:
    """Find the minimum value in an encrypted array using a tournament reduction.

    Args:
        elements (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.

    Returns:
        Any: The minimum value found in the array.

    Example:
        ```python
        from concrete_fhe_toolkit.stats import array_min

        print(array_min([1, 5, 3]))  # 1
        ```
    """
    min_item = np.min(_ensure_tensor(elements))
    return min_item

array_range

array_range(array: Union[ndarray, List[Any]]) -> Any

Calculate the range (max - min) of an encrypted array.

Parameters:

Name Type Description Default
array Union[ndarray, List[Any]]

The encrypted array or list of elements.

required

Returns:

Name Type Description
Any Any

The difference between the maximum and minimum values in the array.

Example
from concrete_fhe_toolkit.stats import array_range

print(array_range([1, 5, 3]))  # 4
Source code in src/concrete_fhe_toolkit/stats.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def array_range(array: Union[np.ndarray, List[Any]]) -> Any:
    """Calculate the range (max - min) of an encrypted array.

    Args:
        array (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.

    Returns:
        Any: The difference between the maximum and minimum values in the array.

    Example:
        ```python
        from concrete_fhe_toolkit.stats import array_range

        print(array_range([1, 5, 3]))  # 4
        ```
    """
    return array_max(array) - array_min(array)

array_variance

array_variance(array: Union[ndarray, List[Any]]) -> Any

Calculate the floor variance of an encrypted array.

Parameters:

Name Type Description Default
array Union[ndarray, List[Any]]

The encrypted array or list of elements.

required

Returns:

Name Type Description
Any Any

The integer floor variance of the elements.

Example
from concrete_fhe_toolkit.stats import array_variance

print(array_variance([2, 4, 6]))  # 2
Source code in src/concrete_fhe_toolkit/stats.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def array_variance(array: Union[np.ndarray, List[Any]]) -> Any:
    """Calculate the floor variance of an encrypted array.

    Args:
        array (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.

    Returns:
        Any: The integer floor variance of the elements.

    Example:
        ```python
        from concrete_fhe_toolkit.stats import array_variance

        print(array_variance([2, 4, 6]))  # 2
        ```
    """
    tensor = _ensure_tensor(array)
    mean_value = array_mean(tensor)
    diffs = tensor - mean_value
    diff_sum = np.sum(diffs * diffs)
    return diff_sum // len(array)