-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSix_10_CountingHead.py
238 lines (99 loc) · 2.9 KB
/
Six_10_CountingHead.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
##Author: Benktesh
#01/03/2017
import numpy as np
'''
Counting heads. Given integers n and k, along with p1; : : : ; pn 2 [0; 1], you want to determine the
probability of obtaining exactly k heads when n biased coins are tossed independently at random,
where pi is the probability that the ith coin comes up heads. Give an O(n2) algorithm for this
task.2 Assume you can multiply and add two numbers in [0; 1] in O(1) time.
Solution Approach:
#http://stackoverflow.com/questions/13099766/counting-heads-dynamic-programming
Consider the situation when (n-1) coins are tossed together and nth coin is tossed apart and take into account mutual independence.
Combine probabilities of simpler cases to get P(1..n, k) (where P(1..n, k) is the probability of obtaining exactly k heads when n coins)
Then apply this rule and fill all the cells in NxK table
Edit:
There are two possible ways to get exactly k heads with n coins -
a) if (n-1) coins have k heads, and Nth coin is tail, and
b) if (n-1) coins have k-1 heads, and Nth coin is head
so
P(n, k) = P(n-1, k) * (1 - p[n]) + P(n-1, k-1) * p[n]
'''
def countingHead(k, P):
'''
Returns probability of k head from n biased coins where probability of coins are given in P.
Arguements:
n = number of coins
k = number of heads
P = prbability of getting head for coins, i.e., p0 p1 p2 p2.... pn-1
#http://www.cforcoding.com/2010/08/coin-tosses-binomials-and-dynamic.html
private static void runDynamic() {
02.
long start = System.nanoTime();
03.
double[] probs = dynamic(0.2, 0.3, 0.4);
04.
long end = System.nanoTime();
05.
int total = 0;
06.
for (int i = 0; i < probs.length; i++) {
07.
System.out.printf("%d : %,.4f%n", i, probs[i]);
08.
}
09.
System.out.printf("%nDynamic ran for %d coinsin %,.3f ms%n%n",
10.
coins.length, (end - start) / 1000000d);
11.
}
12.
13.
private static double[] dynamic(double... coins) {
14.
double[][] table = new double[coins.length + 2][];
15.
for (int i = 0; i < table.length; i++) {
16.
table[i] = new double[coins.length + 1];
17.
}
18.
table[1][coins.length] = 1.0d; // everything else is 0.0
19.
for (int i = 0; i <= coins.length; i++) {
20.
for (int j = coins.length - 1; j >= 0; j--) {
21.
table[i + 1][j] = coins[j] * table[i][j + 1] +
22.
(1 - coins[j]) * table[i + 1][j + 1];
23.
}
24.
}
25.
double[] ret = new double[coins.length + 1];
26.
for (int i = 0; i < ret.length; i++) {
27.
ret[i] = table[i + 1][0];
28.
}
29.
return ret;
30.
}
'''
n = len(P)
M = np.array([[0 for x in range(k)] for x in range(n)])
print M
print "Counting Head Completed k = {} n = {} P = {}".format(k, n, P)
def main():
kHead = 5
P = [0.2, 0.3, 0.4]
countingHead(kHead, P)
if __name__ == '__main__':
import sys
sys.exit(int(main() or 0))