% Script File: plot2ChiB.m
% Generate two Chi-square Distributions r1=4, r2=8 and p1=1/7, p2=1-p1;
% Plot f(x;r)=\frac{1}{\Gamma(r/2)2^{r/2}}x^{r/2-1}e^{-x/2}, x>0
%
r1=4; r2=8; p1=1/7;  p2=1-p1;
X1=0:0.4:12;  X2=0:0.4:12;
Y1=X1.*exp(-X1/2)/4;
Y2=(X2.^3).*exp(-X2/2)/96;
tML=sqrt(24);                 % 2*sqrt(6)=2*2.4495
tBS=2;
x=tML;  y=x*exp(-x/2);        % tML=4.8990;
z=tBS;  w=(z^3)*exp(-z/2)/96; % tBS=2.0;
X3=zeros(11)+tML; Y3=0:0.02:0.2;  
X5=zeros(11)+tBS; Y5=0:0.02:0.2;
t=tML;
ErrML=p1*exp(-t/2)*(2*t+4)/4+p2*(1.0-exp(-t/2)*(2*t^3+12*t^2+48*t+96)/96);
t=tBS;
ErrBS=p1*exp(-t/2)*(2*t+4)/4+p2*(1.0-exp(-t/2)*(2*t^3+12*t^2+48*t+96)/96);
% ErrML, ErrBS
plot(X1,Y1,'b-',X2,Y2,'r-',X3,Y3,'g--',X5,Y5,'m--');
axis([0,12,0,0.24]);
legend('The decision boundry: tML=4.8990, tBS=2','Location','NorthEast');
legend('ErrML=0.2411, ErrBayes=0.1214','Location','NorthWest')
text(1.5,0.15,'\omega_1');  text(7.0,0.08,'\omega_2');
text(x,y,'<'); text(4.8,0.12,'ML');
text(z,w,'<'); text(1.8,0.12,'Bayes');
title('An Example of Two \chi^2 Distributions: r_1=4, r_2=8')
xlabel('The decision boundary: t_{BS}=4.8990, t_{ML}=2 with p_1=1/7, p_2=6/7')