%Load data and make variables

load('../Data/KjonnOgHoyde.mat')

%%
kjonn=KjonnOgHoyde(:,1);
hogde=KjonnOgHoyde(:,2);



%% Plot hight
hist(hogde)

%%
boxplot(hogde)

%%
boxplot(hogde,kjonn)

%% Make vectors with hight of men and women

indexMann=find(kjonn==0);
indexKvinne=find(kjonn==1);
hogdeMann=hogde(indexMann);
hogdeKvinne=hogde(indexKvinne);

%% histograms
subplot(1,2,1)
hist(hogdeMann)
title('Hogde menn')
subplot(1,2,2)
hist(hogdeKvinne)
title('Hogde kvinner')

%% mean
mean(hogdeMann)
mean(hogdeKvinne)

antmenn=length(indexMann)
antkvinner=length(indexKvinne)




%% Sample datasets with antmenn persons from distribution of hights for young Norwegian men, and find
%means

figure
nsample=500; %Number of means
samplemeans=zeros(nsample,1);
for(i=1:nsample)
    ASample=random('Normal',179.8,6.5,1,antmenn);
    subplot(1,2,1)
    hist(ASample)
    title('Eit utvalg')
    samplemeans(i)=mean(ASample);
    subplot(1,2,2) 
    hist(samplemeans(1:i));
    title('Utvalsfordeling for gjennomsnitt')
    pause(0.05)
end

%% Rask
nsample=10000;
samplemeans=zeros(nsample,1);
Samples=random('Normal',179.8,6.5,nsample,antmenn);
samplemeans=mean(Samples');


f=find(samplemeans>mean(hogdeMann));
length(f)/nsample





%% Probability hypothesis test
1-normcdf(mean(hogdeMann),179.8,sqrt(6.5^2/antmenn))
1-normcdf(mean(hogdeKvinne),167.2,sqrt(6.5^2/antkvinner))



%% Demonstrasjon Sentralgrenseteoremet Simulerer med Poisson
nsample=10000; %Number of datasets
ineachsample=20;
samplemeans=zeros(nsample,1);
Samples=random('Poisson',1,nsample,ineachsample);
samplemeans=mean(Samples');
figure
subplot(1,2,1)
hist(Samples(1,:))
title('Eit utvalg, Poisson')
subplot(1,2,2)
hist(samplemeans)
title('Utvalsfordeling for gjennomsnitt, Poisson')



%% Simulerer med eksponensial
nsample=10000; %Number of datasets
ineachsample=100;
samplemeans=zeros(nsample,1);
Samples=random('exp',1,nsample,ineachsample);
samplemeans=mean(Samples');
figure
subplot(1,2,1)
hist(Samples(1,:))
title('Eit utvalg, eksponensial')
subplot(1,2,2)
hist(samplemeans)
title('Utvalsfordeling for gjennomsnitt, eksponensial')

