/*
身長および体重のデータファイルの合計(sum),平均(mean)、
分散(variance)、 標準偏差(standard deviation),
歪度(skewness),)尖度(kurtosis),最小(minimum),最大(maximum),
第1四分位点(1st qrt),中央値(median),第3四分位点(3rd qrt),
共分散(covariance),相関係数(correlation coefficient)
を求めるプログラム

データ: data1.txt

*/
#include 
#include 
#include 
#define N 10

void inputdata(int num[],double x[],double y[],double z[],int n);
void calc_index(double x[],int n,char c[]);
double sum(double x[],int n);
double mean(double x[],int n);
double variance(double x[],int n);
double stndv(double x[],int);
double skewness(double x[],int n);
double kurtosis(double x[],int n);
void sort1(double x[],double y[],int n);
void quartile(double x[],double y[],int n,double *min,double *max, double *q1,double *med,double *q3);
double covar(double x[],double y[],int n);
double correl(double x[],double y[],int n);

main()
{
  int num[N];
  double x[N],y[N],z[N];
  char c[80];
  double covxy,rxy;
  inputdata(num,x,y,z,N);
  strcpy(c,"Hight");
  calc_index(x,N,c);
  strcpy(c,"Weight");
  calc_index(y,N,c);
  covxy=covar(x,y,10);
  rxy=correl(x,y,10);
  strcpy(c,"Hight and Weight");
  printf("\nCovariance and Correlation Coefficient:Variables:%s\n",c);
  printf("Covariance:%10.5f Correlation Coefficient:%10.5f\n",covxy,rxy);
}

void calc_index(double x[],int n,char c[])
{
 double xsum,xmean,xvar,xstndv,xskew,xkur,xmin,xmax,xq1,xmed,xq3,xsort[N];
 xsum=sum(x,n);
 xmean=mean(x,n);
 xvar=variance(x,n);
 xstndv=stndv(x,n);
 xskew=skewness(x,n);
 xkur=kurtosis(x,n);
 quartile(x,xsort,n,&xmin,&xmax,&xq1,&xmed,&xq3);
 printf("\nStatiatics of Variable:%s \n",c);
 printf("Sum:%10.3f Mean:%10.3f Variance:%10.3f Std Dev:%10.3f Skewness:%10.5f,Kurtosis:%10.5f\n",xsum,xmean,xvar,xstndv,xskew,xkur);
 printf("Minimun:%10.3f Maximum:%10.3f 1st Qrt%10.3f Mediam%10.3f,3rd Qrt%10.3f\n\n",xmin,xmax,xq1,xmed,xq3);
}

void inputdata(int num[],double x[],double y[],double z[],int n)
{
  int a1,i;
  double a2,a3,a4;
  FILE *fdata;
  fdata=fopen("data1.txt","r");
  for(i=0;i<=9;i++)
    {
      fscanf(fdata,"%d%lf%lf%lf",&a1,&a2,&a3,&a4);
      num[i]=a1;
      x[i]=a2;
      y[i]=a3;
      z[i]=a4;
    }

  fclose(fdata);
}

double sum(double x[],int n)
{int i;
 double a;
 a=0;
 for(i=0;i<=n-1;i++)a=a+x[i];
 return a;
}

double mean(double x[],int n)
{double a,b;
 a=sum(x,n);
 b=a/n;
 return b;
}

double variance(double x[],int n)
{double a,b,c;
 int i;
 a=mean(x,n);
 b=0;
 for(i=0;i<=n-1;i++)b=b+pow(x[i]-a,2);
 c=b/(n-1);
 return c;
}

double stndv(double x[],int n)
{double a,b;
 a=variance(x,n);
 b=sqrt(a);
 return b;
}

double skewness(double x[],int n)
{double a,b,c,d;
 int i;
 a=mean(x,n);
 b=stndv(x,n);
 c=0;
 for(i=0;i<=n-1;i++)c=c+pow(x[i]-a,3);
 d=c*n/(pow(b,3)*(n-1)*(n-2));
 return d;
}

double kurtosis(double x[],int n)
{double a,b,c,d;
 int i;
 a=mean(x,n);
 b=stndv(x,n);
 c=0;
 for(i=0;i<=n-1;i++)c=c+pow(x[i]-a,4);
 d=c*n*(n+1)/(pow(b,4)*(n-1)*(n-2)*(n-3));
 d=d-3.0*pow((n-1),2)/((n-2)*(n-3));
 return d;
}

void sort1(double x[],double y[],int n)
{int change,i;
 double a;
 for(i=0;i<=n-1;i++)y[i]=x[i];
 do{
   change = 0;
   for(i=0;i<=n-2;i++)
     if(y[i]>y[i+1])
       {change=1;
       a=y[i];
       y[i]=y[i+1];
       y[i+1]=a;}}
 while(change ==1);
}

void quartile(double x[],double y[],int n,double *min,double *max,double *q1,double *med,double *q3)
{
 int i,j1,j2;
 sort1(x,y,n);
 *min=y[0];
 *max=y[n-1];
 i=n%2;
 if(i!=0)*med=y[(n+1)/2-1];
 else*med=(y[n/2-1]+y[n/2])/2;
 i=n%4;
 j1=n/4;
 j2=3*n/4;
 if(i!=0){*q1=y[j1];
 *q3=y[j2];}
 else{*q1=(y[j1-1]+y[j1])/2;
 *q3=(y[j2-1]+y[j2])/2;
 }
}

double covar(double x[],double y[],int n)
{
 double xa,ya,a,b;
 int i;
 xa=mean(x,n);
 ya=mean(y,n);
 a=0;
 for(i=0;i<=9;i++)a=a+(x[i]-xa)*(y[i]-ya);
 b=a/(n-1);
 return b;
}

double correl(double x[],double y[],int n)
{
 double covxy,xstndv,ystndv,a;
 covxy=covar(x,y,n);
 xstndv=stndv(x,n);
 ystndv=stndv(y,n);
 a=covxy/(xstndv*ystndv);
 return a;
}


[出力結果]
Statiatics of Variable:Hight 
Sum:  1643.000 Mean:   164.300 Variance:    60.900 Std Dev:     7.804 Skewness:   0.06354,Kurtosis:  -1.21001
Minimun:   153.000 Maximum:   175.000 1st Qrt   158.000 Mediam   163.000,3rd Qrt   170.000


Statiatics of Variable:Weight 
Sum:   546.000 Mean:    54.600 Variance:    61.378 Std Dev:     7.834 Skewness:   0.50070,Kurtosis:  -0.55863
Minimun:    44.000 Maximum:    69.000 1st Qrt    48.000 Mediam    53.500,3rd Qrt    61.000


Covariance and Correlation Coefficient:Variables:Hight and Weight
Covariance:  45.13333 Correlation Coefficient:   0.73822



Back to C Language

Google




BLOG
PICASAWEB
Panoramio


REF:


Cによる統計データ解析入門