
%% last inn 'spund1ed.csv' vha import data
%%
hogde=nhoyde;
kjonn = zeros(length(nhoyde),1);

for(i=1:length(nhoyde))
    if(strcmp(char(VarName2(i)),'Mann')==1)
        kjonn(i)=1;
    end
    if(strcmp(char(VarName2(i)),'Kvinne')==1)
        kjonn(i)=2;
    end
end

%% Make vectors with hight of men and women

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

%%

figure

nsample=200; %Number of means
samplemeans=zeros(nsample,1);
for(i=1:nsample)
    ASample=random('Normal',mu,sigma,1,antmenn);
    subplot(1,2,1)
    hist(ASample);
    t=title('Eit utvalg');
    set(t, 'FontSize', 18);
    set(gca,'FontSize',20)
    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);
    set(gca,'FontSize',20)
    pause(0.2)
end

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

figure
hist(samplemeans);
 title('Utvalsfordeling for gjennomsnitt');
set(gca,'FontSize',20)

%% Motivasjon normal plott
figure
h=cdfplot(hogdeMann)
set(h,'LineWidth',3)
set(gca,'FontSize',20)
%%
hold
x=160:0.1:205;
plot(x,normcdf(x,mean(hogdeMann), std(hogdeMann)),'r-','LineWidth',3)
legend('Empirisk','Teoretisk','Location','NW');
title('Kummulativ sanns.ford.')
set(gca,'FontSize',20)

%% normal-plot
figure
normplot(hogdeMann)
title('normal plot hogde menn')
set(gca,'FontSize',20)


%% normal-plot
figure
subplot(2,2,1)
normplot(hogdeMann)
title('normal plot hogde menn')
set(gca,'FontSize',20)

subplot(2,2,2)
normplot(hogdeKvinne)
title('normal plot hogde kvinner')
set(gca,'FontSize',20)

subplot(2,2,3)
ASample=random('Normal',mean(hogdeMann),std(hogdeMann),1,antmenn);
normplot(ASample)
title('normal plot simulert normal data')
set(gca,'FontSize',20)

subplot(2,2,4)
ASample2=random('exp',1,1,100);
normplot(ASample2)
title('normal plot simulert exp data')
set(gca,'FontSize',20)


%% Vi har antatt kjent varians 6.5^2
 
var(hogdeMann)
std(hogdeMann)
 %%



%% 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)
    title('Eit utvalg');
    samplevars(i)=var(ASample);
    set(gca,'FontSize',20)
    subplot(1,2,2) 
    hist(samplevars(1:i));
    t=title('Utvalsfordeling for varians');
    set(get(gca,'child'),'FaceColor','r','EdgeColor','k');
    set(gca,'FontSize',20)
    pause(0.05)
end


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


%% Var for kvinner
var(hogdeKvinne)
std(hogdeKvinne)

x2=(antkvinner-1)*var(hogdeKvinne)/6.5^2
chi2cdf(x2,antkvinner-1)


%% 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(gca,'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 Z, sann sigma');
    set(get(gca,'child'),'FaceColor','r','EdgeColor','k');
    set(gca,'FontSize',16)
    
    subplot(1,3,3) 
    hist(samplezs(1:i));
    t=title('Utvalsfordeling Z, innsatt s');
    set(get(gca,'child'),'FaceColor','y','EdgeColor','k');
    set(gca,'FontSize',16)
    pause(0.02)
end

%%

figure
subplot(1,2,1)
normplot(samplezs)
title('Utvalsfordeling Z, med sann sigma')
set(gca,'FontSize',20)
subplot(1,2,2)
normplot(samplezSs)
title('Utvalsfordeling Z, innsatt s')
set(gca,'FontSize',20)

%% 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)


%% Plottar Kji-kvadrat fordeling
figure
x=0:0.1:25;
plot(x,chi2pdf(x,2),'LineWidth',3)
hold
plot(x,chi2pdf(x,5),'LineWidth',3)
plot(x,chi2pdf(x,10),'LineWidth',3)
plot(x,chi2pdf(x,15),'LineWidth',3)
title('\chi^2 fordelingar')
legend('\nu=2', '\nu=5', '\nu=10', '\nu=15')
set(gca,'FontSize',20)


%% Plottar T fordeling saman med N(0,1)
figure
x=-4:0.01:4;
plot(x,normpdf(x,0,1),'LineWidth',3)
hold
plot(x,tpdf(x,2),'LineWidth',3)
%%
plot(x,tpdf(x,5),'LineWidth',3)
plot(x,tpdf(x,10),'LineWidth',3)
plot(x,tpdf(x,15),'LineWidth',3)
title('T fordelingar og N(0,1)')
legend('N(0,1)','\nu=2', '\nu=5', '\nu=10', '\nu=15')
set(gca,'FontSize',20)

