%%
% Example 1.2.1
clc
clear all
close all
data=[9 2 8 7 1 3 4 8 3 2 3 10 9 3 8 9];
x=[1 2 3 4 5 6 7 8 9 10];
[p x]=hist(data,x)
hist(data,x)
title('histogram of the example 1.2.1','fontsize',16)
xlabel('x','fontsize',16); ylabel('not normalised p(x)','fontsize',16);
print -f -depsc 'example.eps'
%%
% Exercise 1.2.3
clc
clear all
close all

rain_sp=[50 48 55 44 47 13 8 18 39 78 60 55];
rain_ma=[62 42 70 35 35 4 1 3 32 94 71 56];
rain_lo=[58 39 51 49 54 53 48 56 58 62 61 63];

x=(0:20:80)
subplot(3,1,1)
hist(rain_sp,x)
xlabel('monthly rainfall [mm] in Spain','fontsize',16); ylabel('p_{sp}(x)','fontsize',16);
subplot(3,1,2)
hist(rain_ma,x)
xlabel('monthly rainfall [mm] in Madrid','fontsize',16); ylabel('p_{ma}(x)','fontsize',16);
subplot(3,1,3)
hist(rain_lo,x)
xlabel('monthly rainfall [mm] in London','fontsize',16); ylabel('p_{lo}(x)','fontsize',16);
print -f -depsc 'rainfall.eps'
%%
% Exercise 1.2.5
clc
clear all
close all
data=[9 2 8 7 1 3 4 8 3 2 3 10 9 3 8 9];
x=[1 2 3 4 5 6 7 8 9 10];
[p x]=hist(data,x)
p=p/length(data)

% check
sum(p)
bar(x,p)
title('histogram of the example 1.2.1','fontsize',16)
xlabel('x','fontsize',16); ylabel('normalised p(x)','fontsize',16);
print -f -depsc 'example_normalised.eps'
%%
% Exercise 1.2.5
clc
clear all
close all
N=200000;
data = randn(N,1);
n_bins=100;
[p,xout]=hist(data,n_bins);
dx=xout(2)-xout(1);
norm=sum(p.*dx);

hold on
bar(xout,p/norm)
title('Gaussian distribution','fontsize',16)
xlabel('x','fontsize',16); ylabel('p(x)','fontsize',16);
p_anal=exp(-xout.^2/2)/sqrt(2*pi);
plot(xout,p_anal,'r','Linewidth',3)

print -f -depsc 'gauss-norm.eps'
%%
close all
data = rand(N,1);
[p,xout]=hist(data,n_bins);
dx=xout(2)-xout(1);
norm=sum(p.*dx);
figure(2)
hold on
bar(xout,p/norm)
title('Uniform distribution','fontsize',16)
xlabel('x','fontsize',16); ylabel('p(x)','fontsize',16);
p_anal=1+0*xout;
plot(xout,p_anal,'r-','Linewidth',3)

print -f -depsc 'uniform_distr.eps'
%%
clc
clear all
close all
% Exercise 1.2.6
N=1000;
y1 = randn(N,1);
N=10000;
y2 = randn(N,1);
N=10000000;
y3 = randn(N,1);

n_bins=50;
x1 = linspace(-4,4,n_bins);
n_bins=100;
x2 = linspace(-4,4,n_bins);
x3 = x2;

[p1,x1]=hist(y1,x1);
dx1=x1(2)-x1(1);
norm1=sum(p1.*dx1);

[p2,x2]=hist(y2,x2);
dx2=x2(2)-x2(1);
norm2=sum(p2.*dx2);

[p3,x3]=hist(y3,x3);
dx3=x3(2)-x3(1);
norm3=sum(p3.*dx3);

hold on
plot(x1,p1/norm1,'r-','Linewidth',1)
plot(x2,p2/norm2,'b-','Linewidth',1)
plot(x3,p3/norm3,'g-','Linewidth',3)
legend('N=1000, N_{bins}=50','N=10000, N_{bins}=100', ...
    'N=1000000, N_{bins}=100',...
    'Location','NorthWest')
xlabel('x','fontsize',16); ylabel('p(x)','fontsize',16);
hold off
print -f -depsc 'comparison.eps'
%%

% two equivalent ways of plotting
%figure(1)
%hist(y,x)
%[n xout]=hist(y,x);
%figure(2)
%bar(xout,n,'r')

%%
clear all
close all
clc
N=200000;
x=linspace(-5,5,50);
y=randn(N,1);

[p,xout]=hist(y,x);
%pnorm=normalize(p,xout)
figure(1)
hold on
bar(x,p)
plot(x,exp(-x.^2/2)/sqrt(2*pi))
%plot(x,exp(-x.^2/2))
%xlabel('x','fontsize',16),ylabel('p(x) [Gaussian distribution]',...
%    'fontsize',16)
hold off
%%
N=200000;
nbins=100;
data = randn(N,1);
[p,xout]=hist(data,nbins)
dx=xout(2)-xout(1);
norm=sum(p*dx);
bar(xout,p/norm)
hold on
panal=exp(-xout.^2/2)/sqrt(2*pi);
plot(xout,panal,'r','Linewidth',3)

%print -f -depsc 'gauss-norm.eps'

%%
clc
clear all
close all
data=[randn(100000,1);+10+randn(100000,1)];
x=linspace(-5,+15,100);
hist(data,x)