[Systolic Diastolic] = textread('NHANES_BP.txt','%d%d','headerlines',1);

x = Systolic;
y = Diastolic;

figure(1);
histogram(x,'BinWidth',10);
xlim([50 250]);
text(170,1800, sprintf('min    = %0.2f \n', min(x)),'FontSize',18);
text(170,1700, sprintf('Q1     = %0.2f \n', prctile(x,25)),'FontSize',18);
text(170,1600, sprintf('median = %0.2f \n', median(x)),'FontSize',18);
text(170,1500, sprintf('mean   = %0.2f \n', mean(x)),'FontSize',18);
text(170,1400, sprintf('Q3     = %0.2f \n', prctile(x,75)),'FontSize',18);
text(170,1300, sprintf('max    = %0.2f \n', max(x)),'FontSize',18);
title('Population');
xlabel('Systolic Pressure');
set(gca,'FontSize',18);

sprintf('min    = %0.2f \n', min(x))
sprintf('Q1     = %0.2f \n', prctile(x,25))
sprintf('median = %0.2f \n', median(x))
sprintf('mean   = %0.2f \n', mean(x))
sprintf('Q3     = %0.2f \n', prctile(x,75))
sprintf('max    = %0.2f \n', max(x))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

n = length(x);
indices = random('Discrete Uniform',n,100,1);
x100 = x(indices);
mp = max(180,0.7*max(x100));

figure(2);
histogram(x100,'BinWidth',10);
xlim([50 250]);
text(mp,23, sprintf('min    = %0.2f \n', min(x100)),'FontSize',18);
text(mp,21, sprintf('Q1     = %0.2f \n', prctile(x100,25)),'FontSize',18);
text(mp,19, sprintf('median = %0.2f \n', median(x100)),'FontSize',18);
text(mp,17, sprintf('mean   = %0.2f \n', mean(x100)),'FontSize',18);
text(mp,15, sprintf('Q3     = %0.2f \n', prctile(x100,75)),'FontSize',18);
text(mp,13, sprintf('max    = %0.2f \n', max(x100)),'FontSize',18);
title('Figure 2.11: Sample');
xlabel('Systolic Pressure');
set(gca,'FontSize',18);

sprintf('min    = %0.2f \n', min(x100))
sprintf('Q1     = %0.2f \n', prctile(x100,25))
sprintf('median = %0.2f \n', median(x100))
sprintf('mean   = %0.2f \n', mean(x100))
sprintf('Q3     = %0.2f \n', prctile(x100,75))
sprintf('max    = %0.2f \n', max(x100))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

n = length(x);
for j=1:1000
    indices = random('Discrete Uniform',n,100,1);
    x100 = x(indices);
    x100mean(j) = mean(x100);
end

figure(3);
histogram(x100mean,'BinWidth',1);
xlim([50 250]);
text(170,230, sprintf('min    = %0.2f \n', min(x100mean)),'FontSize',18);
text(170,210, sprintf('Q1     = %0.2f \n', prctile(x100mean,25)),'FontSize',18);
text(170,190, sprintf('median = %0.2f \n', median(x100mean)),'FontSize',18);
text(170,170, sprintf('mean   = %0.2f \n', mean(x100mean)),'FontSize',18);
text(170,150, sprintf('Q3     = %0.2f \n', prctile(x100mean,75)),'FontSize',18);
text(170,130, sprintf('max    = %0.2f \n', max(x100mean)),'FontSize',18);
title('Figure 2.12: Sample Mean');
xlabel('Systolic Pressure');
set(gca,'FontSize',18);

sprintf('min    = %0.2f \n', min(x100mean))
sprintf('Q1     = %0.2f \n', prctile(x100mean,25))
sprintf('median = %0.2f \n', median(x100mean))
sprintf('mean   = %0.2f \n', mean(x100mean))
sprintf('Q3     = %0.2f \n', prctile(x100mean,75))
sprintf('max    = %0.2f \n', max(x100mean))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The is bootstrap

n = length(x);
indices = random('Discrete Uniform',n,100,1);
x100 = x(indices);
for j=1:1000
    indices = random('Discrete Uniform',100,100,1);
    x100resampled = x100(indices);
    x100mean(j) = mean(x100resampled);
end

figure(4);
histogram(x100mean,'BinWidth',1);
xlim([50 250]);
text(170,200, sprintf('min    = %0.2f \n', min(x100mean)),'FontSize',18);
text(170,180, sprintf('Q1     = %0.2f \n', prctile(x100mean,25)),'FontSize',18);
text(170,160, sprintf('median = %0.2f \n', median(x100mean)),'FontSize',18);
text(170,140, sprintf('mean   = %0.2f \n', mean(x100mean)),'FontSize',18);
text(170,120, sprintf('Q3     = %0.2f \n', prctile(x100mean,75)),'FontSize',18);
text(170,100, sprintf('max    = %0.2f \n', max(x100mean)),'FontSize',18);
title('Figure 2.13: Bootstrap');
xlabel('Means of Bootstrapped samples');
set(gca,'FontSize',18);

sprintf('min    = %0.2f \n', min(x100mean))
sprintf('Q1     = %0.2f \n', prctile(x100mean,25))
sprintf('median = %0.2f \n', median(x100mean))
sprintf('mean   = %0.2f \n', mean(x100mean))
sprintf('Q3     = %0.2f \n', prctile(x100mean,75))
sprintf('max    = %0.2f \n', max(x100mean))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The is bootstrap for pct Americans w/ high blood pressure

n = length(x);
indices = random('Discrete Uniform',n,100,1);
x100 = x(indices);
for j=1:1000
    indices = random('Discrete Uniform',100,100,1);
    x100resampled = x100(indices);
    pct(j) = 100*mean(x100resampled>140);
end
mp = 0.7*max(pct);

figure(5);
histogram(pct);
text(mp,120, sprintf('min    = %0.2f \n', min(x100mean)),'FontSize',18);
text(mp,110, sprintf('Q1     = %0.2f \n', prctile(x100mean,25)),'FontSize',18);
text(mp,100, sprintf('median = %0.2f \n', median(x100mean)),'FontSize',18);
text(mp,90, sprintf('mean   = %0.2f \n', mean(x100mean)),'FontSize',18);
text(mp,80, sprintf('Q3     = %0.2f \n', prctile(x100mean,75)),'FontSize',18);
text(mp,70, sprintf('max    = %0.2f \n', max(x100mean)),'FontSize',18);
title('Figure 2.14: Bootstrap');
xlabel('Percent of Adults with Systolic Pressure > 140');
set(gca,'FontSize',18);

sprintf('min    = %0.2f \n', min(pct))
sprintf('2.5pct = %0.2f \n', prctile(pct, 2.5))
sprintf('Q1     = %0.2f \n', prctile(pct,25))
sprintf('median = %0.2f \n', median(pct))
sprintf('mean   = %0.2f \n', mean(pct))
sprintf('Q3     = %0.2f \n', prctile(pct,75))
sprintf('97.5pct= %0.2f \n', prctile(pct,97.5))
sprintf('max    = %0.2f \n', max(pct))

