-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.py
117 lines (92 loc) · 2.51 KB
/
hw1.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import numpy as np
from control import *
import scipy as sp
from scipy.signal import TransferFunction
import matplotlib.pyplot as plt
# matA = np.mat([[1, 2, 3, 2], [3, 6, 9, 5], [2, 4, 6, 9]])
# print(np.rank(matA))
#
#
# num = [1, 2]
# den = [1, -1, -2]
# tf_2 = tf(num, den)
#
# sys = TransferFunction(num, den)
# print(sys)
# sys_ss = sys.to_ss()
# print(sys_ss)
resultSeq10 = [[]] * 10
numH = 0
numT = 0
likBiased = 0.25
likFair = 0.75
lik_T_bias = 0.75
lik_H_bias = 0.25
lik_T_fair = 0.5
lik_H_fair = 0.5
def coinFlip(type, numFlips, numH, numT):
resultFlip = []
if type == 'fair':
for i in range(numFlips):
coin = np.random.random_sample(1)
if coin >= 0.5:
resultFlip.append('H')
numH = numH + 1
else:
resultFlip.append('T')
numT = numT + 1
elif type == 'biased':
for i in range(numFlips):
coin = np.random.random_sample(1)
if coin <= 0.25:
resultFlip.append('H')
numH = numH + 1
else:
resultFlip.append('T')
numT = numT + 1
prob_seq = (likBiased * (lik_H_bias ** numH) * (lik_T_bias ** numT)) / \
(likFair * (lik_H_fair ** (numH + numT)) + likBiased * (lik_H_bias ** numH) * (lik_T_bias ** numT))
return resultFlip, prob_seq, numH, numT
# 4.(a) and (b)
for i in range(10):
if i <= 4:
flipFair, prob_seq, numH, numT = coinFlip('fair', 40, numH, numT)
resultSeq10[i].append(flipFair)
else:
flipBiased, prob_seq, numH, numT = coinFlip('biased', 40, numH, numT)
resultSeq10[i].append(flipFair)
#4. (c)
resultSeq10 = [[]] * 10
plt.figure(1)
prob_seqList = []
for j in range(5):
numH = 0
numT = 0
for i in range(100):
flipFair, prob_seq, numH, numT = coinFlip('fair', 1, numH, numT)
prob_seqList.append(prob_seq)
plt.plot(prob_seqList, label = (j+1))
prob_seqList = []
plt.xlabel('number of flipping')
plt.ylabel('likelihood')
plt.title('4. (c) fair coin')
plt.legend()
plt.grid()
#4. (d)
resultSeq10 = [[]] * 10
plt.figure(2)
prob_seqList = []
for j in range(5):
numH = 0
numT = 0
for i in range(100):
flipFair, prob_seq, numH, numT = coinFlip('biased', 1, numH, numT)
prob_seqList.append(prob_seq)
plt.plot(prob_seqList, label = (j+1))
prob_seqList = []
plt.xlabel('number of flipping')
plt.ylabel('likelihood')
plt.title('4. (d) biased coin')
plt.legend()
plt.grid()
plt.show()