public mwittels /khan-exercises forked from Khan/khan-exercises

Subversion checkout URL

You can clone with HTTPS or Subversion.

Complete rewrite of dependent probability

Reviewers: eater

Reviewed By: eater

CC: emily

Differential Revision: http://phabricator.khanacademy.org/D509
commit 82379746304b79838e59ef8ec42a97f180d68cb8 1 parent 31797cf
authored

Showing 1 changed file with 390 additions and 120 deletions.

1. exercises/dependent_probability.html
510  exercises/dependent_probability.html
 ... ... @@ -1,141 +1,411 @@ 1 1   2 - 2 + 3 3   4 4   5 5  Dependent probability 6 6   7 - 8 -  17 7   18 8   19 9 
20 -
21 -  22 -  23 - 30 24 - 70 25 - 26 -  27 - 5 28 - 29 - 30 -  31 -  32 - randRange(5, 15) 33 - 34 -  35 -  36 - randRange(2, 4) 37 - 38 - (PERCENT_UNFAIR_HEADS_MAX - PERCENT_UNFAIR_HEADS_MIN) / PERCENT_UNFAIR_HEADS_INCR 39 - 40 - PERCENT_UNFAIR_HEADS_MIN + rand(NUM_INCR + 1) * PERCENT_UNFAIR_HEADS_INCR 41 - 42 -  43 - randRange(2, 4) 44 - 45 - "\\frac{" + NUM_UNFAIR_COINS + "}{" + NUM_COINS + "}" 46 - 47 -  48 - "\\frac{" + NUM_UNFAIR_COINS + "}{" + NUM_COINS + "}" 49 - "\\frac{" + (NUM_COINS-NUM_UNFAIR_COINS) + "}{" + NUM_COINS + "}" 50 - PERCENT_CHANCE_UNFAIR_HEADS / 100 51 -  52 - (function() { 53 - var uhdf = DECIMAL_CHANCE_UNFAIR_HEADS.toString(); 54 - for (var i = 0; i < NUM_FLIPS - 1; i++) { 55 - uhdf += " \\times " + DECIMAL_CHANCE_UNFAIR_HEADS; 56 - } 57 - return uhdf; 58 - })() 59 -  60 -  61 - (function() { 62 - var uhpf = PERCENT_CHANCE_UNFAIR_HEADS + "\\%"; 63 - for (var i = 0; i < NUM_FLIPS - 1; i++) { 64 - uhpf += " \\times " + PERCENT_CHANCE_UNFAIR_HEADS + "\\%"; 65 - } 66 - return uhpf; 67 - })() 68 -  69 - 0.5 70 -  71 - (function() { 72 - var fhdf = DECIMAL_CHANCE_FAIR_HEADS.toString(); 73 - for (var i = 0; i < NUM_FLIPS - 1; i++) { 74 - fhdf += " \\times " + DECIMAL_CHANCE_FAIR_HEADS; 75 - } 76 - return fhdf; 77 - })() 78 -  79 - 80 -  81 - NUM_UNFAIR_COINS / NUM_COINS 82 - 1 - CHANCE_UNFAIR_PICKED 83 - 84 - round(10000 * (CHANCE_UNFAIR_PICKED * pow((PERCENT_CHANCE_UNFAIR_HEADS / 100), NUM_FLIPS) + 85 - CHANCE_FAIR_PICKED * pow(0.5,NUM_FLIPS))) / 100 86 - 87 -
88 - 89 10 
90 -
91 -

You have NUM_COINS coins in a bag. NUM_UNFAIR_COINS of them are unfair in that they have a PERCENT_CHANCE_UNFAIR_HEADS\% chance of coming up heads when flipped (the rest are fair coins). You randomly choose one coin from the bag and flip it NUM_FLIPS times.

92 -

What is the probability, written as a percentage, of getting NUM_FLIPS heads? Round your answer to the nearest hundredth of a percent.

93 -

