**
Fill This Form To Receive Instant Help**

Homework answers / question archive / ECP 3004: Python for Business Analytics Assignment 4 Instructions: Complete this assignment within the space on your private GitHub repo (not a fork of the course repo ECP3004S21!) in a folder called assignment 04

ECP 3004: Python for Business Analytics

Assignment 4

Instructions: Complete this assignment within the space on your private GitHub repo (not a fork of the course repo ECP3004S21!) in a folder called assignment 04. In this folder, save your answers to Questions 1 and 2 in a file called my A4 functions.py, following the sample script in the folder assignment 04 in the course repository. When you are finished, submit it by uploading your files to your GitHub repo using any one of the approaches outlined in Question 3. You are free to discuss your approach to each question with your classmates but you must upload your own work. Please note: In computer programming, many small details are very important. A file with the wrong name in the wrong folder will not run, even if the functions work perfectly. Question 1: Follow the function design recipe to define functions for all of the following Exercises. For each function, create three examples to test your functions. Record the definitions in the sample script my A4 functions.py Example 1 Write a function matrix multiply(mat 1, mat 2) that replicates the numpy method dot using loops. – Your function should take in two numpy arrays and output a numpy array with the same number of rows as mat 1 and the same number of columns as mat 2. – It should use three nested loops, one for the row of the output, one for the column of the output, and one for the sum taken on the multiplication along the elements the corresponding row of mat 1 and column of mat 2. – The element in row i and column j of your output array should equal Pn k=1 mat 1i,k × mat 2k,j , where the number As,t is the element in row s and column t of the array A. – You can use the command mat 1.dot(mat 2) to produce the output for a test case. – Your function should return None and print a warning message if the matrices are not conformable; that is, if the number of rows of mat 2 is not equal to number of columns of mat 1. 1

Example 2 The linear regression model is estimated by minimizing the sum of squared residuals from fitting a linear model β0 + xiβ1 + ε to the observed variable yi over two lists of data x and y. Write a function ssr loops(y, x, beta 0, beta 1) that calculates the value of the sum of squared residuals of this regression model. SSR(y, x, β0, β1) = Xn i=1 (yi − β0 − β1xi) 2 . Example 3 Write another version of sum of squared residuals, except this version, called ssr vec(y, x, beta 0, beta 1) should use vector operations with numpy. The parameters y and x should be numpy arrays and your function should not use loops. You can use the same test cases as you did for ssr loops(y, x, beta 0, beta 1). Note that some functions such as sum() and operations such as addition, subtraction and exponents operate element-by-element on numpy arrays. If necessary, experiment with some examples first. Example 4 The likelihood function of the logistic regression model is used to estimate coefficients in logistic regression. Logistic regression is used to model binary events, i.e. whether or not an event occurred. For each observation i, the observation yi equals 1 if the event occurred and 0 if it did not. Build on the function logit like() from Assignment 3 and write a python function logit like sum() that calculates the sum of the log-likelihood across all observations (yi , xi), i = 1, . . . , n. That is, it returns the sum of either the log of the function `(xi ; β0, β1) if yi = 1 or the log of the function (1−`(xi ; β0, β1)) if yi = 0, over all observations i = 1, . . . , n. . For reference, the logit link function is defined as `(xi ; β0, β1) = P rob(y = 1|x) = e xi0β 1 + e xi0β = e β0+xiβ1 1 + e β0+xiβ1 . This function logit like sum() will have four arguments, (y, x; β0, β1), in that order, where the sample of observations y and x are both either lists or numpy arrays. Question 2: For all of the Exercises in Question 1, use your examples to test the functions you defined. Since the examples are all contained within the docstrings of your functions, you can use the doctest.testmod() function within the doctest module to test your functions automatically. Add some code to the sample program my A4 functions.py, run the lines of code, and paste the output to a file called my A4 functions doctest.txt. Don’t worry about false alarms: if there are some “failures” that are only different in the smaller decimal places, then your function is good enough. It is much more important that your function runs without throwing an error. Question 3: Push your completed files to your GitHub repository following one of these three methods. Method 1: In a Browser Upload your code to your GitHub repo using the interface in a browser.

1. Browse to your assignment 0X folder in your repository (the “X” corresponds to Assignment X.). 2. Click on the “Add file” button and select “Upload files” from the drop-down menu. 3. Revise the generic message “Added files via upload” to leave a more specific message. You can also add a description of what you are uploading in the field marked “Add an optional extended description...” 4. Press the button “‘Commit changes,” leaving the buton set to “Commit directly to the main branch.” Method 2: With GitHub Desktop Upload your code to your GitHub repo using the interface in GitHub Desktop. 1. Save your file within the folder in your repository within the folder referenced in GitHub Desktop. 2. When you see the changes in GitHub Desktop, add a description of the changes you are making in the bottom left panel. 3. Press the button “Commit to main” to commit those changes. 4. Press the button “Push origin” to push the changes to the online repository. After this step, the changes should be visible on a browser, after refreshing the page. Method 3: At the Command Line Push your code directly to the repository from the command line in a terminal window, such as GitBash on a Windows machine or Terminal on a Mac. 1. Open GitBash or Terminal and navigate to the folder inside your local copy of your git repo containing your assignments. Any easy way to do this is to right-click and open GitBash within the folder in Explorer. A better way is to navigate with UNIX commands, such as cd. 2. Enter git add . to stage all of your files to commit to your repo. You can enter git add my filename.ext to add files one at a time, such as my functions.py in this Assignment. 3. Enter git commit -m "Describe your changes here", with an appropriate description, to commit the changes. This packages all the added changes into a single unit and stages them to push to your online repo. 4. Enter git push origin main to push the changes to the online repository. After this step, the changes should be visible on a browser, after refreshing the page.

Already member? Sign In

Purchased 3 times