royston.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "local_proto.h"
  5. /*-
  6. * driver program for AS 181: Royston's extension of the Shapiro-Wilk
  7. * W statistic to n=2000
  8. * needs as181.c as177.c as241.c Cdhc_dcmp.c as66.c
  9. */
  10. double *Cdhc_royston(double *x, int n)
  11. {
  12. static double y[2];
  13. double *a, eps, w, pw, mean = 0, ssq = 0, *xcopy;
  14. int i, ifault, n2;
  15. n2 = (int)floor((double)n / 2);
  16. #ifndef lint
  17. if ((a = (double *)malloc(n2 * sizeof(double))) == NULL) {
  18. fprintf(stderr, "Memory error in royston\n");
  19. exit(EXIT_FAILURE);
  20. }
  21. if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
  22. fprintf(stderr, "Memory error in royston\n");
  23. exit(EXIT_FAILURE);
  24. }
  25. #endif /* lint */
  26. for (i = 0; i < n; ++i) {
  27. xcopy[i] = x[i];
  28. mean += x[i];
  29. }
  30. mean /= n;
  31. qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
  32. for (i = 0; i < n; ++i)
  33. ssq += (mean - x[i]) * (mean - x[i]);
  34. wcoef(a, n, n2, &eps, &ifault);
  35. if (ifault == 0)
  36. wext(xcopy, n, ssq, a, n2, eps, &w, &pw, &ifault);
  37. else {
  38. fprintf(stderr, "Error in wcoef()\n");
  39. return (double *)NULL;
  40. }
  41. if (ifault == 0) {
  42. y[0] = w;
  43. y[1] = pw;
  44. }
  45. else {
  46. fprintf(stderr, "Error in wcoef()\n");
  47. return (double *)NULL;
  48. }
  49. free(a);
  50. free(xcopy);
  51. return y;
  52. }