94 -
95 -
11 +
12 +
13 + randRange(6, 10) 14 + randRange(2, TOTAL-2) 15 + random() < 0.5 16 + randRange(2,  17 + Math.min(ALL ? SPECIAL : TOTAL-SPECIAL,4)) 18 +  19 +  20 + randFromArray( 21 + [["will be eaten by bears", 22 + "will not be eaten by bears", 23 + "will be eaten by bears", 24 + "will not be eaten by bears"], 25 + ["have done their homework", 26 + "have not done their homework", 27 + "has done his homework", 28 + "has not done his homework"], 29 + ["are secretly robots", 30 + "are not secretly robots", 31 + "is secretly a robot", 32 + "is not secretly a robot"], 33 + ["forgot their lunch", 34 + "remembered their lunch", 35 + "forgot his lunch", 36 + "remembered his lunch"], 37 + ["are martial arts masters", 38 + "are not martial arts masters", 39 + "is a martial arts master", 40 + "is not a martial arts master"], 41 + ["play soccer", 42 + "do not play soccer", 43 + "plays soccer", 44 + "does not play soccer"]]) 45 +  46 + ALL ? EVENT_PY : EVENT_PN 47 + ALL ? EVENT_Y : EVENT_N 48 +  49 + (function(){ 50 + prs = []; 51 + _.each(_.range(CHOSEN), function(i) { 52 + if(ALL) { 53 + num = (SPECIAL - i); 54 + den = (TOTAL - i); 55 + prs.push([num,den]); 56 + } else { 57 + num = (TOTAL - SPECIAL - i); 58 + den = (TOTAL - i); 59 + prs.push([num,den]); 60 + } 61 + }); 62 + return prs; 63 + })() 64 +  65 +  66 + (function(){ 67 + n = d = 1; 68 + _.each(PROBS, function(frac) { 69 + n *= frac[0]; 70 + d *= frac[1]; 71 + }); 72 + gcd = getGCD(n, d); 73 + n = n/gcd; 74 + d = d/gcd; 75 + return [n, d]; 76 + })() 77 +  78 +
96 79   97 -
98 -  99 -

You can only pick a fair coin or pick an unfair coin. There is no other outcome.

80 +

81 + In a class of TOTAL,  82 + there are SPECIAL students who 83 + EVENT_PY. 84 +

100 85   101 -  102 -

What chance do you have of picking an unfair coin? How about a fair coin?

86 +

87 + If the teacher chooses CHOSEN 88 + students, what is the probability that  89 + ALL ? ((CHOSEN === 2) ? "both" : "all") : "none" 90 + of them EVENT_PY? 91 +

103 92   104 -  105 -

An unfair coin occurs UNFAIR_COIN_FRACTION_STRING of the time.

93 +
94 + ANS_N/ANS_D 95 +
106 96   107 -  108 -

A fair coin occurs the rest of the time, or FAIR_COIN_FRACTION_STRING of the time.

109 - 110 -  111 -

For that UNFAIR_COIN_FRACTION_STRING of the time that you pick an unfair coin, 112 - what is the chance of flipping NUM_FLIPS heads using that unfair coin?

113 - 114 -  115 -

The chance is UNFAIR_HEADS_PERCENT_FORMULA, or 116 - UNFAIR_HEADS_DECIMAL_FORMULA.

117 - 118 -  119 -

Now, then, your chance of both picking the unfair coin and also flipping 120 - NUM_FLIPS heads--the chance that both these events occur--is what?

97 +
98 +

99 + We can think about this problem as the probability of  100 + CHOSEN events happening. 101 +

102 + The first event is the teacher choosing one student 103 + who EVENT_S. The second event is the teacher 104 + choosing another student who EVENT_S, given 105 + that the teacher already chose someone who  106 + EVENT_S  107 + CHOSEN > 2 ? ", and so on." : "." 108 +

109 +

110 + The probabilty that the teacher will choose someone 111 + who EVENT_S is the number of students who 112 + EVENT_P divided by the total number of 113 + students: \dfrac{PROBS[0][0]} 114 + {PROBS[0][1]}. 115 +

