
clear all
close all

  disp('Definition of Chi2_n via a Sum of Squares of n Gaussians')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 16);
  fs = 15;
  msize = 10;
  

%Demonstrate by simulation that 
% if X_1, ..., X_n are iid N(0,1) -> sum(X_i^2) ~ chi2_n
% needs: histn.m  [normalized histogram]
figure(3)
subplot(2,1,1)
histn(sum(normrnd(0,1,[10, 10000]).^2),0, 1,30)
hold on
plot((0.1:0.1:30), chi2pdf((0.1:0.1:30),10),'r-','LineWidth',2)
axis tight
subplot(2,1,2)
xx = sum(normrnd(0,1,[10, 10000]).^2);
tt = 0.5/10000:1/10000:1;
yy = chi2inv(tt,10);
plot(sort(xx), yy,'*')
hold on
plot(yy, yy,'r-')
%print -depsc nor2chi2.eps
print -depsc 'C:\BESTAT\Norm\Normeps\nor2chi2.eps'