Homework 02 Due on 02/25/2022 Introduction to Python BA222 | Spring 2022 Instructions 1. Download the superBowlAds.csv data. Review the corresponding data dictionary here: https://github....

I need the assignment done in Jupyter Notebook- Question 1 parts c and d are not required to be done.


Homework 02 Due on 02/25/2022 Introduction to Python BA222 | Spring 2022 Instructions 1. Download the superBowlAds.csv data. Review the corresponding data dictionary here: https://github. com/rfordatascience/tidytuesday/blob/master/data/2021/2021-03-02/readme.md 2. All your work should be done in Python. Submit a jupyter notebook (.ipynb file) as your solution. 3. Label all your answers using comments. For instance, use #Q1PC to denote question 1 part c. 4. All cells of your notebook should run without errors. 5. All custom functions should have a docstring. 6. Don’t forget to name the file using the convention HW02–nameLastName where name is your first name and LastName is your last name. 1. Python Basics (a) The following cell contains several errors. Rewrite the code so there are no errors and the result is correct. 1 #Q1PA 2 3 # input should be a string 4 # output should be a boolean 5 # It should test if input is equal 6 # to the word Single 7 8 input = Single 9 output = (input = ’single) 10 11 print output (b) A Magic 8-ball is a spherical device that can be used to answer yes-no questions with fun phrases. Design a function that behaves like a magic 8-ball. That is, the user should be able to input a question and the ball should provide a random answer from a list of several possible outcomes, each one equally likely. (c) (OPTIONAL question) The following function should transform distance in meters into: inches, yards or miles, depending on the user’s input. The following code is an attempt to write such a function, but it contains several errors. Correct the errors and write any missing part so that the function works as intended. 1 #S1Q3 2 3 # Function Implementation 4 5 def meters_transformer(m, unit): 6 """ This function transforms a given number of 7 meters and returns the value in inches , yards or 8 miles , depending on the user’s input. 9 10 ARGUMENTS 11 12 m (float or int) : number of meters 13 unit (string) : should be equal to inches , 14 yards or miles.""" 15 possibleUnits = ["inches", "yards", "miles"] 16 if unit in possibleUnits: 17 if unit == possibleUnits [1]: 18 output = str (39.3701 * m) + " inches" 19 else unit == possibleUnits [2]: 20 output = int (1.09361 * m) + " yards" 21 else: 22 output = str (0.000621371 * m) + miles 23 else: 24 output = "Incorrect input for unit. Please input:" \ 25 "inches , yards or miles. And don’t forget" \ 26 "the quotations!" 27 1 https://github.com/rfordatascience/tidytuesday/blob/master/data/2021/2021-03-02/readme.md https://github.com/rfordatascience/tidytuesday/blob/master/data/2021/2021-03-02/readme.md 28 return output 29 30 # Testing 31 32 ## To inches 33 print(meters_transformer (5, "inches")) 34 35 ## To yards 36 print(meters_transformer (5, "yards")) 37 38 ## To miles 39 print(meters_transformer (5, "miles")) 40 41 ## Incorrect string 42 print(meters_transformer (5, "myles")) (d) (Optional question) An economist wrote a piece of code to simulate the profit of a firm. The demand function should take price as input and return quantity demanded based on the following equation: qd = 100− 5p+ ε where ε, the Greek letter epsilon, is a normally distributed random variable with mean 0 and standard deviation 50, it represents the inherent randomness of the demand. The economist assumes that the unit cost of production is $2 dollars and that the firm will supply whatever quantity is demanded up to 100 units. The code seems to be working without any error. While testing the economist encounter the following two issues: (a) sometimes profits were negative and they should be non-negative if the price is greater than the unit cost and (b) profit seems to be capped at $50.00. Test the following piece of code and see if you can figure out what is causing (a) and (b). You will probably have to run the cell a couple of times to identify each case (case (a) is not that common, you may have to run it about 20 or more times to identify an instance of the error). 1 #Q1Pd 2 3 # Firm’s Profit 4 5 #### Profit = Revenues - Cost 6 7 def profit(r, c): 8 """ Calculate profits based on revenues and cost """ 9 pr = r - c 10 return(pr) 11 12 ### Revenues = Units Sold x Price 13 14 def revenues(q, p): 15 """ Calculate revenues based on units sold q 16 and price p""" 17 revenues = p * q 18 return(revenues) 19 20 ## Units Sold = Min(quantity demanded , 100) 21 22 def units_sold(q): 23 """ Calculates units sold based on the assumption 24 that the firm will only supply up to 100 units """ 25 output = min(q, 100) 26 return(output) 27 28 # Quantity Demanded = 100 - 5p + epsilon 29 30 def demand(p, epsilon): 31 """ Calculates quantity demanded based on price p 32 and a random value epsilon """ 33 qd = 100 - 5 * p + epsilon 34 return(qd) 35 36 ### Cost 37 38 # Units Sold x Cost per Unit 39 40 def cost(q, c): 41 """ Calculates the cost of producing q units 42 at c cost per unit.""" 43 output = q * c 2 44 return(output) 45 46 #### Testing 47 48 import random as r 49 50 p = 1 # price 51 e = r.gauss(0, 50) # random demand shock 52 unit_c = 0.5 # cost per unit 53 54 qd = demand(p, e) 55 q = units_sold(qd) 56 r = revenues(p, q) 57 c = cost(q, unit_c) 58 pr = profit(r, c) 59 60 print(pr) 2. Central Limit Theorem For this exercise we are going to show one of the main implications of the Central Limit Theorem (CLT), that is, the means of a sample are normally distributed regardless of the distribution of the population. For this we are going to be making many simulations. Simulations are a popular technique to generate data and answer questions of interest using probability. Please complete this section using the Numpy and Matplotlib packages. You may want to import the two package at the beginning of your code. (a) Define the parameters for the simulations: n is going to represent the number of simulations (start with 100) and s is going to represent the sample size of each simulation (start with 10). (b) Create an array of uniformly distributed values using the Numpy package. The array should have n columns and s rows. (c) For each column, compute the average. Store the averages in a new array. (d) Make a histogram of the array generated in part (c). (e) Repeat parts (b to d) but now changing the parameters n and s. First increase n keeping s constant, from 100 to 500 to 1000. Then, increase s, keeping n at 100, from 10 to 30 to 100 to 1000. Finally make one last simulation with n = 500 and s = 30. (f) What is the standard deviation of the data generated in (d)? Does it match the value of the standard error? (recall that S.E. = σ/ √ n, where σ is the standard deviation of the population and n is the sample size.) 3. Descriptive Analysis (a) In the Superbowl Ads data, how many variables and rows are included in the data? What is the time range and the unit of observation? What is the source of the data? (b) Produce a summary statistics table (mean, median, standard deviation, max and min) for the follow- ing variables: funny, show product quickly, patriotic, celebrity, danger, animals, use sex, view count, like count, dislike count and comment count. (c) Show the histogram of the like count variable and describe it. (d) For each ad, calculate the like-to-dislike ratio. (e) Which category (funny, show product quickly, patriotic, celebrity, danger, animals, use sex) seems to drive more views? Represent this graphically. (f) Which category, relative to the average, (funny, show product quickly, patriotic, celebrity, danger, animals, use sex) has a greater like-to-dislike ratio? (g) What are the top 5 most popular brands? (h) Use the brand variable to produce a new variable called industry. Where, Beer = (Bud Light, Budweiser), food = (Coca-cola, Doritos, Pepsi), finance = (E-trade), car = (Hyundai, Kia, Toyota), sports = (NFL). (i) What are the most popular ad categories (funny, show product quickly, patriotic, celebrity, danger, animals, use sex) by industry? (j) What is the correlation between like-to-dislike ratio and the comment count? 3
Feb 25, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here