Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Numerical Methods: Mean, Deviation, Regression, and Correlation - Prof. Bryon Ehlmann, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/17/2009

koofers-user-zc0
koofers-user-zc0 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Notes—Chap. 10b Introduction to Numerical Methods
(pp. 413 – 420)
- Provides functions for arithmetic mean, standard deviation, linear regression,
and provides formula for correlation coefficient.
- Arithmetic mean gives the average of values in a set, xi , i = 1, ..., n.
xi
x = arithmetic mean = ────
n
- C++ function that implements arithmetic mean (Figs. 10.5 slightly revised):
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);
}
- Standard deviation measures the variance of values in a set, xi , i = 1, ..., n.
(xi - x)2
s = standard deviation = ────────
n
- C++ function that implements standard deviation (Figs. 10.7 slightly revised):
#include <cmath>
double arithmeticMean(const double x[], int n);
double stdDev(const double x[], int n)
// Calculate standard deviation of first n elements of x.
// Pre: n > 0
pf3
pf4

Partial preview of the text

Download Numerical Methods: Mean, Deviation, Regression, and Correlation - Prof. Bryon Ehlmann and more Study notes Computer Science in PDF only on Docsity!

Notes—Chap. 10b Introduction to Numerical Methods

(pp. 413 – 420)

- Provides functions for arithmetic mean , standard deviation , linear regression ,

and provides formula for correlation coefficient.

- Arithmetic mean gives the average of values in a set, xi , i = 1, ..., n.

∑ xi

x = arithmetic mean = ────

n

- C++ function that implements arithmetic mean (Figs. 10.5 slightly revised):

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); }

- Standard deviation measures the variance of values in a set, xi , i = 1, ..., n.

∑ ( xi - x)

2

s = standard deviation = ────────

n

- C++ function that implements standard deviation (Figs. 10.7 slightly revised):

#include double arithmeticMean(const double x[], int n); double stdDev(const double x[], int n) // Calculate standard deviation of first n elements of x. // Pre: n > 0

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)); }

- Linear regression fits a linear function to a set of points, ( xi , yi ), i = 1, ..., n,

i.e., establishes a y = mx + b relationship between an independent

variable x and a dependent variable y where

∑ ( xi - x) ( yi - y)

m = ────────────

∑ ( xi - x)

2

b = y – m x

- C++ function that computes m and b (Anwer to Review Question 1 on

p. 419, slightly revised):

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; }

  • Questions?