卡平方测试

时间:2014-07-05 07:29:00

标签: matlab statistics chi-squared weibull goodness-of-fit

我在MATLAB中为Chi-Square test编写了代码。我希望得到P值为0.897或0.287等等,但我的结果太小了。以下是我的代码:

pd = fitdist(sample, 'weibull');
[h,p,st] = chi2gof(sample,'CDF',pd)

我也尝试过使用类似结果的AD test

dist = makedist('Weibull', 'a',A, 'b',B);
[h,p,ad,cv] = adtest(sample, 'Distribution',dist)

下面是具有拟合Weibull密度函数的数据的直方图(Weibull参数为A=4.0420B=2.0853

histfit

1 个答案:

答案 0 :(得分:2)

p-value小于预定significance level时(默认为5%或0.05),这意味着null hypotheses被拒绝(在您的情况下,这意味着样本确实不是来自Weibull distribution)。

chi2gof函数第一个输出变量h表示测试结果,其中h=1表示测试拒绝指定显着性水平的原假设。

实施例

sample = rand(1000,1);           % sample from Uniform distribution
pd = fitdist(sample, 'weibull');
[h,p,st] = chi2gof(sample, 'CDF',pd, 'Alpha',0.05)

测试明确拒绝H0,并得出结论,数据并非来自Weibull分布:

h =
     1             % 1: H1 (alternate hypo), 0: H0 (null hypo)

p =
   2.8597e-27      % note that p << 0.05

st = 
    chi2stat: 141.1922
          df: 7
       edges: [0.0041 0.1035 0.2029 0.3023 0.4017 0.5011 0.6005 0.6999 0.7993 0.8987 0.9981]
           O: [95 92 92 97 107 110 102 95 116 94]
           E: [53.4103 105.6778 130.7911 136.7777 129.1428 113.1017 93.1844 72.8444 54.3360 110.7338]

接下来让我们再次尝试使用符合标准的样本:

>> sample = wblrnd(0.5, 2, [1000,1]);   % sample from a Weibull distribution

>> pd = fitdist(sample, 'weibull')
pd = 
  WeibullDistribution

  Weibull distribution
    A = 0.496413   [0.481027, 0.512292]
    B =  2.07314   [1.97524, 2.17589]

>> [h,p] = chi2gof(sample, 'CDF',pd, 'Alpha',0.05)
h =
     0
p =
    0.7340
现在,测试明显通过了高p值。


编辑:

查看您显示的直方图,看起来数据遵循Weibull分布,尽管可能存在异常值的情况(查看直方图的右侧),这可能是解释你为什么会得到糟糕的p值。考虑预处理数据以处理极端异常值。

以下是我模拟异常值的示例:

% 5000 samples from a Weibull distribution
pd = makedist('Weibull', 'a',4.0420, 'b',2.0853);
sample = random(pd, [5000 1]);
%sample = wblrnd(4.0420, 2.0853, [5000 1]);

% add 20 outlier instances
sample(1:20) = [rand(10,1)+15; rand(10,1)+25];

% hypothesis tests using original distribution
[h,p,st] = chi2gof(sample, 'CDF',pd, 'Alpha',0.05)
[h,p,ad,cv] = adtest(sample, 'Distribution',pd)

% hypothesis tests using empirical distribution
[h,p,st] = chi2gof(sample, 'CDF',fitdist(sample,'Weibull'))
[h,p,ad,cv] = adtest(sample, 'Distribution', 'Weibull')

% show histogram
histfit(sample, 20, 'Weibull')

histfit

% chi-squared test
h =
     1
p =
    0.0382
st = 
    chi2stat: 8.4162
          df: 3
       edges: [0.1010 2.6835 5.2659 7.8483 25.9252]
           O: [1741 2376 764 119]
           E: [1.7332e+03 2.3857e+03 788.6020 92.5274]


% AD test
h =
     1
p =
   1.2000e-07
ad =
   Inf
cv =
    2.4924

异常值导致分布测试失败(零假设被拒绝)。我仍然无法重现获得NaN p值(你可能想在Stats.SE上查看这个related question关于获得NaN p值的信息。)