(Yes, it correct that b is used for slope here. There is a reason I\'ll be happy
ID: 3619730 • Letter: #
Question
(Yes, it correct that b is used for slope here. There is a reason I'll be happy to explain, but you can program it without worrying about it.)By the way, this is another of those numerical methods that computers do so well. While it's possible to compute an LSRL by hand, automating all of the tedious calculations makes sense, and leaves us to spend our time interpreting our results in a practical context.
Project Specifications
In this project, you must read the x- and y-coordinate pairs in from a data file of unknown length. Each line in the file must contain both coordinates, separated by whitespace, as shown here. In addition, you must use methods in this project, splitting the work up into smaller components and reinforcing your skills with parameter passing and arrays.
You are required to create the following methods, and you must list them in this order above the main program (no prototypes, please!):
1.)Compute correlation coefficient Input Parameters: - an array of x-coordinates read from file - an array of y-coordinates read from file - logical size of the arrays Returned values: - coefficient correlation of input parameters 2.)To compute the least squares regression line
Input Parameters: - an array of x-corridinates read from file - an array of y-corridinates read from file - logical size of the arrays Output Parameters: - the y-intercept of the line - The slope of the line
You will also need a main program to drive this program. All computation should be done in the six methods; the main program should be extremely short. (I have fewer than a dozen lines of code.)
Notes and Hints
•Remember to think about passing by value vs. passing by reference. Forgetting this distinction could cost you hours of debugging time.
•Pay special attention to writing pre- and postconditions. I recommend that you begin by writing method headers and pre- and postconditions, THEN write the internal code.
•Please give 1 or 2 lines of whitespace between methods.
Sample Screen Output
Regression line: y = 1166.93 + -0.586788x
Explanation / Answer
METHOD 1:
avgx = average(x)
for i = 1:n sumx += (x(i) - avgx)^2 end
sigmax = sqrt(sumx/n)
Once you have sx and sy, you can complete Method 1: for i = 1:n sumr += (x(i) - avgx)/sigmax * (y(i) - avgy)/sigmay
end
r = sumr / (n-1) METHOD 2: You'll need standard deviations again. I suggest making the scope of sigmax and sigmay public or creating a 3rd method (no rule against that!) that computes the standard deviations. You'll also need r, so Method 2 should call Method 1 to retrieve r. r = Method1(x,y,n)
b = r * sigmay / sigmax a = avgy - b * avgx
MAIN FUNCTION: Here's where you will do your i/o and call Method 2 to do all the heavy work.
until the end of file is reached:
[x(i),y(i)] = read line from file
then n = i (or, more robustly, n = length of x or y array) pass n, x, and y to Method 2 and retrieve a and b from it. NOTES: There is mention in the problem description about there being 6 methods used. You are only required to use 2, so this statement confuses me a little. If you want to get 6 methods, make one that receives the filename and returns x, y, and n, one that calculates St.Dev., one that calculates average, and maybe one that writes the output in addition to your 2 required ones. I would not recommend making 6 methods in the solution to this problem. Five at most would be okay and four is probably best.
I hope this was helpful!
avgx = average(x)
for i = 1:n sumx += (x(i) - avgx)^2 end
sigmax = sqrt(sumx/n)
Once you have sx and sy, you can complete Method 1: for i = 1:n sumr += (x(i) - avgx)/sigmax * (y(i) - avgy)/sigmay
end
r = sumr / (n-1) METHOD 2: You'll need standard deviations again. I suggest making the scope of sigmax and sigmay public or creating a 3rd method (no rule against that!) that computes the standard deviations. You'll also need r, so Method 2 should call Method 1 to retrieve r. r = Method1(x,y,n)
b = r * sigmay / sigmax a = avgy - b * avgx
MAIN FUNCTION: Here's where you will do your i/o and call Method 2 to do all the heavy work.
until the end of file is reached:
[x(i),y(i)] = read line from file
then n = i (or, more robustly, n = length of x or y array) pass n, x, and y to Method 2 and retrieve a and b from it. NOTES: There is mention in the problem description about there being 6 methods used. You are only required to use 2, so this statement confuses me a little. If you want to get 6 methods, make one that receives the filename and returns x, y, and n, one that calculates St.Dev., one that calculates average, and maybe one that writes the output in addition to your 2 required ones. I would not recommend making 6 methods in the solution to this problem. Five at most would be okay and four is probably best.
I hope this was helpful!
avgx = average(x)
for i = 1:n sumx += (x(i) - avgx)^2 end
sigmax = sqrt(sumx/n)
Once you have sx and sy, you can complete Method 1: for i = 1:n sumr += (x(i) - avgx)/sigmax * (y(i) - avgy)/sigmay
end
r = sumr / (n-1) METHOD 2: You'll need standard deviations again. I suggest making the scope of sigmax and sigmay public or creating a 3rd method (no rule against that!) that computes the standard deviations. You'll also need r, so Method 2 should call Method 1 to retrieve r. r = Method1(x,y,n)
b = r * sigmay / sigmax a = avgy - b * avgx
MAIN FUNCTION: Here's where you will do your i/o and call Method 2 to do all the heavy work. until the end of file is reached:
[x(i),y(i)] = read line from file
then n = i (or, more robustly, n = length of x or y array) pass n, x, and y to Method 2 and retrieve a and b from it. NOTES: There is mention in the problem description about there being 6 methods used. You are only required to use 2, so this statement confuses me a little. If you want to get 6 methods, make one that receives the filename and returns x, y, and n, one that calculates St.Dev., one that calculates average, and maybe one that writes the output in addition to your 2 required ones. I would not recommend making 6 methods in the solution to this problem. Five at most would be okay and four is probably best.
I hope this was helpful!
until the end of file is reached:
[x(i),y(i)] = read line from file
then n = i (or, more robustly, n = length of x or y array) until the end of file is reached:
[x(i),y(i)] = read line from file
pass n, x, and y to Method 2 and retrieve a and b from it. NOTES: There is mention in the problem description about there being 6 methods used. You are only required to use 2, so this statement confuses me a little. If you want to get 6 methods, make one that receives the filename and returns x, y, and n, one that calculates St.Dev., one that calculates average, and maybe one that writes the output in addition to your 2 required ones. I would not recommend making 6 methods in the solution to this problem. Five at most would be okay and four is probably best.
I hope this was helpful!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.