116 +

117 + Once the teacher's chosen one student, there are only 118 + TOTAL-1 left. 119 +

120 +

121 + There's also one fewer student who EVENT_S, 122 + since the teacher isn't going to pick the same student 123 + twice. 124 +

125 +

126 + So, the probability that the teacher picks a second 127 + student who also EVENT_S is  128 + \dfrac{PROBS[1][0]} 129 + {PROBS[1][1]}. 130 +

131 +

132 + The probability of the teacher picking two 133 + students who EVENT_P must then be 134 + \dfrac{PROBS[0][0]} 135 + {PROBS[0][1]} \cdot 136 + \dfrac{PROBS[1][0]} 137 + {PROBS[1][1]}. 138 +

139 +

140 + We can continue using the same logic for the rest of 141 + the students the teacher picks. 142 +

143 +

144 + So, the probability of the teacher picking  145 + CHOSEN students such that 146 + ALL ? ((CHOSEN === 2) ? "both" : "all")  147 + : "none" of them  148 + EVENT_PY is  149 + _.map(PROBS, function(p){ 150 + return "\\dfrac{"+p[0]+"}{"+p[1]+"}"; 151 + }).join("\\cdot") = 152 + \dfrac{ANS_N}{ANS_D} 153 +

154 +
155 +
156 +
157 +
158 +  159 + randFromArray(["bag", "jar", "box", "goblet"]) 160 +  161 +  162 + randFromArray(["marble", "ball", "jelly bean"]) 163 +  164 + randRange(2, 6) 165 + randRange(2, 6) 166 + randRange(2, 6) 167 + REDMAR + GREENMAR + BLUEMAR 168 +  169 + randFromArray([["red", REDMAR], ["green", GREENMAR], 170 + ["blue", BLUEMAR]]) 171 +  172 +  173 + randFromArray([["red", REDMAR], ["green", GREENMAR], 174 + ["blue", BLUEMAR]]) 175 +  176 +  177 + COLOR_ONE === COLOR_T ?  178 + "another " + COLOR_T : "a " + COLOR_T 179 +  180 +  181 + COLOR_ONE === COLOR_T ?  182 + NUM_TWO - 1 : NUM_TWO 183 +  184 +  185 + NUM_ONE*AFTER_NUM/ 186 + getGCD(NUM_ONE*AFTER_NUM,TOTAL*(TOTAL-1)) 187 +  188 +  189 + TOTAL*(TOTAL-1)/ 190 + getGCD(NUM_ONE*AFTER_NUM,TOTAL*(TOTAL-1)) 191 +  192 +
121 193   122 -  123 -

194 +

195 +

A CONTAINER contains  196 + REDMAR red MARBLEs, 197 + GREENMAR green MARBLEs, 198 + and BLUEMAR 199 + blue MARBLEs.

200 +

201 +
202 +

If we choose a MARBLE, then another  203 + MARBLE 204 + without putting the first one back in the  205 + CONTAINER, what is the probability that  206 + the first MARBLE will be COLOR_ONE  207 + and the second will be COLOR_TCOLOR_ONE === COLOR_T ? " as well?" : "?" Write  208 + your answer as a simplified fraction.

209 +
210 +
211 + (NUM_ONE/TOTAL)*(AFTER_NUM/(TOTAL-1)) 212 +
213 +
214 +

215 + The probability of event A happening, then event B, is 216 + the probability of event A happening times 217 + the probability of event B happening given that 218 + event A already happened.  219 +

220 + In this case, event A is picking a COLOR_ONE 221 + MARBLE and leaving it out. 222 + Event B is picking COLOR_TWO  223 + MARBLE. 224 +

225 +

226 + Let's take the events one at at time.  227 + What is the probability that the first marble chosen  228 + will be COLOR_ONE? 229 +

230 +

231 + There are NUM_ONE  232 + COLOR_ONE MARBLEs,  233 + and TOTAL total, so the  234 + probability we will pick a COLOR_ONE  235 + MARBLE is  236 + \dfrac{NUM_ONE} 237 + {TOTAL}. 238 +

