Fill This Form To Receive Instant Help
Homework answers / question archive / Write functions of mathematics equation using python
Write functions of mathematics equation using python. And do a doctest with it at the end. There will be informative details under the pdf which I will be uploading. Please use the my_A5_functions.py as a base to work on. And at the end please do the doctest with the my_A5_test.py as a material.
ECP 3004: Python for Business Analytics
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 05. In this folder, save your answers to Questions 1 and 2 in a file called my A5 module.py, following the sample script in the folder assignment 05 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.
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 A5 module.py.
Together, the function definitions will form a module called my A5 module that you can read in and test using the script my A5 tests.py. There are no particular requirements for my A5 tests.py but you can use it to conduct any calculations or tests of your module. That way, you will not have any unnecessary commands in your module in my A5 module.py, except for the function definitions.
Exercise 1 Write a function variance(x) that calculates the sample variance of the variable in the list
x. The formula for the variance is
,
where n is the number of items in the list x and x¯ is the average of x.
Exercise 2 Write a function covariance(y, x) that calculates the sample covariance of the variables in
the lists y and x. The formula for the covariance is
,
where the lists y and x both have length n.
Exercise 3 Now write a function that calculates the slope coefficients for the linear regression model. Using calculus, you can show that the following function minimizes SSR(y,x,β0,β1) for β1:
,
which is called the ordinary least squares (OLS) estimator. Write a function that performs this calculation called ols slope(y, x).
Exercise 4 Now write a function ols intercept(y, x, beta 1 hat) that calculates the intercept coefficient for the linear regression model. With the slope coefficient, the intercept can be calculated with
βˆ0 = y¯− βˆ1x.¯
Exercise 5 Write a function ssr(y, x, beta 0, beta 1) that calculates the sum of squared residuals for the linear regression model.
n
SSR(y,x,β0,β1) = X(yi − β0− β1xi)2.
i=1
You can use the function ssr loops(y, x, beta 0, beta 1) from Assignment 4 ( including the solutions) as a template.
Exercise 6 Now find values of beta 0 and beta 1 that minimize ssr(y, x, beta 0, beta 1) for given x and y. Write a function min ssr(y, x, beta 0 min, beta 0 max, beta 1 min, beta 1 max, step) as follows:
, where the neighboring values of β0 or β1 are separated by distance step.
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. To conduct any tests, use the sample program my A5 tests.py, run the lines of code, and make any corrections, as necessary.
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.
Method 2: With GitHub Desktop
Upload your code to your GitHub repo using the interface in GitHub Desktop.
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.