-
Notifications
You must be signed in to change notification settings - Fork 0
/
AEP.py
85 lines (77 loc) · 2.65 KB
/
AEP.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from scipy.stats import bernoulli
from collections import Counter
import numpy as np
import sys
import matplotlib.pyplot as plt
from math import log
def gen_bernoulli(X, n, p):
bernoulli_sequences = []
for i in range(0, X):
sequence = bernoulli.rvs(size=n, p=p)
sequence = tuple(sequence)
bernoulli_sequences.append(sequence)
# print("The sequences are:\n")
# print(*bernoulli_sequences, sep="\n")
return bernoulli_sequences
def get_prob(sequence):
return pow(p,sequence.count(1))*pow((1-p),sequence.count(0))
def entropy(p):
H = p*(log(1/p)/log(2))+(1-p)*(log(1/(1-p))/log(2))
return H
def get_list(sequences, n, X, epsilon, H):
frequency_count = Counter(sequences)
labels, values = zip(*frequency_count.items())
indices = np.arange(0, len(labels))
width = 0.8
probabilities = [a/X for a in values]
plt.bar(indices, probabilities, align='center', width=width)
plt.xlabel("Sequence number")
plt.ylabel("Probability of occurence")
plt.legend(['All Sequences'])
plt.show()
plt.savefig("histogram")
# for i, v in enumerate(probabilities):
# plt.text(i-0.5, v, str(v), color='blue')
plt.show()
return labels, indices
def get_typ_seq(indices, frequent, joint_PMF, epsilon, n, H):
typical_prob = [0] * len(frequent)
typical_set = []
for i in range(0, len(frequent)):
if (joint_PMF[i] >= pow(2, -n*(H + epsilon)) and joint_PMF[i] <= pow(2, -n*(H - epsilon))):
typical_set.append(frequent[i])
typical_prob[i] = joint_PMF[i]
plt.bar(indices, typical_prob, align='center', width=0.8)
plt.xlabel("Typical sequence number")
plt.ylabel("Probability of occurence")
plt.legend(['All Sequences','Typical Sequences'])
plt.show()
plt.savefig("Typical set")
print("Total number of typical sequences are:", len(typical_set))
print("The sequences that belong the the typical set are:\n")
print(*typical_set, sep ="\n")
return
if __name__ == '__main__':
possible_sequences = []
try:
n = int(sys.argv[1]) # length of sequence
X = int(sys.argv[2]) # No.of Sequences
p = float(sys.argv[3]) # Bernoulli parameter p
except Exception as e:
n = 10
X = 50000
p = 0.3
finally:
print("\n")
print("Length of sequence is:", n)
print("No.of Sequences is:", X)
print("Bernoulli parameter is:", p)
print("\n")
H = entropy(p)
print("The entropy H(X) is: %f bits" % H)
sequences = gen_bernoulli(X, n, p)
print("The size of the sample space is: 2 ^", n)
epsilon = 0.1
frequent, indices = get_list(sequences, n, X, epsilon, H)
joint_PMF = [get_prob(sequence) for sequence in frequent]
get_typ_seq(indices, frequent, joint_PMF, epsilon, n, H)