239 +

240 + After we take out the first MARBLE, we  241 + don't put it back in, so there are only  242 + TOTAL-1 MARBLEs 243 + left. 244 +

245 +

246 + Also, we've taken out one of the COLOR_ONE  247 + MARBLEs, so there are only  248 + AFTER_NUM left altogether. 249 +

250 +

251 + Since the first MARBLE was  252 + COLOR_ONE, there are still  253 + AFTER_NUM  254 + COLOR_T MARBLEs 255 + left. 256 +

257 +

258 + So, the probability of picking COLOR_TWO  259 + MARBLE after taking out a  260 + COLOR_ONE MARBLE is  261 + \dfrac{AFTER_NUM} 262 + {TOTAL-1}. 263 +

264 +

265 + Therefore, the probability of picking a  266 + COLOR_ONE MARBLE, then  267 + COLOR_TWO MARBLE 268 + is \dfrac{NUM_ONE}{TOTAL}  269 + \cdot \dfrac{AFTER_NUM}{TOTAL-1} 270 + = \dfrac{ANS_N}{ANS_D} 271 +

272 +
273 +
274 +
275 +
276 + random() < 0.5 277 + randRange(1,4) 278 + CAPTAIN_NUM + randRange(1,6) 279 + CAPTAIN_NUM/CAPTAIN_DEM 280 + randRange(1,4) 281 + PIRATE_NUM + randRange(4,6) 282 + PIRATE_NUM/PIRATE_DEM 283 + [getGCD(CAPTAIN_NUM,CAPTAIN_DEM), getGCD(PIRATE_NUM,PIRATE_DEM)] 284 + "\\dfrac{" + CAPTAIN_NUM/CGCD + "}{" + CAPTAIN_DEM/CGCD + "}" 285 + "\\dfrac{" + (CAPTAIN_DEM/CGCD - CAPTAIN_NUM/CGCD) + "}{" + CAPTAIN_DEM/CGCD + "}" 286 + "\\dfrac{" + PIRATE_NUM/PGCD + "}{" + PIRATE_DEM/PGCD + "}" 287 + "\\dfrac{" + (PIRATE_DEM/PGCD - PIRATE_NUM/PGCD) + "}{" + PIRATE_DEM/PGCD + "}" 288 + randRange(0,2) 289 +  290 + (function(){ 291 + if(C_FIRST) { 292 + return ["the Captain hits the pirate ship, but the pirate misses", 293 + "the Captain misses the pirate ship, but the pirate hits", 294 + "both the Captain and the pirate hit each other's ships"][INDEX]; 295 + } else { 296 + return ["the pirate misses the Captain's ship, but the Captain hits", 297 + "the pirate hits the Captain's ship, but the Captain misses", 298 + "both the pirate and the Captain hit each other's ships"][INDEX]; 299 + } 300 + })() 301 +  302 +  303 + (function(){ 304 + if(C_FIRST) { 305 + return [[CAPTAIN_NUM, CAPTAIN_DEM, CAPTAIN_PROB], 306 + [(CAPTAIN_DEM-CAPTAIN_NUM) * PIRATE_NUM, CAPTAIN_DEM*PIRATE_DEM, (1-CAPTAIN_PROB)*PIRATE_PROB], 307 + [0,1,0]][INDEX]; 308 + } else { 309 + return [[CAPTAIN_NUM * (PIRATE_DEM-PIRATE_NUM), CAPTAIN_DEM*PIRATE_DEM, CAPTAIN_PROB*(1-PIRATE_PROB)], 310 + [PIRATE_NUM, PIRATE_DEM, PIRATE_PROB], 311 + [0,1,0]][INDEX]; 312 + } 313 + })() 314 +  315 + INDEX === 0 || INDEX === 2 316 + INDEX === 1 || INDEX === 2 317 +  318 + [C_FIRST ? (C ? "the Captain hitting the pirate ship" : "the Captain missing the pirate ship") 319 + : (P ? "the pirate hitting the Captain's ship" : "the pirate missing the Captain's ship"), 320 + C_FIRST ? (C ? C_HIT_PRETTY : C_MISS_PRETTY) : (P ? P_HIT_PRETTY : P_MISS_PRETTY)] 321 +  322 +  323 + (function(){ 324 + if(C_FIRST) { 325 + if(P && C) {  326 + return ["the pirate hitting the Captain's ship", 0]; 327 + } else if(P) { 328 + return ["the pirate hitting the Captain's ship", P_HIT_PRETTY]; 329 + } else if(!P && C) { 330 + return ["the pirate missing the Captain's ship", 1]; 331 + } else { 332 + return ["the pirate missing the Captain's ship", P_MISS_PRETTY]; 333 + } 334 + } else { 335 + if(C && P) {  336 + return ["the Captain hitting the pirate ship", 0]; 337 + } else if(C) { 338 + return ["the Captain hitting the pirate ship", C_HIT_PRETTY]; 339 + } else if(!C && P) { 340 + return ["the Captain missing the pirate ship", 1]; 341 + } else { 342 + return ["the Captain missing the pirate ship", C_MISS_PRETTY]; 343 + } 344 + } 345 + })() 346 +  347 +
124 348   125 -  126 -

