forked from usnistgov/SP800-90B_EntropyAssessment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mostCommonValue.py
49 lines (36 loc) · 1.29 KB
/
mostCommonValue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Most Common Value Functions
#
# NOTE: this software is made available with no guarantee - implied or otherwise -
# of correctness or completeness. See user guide for full disclaimer.
#
# Kerry McKay
# CSD/ITL/NIST
#
# February 2016
import math
from collections import Counter
# This method first finds the proportion p_hat of the most common value in the
# input dataset, and then constructs a confidence interval for this proportion.
# The upper bound of the confidence interval is used to estimate the min-entropy
# per sample of the source.
def most_common(s):
# Get a count of all the sample values.
d = Counter(s)
L = float(len(s))
# 1. find the proportion of the most common value
cmax = d.most_common(1)[0][1]
pmax = cmax/L
# 2. Calculate an upper bound on the probability of the most common value,
# p_u
ubound = pmax + 2.576*math.sqrt( pmax * (1.0-pmax)/L )
pu = min(1,ubound)
# 3. The estimated min-entropy is -log_2(p_u)
return [pu,-math.log(pu,2.0)]
# Section 3.1.4.3 Sanity Check - Most Common Value in the Rows and Columns
def most_common_restart(s):
# Get a count of all the sample values.
d = Counter(s)
L = float(len(s))
# 1. find the proportion of the most common value
cmax = d.most_common(1)[0][1]
return cmax