随机变量的缩放例子

发布时间 2023-05-06 10:04:44作者: ChrainY
%% noncentral chi-squared dist.
rng(1);

N = 100000;
muv = [1 2];
k = numel(muv);
lambda = sum(muv.^2);
Z1 = randn(1, N) + muv(1);
Z2 = randn(1, N) + muv(2);
X = Z1.^2 + Z2.^2;

% test cdf
fig1 = figure(1);
clf(fig1)
fig1.Name = 'CDF';
[ff, xx] = ecdf(X);
plot(xx, ff)
hold on
p = ncx2cdf(xx, k, lambda);
plot(xx, p)

x1 = linspace(0, xx(end), 100);
cdft = 1 - marcumq(sqrt(lambda), sqrt(x1), k/2);
hold on
plot(x1, cdft)

% test pdf
fig2 = figure(2);
clf(fig2)
fig2.Name = 'PDF';
nbins = 100;
[Nc,edges] = histcounts(X, nbins, 'Normalization', 'pdf');
plot(edges(2:end), Nc);
hold on
Y = ncx2pdf(edges, k, lambda);
plot(edges, Y);

I = (1/2) .* exp(-(x1+lambda)/2) .* (x1/lambda).^(k/4 - 1/2) .* besseli(k/2 - 1, sqrt(lambda .* x1));
plot(x1, I);

%% scaling
sigma = 0.01;
mu = 0.5;
rr = [0.1, 0.2];

xk = randn(1, N)/sqrt(2);
yk = randn(1, N)/sqrt(2);

g = (sigma .* (sqrt(1 - mu.^2) .* xk + rr(1))).^2 + (sigma .* (sqrt(1 - mu.^2) .* yk + rr(2))).^2;
% test cdf
fig3 = figure(3);
clf(fig3)
fig3.Name = 'CDF';
[ff, xx] = ecdf(g);
plot(xx, ff)
hold on

scalingfactor = sigma^2 * (1 - mu^2) / 2;
lambda = sum(rr.^2) .* (2 / (1 - mu^2));
% p = ncx2cdf(xx/scalingfactor, k, lambda);
% plot(xx, p)

x1 = linspace(0, xx(end), 100);
cdft = 1 - marcumq(sqrt(lambda), sqrt(x1/scalingfactor), k/2);
hold on
plot(x1, cdft)

%test pdf
fig4 = figure(4);
clf(fig4)
fig4.Name = 'PDF';

nbins = 100;
[Nc,edges] = histcounts(g, nbins, 'Normalization', 'pdf');
plot(edges(2:end), Nc);
hold on
% Y = ncx2pdf(edges/scalingfactor, k, lambda) / scalingfactor;
% plot(edges, Y);

I = (1/2) .* exp(-(x1/scalingfactor+lambda)/2) .* ...
    (x1/(lambda * scalingfactor)).^(k/4 - 1/2) .* ...
    besseli(k/2 - 1, sqrt(lambda .* x1 / scalingfactor)) / scalingfactor;
plot(x1, I);