Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n",
- "\n",
- "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name and collaborators below:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "NAME = \"Abraham Esquivel\"\n",
- "COLLABORATORS = \"\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "---"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "fe57a13a2ba710371e280641c9f21c35",
- "grade": false,
- "grade_id": "cell-90b6f68e307cf4d7",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "# CS110 Pre-class Work 4.2\n",
- "\n",
- "## Part A. The Hire-Assistant Problem.\n",
- "\n",
- "Imagine that you need to hire a new assistant. Every day an agency sends a new assistant for you to interview. If the assistant is better than your current assistant, then you fire your current assistant and you hire the better assistant. You may assume that assistant quality is uniformly distributed between 0 and 1.\n",
- "\n",
- "## Question 1.\n",
- "Write a function, named hire_assistant, that takes applicants (a list of the numbers that represent the level of qualification of the applicants; the higher the number, the better qualified), and returns the number hires if the applicants are presented in the exact same order as the input list applicants. Note that your function should not randomize anything (or else it would be called a randomized algorithm)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "3e823066b88c3701b5aa6feb0b29ea00",
- "grade": false,
- "grade_id": "cell-d011f5f4707fe41a",
- "locked": false,
- "schema_version": 1,
- "solution": true
- }
- },
- "outputs": [],
- "source": [
- "def hire_assistant(applicants):\n",
- " \"\"\"\n",
- " Return the number of assistant hired.\n",
- " Inputs:\n",
- " - applicants: a list of the numbers that represent the level of qualification of \n",
- " the applicants; the higher the number, the better qualified\n",
- " \n",
- " Outputs:\n",
- " - hires: Number of assistants hired\n",
- " \"\"\"\n",
- " # YOUR CODE HERE\n",
- " best=[applicants[0]] #Add the first person in line and from there the other candidates\n",
- " #will be compared against the current (and undesirable) assistant\n",
- " count=0 #This will be the counter to see if someone was hired. The counter will either be 1 or 0\n",
- " \n",
- " for i in range(len(applicants)):\n",
- " if applicants[count]>=best[(len(best)-1)]: #This for loop represents the process of interviews\n",
- " best.append(applicants[count]) #This adds the best candidates in the best list\n",
- " count=count+1\n",
- " \n",
- " \n",
- " return(count)\n",
- "\n",
- " raise NotImplementedError()\n",
- "print(hire_assistant([-1,-2,-3,-4]))\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "1cf91a3b99ed87bfe9ea81d9a9252e16",
- "grade": true,
- "grade_id": "cell-66778b97ad66f71e",
- "locked": true,
- "points": 1,
- "schema_version": 1,
- "solution": false
- }
- },
- "outputs": [],
- "source": [
- "assert(hire_assistant([1])==1)\n",
- "assert(hire_assistant([-1, -2, -3, -4])==1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "950e8b4c047988bb6493460be72d1bc7",
- "grade": false,
- "grade_id": "cell-e5d810828093b20d",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## Question 2. \n",
- "Assuming the applicants are presented in a random order, write a function that receives the number of applicants as input and returns the average number of assistants hired.\n",
- "\n",
- "**N.B.:** Don’t forget to run the simulation several times for each given number of applicants to better estimate the number of hires (please refer to task 3 of the Study Guide)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "7038d9d8cc9239d5ca15f5d21aa986e3",
- "grade": true,
- "grade_id": "cell-b223520ca72942a0",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "outputs": [],
- "source": [
- "import random\n",
- "def experimental_hires(N):\n",
- " # YOUR CODE HERE\n",
- " random.shuffle(N)\n",
- " print(N)\n",
- " best=[N[0]] #Add the first person in line and from there the other candidates\n",
- " #will be compared against the current (and undesirable) assistant\n",
- " count=0 #This will be the counter to see if someone was hired. The counter will either be 1 or 0\n",
- " \n",
- " for i in range(len(N)):\n",
- " if N[count]>=best[(len(best)-1)]: #This for loop represents the process of interviews\n",
- " best.append(N[count]) #This adds the best candidates in the best list\n",
- " count=count+1\n",
- " \n",
- " n=(count,\"number of assistants hired. List\",best[1:]) \n",
- " return(n)\n",
- "\n",
- " raise NotImplementedError()\n",
- " \n",
- "print(experimental_hires([-1,-3,8,12]))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "7f78b31a96cb5ddc8eb534ab037d9fee",
- "grade": false,
- "grade_id": "cell-a55a7b3d12ef78bb",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## Question 3.\n",
- "\n",
- "Use the function below, `analytical_hires(N)`, which returns the analytical expected number of hires, given the number of applicants, along with the function you created in question 2 to create a graph with two curves such that:\n",
- "* The x-axis shows the total number of applicants (make sure label the x-axis)\n",
- "* The y-axis shows the average number of hires (make sure label the y-axis)\n",
- "* The graph contains two curves;\n",
- " * Curve 1: the theoretical performance estimates computed calls to the function `analytical_hires`.\n",
- " * Curve 2: the simulated or experimental estimates using the function you created in question 2.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "1e514458253b863a6c69ce09ccd2d9de",
- "grade": false,
- "grade_id": "cell-4092502cb05933d4",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "outputs": [],
- "source": [
- "def analytical_hires(N):\n",
- " \"\"\"\n",
- " Return the analytical expected number of hires if there are N applicants\n",
- " Inputs:\n",
- " - N: Number of applicants\n",
- " Outputs:\n",
- " - hires: Average number of assistants hired\n",
- " \"\"\"\n",
- " # from the textbook, we know that the analytical result is \n",
- " # 1 + 1/2 + 1/3 + ... + 1/N\n",
- " hires = 0\n",
- " for n in range(N):\n",
- " hires += 1/(n+1)\n",
- " return hires"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "055b3a48707a83f9330ab3b00c45144a",
- "grade": true,
- "grade_id": "cell-f9c07920c069ce20",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "outputs": [],
- "source": [
- "# YOUR CODE HERE\n",
- "import matplotlib.pyplot as plt\n",
- "import random\n",
- "import numpy as np\n",
- "def experimental_hires(N):\n",
- " # YOUR CODE HERE\n",
- " np.array((random.shuffle(N)),dtype=object)\n",
- " print(N)\n",
- " best=[N[0]] #Add the first person in line and from there the other candidates\n",
- " #will be compared against the current (and undesirable) assistant\n",
- " count=0 #This will be the counter to see if someone was hired. The counter will either be 1 or 0\n",
- " \n",
- " for i in range(len(N)):\n",
- " if N[count]>=best[(len(best)-1)]: #This for loop represents the process of interviews\n",
- " best.append(N[count]) #Th\n",
- " count=count+1\n",
- " \n",
- " n=(count,\"number of assistants hired. List\",best[1:]) \n",
- " return(n)\n",
- "\n",
- " raise NotImplementedError()\n",
- " \n",
- "def analytical_hires(N):\n",
- " \"\"\"\n",
- " Return the analytical expected number of hires if there are N applicants\n",
- " Inputs:\n",
- " - N: Number of applicants\n",
- " Outputs:\n",
- " - hires: Average number of assistants hired\n",
- " \"\"\"\n",
- " # from the textbook, we know that the analytical result is \n",
- " # 1 + 1/2 + 1/3 + ... + 1/N\n",
- " hires = 0\n",
- " for n in range(len(N)):\n",
- " hires += 1/(n+1)\n",
- " return hires\n",
- "\n",
- "def plot(N):\n",
- " np.array(N, dtype=object)\n",
- " x=len(N)\n",
- " Curve1y= analytical_hires(N)\n",
- " Curve2y= experimental_hires(N)\n",
- " plt.plot(x,Curve1y,'r',) #################################################\n",
- " plt.plot(x,Curve2y,'b')\n",
- " plt.xlabel(\"Number of applicants\") #This section builds the plot\n",
- " plt.ylabel(\"Number of hires\")\n",
- " plt.show() #################################################\n",
- " raise NotImplementedError()\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "f5c0fc54ac7e38140eacf7a0d3877a00",
- "grade": false,
- "grade_id": "cell-8720f8d8a6a98422",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## Question 4.\n",
- "\n",
- "Plot a graph with the x-axis showing the total number of applicants and the y-axis showing the probability that exactly one assistant is hired."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "99500575978918dad34be4dfe49fff36",
- "grade": true,
- "grade_id": "cell-d3fe1b7d6d175ad7",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "outputs": [],
- "source": [
- "# YOUR CODE HERE\n",
- "raise NotImplementedError()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "998ef0b673bc47c929e5543e6f86ccb2",
- "grade": false,
- "grade_id": "cell-2bd2500c3ca4cf02",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## [Optional] Question 5.\n",
- "Assume that an assistant is able to perform an amount of work each day that is equal to their “quality”. You have a total amount of work M that needs to be accomplished. Your costs are as follows:\n",
- "* X = daily salary for the assistant,\n",
- "* Y = fee to the employment agency,\n",
- "* Z = retrenchment fee for the old assistant.\n",
- "\n",
- "Try to formulate an optimal stopping rule (ie. at what point should one stop requesting new potential hires from the agency?) Make any necessary assumptions to ensure the problem is well-formulated.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "43b6a51878665a39b0ede1313448eaa6",
- "grade": true,
- "grade_id": "cell-af2f0291eced6982",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "outputs": [],
- "source": [
- "# YOUR CODE HERE\n",
- "raise NotImplementedError()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "b0c67a7805b6596f1ba87521c45df302",
- "grade": false,
- "grade_id": "cell-92211f5b42929c46",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## Part B. The Hat Check Problem.\n",
- "\n",
- "There is a coat check at a party, where an attendant stores everyone’s hat while they attend the party. The attendant receives the N hats from everyone attending (all attendees come with a hat). Unfortunately, the coat check attendant forgets which hat belongs to whom. Rather than admitting a mistake, the attendant simply returns random hats back to the party goers. \n",
- "What is the average number of correct hats returned? Here are some guiding questions to help you to simulate this problem. \n",
- "\n",
- "## Question 1. \n",
- "Knowing that everyone’s hats are unique and every guest has a hat. Do you need to generate a random sample in a similar way as what you did for the hiring assistant problem? "
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "259c6115bee56676178f28ab36d6db2f",
- "grade": true,
- "grade_id": "cell-e786799fc4eb1499",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "source": [
- "That would be the case, only if we want to know who got the right hat. In this case, it can be built a similar code"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "c9f8182f3dd59f572cb797f373fb7464",
- "grade": false,
- "grade_id": "cell-e2f68e2bd4c2d099",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## Question 2. \n",
- "Which of the following commands do you think is the Pythonic way to implement that? \n",
- "```\n",
- "import numpy as np\n",
- "n = 100 #the number of party attendants `\n",
- "```\n",
- "**Command 1. **\n",
- "```\n",
- "hat_list = [np.random.integers(0,n) for i in range(n)]`\n",
- "```\n",
- "**Command 2.**\n",
- "```\n",
- "hat_list = list(range(n)) \n",
- "np.random.shuffle(hat_list) \n",
- "```\n",
- "**Command 3.**\n",
- "```\n",
- "hat_list = np.random.sample(n)\n",
- "```"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "b5e83025692b2772640e9e58f0f36af1",
- "grade": true,
- "grade_id": "cell-b8da78e72c1c0738",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "source": [
- "Command 2"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "ec25d5c32cc709928fa50666f21d9808",
- "grade": false,
- "grade_id": "cell-8915979a0b8cf6ce",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## Question 3.\n",
- "Now write a function `hat_check(N)` that has: \n",
- "* Input: N the number of party attendants. \n",
- "* Output: the number of hats correctly returned despite the fact that hats are randomly handed back to the guests.\n",
- "\n",
- "You should use the command you picked for question 2. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "c37f6cdc2ca8cbb92644fa2746445779",
- "grade": true,
- "grade_id": "cell-c8499aeb1b1d76c7",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "outputs": [],
- "source": [
- "# YOUR CODE HERE\n",
- "raise NotImplementedError()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "1ff8b95312de63513a2107ffb7ab9d5a",
- "grade": false,
- "grade_id": "cell-086d4cc0fc5b0155",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## Question 4.\n",
- "\n",
- "Plot a curve with the x-axis showing the total number of party attendants and the y-axis showing the average number of hats correctly returned. As always, remember to run several trials. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "c4d1251529b962f3d3ce28f6ac9f244e",
- "grade": true,
- "grade_id": "cell-597031ea2a5a512a",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "outputs": [],
- "source": [
- "# YOUR CODE HERE\n",
- "raise NotImplementedError()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "editable": false,
- "nbgrader": {
- "checksum": "aad5d529ed9af56148bfc12691cdb950",
- "grade": false,
- "grade_id": "cell-f74b2078132a5177",
- "locked": true,
- "schema_version": 1,
- "solution": false
- }
- },
- "source": [
- "## [Optional] Question 5.\n",
- "As $N$ tends to infinity, the number of correct hats returned tends towards a well-known statistical distribution. State the distribution with all its parameters. Plot several samples using your code. Does the empirical distribution match your theoretical prediction?"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "deletable": false,
- "nbgrader": {
- "checksum": "33f94a80e6d5d9c371e6c39790bd67eb",
- "grade": true,
- "grade_id": "cell-32fe26c1d99fdd2a",
- "locked": false,
- "points": 0,
- "schema_version": 1,
- "solution": true
- }
- },
- "source": [
- "YOUR ANSWER HERE"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.5"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement