|
612 | 612 | "source": [
|
613 | 613 | "### Exercise 3\n",
|
614 | 614 | "\n",
|
615 |
| - "Write a function that returns one realization of the following random device:\n", |
| 615 | + "First, write a function that returns one realization of the following random device\n", |
616 | 616 | "\n",
|
617 |
| - "- Flip an unbiased coin 10 times. \n", |
618 |
| - "- If a head occurs more than `k` times consecutively within this sequence at least once, pay one dollar. \n", |
619 |
| - "- If not, pay nothing. \n", |
| 617 | + "1. Flip an unbiased coin 10 times. \n", |
| 618 | + "1. If a head occurs `k` or more times consecutively within this sequence at least once, pay one dollar. \n", |
| 619 | + "1. If not, pay nothing. \n", |
| 620 | + "\n", |
| 621 | + "\n", |
| 622 | + "Second, write another function that does the same task except that the second rule of the above random device becomes\n", |
| 623 | + "\n", |
| 624 | + "- If a head occurs `k` or more times within this sequence, pay one dollar. \n", |
620 | 625 | "\n",
|
621 | 626 | "\n",
|
622 | 627 | "Use no import besides `from numpy.random import uniform`."
|
|
687 | 692 | "cell_type": "markdown",
|
688 | 693 | "metadata": {},
|
689 | 694 | "source": [
|
690 |
| - "### Exercise 3" |
| 695 | + "### Exercise 3\n", |
| 696 | + "\n", |
| 697 | + "Here’s a function for the first random device." |
691 | 698 | ]
|
692 | 699 | },
|
693 | 700 | {
|
|
700 | 707 | "source": [
|
701 | 708 | "from numpy.random import uniform\n",
|
702 | 709 | "\n",
|
703 |
| - "def draw(k): # pays if k successes in a row\n", |
| 710 | + "def draw(k): # pays if k consecutive successes in a sequence\n", |
704 | 711 | "\n",
|
705 | 712 | " payoff = 0\n",
|
706 | 713 | " count = 0\n",
|
707 | 714 | "\n",
|
708 | 715 | " for i in range(10):\n",
|
709 | 716 | " U = uniform()\n",
|
710 | 717 | " count = count + 1 if U < 0.5 else 0\n",
|
| 718 | + " print(count) # print counts for clarity\n", |
711 | 719 | " if count == k:\n",
|
712 | 720 | " payoff = 1\n",
|
713 | 721 | "\n",
|
714 | 722 | " return payoff\n",
|
715 | 723 | "\n",
|
716 | 724 | "draw(3)"
|
717 | 725 | ]
|
| 726 | + }, |
| 727 | + { |
| 728 | + "cell_type": "markdown", |
| 729 | + "metadata": {}, |
| 730 | + "source": [ |
| 731 | + "Here’s another function for the second random device." |
| 732 | + ] |
| 733 | + }, |
| 734 | + { |
| 735 | + "cell_type": "code", |
| 736 | + "execution_count": null, |
| 737 | + "metadata": { |
| 738 | + "hide-output": false |
| 739 | + }, |
| 740 | + "outputs": [], |
| 741 | + "source": [ |
| 742 | + "def draw_new(k): # pays if k successes in a sequence\n", |
| 743 | + "\n", |
| 744 | + " payoff = 0\n", |
| 745 | + " count = 0\n", |
| 746 | + "\n", |
| 747 | + " for i in range(10):\n", |
| 748 | + " U = uniform()\n", |
| 749 | + " count = count + ( 1 if U < 0.5 else 0 )\n", |
| 750 | + " print(count)\n", |
| 751 | + " if count == k:\n", |
| 752 | + " payoff = 1\n", |
| 753 | + "\n", |
| 754 | + " return payoff\n", |
| 755 | + "\n", |
| 756 | + "draw_new(3)" |
| 757 | + ] |
718 | 758 | }
|
719 | 759 | ],
|
720 | 760 | "metadata": {
|
721 |
| - "date": 1583028114.0955436, |
| 761 | + "date": 1583215096.906643, |
722 | 762 | "filename": "functions.rst",
|
723 | 763 | "kernelspec": {
|
724 | 764 | "display_name": "Python",
|
|
0 commit comments