Search for question
Question

2/10/24, 3:10 AM { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework 04\n", "\n", "You decided to take a break from studies to prepare for spring break. Your friends are taking to to Vegas and you want to plan ahead for the new games being released. As part of your preparations you need to understand the chances of different games paying out. \n", "\n", "**Some answers were already attempted in a naïve way. Track down the errors and fix them.**\n" { ] }, "cell_type": "markdown", "metadata": {}, "source": [ ] }, "cell_type": "code", "execution_count": 11, "## Question (10 pts)\n", "\n", "Prepare a function that calculates the factorial of any given number. Do not use the built in math function for this one. "metadata": { "execution": { "iopub.execute_input": "2024-01-01T23:42:37.601417Z", "iopub.status.busy": "2024-01-01T23:42:37.6014177", "iopub.status.idle": "2024-01-01T23:42:37.6048987", "shell.execute_reply": "2024-01-01T23:42:37.604898Z", "shell.execute_reply.started": "2024-01-01T23:42:37.6014177" }, "tags": [] }, "outputs": [], "source": [ ] }, { mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt "# Answer Factorial\n", "\n" "cell_type": "markdown", "metadata": {}, "source": [ "## Question (10 pts)\n", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt 11 1/10 2/10/24, 3:10 AM "\n", "Make a function that calculates n choose r. Presume that if no r is given then it is equal to n. Do not use the built in library function for this one. \n", "\n", "$$nCr = \\frac{n!}{r! (n - r)!}$$" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-01-01T23:42:59.586847Z", "iopub.status.busy": "2024-01-01T23:42:59.5868477", "iopub.status.idle": "2024-01-01T23:42:59.591727Z", "shell.execute_reply": "2024-01-01T23:42:59.590726Z", "shell.execute_reply.started": "2024-01-01T23:42:59.5868477" }, "tags": [] }, "outputs": [], "source": [ ] }, { "# Answer nCr\n", "\n" mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt "cell_type": "markdown", "metadata": {}, "source": [ "## Question (5 pts)\n", "\n", "This new game involves picking the correct spheres when randomly chosen. You want to know how many possible combinations of spheres can be selected in this new game when there are 10 spheres (each one has a different number), you pick out 3. Based on the number combination different prizes are given. This is a rare game in Vegas because each sphere and each combination has equal chances of happening even though different payouts are available. This must mean the chances of any given combination are low, like the lottery. Report how many combinations exist.\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-01-01T23:43:04.035369Z", "iopub.status.busy": "2024-01-01T23:43:04.0353697", "iopub.status.idle": "2024-01-01T23:43:04.040720Z", "shell.execute_reply": "2024-01-01T23:43:04.039720Z", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt 2/10 2/10/24, 3:10 AM }, "outputs": [ { }, "tags": [] ], }, { } ] "shell.execute_reply.started": }, { "name": "stdout", "source": [ "# Answer\n", "\n" ] "output_type": "stream", "text":[ ] "cell_type": "markdown", "metadata": {}, "source": [ "1.043963524251648e+20\n" "## Question (15 pts)\n", "\n", "What is the expected return on investment (RoI) with this game? If the game costs $5 to play and only the top 10 combinations of spheres pay out. The top payout is $1M with each subsequent lower payout calculated as:\n", "\n", "$$x_{n+1} \\lfloor\\frac{x_n}{2^n}\\rfloor$$\n", "\n", "$$roi = = \\frac{Expected Value Investment}{Investment}$$\n", "\n", "Think of expected return as a weighted mean of return values and each weighted by the chance of it coming up. "cell_type": "code", "execution_count": 14, ] mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt "metadata": {}, "outputs": [ { "2024-01-01T23:43:04.035369Z" "name": "stdout", "output_type": "stream", "text": [ "[1000000. 500000. 250000. 125000. 62500. 31250. 15625. 3906. 1953.]\n", "-100%\n" 7812.\n", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt 11 3/10 2/10/24, 3:10 AM } ], "source": [ "# Answer\n", }, { "import numpy as np\n", "\n", "investment = 5\n", "\n", "payouts = np.array([1e6 // (2**n) for i in range(0, 15)])\n", "print (payouts\n", "\n", ] "\n", "roi = "\n", "print (f{roi:.0%}\")" }, { "expected_value = np.sum (payouts/ combinations)\n", (expected_value investment) / investment\n", "cell_type": "markdown", "metadata": {}, "source": [ ] "## Question (5 pts)\n", "\n", "To better understand the new dice game, find how many different combinations of dice rolls exist. The new game uses 8 sided dice. Some rumors on the new game suggest there are 2 dice and some suggest there are 3 dice. Calculate how many possible combinations of values exist with 2 and 3 dice.\n" "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", ], "output_type": "stream", "text": [ "Two dice combinations: 64, Three dice combinations 512\n" ] } "source": [ "# Combinations\n", "\n", "pairs "triples mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt = 8^2\n", = 8^3\n", https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt 4/10 2/10/24, 3:10 AM "\n", "print (f\"Two dice combinations: {pairs}, Three dice combinations {triples}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Question (20 pts)\n", "\n", "But wait!? Lots of individual dice values sum to the same total. Now determine how many unique values are possible when the dice values are added.\n", "\n", "*Hint:* meshgrid might help." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { } ], 11 "name": "stdout", ] "output_type": "stream", "text":[ "Two dice unique roles: 15, tree dice unique roles 22\n" 'source": [ "# unique roles\n", "\n", "d1 "d2 "d3 = = "\n", "d1_2, d2_2 = "ds_2 11 np.array(range(8)) + 1\n", np.array(range(8)) + 1\n", np.array(range(8)) + 1\n", "\n", "\n", "pairs_unique d1_2 + d2_2\n", np.unique(ds_2.reshape(1, -1))\n", "\n", "d1_3, d2_3, d3_3 = np.meshgrid(d1, d2, d3)\n", "ds_3 = d1_3 + d2_3\n", "triples_unique mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt np.meshgrid(d1, d2)\n", = = np.unique (ds_3.reshape(1, -1))\n", "print(\n", f\"Two dice unique roles: {len(pairs_unique)}, tree dice unique roles {len(triples_unique)}\"\n" https://mediatb.blob.core.windows.net/media/65bae9d6bc66b89155934145/questions/08.2homework04.ipynb_1707514813368.txt 5/10