%WSiRTexact
% needs symbolic package for MATLAB
syms t
n = 30
k=0:(n*(n+1)/2);
%
%probability generating function
a=2^(-n)* prod( 1+t.^(1:n));
co = coeffs(expand(a));
%table of [k P(T=k)] for k=0,1,..., n*(n+1)/2
[k'  (fliplr(co))']
format long
%cdf for m, somewhere in the middle
m=floor(n*(n+1)/4);
double(sum(co(1:m+1))) %exact...+1 added since co starts with 0, not 1
normcdf(m+1, n*(n+1)/4,  sqrt(n*(n+1)*(2*n+1)/24)) %normal approximation

 % find P(T=m)
double(co(m+1))  % exact
normcdf(m+1.5, n*(n+1)/4,  sqrt(n*(n+1)*(2*n+1)/24))- ...
normcdf(m+0.5, n*(n+1)/4,  sqrt(n*(n+1)*(2*n+1)/24)) %normal approximation

close all force
figure(1)
n=7;
k=0:(n*(n+1)/2);
co=coeffs(expand(2^(-n)* prod( 1+t.^(1:n))));
stairs( k, cumsum(co),'linewidth',2)
hold on
plot( k, normcdf(k, n*(n+1)/4,  sqrt(n*(n+1)*(2*n+1)/24)),'r-','linewidth',2)
axis tight
