声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 4893|回复: 1

[编程技巧] 有人知道matlab的Anderson-Darling检验(A-D检验)程序吗?

[复制链接]
发表于 2008-6-19 19:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
如题:A-D检验方法我知道,但是自己编程的话有点复杂。
应该有matlab的AndersoA-D检验)函数吧?
回复
分享到:

使用道具 举报

发表于 2008-6-20 13:41 | 显示全部楼层
  1. function [AnDartest] = AnDartest(x,alpha)
  2. %ANDARTEST Anderson-Darling test for assessing normality of a sample data.
  3. % The Anderson-Darling test (Anderson and Darling, 1952) is used to test if
  4. % a sample of data comes from a specific distribution. It is a modification
  5. % of the Kolmogorov-Smirnov (K-S) test and gives more weight to the tails
  6. % than the K-S test. The K-S test is distribution free in the sense that the
  7. % critical values do not depend on the specific distribution being tested.
  8. % The Anderson-Darling test makes use of the specific distribution in calculating
  9. % critical values. This has the advantage of allowing a more sensitive test
  10. % and the disadvantage that critical values must be calculated for each
  11. % distribution.
  12. % The Anderson-Darling test is only available for a few specific distributions.
  13. % The test is calculated as:
  14. %              
  15. %        AD2 = integral{[F_o(x)-F_t(x)]^2/[F_t(x)(1-F_t(x)0]}dF_t(x)
  16. %
  17. %        AD2a = AD2*a
  18. %
  19. % Note that for a given distribution, the Anderson-Darling statistic may be
  20. % multiplied by a constant, a (which usually depends on the sample size, n).
  21. % These constants are given in the various papers by Stephens (1974, 1977a,
  22. % 1977b, 1979, 1986). This is what should be compared against the critical
  23. % values. Also, be aware that different constants (and therefore critical
  24. % values) have been published. You just need to be aware of what constant
  25. % was used for a given set of critical values (the needed constant is typically
  26. % given with the critical values).
  27. % The critical values for the Anderson-Darling test are dependent on the
  28. % specific distribution that is being tested. Tabulated values and formulas
  29. % have been published for a few specific distributions (normal, lognormal,
  30. % exponential, Weibull, logistic, extreme value type 1). The test is a one-sided
  31. % test and the hypothesis that the distribution is of a specific form is
  32. % rejected if the test statistic, AD2a, is greater than the critical value.
  33. % Here we develop the m-file for detecting departure from normality. It is
  34. % one of the most powerful statistics for test this.
  35. %
  36. % Syntax: function AnDartest(X)
  37. %      
  38. %     Input:
  39. %          x - data vector
  40. %      alpha - significance level (default = 0.05)
  41. %
  42. %     Output:
  43. %            - Complete Anderson-Darling normality test
  44. %
  45. % Example: From the Table 1 base data of secondary coating diameter, SCD
  46. %    (micrometers) for an optical fiber manufacturing and testing [Application
  47. %    and acceptance sampling in testing of optical fiber (Bhaumik and Bhargava,
  48. %    2005)], taked from Internet:
  49. %    http://www.sterliteoptical.com/pdf/ProdWithContents/Application%20of%
  50. %    20acceptance%20sampling%20in%20testing%20of%20optical%20fiber.pdf
  51. %    The measurements of the sample are analyzed for the normality using the
  52. %    Anderson-Darling test with a significance of 0.05.
  53. %   
  54. % Data vector is:
  55. %  x=[245.3;245.6;245.0;244.7;244.6;244.6;245.9;245.2;245.5;246.1;246.5;246.7;
  56. %  246.0;245.8;245.3;245.3;246.0;245.5;246.7;245.3;245.8;245.8;245.2;245.7;
  57. %  244.8;246.7;246.5;245.5;246.0;244.8;244.8;244.4;244.8;245.6;245.5;244.0;
  58. %  245.2;244.8;245.4;246.1;245.9;245.6;245.2];
  59. %
  60. % Calling on Matlab the function:
  61. %            AnDartest(x)
  62. %
  63. % Answer is:
  64. %
  65. % Sample size: 43
  66. % Anderson-Darling statistic: 0.2858
  67. % Anderson-Darling adjusted statistic: 0.2912
  68. % Probability associated to the Anderson-Darling statistic = 0.6088
  69. % With a given significance = 0.050
  70. % The sampled population is normally distributed.
  71. % Thus, this sample have been drawn from a normal population with a mean & variance = 245.4814    0.4139
  72. %
  73. % Created by A. Trujillo-Ortiz, R. Hernandez-Walls, K. Barba-Rojo and
  74. %             A. Castro-Perez
  75. %             Facultad de Ciencias Marinas
  76. %             Universidad Autonoma de Baja California
  77. %             Apdo. Postal 453
  78. %             Ensenada, Baja California
  79. %             Mexico.
  80. %             atrujo@uabc.mx
  81. %
  82. % Copyright. April 20, 2007.
  83. %
  84. % To cite this file, this would be an appropriate format:
  85. % Trujillo-Ortiz, A., R. Hernandez-Walls, K. Barba-Rojo and A. Castro-Perez. (2007).
  86. %   AnDartest:Anderson-Darling test for assessing normality of a sample data.
  87. %   A MATLAB file. [WWW document]. URL http://www.mathworks.com/matlabcentral/
  88. %   fileexchange/loadFile.do?objectId=14807
  89. %
  90. % References:
  91. %  Anderson, T. W. and Darling, D. A. (1952), Asymptotic theory of certain
  92. %      'goodness-of-fit' criteria based on stochastic processes. Annals of
  93. %      Mathematical Statistics, 23:193-212.
  94. %  Stephens, M. A. (1974), EDF Statistics for goodness of fit and some
  95. %      comparisons. Journal of the American Statistical Association,
  96. %      69:730-737.
  97. %  Stephens, M. A. (1976), Asymptotic Results for goodness-of-fit statistics
  98. %      with unknown parameters. Annals of Statistics, 4:357-369.
  99. %  Stephens, M. A. (1977a), Goodness of fit for the extreme value distribution.
  100. %      Biometrika, 64:583-588.
  101. %  Stephens, M. A. (1977b), Goodness of fit with special reference to tests
  102. %      for exponentiality. Technical Report No. 262, Department of Statistics,
  103. %      Stanford University, Stanford, CA.
  104. %  Stephens, M. A. (1979), Tests of fit for the logistic distribution based
  105. %      on the empirical distribution function. Biometrika, 66:591-595.
  106. %  Stephens, M. A. (1986), Tests based on EDF statistics. In: D'Agostino,
  107. %      R.B. and Stephens, M.A., eds.: Goodness-of-Fit Techniques. Marcel
  108. %      Dekker, New York.
  109. %

  110. switch nargin
  111.     case{2}
  112.         if isempty(x) == false && isempty(alpha) == false
  113.             if (alpha <= 0 || alpha >= 1)
  114.                 fprintf('Warning: Significance level error; must be 0 < alpha < 1 \n');
  115.                 return;
  116.             end
  117.         end
  118.     case{1}
  119.         alpha = 0.05;
  120.     otherwise
  121.         error('Requires at least one input argument.');
  122. end

  123. n = length(x);

  124. if n < 7,
  125.     disp('Sample size must be greater than 7.');
  126.     return,
  127. else
  128.     x = x(:);
  129.     x = sort(x);
  130.     fx = normcdf(x,mean(x),std(x));
  131.     i = 1:n;
  132.    
  133.     S = sum((((2*i)-1)/n)*(log(fx)+log(1-fx(n+1-i))));
  134.     AD2 = -n-S;
  135.    
  136.     AD2a = AD2*(1 + 0.75/n + 2.25/n^2); %correction factor for small sample sizes: case normal
  137.    
  138.     if (AD2a >= 0.00 && AD2a < 0.200);
  139.         P = 1 - exp(-13.436 + 101.14*AD2a - 223.73*AD2a^2);
  140.     elseif (AD2a >= 0.200 && AD2a < 0.340);
  141.         P = 1 - exp(-8.318 + 42.796*AD2a - 59.938*AD2a^2);
  142.     elseif (AD2a >= 0.340 && AD2a < 0.600);
  143.         P = exp(0.9177 - 4.279*AD2a - 1.38*AD2a^2);
  144.     else (AD2a >= 0.600 && AD2a <= 13);
  145.         P = exp(1.2937 - 5.709*AD2a + 0.0186*AD2a^2);
  146.     end
  147. end

  148. disp(' ')
  149. fprintf('Sample size: %i\n', n);
  150. fprintf('Anderson-Darling statistic: %3.4f\n', AD2);
  151. fprintf('Anderson-Darling adjusted statistic: %3.4f\n', AD2a);
  152. fprintf('Probability associated to the Anderson-Darling statistic = %3.4f\n', P);
  153. fprintf('With a given significance = %3.3f\n', alpha);
  154. if P >= alpha;
  155.    disp('The sampled population is normally distributed.');
  156.    s = [mean(x),var(x)];
  157.    fprintf('Thus, this sample have been drawn from a normal population with a mean & variance = %6.4f  %8.4f\n',s);
  158. else
  159.    disp('The sampled population is not normally distributed.');
  160. end

  161. return,
复制代码

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-29 04:22 , Processed in 0.062480 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表