[bootstrap] finished code for new exercises

This commit is contained in:
Jan Benda 2020-12-13 23:35:37 +01:00
parent c9dd1ffbe6
commit af45ae7f9e
5 changed files with 95 additions and 66 deletions

View File

@ -37,7 +37,7 @@ jan.benda@uni-tuebingen.de}
commentstyle=\itshape\color{darkgray},
breaklines=true,
breakautoindent=true,
columns=flexible,
% columns=flexible,
frame=single,
xleftmargin=1em,
xrightmargin=1em,
@ -239,7 +239,7 @@ differ in their mean by means of a permutation test.
\end{parts}
\begin{solution}
\lstinputlisting{meandiffpermutation.m}
%\lstinputlisting{meandiffplots.m}
\lstinputlisting{meandiffplot.m}
\lstinputlisting{meandiffsignificance.m}
\includegraphics[width=1\textwidth]{meandiffsignificance}
\end{solution}

View File

@ -1,7 +1,7 @@
function [md, ds, dq] = meandiffpermutation(x, y, nperm, alpha)
% Permutation test for difference of means of two independent samples.
%
% [md, ds] = meandiffpermutation(x, y, nperm, alpha);
% [md, ds, dq] = meandiffpermutation(x, y, nperm, alpha);
%
% Arguments:
% x: vector with the samples of the x data set.

View File

@ -0,0 +1,40 @@
function meandiffplot(x, y, md, ds, dq, k, nrows)
% Plot histogram of data sets and of null hypothesis for differences in mean.
%
% meandiffplot(x, y, md, ds, dq, k, rows);
%
% Arguments:
% x: vector with the samples of the x data set.
% y: vector with the samples of the y data set.
% md: difference of means of the two data sets.
% ds: vector containing the differences of the means of the resampled data sets
% dq: minimum difference of the means considered significant.
% k: current row for the plot panels.
% nrows: number of rows of panels in the figure.
%% (b) plot histograms:
subplot(nrows, 2, k*2-1);
bmin = min([x; y]);
bmax = max([x; y]);
bins = bmin:(bmax-bmin)/20.0:bmax;
hist(x, bins, 'facecolor', 'b');
hold on
hist(y, bins, 'facecolor', 'r');
xlabel('x and y')
ylabel('counts')
hold off
%% (f) pdf of the differences:
[h, b] = hist(ds, 20);
h = h/sum(h)/(b(2)-b(1)); % normalization
%% plot:
subplot(nrows, 2, k*2)
bar(b, h, 'facecolor', 'b');
hold on;
bar(b(b>=dq), h(b>=dq), 'facecolor', 'r');
plot([md md], [0 4], 'r', 'linewidth', 2);
xlabel('Difference of means');
ylabel('Probability density of H0');
hold off;
end

View File

@ -1,48 +1,37 @@
%% (a) generate data:
n = 200;
mx = -40.0;
my = -40.5;
nperm = 1000;
alpha = 0.05;
%% (h) repeat for various means of the y-data set:
mys = [-40.1, -40.2, -40.5];
for k=1:length(mys)
%% (a) generate data:
my = mys(k);
x = randn(n, 1) + mx;
y = randn(n, 1) + my;
%% (b) plot histograms:
subplot(1, 2, 1);
bmin = min([x; y]);
bmax = max([x; y]);
bins = bmin:(bmax-bmin)/20.0:bmax;
hist(x, bins, 'facecolor', 'b');
hold on
hist(y, bins, 'facecolor', 'r');
xlabel('x and y')
ylabel('counts')
hold off
% permutation test:
%% (d), (e) permutation test:
[md, ds, dq] = meandiffpermutation(x, y, nperm, alpha);
%% (c) difference of means:
fprintf('\nmean x = %.1fmV, mean y = %.1fmV\n', mx, my);
fprintf(' difference of means = %.2fmV\n', md);
%% (f) pdf of the differences:
[h, b] = hist(ds, 20);
h = h/sum(h)/(b(2)-b(1)); % normalization
%% (g) significance:
fprintf(' difference of means at 5%% significance = %.2fmV\n', dq);
if md >= dq
fprintf('--> difference of means %.2fmV is significant\n', md);
fprintf(' --> measured difference of means is significant\n');
else
fprintf('--> %.2fmV is not a significant difference of means\n', md);
fprintf(' --> measured difference of means is not significant\n');
end
subplot(length(mys), 2, k*2-1);
title(sprintf('mx=%.1fmV, my=%.1fmV', mx, my))
%% (b), (f) plot histograms of data and pdf of differences:
meandiffplot(x, y, md, ds, dq, k, length(mys));
end
%% plot:
subplot(1, 2, 2)
bar(b, h, 'facecolor', 'b');
hold on;
bar(b(b>=dq), h(b>=dq), 'facecolor', 'r');
plot([md md], [0 4], 'r', 'linewidth', 2);
xlabel('Difference of means');
ylabel('Probability density of H0');
hold off;
savefigpdf(gcf, 'meandiffsignificance.pdf', 12, 6);
savefigpdf(gcf, 'meandiffsignificance.pdf', 12, 10);