%Load data and make variables

load('../Data/StudData2014.txt')

hogde=StudData2014(:,1);
hogdeMor=StudData2014(:,2);
hogdeFar=StudData2014(:,3);
kjonn=StudData2014(:,4);
har=StudData2014(:,5);
auge=StudData2014(:,6);
Matlab=StudData2014(:,7);


%Make vectors with hight of men and women

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

%histograms
subplot(1,2,1)
hist(hogdeMann)
subplot(1,2,2)
hist(hogdeKvinne)

%gjennomsnitt
mean(hogdeMann)
mean(hogdeKvinne)
%median
median(hogdeMann)
median(hogdeKvinne)
%standardavvik
std(hogdeMann)
std(hogdeKvinne)
%varians
var(hogdeMann)
var(hogdeKvinne)



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


%Sample from distribution of hights for young Norwegian mans, and find
%means

figure
nsample=500;
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');
subplot(1,2,1)
hist(Samples(1,:))
title('Eit utvalg, normal')
subplot(1,2,2)
hist(samplemeans)
title('Utvalsfordeling for gjennomsnitt, normal')

%qq-plot
figure
subplot(2,2,1)
normplot(hogdeMann)
title('qq-plot hogde menn')
subplot(2,2,2)
normplot(hogdeKvinne)
title('qq-plot hogde kvinner')
subplot(2,2,3)
normplot(ASample)
title('qq-plot simulerte normalfordelte data')
subplot(2,2,4)
ASample2=random('exp',1,1,100);
normplot(ASample2)
title('qq-plot simulerte eksponensialfordelte data')



%Utvalsfordeling for varians
figure
nsample=500;
samplevars=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')
    samplevars(i)=var(ASample);
    subplot(1,2,2) 
    hist(samplevars(1:i));
    title('Utvalsfordeling for varians')
    pause(0.05)
end

figure
normplot(samplevars)



%Simulerer for n=5 kvinner hogde
figure
nsample=200;
samplemeans=zeros(nsample,1);
antkvinner5=5;
for(i=1:nsample)
    ASample=random('Normal',167.7,3.97,1,antkvinner5);
    subplot(1,3,1)
    hist(ASample)
    title('Eit utvalg')
    samplezs(i)=(167.7-mean(ASample))/(3.97/sqrt(antkvinner5));
    samplezSs(i)=(167.7-mean(ASample))/(var(ASample)/sqrt(antkvinner5));
    subplot(1,3,2) 
    hist(samplezs(1:i));
    title('Utvalsfordeling for z, med sann sigma')
    subplot(1,3,3) 
    hist(samplezs(1:i));
    title('Utvalsfordeling for z, innsatt s')
    pause(0.02)
end

figure
subplot(1,2,1)
normplot(samplezs)
title('Utvalsfordeling for z, med sann sigma')
subplot(1,2,2)
normplot(samplezSs)
title('Utvalsfordeling for z, innsatt s')

%observert x^2 for v?re data (menn)
x2=(antmenn-1)*var(hogdeMann)/6.5^2
chi2cdf(x2,antmenn-1)


%observert x^2 for v?re data (kvinner)
x2=(antkvinner-1)*var(hogdeKvinne)/6.5^2
chi2cdf(x2,antkvinner-1)
