# Chi-square goodness-of-fit test of Rayleighchannel

Discussion in 'MATLAB' started by Nicol?s Vignale, Sep 14, 2007.

1. ### Nicol?s VignaleGuest

I´m doing a Fading Channel (rayleighchan) and i would like
test the equivalence of the probability density function of
the sampled data to Rayleigh density function with a "chi-
square goodness-of-fit test" (chi2gof).

The hypothesis of Rayleigh is allways rejected ! Although
the Histogram looks like Rayleigh Way?

Nicol?s Vignale, Sep 14, 2007

2. ### NicolasGuest

Here goes my code:

sigma = 1/sqrt(2);
c1 = rayleighchan; % Create object.
c1.InputSamplePeriod = 1/2400;
c1.PathDelays = [0 2e-3]; % number of delays.
c1.MaxDopplerShift = 10; % Hz
c1.DopplerSpectrum = ...
doppler.gaussian(sigma*c1.MaxDopplerShift);
c1.ResetBeforeFiltering = 1;
t = [0:c1.InputSamplePeriod10000*c1.InputSamplePeriod)];
y = [ones(1, length(t))*i]'; % input signal
post_channel = filter(c1, y); % output signal
figure;
[n, xout] = hist(abs(post_channel), 20);
bar(xout, n);

% Chi Square Test
[h,p, stats] = ...
chi2gof(abs(post_channel),'cdf',{@raylcdf}, 'nbins', 30)

if (h == 1)
disp('Rejected');
else
disp('Accepted')
end

Nicolas, Sep 14, 2007

3. ### Tom LaneGuest

I´m doing a Fading Channel (rayleighchan) and i would like
Nicolas, I'm not familiar with rayleighchan. But if I run your code from
the other post and fit a Rayleigh distribution, I get this:
ans =
0.7010

You compared against a standard Rayleigh. To compare against the
distribution with this parameter value, you can use:
h =
0
p =
0.0956
stats =
chi2stat: 36.9709
df: 27
edges: [1x30 double]
O: [1x29 double]
E: [1x29 double]

The 'nparams' parameter tells the chi2gof function to take into account that
the parameter has been estimated from the data.

-- Tom

Tom Lane, Sep 14, 2007
4. ### Peter PerkinsGuest

Nicholas, your code is comparing to the "standard" Rayleigh, i.e., with
parameter 1. You almost certainly want to compare against the best-fit
Rayleigh. I think this is what you want:

x = abs(post_channel);
p = raylfit(x)
[h,p, stats] = chi2gof(x,'cdf',@(x) raylcdf(x,p), 'nbins', 30, 'nparams',2)

I get a p-value of .075, which still might seem small, given, for example, the
CDF plot you can make by reading abs(post_channel) into the DFITTOOL GUI,
fitting a Rayleigh dist'n, and plotting the fitted CDF against the empirical CDF
(an exercise that I recommend).

The small p-value is, I believe, due to a standard phenomenon when you have a
large amount of data -- small departures from the theoretical dist'n, departures
that have no _practical_ significance, make the data seem "unlikely" by a
probabilistic measure used in the chi-squared test, simply because the large
amount of data give the test such a high power, and thus the departures are
given _statistical_ significance.

Run the same test with several subsample of size, say, 1000, generated using
RANDSAMPLE. You'll find the p-values are typically larger. Fewer data, lower
power.

Hope this helps.

- Peter Perkins
The MathWorks, Inc.

Peter Perkins, Sep 14, 2007
5. ### NicolasGuest

Tom, Peter;

Peter: Why have you used nparams = 2 instead of 1?
chi2gof(x,'cdf',@(x) raylcdf(x,p), 'nbins', 30, 'nparams',2)

Nicolas, Sep 14, 2007
6. ### Peter PerkinsGuest

'cause I'm a dope. Cut and paste error. Should be 1, of course.

Peter Perkins, Sep 14, 2007
7. ### ZhihongGuest

Zhihong, Nov 8, 2007