-
Notifications
You must be signed in to change notification settings - Fork 1
/
p2_q6.py
39 lines (31 loc) · 1.11 KB
/
p2_q6.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
# -*- coding: utf-8 -*-
"""p2_q6.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1OIsDsX2UwGbqDEG21pfBe-gvsHnvdFXi
"""
""" Q6. The expected length is sum p_i *l_i, probability of a symbol multiplied by the length of the encoding"""
# Calculate the probabilities of the symbols : take the frequencies and divide it by the number of simbols
f = len(genome)/3
for key, value in codons_cnt.items():
codons_cnt[key] = value/f # The sum of the new codons_cnt.values() gives 1.
# From code_map.values() we can take the length of the encoded symbol
array = []
for value in code_map.values():
array.append(value)
array1 = []
for value in codons_cnt.values():
array1.append(value)
length = np.array(array)
print(length)
prob = np.array(array1)
print(prob)
prob = prob.astype(np.float)
length = length.astype(np.float)
# Expected length
final = np.dot(prob, length)
#total length of the encoded text divided by the number of encoded symbols,
empirical = len(compressed)/len(length)
print(empirical)
entropy = - np.sum(prob*np.log(prob))
print(entropy)