%Example to understand the meaning of confidence intervals %Generate a Gaussian population of 10000 points, with true mean=1 and %std=2: x=1+2*randn(10000,1); %randn itself creates points with zero mean and variance 1. %Multiplying by 2 and adding one shifts the mean to 1 and the std to 2. pop_mean=mean(x); pop_std=std(x,1); %Use "1" to divide by n, since we are calculating the true population std %These are not exactly 1 and 2, since x is mathematically still a sample!!! %Now take a series of 100 random samples, each having 61 points, %and calculate the 95% confidence intervals for the mean in each case. %From Holman, Table 3.7, we see that the t-value for 60 degrees of %freedom (sample size minus 1) is found for 95% as: t=2; in_bracket_count=0; for i=1:100, index_set = ceil(10000.*rand(61,1)); sample=x(index_set); sample_mean=mean(sample); sample_std=std(sample); %Now we divide by n-1, since it's a sample! bracket_delta=t*sample_std/sqrt(61); low_limit=sample_mean-bracket_delta; hi_limit=sample_mean+bracket_delta; if (pop_mean>=low_limit & pop_mean<=hi_limit), in_bracket_count=in_bracket_count+1; end end %Now find the percentage of samples whose confidence intervals contained %the population mean: in_bracket_count