Use table.txt , found in the web address below, to help complete the question. C
ID: 3601799 • Letter: U
Question
Use table.txt, found in the web address below, to help complete the question. Copy and paste the web address into your browser if necessary.
https://drive.google.com/open?id=0BzmYuKtE2cW0NTlaLThCMVNGUk0
Formulas for the mean, standard deviation, and Pearson correlation between two variables are given in equations 37.12-37.13 of the text. Write functions mymean(x), mystd(x), and mycor (x,y) that will return the mean of a list x, the standard deviation of a list x, and the correlation by the lists x and y Using the file table.txt, assign the first column of data to x and the third column to y. Then find the mean and standard deviation of x, the mean and standard deviation of y, and the correlation between x and y using the functions mymean, mystd, and mycor. Do not use numpy or any statistical analysis packages for this exercise. You are expected to write your own functions to implement the calculations tor the mean, standard deviation, and correlation. You do not have to use the formulas given in the book if you know of other, equivalent formulas Your program should look for its input data in the file table.txt. Do not rename the file, and this file must be in the same folder as your Jupyter notebook.Explanation / Answer
Steps (Hints) in C-program:
Read values from table.txt and assign in x[i] and y[i]
For instance, Storing first column of file in an array, sample code :
FILE* f = fopen("table.txt", "r");
int n = 0, i = 0;
int x[30]; // assuming there are only 30 numbers in the file
while( fscanf(f, "%d ", &n) > 0 ) // parse %d followed by empty space ' '
{
x[i++] = n;
}
fclose(f);
Step-2: Claculate necessary formulas(individual parameters) like mean of x, mean of y, x*y, x*x, y*y etc.:
For instance :
declare variables like int x[30], y[30], xy[30], xsquare[30], ysquare[30], xsum, ysum, xysum etc.
for (i = 0; i < n; i++) { // n equivalent to number of items, #30 here
xy[i] = x[i] * y[i];
xsquare[i] = x[i] * x[i];
ysquare[i] = y[i] * y[i];
xsum = xsum + x[i];
ysum = ysum + y[i];
xysum = xysum + xy[i];
xsqr_sum = xsqr_sum + xsquare[i];
ysqr_sum = ysqr_sum + ysquare[i];
}
step-3: Calculate the formula and correlation coefficient :
Now average(mean) = sum/n ; so mean for x = xsum/n , mean for y = ysum/n ; n being 30
Now, variance and standard deviation can be found as follows :
for (i=0;i<n;i++)
{
sum1 = sum1+pow((x[i] - average), 2);
}
variance = sum1/ n;
std_deviation = sqrt(variance);
Now calculate the formula of summation and calculate correlation coefficient
Hope this helps.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.