%%
load('../Data/StudDataV16.txt')
%%
hogde=StudDataV16(:,1);
hogdeFar=StudDataV16(:,2);
hogdeMor=StudDataV16(:,3);
kjonn=StudDataV16(:,4);
har=StudDataV16(:,5);
auge=StudDataV16(:,6);



%% 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=200;
samplemeans=zeros(nsample,1);
for(i=1:nsample)
     ASample=random('Normal',179.8,6.5,1,antmenn);
    subplot(1,2,1)
    hist(ASample);
    t=title('Eit utvalg');
    set(t, 'FontSize', 18);
    samplemeans(i)=mean(ASample);
    subplot(1,2,2) 
    hist(samplemeans(1:i));
    t=title('Utvalsfordeling for gjennomsnitt');
    set(get(gca,'child'),'FaceColor','r','EdgeColor','k');
    set(t, 'FontSize', 18);
    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,:))
t=title('Eit utvalg, normal')
set(t, 'FontSize', 18);
subplot(1,2,2)
hist(samplemeans)
t=title('Utvalsfordeling for gjennomsnitt, normal');
set(get(gca,'child'),'FaceColor','r','EdgeColor','k');
set(t, 'FontSize', 18);
%% normal qq-plot
figure
subplot(2,2,1)
normplot(hogdeMann)
t=title('normal plot hogde menn')
set(t, 'FontSize', 16);
subplot(2,2,2)
normplot(hogdeKvinne)
t=title('normal plot hogde kvinner')
set(t, 'FontSize', 16);
subplot(2,2,3)
normplot(ASample)
t=title('qq-plot simulerte normalfordelte data')
set(t, 'FontSize', 16);
subplot(2,2,4)
ASample2=random('exp',1,1,100);
normplot(ASample2)
t=title('normal plot simulerte eksponensialfordelte data')
set(t, 'FontSize', 16);



%% Utvalsfordeling for varians
figure
nsample=200;
samplevars=zeros(nsample,1);
for(i=1:nsample)
    ASample=random('Normal',179.8,6.5,1,antmenn);
    subplot(1,2,1)
    hist(ASample)
    t=title('Eit utvalg');
    set(t, 'FontSize', 18);
    samplevars(i)=var(ASample);
    subplot(1,2,2) 
    hist(samplevars(1:i));
    t=title('Utvalsfordeling for varians');
    set(get(gca,'child'),'FaceColor','r','EdgeColor','k');
    set(t, 'FontSize', 18);
    pause(0.05)
end

%%
figure
normplot(samplevars)



%% Simulerer for n=5 kvinner hogde
figure
nsample=200;
samplemeans=zeros(nsample,1);
antkvinner5=5;
mymean=mean(hogdeKvinne)
mystd=std(hogdeKvinne)
for(i=1:nsample)
    ASample=random('Normal',mymean,mystd,1,antkvinner5);
    subplot(1,3,1)
    hist(ASample)
    t=title('Eit utvalg');
    set(t, 'FontSize', 16);
    samplezs(i)=(mymean-mean(ASample))/(mystd^2/sqrt(antkvinner5));
    samplezSs(i)=(mymean-mean(ASample))/(std(ASample)/sqrt(antkvinner5));
    subplot(1,3,2) 
    hist(samplezs(1:i));
    t=title('Utvalsfordeling for z, med sann sigma');
    set(get(gca,'child'),'FaceColor','r','EdgeColor','k');
    set(t, 'FontSize', 16);
    subplot(1,3,3) 
    hist(samplezs(1:i));
    t=title('Utvalsfordeling for z, innsatt s');
    set(get(gca,'child'),'FaceColor','y','EdgeColor','k');
    set(t, 'FontSize', 16);
    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 vaare data (menn)
x2=(antmenn-1)*var(hogdeMann)/6.5^2
chi2cdf(x2,antmenn-1)


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