diff --git a/bootstrap/exercises/exercises01.tex b/bootstrap/exercises/exercises01.tex index dd2380d..12ffb94 100644 --- a/bootstrap/exercises/exercises01.tex +++ b/bootstrap/exercises/exercises01.tex @@ -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} diff --git a/bootstrap/exercises/meandiffpermutation.m b/bootstrap/exercises/meandiffpermutation.m index f8fb58a..5b02a53 100644 --- a/bootstrap/exercises/meandiffpermutation.m +++ b/bootstrap/exercises/meandiffpermutation.m @@ -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. diff --git a/bootstrap/exercises/meandiffplot.m b/bootstrap/exercises/meandiffplot.m new file mode 100644 index 0000000..24fd625 --- /dev/null +++ b/bootstrap/exercises/meandiffplot.m @@ -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 diff --git a/bootstrap/exercises/meandiffsignificance.m b/bootstrap/exercises/meandiffsignificance.m index cf6586d..e234270 100644 --- a/bootstrap/exercises/meandiffsignificance.m +++ b/bootstrap/exercises/meandiffsignificance.m @@ -1,48 +1,37 @@ -%% (a) generate data: n = 200; mx = -40.0; -my = -40.5; -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: -[md, ds, dq] = meandiffpermutation(x, y, nperm, alpha); - -%% (c) difference of means: -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); -else - fprintf('--> %.2fmV is not a significant difference of means\n', md); +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; + + %% (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); + + %% (g) significance: + fprintf(' difference of means at 5%% significance = %.2fmV\n', dq); + if md >= dq + fprintf(' --> measured difference of means is significant\n'); + else + 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); diff --git a/bootstrap/exercises/savefigpdf.m b/bootstrap/exercises/savefigpdf.m index fbe2dc2..97a28a3 100644 --- a/bootstrap/exercises/savefigpdf.m +++ b/bootstrap/exercises/savefigpdf.m @@ -1,28 +1,28 @@ -function savefigpdf( fig, name, width, height ) +function savefigpdf(fig, name, width, height) % Saves figure fig in pdf file name.pdf with appropriately set page size % and fonts -% default width: -if nargin < 3 - width = 11.7; -end -% default height: -if nargin < 4 - height = 9.0; -end + % default width: + if nargin < 3 + width = 11.7; + end + % default height: + if nargin < 4 + height = 9.0; + end -% paper: -set( fig, 'PaperUnits', 'centimeters' ); -set( fig, 'PaperSize', [width height] ); -set( fig, 'PaperPosition', [0.0 0.0 width height] ); -set( fig, 'Color', 'white') + % paper: + set(fig, 'PaperUnits', 'centimeters'); + set(fig, 'PaperSize', [width height]); + set(fig, 'PaperPosition', [0.0 0.0 width height]); + set(fig, 'Color', 'white') -% font: -set( findall( fig, 'type', 'axes' ), 'FontSize', 12 ) -set( findall( fig, 'type', 'text' ), 'FontSize', 12 ) + % font: + set(findall(fig, 'type', 'axes'), 'FontSize', 12) + set(findall(fig, 'type', 'text'), 'FontSize', 12) -% save: -saveas( fig, name, 'pdf' ) + % save: + saveas(fig, name, 'pdf') end