


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Functions for calculating the arithmetic mean, standard deviation, linear regression, and correlation coefficient in c++. It includes formulas, explanations, and example code for each statistical method.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
double arithmeticMean(const double x[], int n) // Compute the arithmetic mean (average) of the first n elements of x. // Pre: n > 0 { int i; double sum = 0; // running sum of elements in list for (i = 0; i < n; ++i) sum += x[i]; return (sum / n); }
2
#include
double mean; // arithmetic mean double dev; // deviation of a list element from mean double devSqrSum = 0; // running sum of squares of deviations int i; mean = arithmeticMean(x, n); for (i = 0; i < n; ++i) { dev = x[i] - mean; devSqrSum += dev * dev; } return (sqrt(devSqrSum / n)); }
2
double arithmeticMean(const double x[], int n); void linearRegression(const double x[], const double y[], int n, double& m, double& b) // Approximate the slope m and y-intercept b of a line fitted to // a set of n (x, y) points defined by the first n elements // of the given arrays x and y. // Note: Use the least-squares line fitting method. { double xMean, yMean, xDev, // deviation of an x from xMean xyDiffSum = 0, // running sums xDiffSquaredSum = 0; xMean = arithmeticMean(x, n); yMean = arithmeticMean(y, n);
for(i = 0; i < n; ++i) { xSum += x[i]; ySum += y[i]; xyProductSum += (x[i] * y[i]); xSquaredSum += (x[i] * x[i]); ySquaredSum += (y[i] * y[i]); } r = ((n * xyProductSum) - (xSum * ySum)) / (sqrt((n * xSquaredSum) - (xSum * xSum)) * sqrt((n * ySquaredSum) - (ySum * ySum))); return r; }