<\/span><\/h2>\n\n\n\nIn this challenge, we practice using linear regression<\/em> techniques. <\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nA group of five students enrolls in Statistics immediately after taking a Math aptitude test. Each student’s Math aptitude test score, x<\/em><\/strong>, and Statistics course grade, y<\/em><\/strong>, can be expressed as the following list of (x<\/em>, y<\/em>)<\/strong> points:<\/p>\n\n\n\n- (95, 85)<\/strong><\/li>
- (85, 95)<\/strong><\/li>
- (80, 70)<\/strong><\/li>
- (70, 65)<\/strong><\/li>
- (60, 70)<\/strong><\/li><\/ol>\n\n\n\n
If a student scored an 80<\/strong> on the Math aptitude test, what grade would we expect them to achieve in Statistics? Determine the equation of the best-fit line using the least squares method, then compute and print the value of y<\/em><\/strong> when x<\/em> = 80<\/strong>.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThere are five lines of input; each line contains two space-separated integers describing a student’s respective x<\/em><\/strong> and y<\/em><\/strong> grades:<\/p>\n\n\n\n95 85\n85 95\n80 70\n70 65\n60 70<\/code><\/pre>\n\n\n\nIf you do not wish to read this information from stdin, you can hard-code it into your program.<\/p>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint a single line denoting the answer, rounded to a scale of 3<\/strong> decimal places (i.e., 1.234<\/strong> format).<\/p>\n\n\n\n<\/span>Solution – Least Square Regression Line<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <cmath>\n#include <cstdio>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\ndouble reg(vector<int>x, vector<int>y)\n{\n \/\/ mean\n long sum1 = 0;\n for(int i=0; i<x.size(); i++)\n sum1 += x[i];\n double mean1 = (double)sum1\/x.size();\n \n long sum2 = 0;\n for(int i=0; i<y.size(); i++)\n sum2 += y[i];\n double mean2 = (double)sum2\/y.size();\n \n \/\/ sqre sum\n long xsq = 0;\n for(int i=0; i<x.size(); i++)\n xsq += (x[i]*x[i]);\n \n \/\/ sum: xy\n long xy = 0;\n for(int i=0; i<x.size(); i++)\n xy += (x[i]*y[i]);\n \n \/\/ finding a & b;\n double b = (5.0*xy - sum1*sum2)\/(5.0*xsq - sum1*sum1);\n double a = mean2 - b*mean1;\n \n return a + b*80;\n}\n\nint main() {\n \/* Enter your code here. Read input from STDIN. Print output to STDOUT *\/ \n \n vector<int> x,y;\n \n for(auto i=0; i<5; i++)\n {\n int p, q;\n cin>>p;\n x.push_back(p);\n cin>>q;\n y.push_back(q);\n }\n \n double res = reg(x, y);\n \n printf(\"%0.3f\", res);\n \n return 0;\n}<\/pre>\n\n\n\n