Question: Python 3.7 Question 4: (25 points) Define a function called 1_cover that calculates the sample covariance between two variables. Here are the values of the
Question 4: (25 points) Define a function called 1_cover that calculates the sample covariance between two variables. Here are the values of the two variables: 7- [1., 5.5, 7.8, 4.2, -2.7, -5.4,8.9] 7 -0.1, 1.5, 0.8, -4.2, 2.7, -9.4,-1.9). Hint: Define two vectors xv and yv using the values above. Then write a function that has the two vectors as inputs and one value as output. The formula for the sample covariance is: Cou(z,y) - Li=1(1- )(yi - y) where 1 and y are the averages of the x and y vector values, respectively and n are the number of observations in your data (i.e., the number of values you have in your x-vector). Here is the algorithm description in words, you can use it as a structure to build your function: 1. Calculate the average of x vector values and assign this value to variable xbar. 2. Calculate the average of y vector values and assign this value to variable ybar. 3. Subtract the x average (xbar) from all the values in the x vector and store the results in vector xdeviation. 4. Subtract the y average (ybar) from all the values in the y vector and store the results in vector ydeviation. 5. Multiply xdeviation and ydeviation and store in vector xymultv 6. Sum up all the values of vector xymultv and divide this sum by 12- 1 where n is the number of values in the x vector. Evaluate the covariance with the two vectors given above, ie, call your f_covar function with xv and yv as input vectors. In order to get full points you really need to program this function by hand. Do not call a covariance routine from pandas or numpy but implement the above formula yourself
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