Now, the other possibility, picking the fair coin and flipping NUM_FLIPS heads is what?

349 +

350 + Captain person(1) and his(1) ship, the H.M.S Crimson Lynx, are two furlongs 351 + from the dread pirate person(2) and his(2) merciless band of scallawags. 352 +

353 +

354 + The Captain has probability C_HIT_PRETTY of hitting the pirate ship, if his(1) 355 + ship hasn't already been hit. If it has been hit, he(1) will always miss. 356 + The pirate has probability P_HIT_PRETTY of hitting the 357 + Captain's ship, if his(2) ship hasn't already been hit. If it has been hit, he(2) will always 358 + miss as well. 359 +

127 360   128 -  129 -

361 +

362 + If the C_FIRST ? "Captain" : "pirate" shoots first, what is the probability that 363 + QUESTION? 364 +

130 365   131 -  132 -

How do you combine these two mutually exclusive events to find the chance that either occurs?

366 +
367 + ANSWER 368 +
133 369   134 -  135 -

Add them! So your answer is 136 - UNFAIR_COIN_FRACTION_STRING \times UNFAIR_HEADS_DECIMAL_FORMULA+ 137 - FAIR_COIN_FRACTION_STRING \times FAIR_HEADS_DECIMAL_FORMULA, or 138 - ANSWER\%.

370 +
371 +

372 + The probability of event A happening, then event B, is the probability of  373 + event A happening times 374 + the probability of event B happening given that event A already happened.  375 +

376 + In this case, event A is EV_A and event B is EV_B. 377 +

378 +

379 + The C_FIRST ? "Captain" : "pirate" fires first, so  380 + his(C_FIRST ? 1 : 2) ship can't be sunk before 381 + he(C_FIRST ? 1 : 2) fires his cannons. 382 +

383 +

384 + So, the probability of EV_A is PR_A. 385 +

386 +

387 + If the C_FIRST ? "Captain" : "pirate" hit the C_FIRST ? "pirate" : "Captain's" ship, 388 + the C_FIRST ? "pirate" : "Captain" has no chance of firing back. 389 +

390 +

391 + If the C_FIRST ? "Captain" : "pirate"  392 + missed the C_FIRST ? "pirate" : "Captain's" 393 + ship, 394 + the C_FIRST ? "pirate" : "Captain" has a normal chance to fire back. 395 +

396 +

397 + So, the probability of EV_B given EV_A is PR_B. 398 +

399 +

400 + The probability that QUESTION is then the probability of EV_A times 401 + the probability of EV_B given EV_A. 402 +

403 +

404 + This is PR_A \cdot PR_B  405 + = fraction(ANS_N, ANS_D, true, true) 406 +

407 +
408 +
139 409 
140 410 
141 411