Created
January 5, 2012 03:21
-
-
Save andrewrcollins/1563551 to your computer and use it in GitHub Desktop.
#TJHSST ~ Simple Statistics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Statistics | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
#include "statisti.h" | |
main(argc,argv) | |
int argc; | |
char *argv[]; | |
{ | |
FILE *datafile; | |
double x,y,sumx,sumy,sumxy,sumx2,sumy2,stddevx,stddevy,r,b,a,xbar,ybar; | |
int n; | |
if(argc<2) { | |
fputs("Enter Data Filename\n",stderr); | |
exit(1); | |
} | |
datafile=fopen(argv[1],"r"); | |
if(!datafile) { | |
fputs("Error Opening Data File\n",stderr); | |
exit(1); | |
} | |
sumx=sumy=sumxy=sumx2=sumy2=0.0; | |
n=0; | |
while(fscanf(datafile,"%lf%*c%lf%*c",&x,&y)!=EOF) { | |
sumx+=x; | |
sumy+=y; | |
sumxy+=(x*y); | |
sumx2+=(x*x); | |
sumy2+=(y*y); | |
n++; | |
} | |
fclose(datafile); | |
xbar=sumx/n; | |
ybar=sumy/n; | |
stddevx=stat_stddev(sumx,sumx2,n); | |
stddevy=stat_stddev(sumy,sumy2,n); | |
r=linear_r(sumxy,sumx,sumy,stddevx,stddevy,n); | |
b=linear_b(r,stddevx,stddevy); | |
a=linear_a(xbar,ybar,b); | |
printf("Data points : %3d\n",n); | |
printf("Means\n"); | |
printf(" x bar : %3f\n",xbar); | |
printf(" y bar : %3f\n",ybar); | |
printf(" %c x : %3f\n",(char)228,sumx); | |
printf(" %c y : %3f\n",(char)228,sumy); | |
printf(" %c xy : %3f\n",(char)228,sumxy); | |
printf(" %c x^2 : %3f\n",(char)228,sumx2); | |
printf(" %c y^2 : %3f\n",(char)228,sumy2); | |
printf("Standard Deviations\n"); | |
printf(" sx : %3f\n",stddevx); | |
printf(" sy : %3f\n",stddevy); | |
printf(" y = a + bx\n"); | |
printf(" a : %3f\n",a); | |
printf(" b : %3f\n",b); | |
printf("Correlation Coefficent\n"); | |
printf(" r : %3f\n",r); | |
} | |
double stat_stddev(sumx,sumx2,n) | |
double sumx,sumx2; | |
int n; | |
{ | |
if(n<=1) | |
return (-1.0); | |
return (1/(n-1.0))*((sumx2)-(sumx*sumx)/n); | |
} | |
double linear_r(sumxy,sumx,sumy,stddevx,stddevy,n) | |
double sumxy,sumx,sumy,stddevx,stddevy; | |
int n; | |
{ | |
if(n<=1) | |
return (-1.0); | |
return (sumxy-(sumx*sumy/n))/((n-1)*stddevx*stddevy); | |
} | |
double linear_b(sumxy,sumx,sumy,sumx2,n) | |
double sumxy,sumx,sumy,sumx2; | |
int n; | |
{ | |
return (r*stddevx/stddevy); | |
} | |
double linear_a(xbar,ybar,b) | |
double xbar,ybar,b; | |
{ | |
return (ybar-b*xbar); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A C program found on an old 5.25 floppy disk from when I was a nerd at Thomas Jefferson High School for Science and Technology between 1988 and 1992.
The program calculates (and displays) simple statistics of a bi-variate data set.
Anyone can do whatever they'd like to with this program--if anything.
I remain a math nerd.