Download Question 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Question2
function f = gaussian(xvals, m, stdev)
% Lecture 2 Problem Set Question 2
% Generates a gaussian distribution given x values, mean and std
f = 1/(stdev*sqrt(2*pi))*exp((-1/2)*((xvals-m)/stdev)^2);
end
xvals = -6:12/10000:6;
f = gaussian(xvals, 0, 1);
cdf_gauss = cumsum(f)*12/10000;
plot(xvals,cdf_gauss)
title('CDF')
ylabel('Probability')
set(gca,'FontSize',16)
% 2a
p1sd = 1-cdf_gauss(round(10000/12*7)+1);
disp(['P(1 SD more than mean) = ' num2str(p1sd)])
%2b
p2sd = 1-cdf_gauss(round(10000/12*8)+1);
disp(['P(2 SD more than mean) = ' num2str(p2sd)])
%2c
p3sd = 1-cdf_gauss(round(10000/12*9)+1);
disp(['P(3 SD more than mean) = ' num2str(p3sd)])
P(1 SD more than mean) = 0.15861
P(2 SD more than mean) = 0.022696
P(3 SD more than mean) = 0.0013472
Question3
function vals = drawfromcdf(cdf, xvals, n)
tmp = find(diff(cdf)==0);
cdf(tmp)=[];
xvals(tmp)=[];
x = rand(n,1);
vals = interp1(cdf, xvals, x);
end
vals = drawfromcdf(cdf_gauss, xvals, 1000);
disp(['P(1 SD more than mean) = ' num2str(sum(vals>1)/1000)])
disp(['P(2 SD more than mean) = ' num2str(sum(vals>2)/1000)])
disp(['P(3 SD more than mean) = ' num2str(sum(vals>3)/1000)])
P(1 SD more than mean) = 0.146
P(2 SD more than mean) = 0.019
P(3 SD more than mean) = 0.001
Question4
function f = binom(k, p, n)
f = zeros(length(k),1);
for i = 1:length(k)
f(i) = nchoosek(n,k(i))*(p.^k(i))*((1-p).^(n-k(i)));
end
end
k = 0:50;
pB = 0.05:0.05:0.95;
distance = zeros(length(pB),2);
for i=1:length(pB)
bi = binom(k,pB(i),50); % binomial dist with p=0.05 to 0.5, n=50
gauss = gaussian(k,50*pB(i),sqrt(50*pB(i)*(1-pB(i)))); % gaussian with mean=np, var=np(1-p)
bivals = drawfromcdf(cumsum(bi),k,100000);
gaussvals = drawfromcdf(cumsum(gauss),k,100000);
[~,distance(i,2),distance(i,1)]=kstest2(bivals,gaussvals);
end
figure
subplot(1,2,1)
plot(pB,distance(:,1))
title('KS-test statistic')
set(gca,'FontSize',16)
subplot(1,2,2)
plot(pB,distance(:,2))
title('p-value')
set(gca,'FontSize',16)
Question5
normdraws = normrnd(10,5,100000,1);
figure
hist(normdraws,50)
set(gca,'FontSize',16)
title('Draws from normal dist.')
%5a
samples = normdraws(randi(100000,5,1000));
%1: mean of samples
sample_m = mean(samples);
%2: s_n-1
sample_sd = sqrt(sum((bsxfun(@minus,samples,sample_m).^2)/(5-1)));
disp(['2. s_n-1: ' num2str(mean(sample_sd))])
%3: s_n
sample_sd_n = sqrt(sum((bsxfun(@minus,samples,sample_m).^2)/5));
disp(['3. s_n: ' num2str(mean(sample_sd_n))])
%4: sem
sem_n1 = sample_sd./sqrt(5-1);
disp(['4. sem_n-1: ' num2str(mean(sem_n1))])
sem_n = sample_sd_n./sqrt(5);
disp(['4. sem_n: ' num2str(mean(sem_n))])
% 2 is closer to the trus standard deviation than 3
%5b
disp(['SD of mean from 5a1: ' num2str(std(sample_m))])
% s_n-1/sqrt(n-1) best reflects actual variability in the means
%5c
figure
[semcount, x] = hist(sem_n1, 25);
subplot(1,2,1)
sem_pdf=semcount./sum(semcount);
plot(x,sem_pdf)
set(gca,'FontSize',16);
title('PDF of SEM')
subplot(1,2,2)
plot(x,cumsum(sem_pdf))
title('CDF of SEM')
set(gca,'FontSize',16);
2. s_n-1: 4.6657
3. s_n: 4.1731
4. sem_n-1: 2.3329
4. sem_n: 1.8663
SD of mean from 5a1: 2.2765
Related documents