% eda03_05
% the all these column-vectors have 40 rows
N=40;

% data, d
Dd=1.0;
d = Dd*[0:N]';

% distribution, p(d)
p = sin(pi*d/d(N))./(d+10);  % cooked up to be skewed
p = p/(Dd*sum(p)); % normalize

% mean
dbar = Dd*sum(d.*p);

% quadratic
q = (d-dbar).^2;

% variance
qp = q.*p;
sigma2 = Dd*sum(qp);
sigma = sqrt(sigma2)

% Set up graphics
figure(1);
clf;
set(gca,'LineWidth',2);
hold on;
axis([-N/4, 7*N/4, 0, 2*N]);
axis equal;
set(gca,'XTick',[]); % turn off horizontal axis
set(gca,'YTick',[]); % turn off vertical axis

% black and white color map
bw=0.9*(256-linspace(0,255,256)')/256;
colormap([bw,bw,bw]);
hold on;

% plot the vecror p
range=max(p)-min(p);
if( range == 0.0 )
    range=1;
end
imagesc( [1, N/16], [N-1, 0], (p-min(p))/range);
text(N/16,-N/16,'p');
hold on;

% plot the vecror q
range=max(q)-min(q);
if( range == 0.0 )
    range=1;
end
imagesc( [1+N/4, N/16+N/4], [N-1, 0], (q-min(q))/range);
text(N/32+N/4,-N/16,'q');
hold on;

% plot the vecror qp
range=max(qp)-min(qp);
if( range == 0.0 )
    range=1;
end
imagesc( [1+2*N/4, N/16+2*N/4], [N-1, 0], (qp-min(qp))/range);
text(N/32+2*N/4,-N/16,'pq');
hold on;

% plot mean
y = dbar;
plot( [-N/8, 6*N/8], [N-y/Dd, N-y/Dd], 'k-', 'LineWidth', 2 );
text( 6*N/8, N-y/Dd, 'xbar');
y=dbar-sigma;
plot( [-N/8, 6*N/8], [N-y/Dd, N-y/Dd], 'k-', 'LineWidth', 2 );
text( 6*N/8, N-y/Dd, 'xbar-sigma');
y=dbar+sigma;
plot( [-N/8, 6*N/8], [N-y/Dd, N-y/Dd], 'k-', 'LineWidth', 2 );
text( 6*N/8, N-y/Dd, 'xbar+sigma');
