Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Write functions of mathematics equation using python

Write functions of mathematics equation using python

Computer Science

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

Assignment 5

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,β01) 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,β01) = 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:

 

 

 

 

  1. Find these values by evaluating ssr(y, x, beta 0, beta 1) over every combination of of (β01) in two lists.
  2. Create lists beta 0 list and beta 1 list from ranges

 

 

, where the neighboring values of β0 or β1 are separated by distance step.

 

  1. Start with min SSR = 999999. Loop over the index numbers i and j, corresponding to lists beta 0 list and beta 1 list.
  2. For each pair of i and j, extract the value beta 0 list[i] and beta 1 list[j].
  3. For each pair of i and j, evaluate SSR(y,x,β01). If it is lower than min SSR, record the new i min = i and j min = j and update the newest value of min SSR.
  4. After the loops, return [ beta 0[i min], beta 1[j min] ]

 

 

  1. Verify that the result matches the values in Exercises 3 and 4 (up to accuracy 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.

  1. Browse to your assignment 0X folder in your repository (“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 “‘Commit changes,” leaving the button 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 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.

 

  1. 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.
  2. 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.

Option 1

Low Cost Option
Download this past answer in few clicks

24.99 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE

Related Questions