Advertisement
Eather

co-relation analysis in C

Mar 5th, 2012
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.38 KB | None | 0 0
  1. /* Correlation Analaysis */
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<math.h>
  5.  
  6. char tolower(char c)
  7. {
  8.     if(c>='A' && c<='Z')return c+'a';
  9. }
  10.  
  11. int main()
  12. {
  13.  
  14.     while(1){
  15.     char Item1[10];
  16.     char Item2[10];
  17.     /*printf("Give Item1 Name: \n");*/
  18.     scanf("%s",&Item1);
  19.     /*printf("Give Item2 Name: \n");*/
  20.     scanf("%s",&Item2);
  21.     printf("\n");
  22.     int a,b,c,d,sum1=0,sum2=0,sum3=0,sum4=0,Total=0;
  23.     /*printf("Type the amount of %s",Item1);printf(" & %s \n",Item2);*/
  24.     scanf("%d",&a);
  25.     /*printf("Type the amount of %s",Item1);printf(" & not %s \n",Item2);*/
  26.     scanf("%d",&b);
  27.     /*printf("Type the amount of not %s",Item1);printf(" & %s \n",Item2);*/
  28.     scanf("%d",&c);
  29.     /*printf("Type the amount of not %s",Item1);printf(" & not %s \n",Item2);*/
  30.     scanf("%d",&d);
  31.     sum1=a+b;
  32.     sum2=c+d;
  33.     sum3=a+c;
  34.     sum4=b+d;
  35.     Total=sum1+sum2;
  36.     printf("\n");
  37.     printf("  Table 1: A 2x2 Contingency table with respect to "); printf("%s & %s",Item1,Item2);printf(" purchase \n");
  38.     printf("  ----------------------------------------------------\n");
  39.     printf("\t \t %s",Item2);printf("\t \t'%s",Item2);printf("\t \t Erow \n");
  40.     printf("  ----------------------------------------------------\n");
  41.     printf("\n");
  42.     printf("  %s",Item1);printf("\t \t %d",a);printf(" \t \t %d",b);printf(" \t \t %d \n",sum1);
  43.     printf("\n");
  44.     printf(" '%s",Item1);printf("\t \t %d",c);printf(" \t \t %d",d);printf(" \t \t %d \n",sum2);
  45.     printf("  ----------------------------------------------------\n");
  46.     printf("  Ecol");printf("\t \t %d",sum3);printf(" \t \t %d",sum4);printf(" \t \t %d \n",Total);
  47.  
  48.     printf("\n");
  49.     printf("\n");
  50.     printf("  Total transaction= %d \n",Total);
  51.     printf("\n");
  52.     printf("  Burger: %d \n",sum1);
  53.     printf("\n");
  54.     printf("  Hotdog: %d \n",sum3);
  55.     printf("\n");
  56.     printf("  Burgur & Hotdog Both: %d \n",sum4);
  57.     printf("\n");
  58.  
  59.     float e,f,g,L,min_sup,min_conf,sup,conf,all_conf,cosine;
  60.     printf("  Enter Minimum Support:");
  61.     scanf("%f",&min_sup);
  62.     printf("  Enter Minimum Confidence:");
  63.     scanf("%f",&min_conf);
  64.     sup=(float (sum4)/float (Total));
  65.     conf=(float (sum4)/float (sum3));
  66.     printf("\n");
  67.     printf("  SUPPORT: %.1f \n",sup);
  68.     printf("\n");
  69.     printf("  CONFIDENCE: %.2f \n",conf);
  70.  
  71.     double e1,e2,e3,e4;
  72.     double  X2=0.00;
  73.  
  74.     e1=(sum1*sum3)/Total;
  75.     e2=sum1-e1;
  76.     e3=sum3-e1;
  77.     e4=sum2-e3;
  78.  
  79.     printf("\n");
  80.     printf("\n");
  81.     printf("  Table 2: After Calculate the Expected Values.........\n");
  82.     printf("\n");
  83.     printf("  ----------------------------------------------------\n");
  84.     printf("\t \t %s",Item2);printf("\t \t'%s",Item2);printf("\t \t Erow \n");
  85.     printf("  ----------------------------------------------------\n");
  86.     printf("\n");
  87.     printf("  %s",Item1);printf("\t \t %d",a);printf(" [%.0lf",e1);printf("]");printf(" \t %d",b);printf(" [%.0lf",e2);printf("]");printf(" \t %d \n",sum1);
  88.     printf("\n");
  89.     printf(" '%s",Item1);printf("\t \t %d",c);printf(" [%.0lf",e3);printf("]");printf(" \t %d",d);printf(" [%.0lf",e4);printf("]");printf(" \t %d \n",sum2);
  90.     printf("  ----------------------------------------------------\n");
  91.     printf("  Ecol");printf("\t \t %d",sum3);printf(" \t \t %d",sum4);printf(" \t \t %d \n",Total);
  92.  
  93.     X2=((a-e1)*(a-e1))/e1+((b-e2)*(b-e2))/e2+((c-e3)*(c-e3))/e3+((d-e4)*(d-e4))/e4;
  94.     printf("\n");
  95.     printf("  Correlation Analysis X2 = %.2lf",X2);
  96.     printf("\n");
  97.     printf("\n");
  98.  
  99.     if (sup >min_sup && conf >min_conf){
  100.         printf("  Strong Association Rule \n");
  101.     }
  102.     else {
  103.         printf("  Not Strong Association Rule \n");
  104.     }
  105.  
  106.     printf("\n");
  107.  
  108.     e=(float (sum4)/float (Total));
  109.     f=(float (sum1)/float (Total));
  110.     g=(float (sum3)/float (Total));
  111.     L=e/(f*g);
  112.     printf("  Lift is %.2f \n",L);
  113.     if (L>1){printf("  Positively Correlated \n");}
  114.     else if (L<1){printf("  Negatively Correlated \n");}
  115.     else {printf("  Independent \n");}
  116.  
  117.     printf("\n");
  118.  
  119.     if(float (sum1) > float(sum3)){
  120.     all_conf=(float (sum4)/float (sum1));}
  121.     else
  122.     {all_conf=(float (sum4)/float (sum3));}
  123.  
  124.     printf("  All_Conf(x) is %.3f \n",all_conf);
  125.     if (all_conf>.5){printf("  Positively Correlated \n");}
  126.     else if (all_conf<.5){printf("  Negatively Correlated \n");}
  127.     else {printf("  Independent \n");}
  128.  
  129.     printf("\n");
  130.  
  131.     cosine=(float (sum4)/sqrt(float (sum1) * float (sum3)));
  132.     printf("  cosine(H,B) is %.2f \n",cosine);
  133.     if (cosine>.5){printf("  Positively Correlated \n");}
  134.     else if (cosine<.5){printf("  Negatively Correlated \n");}
  135.     else {puts("  Independent \n");}
  136.  
  137.     puts("-----------------X-----------------");
  138.         char ch;
  139.  
  140.         while(tolower(ch)!='y'||tolower(ch)!='n')
  141.         {
  142.             puts("\n\nDo you want to continue? (press Y/y for YES or N/n for NO)");
  143.             getchar();
  144.             scanf("%c",&ch);
  145.  
  146.  
  147.             if(tolower(ch)=='n'){puts("So, see you again. (o.o)");goto end;}
  148.             else if(tolower(ch)=='y'){puts("Ok lets fun again. :D");puts("___________________________________________________________\n");break;}
  149.         }
  150.  
  151.     }
  152.     end:
  153.     printf("\n");
  154.     printf("Developed By Zobayer Alam Pantho, ID:08092103012, Intake:11, Dept; Department of Computer Science and Engineering, BUBT");
  155.     printf("\n");
  156.     printf("\n");
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement