|
414 | 414 | ], |
415 | 415 | "source": [ |
416 | 416 | "n_samples = 10000\n", |
417 | | - "sum(np.random.choice(lengths,n_samples, replace=True) > 10)/n_samples" |
| 417 | + "sum(np.random.choice(lengths, n_samples, replace=True) > 10)/n_samples" |
418 | 418 | ] |
419 | 419 | }, |
420 | 420 | { |
|
473 | 473 | "np.random.seed(seed=16071982)\n", |
474 | 474 | "\n", |
475 | 475 | "# Simulate one run of flipping the biased coin 10 times\n", |
476 | | - "np.random.binomial(10,0.7)" |
| 476 | + "np.random.binomial(10, 0.7)" |
477 | 477 | ] |
478 | 478 | }, |
479 | 479 | { |
|
503 | 503 | ], |
504 | 504 | "source": [ |
505 | 505 | "# Simulate 1,000 run of flipping the biased coin 10 times\n", |
506 | | - "x = np.random.binomial(10,0.3,10000)\n", |
| 506 | + "x = np.random.binomial(10, 0.3, 10000)\n", |
507 | 507 | "\n", |
508 | 508 | "# Plot normalized histogram of results\n", |
509 | 509 | "plt.hist(x, density=True, bins=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);" |
|
548 | 548 | ], |
549 | 549 | "source": [ |
550 | 550 | "# Solution\n", |
551 | | - "sum(np.random.binomial(20,0.3,10000) >= 5)/10000" |
| 551 | + "sum(np.random.binomial(20, 0.3, 10000) >= 5)/10000" |
552 | 552 | ] |
553 | 553 | }, |
554 | 554 | { |
|
603 | 603 | ], |
604 | 604 | "source": [ |
605 | 605 | "# Plot histogram \n", |
606 | | - "x = np.random.binomial(10,0.5,10000)\n", |
| 606 | + "x = np.random.binomial(10, 0.5, 10000)\n", |
607 | 607 | "plt.hist(x, density=True);" |
608 | 608 | ] |
609 | 609 | }, |
|
685 | 685 | ], |
686 | 686 | "source": [ |
687 | 687 | "# Solution: Calculate P(A,B)\n", |
688 | | - "x_0 = np.random.binomial(2,0.5,10000)\n", |
| 688 | + "x_0 = np.random.binomial(2, 0.5, 10000)\n", |
689 | 689 | "p_ab = sum(x_0==2)/len(x_0)\n", |
690 | 690 | "plt.hist(x_0);\n", |
691 | 691 | "print(p_ab)" |
|
709 | 709 | ], |
710 | 710 | "source": [ |
711 | 711 | "# Solution: Calculate P(A)P(B)\n", |
712 | | - "x_1 = np.random.binomial(1,0.5,10000)\n", |
713 | | - "x_2 = np.random.binomial(1,0.5,10000)\n", |
| 712 | + "x_1 = np.random.binomial(1, 0.5, 10000)\n", |
| 713 | + "x_2 = np.random.binomial(1, 0.5, 10000)\n", |
714 | 714 | "p_a = sum(x_1 == 1)/len(x_1)\n", |
715 | 715 | "p_b = sum(x_2 == 1)/len(x_2)\n", |
716 | 716 | "p_a*p_b" |
|
789 | 789 | "source": [ |
790 | 790 | "# Calculate P(A)P(B) using resampling methods\n", |
791 | 791 | "n_samples = 100000\n", |
792 | | - "p_a = sum(np.random.choice(lengths,n_samples, replace=True) > 10)/n_samples\n", |
793 | | - "p_b = sum(np.random.choice(lengths,n_samples, replace=True) > 10)/n_samples\n", |
| 792 | + "p_a = sum(np.random.choice(lengths, n_samples, replace=True) > 10)/n_samples\n", |
| 793 | + "p_b = sum(np.random.choice(lengths, n_samples, replace=True) > 10)/n_samples\n", |
794 | 794 | "p_a*p_b" |
795 | 795 | ] |
796 | 796 | }, |
|
820 | 820 | "source": [ |
821 | 821 | "# Calculate P(A,B) using resampling methods\n", |
822 | 822 | "n_samples = 100000\n", |
823 | | - "samples = np.random.choice(lengths,(n_samples,2), replace=True)\n", |
824 | | - "_ = samples > (10,10)\n", |
| 823 | + "samples = np.random.choice(lengths, (n_samples,2), replace=True)\n", |
| 824 | + "_ = samples > (10, 10)\n", |
825 | 825 | "p_ab = sum(np.prod(_, axis=1))/n_samples\n", |
826 | 826 | "p_ab" |
827 | 827 | ] |
|
983 | 983 | "# Take 10,000 subjects\n", |
984 | 984 | "n = 100000\n", |
985 | 985 | "# Sample for number of users, non-users\n", |
986 | | - "users = np.random.binomial(n,0.005,1) \n", |
| 986 | + "users = np.random.binomial(n, 0.005, 1) \n", |
987 | 987 | "non_users = n - users" |
988 | 988 | ] |
989 | 989 | }, |
|
994 | 994 | "outputs": [], |
995 | 995 | "source": [ |
996 | 996 | "# How many of these users tested +ve ?\n", |
997 | | - "u_pos = np.random.binomial(users,0.99)\n", |
| 997 | + "u_pos = np.random.binomial(users, 0.99)\n", |
998 | 998 | "# How many of these non-users tested +ve ?\n", |
999 | | - "non_pos = np.random.binomial(non_users,0.01)" |
| 999 | + "non_pos = np.random.binomial(non_users, 0.01)" |
1000 | 1000 | ] |
1001 | 1001 | }, |
1002 | 1002 | { |
|
0 commit comments