|
269 | 269 | "outputs": [], |
270 | 270 | "source": [ |
271 | 271 | "n_samples = 10000\n", |
272 | | - "sum(np.random.choice(lengths,n_samples, replace=True) > 10)/n_samples" |
| 272 | + "sum(np.random.choice(lengths, n_samples, replace=True) > 10)/n_samples" |
273 | 273 | ] |
274 | 274 | }, |
275 | 275 | { |
|
317 | 317 | "np.random.seed(seed=16071982)\n", |
318 | 318 | "\n", |
319 | 319 | "# Simulate one run of flipping the biased coin 10 times\n", |
320 | | - "np.random.binomial(10,0.7)" |
| 320 | + "np.random.binomial(10, 0.7)" |
321 | 321 | ] |
322 | 322 | }, |
323 | 323 | { |
|
336 | 336 | "outputs": [], |
337 | 337 | "source": [ |
338 | 338 | "# Simulate 1,000 run of flipping the biased coin 10 times\n", |
339 | | - "x = np.random.binomial(10,0.3,10000)\n", |
| 339 | + "x = np.random.binomial(10, 0.3, 10000)\n", |
340 | 340 | "\n", |
341 | 341 | "# Plot normalized histogram of results\n", |
342 | 342 | "plt.hist(x, density=True, bins=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);" |
|
370 | 370 | "outputs": [], |
371 | 371 | "source": [ |
372 | 372 | "# Solution\n", |
373 | | - "sum(np.random.binomial(20,0.3,10000) >= 5)/10000" |
| 373 | + "sum(np.random.binomial(20, 0.3, 10000) >= 5)/10000" |
374 | 374 | ] |
375 | 375 | }, |
376 | 376 | { |
|
403 | 403 | "outputs": [], |
404 | 404 | "source": [ |
405 | 405 | "# Plot histogram \n", |
406 | | - "x = np.random.binomial(10,0.5,10000)\n", |
| 406 | + "x = np.random.binomial(10, 0.5, 10000)\n", |
407 | 407 | "plt.hist(x, density=True);" |
408 | 408 | ] |
409 | 409 | }, |
|
465 | 465 | "outputs": [], |
466 | 466 | "source": [ |
467 | 467 | "# Solution: Calculate P(A,B)\n", |
468 | | - "x_0 = np.random.binomial(2,0.5,10000)\n", |
| 468 | + "x_0 = np.random.binomial(2, 0.5, 10000)\n", |
469 | 469 | "p_ab = sum(x_0==2)/len(x_0)\n", |
470 | 470 | "plt.hist(x_0);\n", |
471 | 471 | "print(p_ab)" |
|
478 | 478 | "outputs": [], |
479 | 479 | "source": [ |
480 | 480 | "# Solution: Calculate P(A)P(B)\n", |
481 | | - "x_1 = np.random.binomial(1,0.5,10000)\n", |
482 | | - "x_2 = np.random.binomial(1,0.5,10000)\n", |
| 481 | + "x_1 = np.random.binomial(1, 0.5, 10000)\n", |
| 482 | + "x_2 = np.random.binomial(1, 0.5, 10000)\n", |
483 | 483 | "p_a = sum(x_1 == 1)/len(x_1)\n", |
484 | 484 | "p_b = sum(x_2 == 1)/len(x_2)\n", |
485 | 485 | "p_a*p_b" |
|
536 | 536 | "source": [ |
537 | 537 | "# Calculate P(A)P(B) using resampling methods\n", |
538 | 538 | "n_samples = 100000\n", |
539 | | - "p_a = sum(np.random.choice(lengths,n_samples, replace=True) > 10)/n_samples\n", |
540 | | - "p_b = sum(np.random.choice(lengths,n_samples, replace=True) > 10)/n_samples\n", |
| 539 | + "p_a = sum(np.random.choice(lengths, n_samples, replace=True) > 10)/n_samples\n", |
| 540 | + "p_b = sum(np.random.choice(lengths, n_samples, replace=True) > 10)/n_samples\n", |
541 | 541 | "p_a*p_b" |
542 | 542 | ] |
543 | 543 | }, |
|
556 | 556 | "source": [ |
557 | 557 | "# Calculate P(A,B) using resampling methods\n", |
558 | 558 | "n_samples = 100000\n", |
559 | | - "samples = np.random.choice(lengths,(n_samples,2), replace=True)\n", |
560 | | - "_ = samples > (10,10)\n", |
| 559 | + "samples = np.random.choice(lengths, (n_samples,2), replace=True)\n", |
| 560 | + "_ = samples > (10, 10)\n", |
561 | 561 | "p_ab = sum(np.prod(_, axis=1))/n_samples\n", |
562 | 562 | "p_ab" |
563 | 563 | ] |
|
686 | 686 | "# Take 10,000 subjects\n", |
687 | 687 | "n = 100000\n", |
688 | 688 | "# Sample for number of users, non-users\n", |
689 | | - "users = np.random.binomial(n,0.005,1) \n", |
| 689 | + "users = np.random.binomial(n, 0.005, 1) \n", |
690 | 690 | "non_users = n - users" |
691 | 691 | ] |
692 | 692 | }, |
|
697 | 697 | "outputs": [], |
698 | 698 | "source": [ |
699 | 699 | "# How many of these users tested +ve ?\n", |
700 | | - "u_pos = np.random.binomial(users,0.99)\n", |
| 700 | + "u_pos = np.random.binomial(users, 0.99)\n", |
701 | 701 | "# How many of these non-users tested +ve ?\n", |
702 | | - "non_pos = np.random.binomial(non_users,0.01)" |
| 702 | + "non_pos = np.random.binomial(non_users, 0.01)" |
703 | 703 | ] |
704 | 704 | }, |
705 | 705 | { |
|
0 commit comments