% gdama03
% W. Menke (2024), Geophysical Data Analysis and Inverse Theory
% with MATLAB(R) or Python, 5th Edition
% Code History:
% 2022-10-01, Imported from 4th Edition by W. Menke
% 2023-04-02, Edit and integration with text
% gdama03_01
% distribution and histogram
 
clear all;
fprintf('gdama03_01\n');
gdama03_01
 
% axes
Dd = 0.1;
N = 101;
d = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
sd = 1.0;
dbar = 5.0;
p = exp(-0.5*(d-dbar).^2/(sd^2))/(sqrt(2*pi)*sd);
norm = Dd*sum(p);
p = p/norm;
 
M=200;
r=random('Normal',dbar,sd,M,1);
Nb=26;
Db=(dmax-dmin)/(Nb-1);
bins=dmin+Db*[0:Nb-1]';
h = hist(r,bins)';
hmax=max(h);
 
figure(1);
clf;
 
% plot pdf
subplot(1,3,2);
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 0.5 ] );
plot(d,p,'r-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
 
%plot histogram
subplot(1,3,1);
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, hmax ] );
% improvise bar chart
for i = (1:Nb)
tb = [bins(i)-Db/2, bins(i)-Db/2, bins(i)+Db/2, bins(i)+Db/2]';
th = [0, h(i), h(i), 0]';
plot(tb,th,'b-','LineWidth',3);
end
xlabel('d');
ylabel('counts');
 
% convert histogram to an approximate pdf
norm = Db*sum(h);
h=h/norm;
 
% plot dpf and histogram superimposed
subplot(1,3,3);
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 0.5 ] );
% improvise bar chart
for i = (1:Nb)
tb = [bins(i)-Db/2, bins(i)-Db/2, bins(i)+Db/2, bins(i)+Db/2]';
th = [0, h(i), h(i), 0]';
plot(tb,th,'b-','LineWidth',3);
end
plot(d,p,'r-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
fprintf('(Left) Histogram (blue). (Middle) Probability density function (pdf, red). (Right)\n');
(Left) Histogram (blue). (Middle) Probability density function (pdf, red). (Right)
fprintf('Normalize histogram (blue) as a empirical pdf, with true pdf (red), superimposed\n');
Normalize histogram (blue) as a empirical pdf, with true pdf (red), superimposed
% gdama03_02
 
% a Normal pdf
 
clear all;
fprintf('gdama03_02\n');
gdama03_02
 
% d-axis
Dd = 0.1;
N = 101;
d = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% Normal pdf
dbar = 5;
sd = 1;
p = exp(-0.5*((d-dbar).^2)/sd^2)/(sqrt(2*pi)*sd);
 
% point on the curve
n = floor(4.5*N/10.0)+1;
dn = d(n);
pn = p(n);
dn2 = d(n+1);
pn2 = p(n+1);
 
% plot
figure(1);
clf;
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 0.5 ] );
plot(d,p,'b-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
plot([dn,dn], [0.0,pn], 'k-', 'LineWidth', 1);
plot([dn2,dn2], [0.0,pn2], 'k-', 'LineWidth', 1);
plot([0,dn], [pn,pn], 'k:', 'LineWidth', 1);
 
fprintf('Caption: Normal pdf with a mean of 5 and a variance of (1)^2\n');
Caption: Normal pdf with a mean of 5 and a variance of (1)^2
fprintf('Sliver of probsbility has area p*Dd.\n');
Sliver of probsbility has area p*Dd.
% gdama03_03
 
% operations on a probability distributions
 
clear all;
fprintf('gdama03_03\n');
gdama03_03
 
% axes
Dd = 0.1;
N = 101;
d = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% Normal distribution
dbar = 5;
sigma = 1;
sigma2=sigma^2;
p = exp(-0.5*(d-dbar).^2/sigma2) / (sqrt(2*pi)*sigma);
 
% plot p
figure(1);
clf;
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis([0,10,0,max(p)]);
plot(d,p,'k-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
fprintf('Caption: Normal pdf with mean 5 and variance (1)^2.\n');
Caption: Normal pdf with mean 5 and variance (1)^2.
 
% total probability
Ptotal = Dd * sum(p);
fprintf('total probabilty %f\n', Ptotal );
total probabilty 1.000000
 
% cumulative probability
P = Dd * cumsum(p);
figure(2);
clf;
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis([0,10,0,max(P)]);
plot(d,P,'k-','LineWidth',3);
xlabel('d');
ylabel('P(d)');
fprintf('Caption: Cumulative probability distribution\n');
Caption: Cumulative probability distribution
 
% mean and variance
Ed = Dd * sum(d.*p);
sigma2 = Dd * sum(((d-Ed).^2).*p);
fprintf('mean %f and variance %f\n', Ed, sigma2 );
mean 4.999998 and variance 0.999988
% gdama03_04
 
% calculaion of mode and mean for a skewed pdf
 
clear all;
fprintf('gdama03_04\n');
gdama03_04
 
% d-axis
Dd = 0.1;
N = 101;
d = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% complicated mpn-Normal pdf
dbar = 0;
sd = 3;
p = d .* exp(-0.5*((d-dbar).^2)/sd^2);
dbar = 1;
sd = 0.3;
p = p + 4*exp(-0.5*((d-dbar).^2)/sd^2);
norm = Dd*sum(p);
p = p/norm;
 
% maximum liklihood point
[pmax, imax] = max(p);
dml = d(imax);
 
% mean
dbar = Dd*sum(d.*p);
 
% plot
figure(1);
clf;
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 0.5 ] );
plot(d,p,'b-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
plot( [dml, dml]', [0, 0.05]', 'r-', 'LineWidth', 2 );
plot( [dbar, dbar]', [0, 0.05]', 'k-', 'LineWidth', 2 );
fprintf('Caption: Skewed pdf (blue), showing maximum likelihood point (red bar)\n');
Caption: Skewed pdf (blue), showing maximum likelihood point (red bar)
fprintf('and mean (black bar)\n');
and mean (black bar)
 
% gdama03_05
% calculaion of variance
 
clear all;
fprintf('gdama03_05\n');
gdama03_05
 
% d-axis
Dd = 0.1;
N = 101;
d = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% two Normal pdfs with different variances
dbar = 5.0;
sd = 0.5;
p = exp(-0.5*((d-dbar).^2)/sd^2)/(sqrt(2*pi)*sd);
norm1 = Dd*sum(p);
 
dbarB = 5.0;
sdB = 1.5;
pB = exp(-0.5*((d-dbarB).^2)/sdB^2)/(sqrt(2*pi)*sdB);
norm2 = Dd*sum(pB);
 
% expected values (means calculated from pdfs)
Ed = Dd * sum(d.*p);
EdB = Dd * sum(d.*pB);
fprintf('means:\n');
means:
fprintf('p1: true %.4f calculated %.4f\n', dbar, Ed);
p1: true 5.0000 calculated 5.0000
fprintf('p2: true %.4f calculated %.4f\n', dbarB, EdB);
p2: true 5.0000 calculated 4.9962
fprintf(' \n');
 
% qudratic
q = (d-Ed).^2;
qB = (d-EdB).^2;
 
% estimated variances
sd2 = Dd * sum(q.*p);
sdB2 = Dd * sum(qB.*pB);
sdest = sqrt(sd2);
sdBest = sqrt(sdB2);
fprintf('std dev 1: true: %f estimated: %f\n', sd, sdest);
std dev 1: true: 0.500000 estimated: 0.500000
fprintf('std dev 2: true: %f estimated: %f\n', sdB, sdBest);
std dev 2: true: 1.500000 estimated: 1.492462
 
% plot
figure();
clf;
 
top=1;
 
subplot(2, 3, 1 );
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 10*top ] );
plot(d,q,'k-','LineWidth',3);
xlabel('d');
ylabel('q(d)');
 
subplot(2, 3, 2 );
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, top ] );
plot(d,p,'k-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
 
subplot(2, 3, 3 );
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, top ] );
plot(d,q.*p,'k-','LineWidth',3);
xlabel('d');
ylabel('q(d)*p(d)');
 
subplot(2, 3, 4 );
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 10*top ] );
plot(d,qB,'k-','LineWidth',3);
xlabel('d');
ylabel('q(d)');
 
subplot(2, 3, 5 );
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, top ] );
plot(d,pB,'k-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
 
subplot(2, 3, 6 );
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, top ] );
plot(d,qB.*pB,'k-','LineWidth',3);
xlabel('d');
ylabel('q(d)*p(d)');
 
fprintf('Caption: Depiction of the calculation of variacne. (Top row) (Left) Quadratic\n');
Caption: Depiction of the calculation of variacne. (Top row) (Left) Quadratic
fprintf('curve q multiplies pdf p (middle) to produce product q*p (right). The area under\n');
curve q multiplies pdf p (middle) to produce product q*p (right). The area under
fprintf('the product q*p is the variacnce. (Bottom row) Same as top row but for a\n');
the product q*p is the variacnce. (Bottom row) Same as top row but for a
fprintf('wider pdf\n');
wider pdf
 
% realizations of these pdfs
Nr = 1000; % number of realizations
dr = random('Normal',dbar,sd,Nr,1); % realizations of p
drB = random('Normal',dbarB,sdB,Nr,1); % realizations of pB
 
% sample mean
dbarest = mean(dr);
dbarBest = mean(drB);
fprintf('p1: True mean %.4f, estimated from ensemble: %.4f\n',dbar,dbarest);
p1: True mean 5.0000, estimated from ensemble: 5.0120
fprintf('p2: True mean %.4f, estimated from ensemble: %.4f\n',dbar,dbarBest);
p2: True mean 5.0000, estimated from ensemble: 4.9526
fprintf(' \n');
 
% sample standard deviation
sigmadest = std(dr);
sigmadBest = std(drB);
fprintf('True sigma %.4f, estimated from ensemble: %.4f\n',sd,sigmadest);
True sigma 0.5000, estimated from ensemble: 0.5150
fprintf('True sigma %.4f, estimated from ensemble: %.4f\n',sd,sigmadBest);
True sigma 0.5000, estimated from ensemble: 1.4771
 
% gdama03_06
% 2D Normal distribution, uncorrelated
 
clear all;
fprintf('gdama03_06\n');
gdama03_06
 
% d-axis
Dd = 0.1;
N = 101;
d1 = Dd*[0:N-1]';
d2 = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% parameters for 2D pdf p(d1,d2)
d1bar = 5;
d2bar = 5;
sd1 = 1.5;
sd2 = 0.5;
C=zeros(2,2);
C(1,1)=sd1^2;
C(2,2)=sd2^2;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
 
% set elements of p(d2,d2z)
P=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d1bar, dmin+Dd*(j-1)-d2bar ]';
P(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
 
% area
A = sum(sum(P))*(Dd^2);
 
% plot
gda_draw(' ',P);
colorbar;
fprintf('Caption: Depiction of an 2D uncorrelated pdf p(d1,d2) (colors)\n');
Caption: Depiction of an 2D uncorrelated pdf p(d1,d2) (colors)
 
% two-column table of random numbers
Nr = 1000; % number of realizations in the ensemble
 
% the next statement works only for uncorrelated data
S = [ random('Normal',d1bar,sd1,Nr,1), random('Normal',d2bar,sd2,Nr,1) ];
 
% but this works fine irrespective of currenaltion
dbarest = mean(S);
Cest = cov(S);
size(Cest)
ans = 1×2
2 2
 
fprintf('Estimates from ensemble\n');
Estimates from ensemble
fprintf('true means: %.4f %.4f\n',d1bar,d2bar);
true means: 5.0000 5.0000
fprintf('estimated means: %.4f %.4f\n',dbarest(1),dbarest(2));
estimated means: 4.9620 5.0101
fprintf('true sigmas: %.4f %.4f\n',sd1,sd2);
true sigmas: 1.5000 0.5000
fprintf('estimated sigmas: %.4f %.4f\n',sqrt(Cest(1,1)),sqrt(Cest(2,2)));
estimated sigmas: 1.4523 0.4955
fprintf('true covariance %.4f\n',0.0);
true covariance 0.0000
fprintf('estimated covariance %.4f\n',Cest(1,2));
estimated covariance -0.0127
 
% gdama03_07
% 2D Normal distribution, correlated
 
clear all;
fprintf('gdama03_07\n');
gdama03_07
 
% d-axis
Dd = 0.1;
N = 101;
d1 = Dd*[0:N-1]';
d2 = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% parameters for 2D pdf p(d1,d2)
d1bar = 5;
d2bar = 5;
sd1 = 1.5;
sd2 = 0.5;
cov = 0.4;
C=zeros(2,2);
C(1,1)=sd1^2;
C(2,2)=sd2^2;
C(1,2)=cov;
C(2,1)=cov;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
 
% set elements of p(d2,d2z)
P=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d1bar, dmin+Dd*(j-1)-d2bar ]';
P(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
 
% area
A = sum(sum(P))*(Dd^2);
 
% plot
gda_draw(' ',P);
colorbar;
fprintf('Caption: Depiction of an 2D positively-correlated pdf p(d1,d2) (colors)\n');
Caption: Depiction of an 2D positively-correlated pdf p(d1,d2) (colors)
% gdama03_08
% 2D Normal distribution, uncorrelated, + correlation, - correlation
 
clear all;
fprintf('gdama03_08\n');
gdama03_08
 
% d-axis
Dd = 0.1;
N = 101;
d1 = Dd*[0:N-1]';
d2 = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% uncorrelated
d1bar = 5;
d2bar = 5;
sd1 = 1.25;
sd2 = 0.75;
cov = 0.0;
C=zeros(2,2);
C(1,1)=sd1^2;
C(2,2)=sd2^2;
C(1,2)=cov;
C(2,1)=cov;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
P1=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d1bar, dmin+Dd*(j-1)-d2bar ]';
P1(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
 
mycov=0.5;
 
% positively correlated
d1bar = 5;
d2bar = 5;
sd1 = 1.25;
sd2 = 0.75;
C=zeros(2,2);
C(1,1)=sd1^2;
C(2,2)=sd2^2;
C(1,2)=mycov;
C(2,1)=mycov;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
P2=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d1bar, dmin+Dd*(j-1)-d2bar ]';
P2(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
 
% negatively
d1bar = 5;
d2bar = 5;
sd1 = 1.25;
sd2 = 0.75;
C=zeros(2,2);
C(1,1)=sd1^2;
C(2,2)=sd2^2;
C(1,2)=-mycov;
C(2,1)=-mycov;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
P3=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d1bar, dmin+Dd*(j-1)-d2bar ]';
P3(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
 
gda_draw(' ',P1,' ',P2,' ',P3);
colorbar;
fprintf('Caption: Depiction of 2D pdfs (colors). (Left) Uncorrelated. (middle)\n');
Caption: Depiction of 2D pdfs (colors). (Left) Uncorrelated. (middle)
fprintf('Positively correlated. (Right) Negatively correlated\n');
Positively correlated. (Right) Negatively correlated
% gdama03_09
 
% 1D uniform p.d.f., p(d)=constant, traansformed to p(m) with m(d)=d^2
% note that m=sqrt(d) and that dm/dd=0.5/sqrt(d)
 
clear all;
fprintf('gdama03_09\n');
gdama03_09
 
% d-axis
Dd = 0.01;
N = 100;
d = Dd*[1:N]';
dmin=0;
dmax=1;
 
% m-axis
Dm = 0.01;
M = 100;
m = Dm*[1:M]';
mmin=0;
mmax=1;
 
% uniform, p(d)
d1bar = 5;
pd = ones(N,1);
 
% transform to p(m)
J = abs(0.5 ./sqrt(d));
pm = pd.*J;
 
% plot
figure(1);
clf;
 
subplot(2,1,1);
set(gca,'LineWidth',2,'FontName','Cambria Math','FontAngle','italic');
hold on;
axis( [0, 1, 0, 2]' );
plot(d,pd,'r-','LineWidth',3);
xlabel('d','FontName','Cambria Math','FontAngle','italic');
ylabel('p(d)','FontName','Cambria Math','FontAngle','italic');
 
subplot(2,1,2);
set(gca,'LineWidth',2,'FontName','Cambria Math','FontAngle','italic');
hold on;
axis( [0, 1, 0, 2]' );
plot(m,pm,'b-','LineWidth',3);
xlabel('d','FontName','Cambria Math','FontAngle','italic');
ylabel('p(m)','FontName','Cambria Math','FontAngle','italic');
fprintf('Caption: (Top) Uniform pdf p(d). (Bottom) Non-uniform pdf p(mp) resulting\n');
Caption: (Top) Uniform pdf p(d). (Bottom) Non-uniform pdf p(mp) resulting
fprintf('from transformation mp=m^2.\n');
from transformation mp=m^2.
 
% gdama03_10
% two Normal curves of different variance
 
clear all;
fprintf('gdama03_10\n');
gdama03_10
 
% axes
Dd = 0.1;
N = 101;
dmin=-5;
d = dmin+Dd*[0:N-1]';
dmax=5;
 
% narrow Normal p.d.f.
sd = 1.0;
dbar = 0.0;
p = exp(-0.5*(d-dbar).^2/(sd^2))/(sqrt(2*pi)*sd);
 
% make realizations of p(d)
Nr=1000;
rp = random('Normal',dbar,sd,Nr,1);
 
% sample mean and std dev
dbarest = mean(rp);
sdest = std(rp);
 
fprintf("pa: mean: true %.4f estimated %.4f\n",dbar, dbarest);
pa: mean: true 0.0000 estimated 0.0057
fprintf("pa: stddev: true %.4f estimated %.4f\n",sd, sdest);
pa: stddev: true 1.0000 estimated 0.9947
 
% wide Normal p.d.f.
sdb = 2.0;
dbarb = 0.0;
pb = exp(-0.5*(d-dbarb).^2/(sdb^2))/(sqrt(2*pi)*sdb);
 
% make realizations of pb(d)
Nr=1000;
rpb = random('Normal',dbarb,sdb,Nr,1);
 
% sample mean and std dev
dbarbest = mean(rpb);
sdbest = std(rpb);
 
fprintf("pb: mean: true %.4f estimated %.4f\n",dbarb, dbarbest);
pb: mean: true 0.0000 estimated -0.0146
fprintf("pb: stddev: true %.4f estimated %.4f\n",sdb, sdbest);
pb: stddev: true 2.0000 estimated 2.0116
 
figure(1);
clf;
 
% plot pdf
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 0.5 ] );
plot(d,p,'r-','LineWidth',3);
plot(d,pb,'b-','LineWidth',3);
plot([dbar,dbar]',[0,0.1]','r-','LineWidth',2);
plot([dbarest,dbarest]',[0,0.1]','r:','LineWidth',2);
plot([dbarb,dbarb]',[0,0.1]','b-','LineWidth',2);
plot([dbarbest,dbarbest]',[0,0.1]','b:','LineWidth',2);
xlabel('d');
ylabel('p(d)');
fprintf('Caption: Two normal curves (blue and red curves) of equal mean\n');
Caption: Two normal curves (blue and red curves) of equal mean
fprintf('and unequal variance, along with estimated mean (blue and red bars).\n');
and unequal variance, along with estimated mean (blue and red bars).
 
% true covariance is zero
covab=0.0;
 
pab = p * pb';
gda_draw(pab);
 
% estimated covariance is
S = [rp,rpb];
Cest = cov(S);
 
fprintf('Covariance of p(d1,d2): true %.4f, estimated %.4f\n', covab, Cest(1,2) );
Covariance of p(d1,d2): true 0.0000, estimated -0.0435
% gdama03_11
 
% product of two 2D Normal distributions
 
clear all;
fprintf('gdama03_11\n');
gdama03_11
 
% d-axis
Dd = 0.1;
N = 101;
d1 = Dd*[0:N-1]';
d2 = Dd*[0:N-1]';
dmin=0;
dmax=10;
 
% P1
d1bar = 4;
d2bar = 6;
sd1 = 1.25;
sd2 = 0.75;
mycov = 0.5;
C=zeros(2,2);
C(1,1)=sd1^2;
C(2,2)=sd2^2;
C(1,2)=mycov;
C(2,1)=mycov;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
P1=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d1bar, dmin+Dd*(j-1)-d2bar ]';
P1(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
% save these parameters
C1=C;
C1I=CI;
DBAR1=[d1bar, d2bar]';
 
% P2
d1bar = 6;
d2bar = 4;
sd1 = 0.75;
sd2 = 1.25;
mycov = -0.5;
C=zeros(2,2);
C(1,1)=sd1^2;
C(2,2)=sd2^2;
C(1,2)=mycov;
C(2,1)=mycov;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
P2=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d2bar, dmin+Dd*(j-1)-d2bar ]';
P2(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
% save these parameters
C2=C;
C2I=CI;
DBAR2=[d1bar, d2bar]';
 
P1P2 = P1.*P2;
norm = (Dd^2)*sum(sum(P1P2))
norm = 0.0292
 
 
% from analytic formula
C3 = inv( C1I + C2I );
DBAR3 = C3 * (C1I*DBAR1 + C2I*DBAR2);
d1bar = DBAR3(1);
d2bar = DBAR3(2);
C=C3;
norm = 2*pi*sqrt(det(C));
CI=inv(C);
P3=zeros(N,N);
for i = (1:N)
for j = (1:N)
dd = [ dmin+Dd*(i-1)-d1bar, dmin+Dd*(j-1)-d2bar ]';
P3(i,j) = exp(-0.5*dd'*CI*dd)/norm;
end
end
 
% plot the analytic one, too, to check results
% gda_draw(' ',P1,' ',P2,' ',P1P2,' ',P3);
 
gda_draw(' ',P1,' ',P2,' ',P1P2);
fprintf('Caption: (left) Normal pdf. (middle) Another normal pdf. (right) Their\n');
Caption: (left) Normal pdf. (middle) Another normal pdf. (right) Their
fprintf('product, also a Normal pdf\n');
product, also a Normal pdf
 
% gdama03_12
% example of a Pierson's chi-squared test
 
clear all;
fprintf('gdama03_12\n');
gdama03_12
 
figure(1);
set(gcf,'pos',[10, 10, 600, 250] );
clf;
 
% Part 1: Correct Distribution
fprintf('Part 1: Correct Distribution\n');
Part 1: Correct Distribution
 
% make some Gaussian random daya
Ndata = 200;
dbar = 5;
sigmad = 1;
drandom = random('Normal',dbar,sigmad,Ndata,1);
 
% estimate mean and standard deviation of d's
dbarest = mean(drandom);
sigmadest = std(drandom);
fprintf('mean: true %f estimated %f\n', dbar, dbarest);
mean: true 5.000000 estimated 4.896683
fprintf('sigma: true %f estimated %f\n', sigmad, sigmadest);
sigma: true 1.000000 estimated 0.925331
 
% make histogram. The bins are from d +/- Dd/2.
dmin = 0;
dmax = 10;
Nbin=40;
Dd = (dmax-dmin)/(Nbin-1);
d = Dd*[0:Nbin-2]+Dd/2;
dhist = hist( drandom, d);
 
% normalize to unit area
norm = sum(dhist);
pdest = dhist / norm;
 
% theoretical distribution
pdtrue = normcdf(d+Dd/2,dbarest,sigmadest)-normcdf(d-Dd/2,dbarest,sigmadest);
 
% plot
subplot(1,2,1);
hold on;
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
plot(d,pdest,'r-','LineWidth',3);
plot(d,pdtrue,'b-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
 
% compute chi squared statistic
x2est = Ndata*sum( ((pdest-pdtrue).^2) ./ pdtrue );
K = Nbin-3;
 
% compute P( x2 >= x2est ) = 1 - P(x2<x2est);
P = 1-chi2cdf( x2est, K );
fprintf('K %d chi-squared-est %f P(x2>=x2est) %f\n', K, x2est, P );
K 37 chi-squared-est 17.024597 P(x2>=x2est) 0.997983
 
fprintf('\n');
 
% Part 2: Incorrect Distribution
fprintf('Part 2: Incorrect Distribution\n');
Part 2: Incorrect Distribution
 
% estimate mean and standard deviation of d's
% then make them incorrect
dbarest = mean(drandom)-0.5;
sigmadest = std(drandom)*1.5;
fprintf('mean: true %f estimated %f\n', dbar, dbarest);
mean: true 5.000000 estimated 4.396683
fprintf('sigma: true %f estimated %f\n', sigmad, sigmadest);
sigma: true 1.000000 estimated 1.387997
 
% incorrecr theoretical distribution
pdtrue = normcdf(d+Dd/2,dbarest,sigmadest)-normcdf(d-Dd/2,dbarest,sigmadest);
 
% plot
subplot(1,2,2);
hold on;
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
plot(d,pdest,'r-','LineWidth',3);
plot(d,pdtrue,'b-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
fprintf('Caption: (Left) Histogram (red) and pdf (blue) that fits it well.\n');
Caption: (Left) Histogram (red) and pdf (blue) that fits it well.
fprintf('Same histogram (red) and pdf (blue) that fits it poorly.\n');
Same histogram (red) and pdf (blue) that fits it poorly.
 
% compute chi squared statistic
x2est = Ndata*sum( ((pdest-pdtrue).^2) ./ pdtrue );
K = Nbin-3;
 
% compute P( x2 >= x2est ) = 1 - P(x2<x2est);
P = 1-chi2cdf( x2est, K );
fprintf('K %d chi-squared-est %f P(x2>=x2est) %f\n', K, x2est, P );
K 37 chi-squared-est 76.878804 P(x2>=x2est) 0.000130
% gdama03_13
% Illustrate a computing conditional distributions
% from a joint distribution.
 
clear all;
fprintf('gdama03_13\n');
gdama03_13
 
% set up vectors da and d2
L=40;
Dd = 1.0;
d1 = Dd*[0:L-1]';
d2 = Dd*[0:L-1]';
 
% make a normal distribution
d1bar=15;
d2bar=25;
s1=7;
s2=8;
norm=1/(2*pi*s1*s2);
p1=exp(-((d1-d1bar).^2)/(2*s1*s1));
p2=exp(-((d2-d2bar).^2)/(2*s2*s2));
P=norm*p1*p2';
 
% sum along columns, which integrates P along d2 to get p1=p(d1)
p1 = Dd*sum(P,2);
% sum along rows, which integrates P along d1 to get p2=p(d2)
p2 = Dd*sum(P,1)';
 
% conditional distribution P1g2 = P(d1|d2) = P(d1,d2)/p2
P1g2 = P ./ (ones(L,1)*p2');
 
% conditional distribution P2g1 = P(d2|d1) = P(d1,d2)/p1
P2g1 = P ./ (p1*ones(L,1)');
 
% use simple drawing function that encapsulates all the graphics
gda_draw(P,'caption p(d1,d2)',P1g2,'caption p(d1|d2)',P2g1,'caption p(d2|d1)');
fprintf('Caption: (Left) 2D pdf p(d1,d2). (Middle) Corresponding conditional pdf p(d1|d2).\n');
Caption: (Left) 2D pdf p(d1,d2). (Middle) Corresponding conditional pdf p(d1|d2).
fprintf('(Right) Corresponding conditional pdf p(d2|d1).\n');
(Right) Corresponding conditional pdf p(d2|d1).
% gdama03_14
 
% Realization of Normally-distributed data, calculated several ways
 
clear all;
fprintf('gdama03_14\n');
gdama03_14
 
% generate Normal random numbers with these mean and variance
Nr=1000;
mbar = 5.0;
sigmam = 1.0;
 
% by built in function
m = random('Normal',mbar,sigmam,Nr,1);
mnormal1 = m; % save for future use
 
% by transformation of a uniform distribution
d = random('unif',0,1,Nr,1);
m = norminv(d,mbar,sigmam);
mnormal2 = m; % save for future use
 
% histogram
Nbins=100;
mmin=0;
mmax=10;
Dm = (mmax-mmin)/(Nbins-1);
m = mmin+Dm*[0:Nbins-2]'+Dm/2;
h1 = hist(mnormal1,m);
norm = Dm*sum(h1);
h1 = h1/norm;
h2 = hist(mnormal2,m);
norm = Dm*sum(h2);
h2 = h2/norm;
p = normpdf(m,mbar,sigmam);
 
% plot
figure(1)
clf;
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [mmin, mmax, 0, 0.5] );
plot( m, h1, 'r-', 'LineWidth', 3 );
plot( m, h2, 'b-', 'LineWidth', 3 );
plot( m, p, 'k-', 'LineWidth', 3 );
xlabel('m');
ylabel('p(m)');
 
fprintf('Caption: True Normal pdf (black), and two empirical pdfs (red and blue)\n');
Caption: True Normal pdf (black), and two empirical pdfs (red and blue)
fprintf('computed by from ensembles computed two different ways, using Normal random\n');
computed by from ensembles computed two different ways, using Normal random
fprintf('numbers (red) and by transforming uniform random numbers.\n');
numbers (red) and by transforming uniform random numbers.
% gdama03_15
 
% create realizations of an exponential p.d.f. in two ways,
% transformation of a uniform distribution, and the Metropolis
% algoritm. In this example, the p.d.f. is
% p(d) = c*exp(-d)/c); for d>0
 
clear all;
fprintf('gdama03_15\n');
gdama03_15
 
% d-axis
dmin = -10;
dmax = 10;
N = 201;
Dd = (dmax-dmin)/(N-1);
d = dmin + Dd*[0:N-1]';
 
% evaluate exponential distribution
c = 2.0;
pexp = (0.5/c)*exp(-abs(d)/c);
 
% the usual transformation rule is p(d) = p(m(d)) |dm/dd|
% suppose that p(m) is uniform over m=(-1,1) with amplitude 0.5
% handle the absolute value sign by breaking into two parts,
% Part 1: m>0,
% (1/c)*exp(-d/c)=(+/-)dm/dd. Choose the + sign, in order
% to map m=0 with d=0 and m=1 to d=infinity. Then
% m =(integral)(1/c)*exp(-d/c)dd+constant. Choose constant=1
% so m=1-exp(-d/c). Inverting gives d=-c*ln((1-m))
% Part 2: m<0
% similar calculation gives d=-c*ln((1+m))
% so overall d=-sgn(m)*c*ln((1-abs(m)))
 
% transform realizations of a uniform distribution to p(d)
 
M=10000;
rm=random('Uniform',-1,1,M,1);
rd=-sign(rm).*c.*log((1-abs(rm)));
 
% histogram
Nb=26;
Db=(dmax-dmin)/(Nb-1);
bins=dmin+Db*[0:Nb-1]';
h = hist(rd,bins)';
 
% convert histogram to a p.d.f.
norm=Db*sum(h);
h=h/norm;
 
figure(1);
set(gcf,'pos',[10, 10, 600, 250]);
clf;
 
% plot histogram of transformed realizations and true pdf
subplot(1,2,1);
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 0.3 ] );
% improvise bar chart
for i = (1:Nb-1)
tb = [bins(i)-Db/2, bins(i)-Db/2, bins(i)+Db/2, bins(i)+Db/2]';
th = [0, h(i), h(i), 0]';
plot(tb,th,'b-','LineWidth',3);
end
plot(d,pexp,'r-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
 
% Metropolis
logfactor = log(0.5/c);
Nburn=1000; % delete this number of members at the start of the ensemble
Niter=10000+Nburn;
rd = zeros(Niter,1);
logprd = zeros(Niter,1);
rd(1) = 0.0;
logprd(1) = logfactor-abs(rd(1))/c;
s = 1;
 
for k = (2:Niter)
% old realization
rdo = rd(k-1);
logprdo = logprd(k-1);
rdn = random('Normal',rdo,s);
%prdn = (0.5/c)*exp(-abs(rdn)/c);
logprdn = logfactor-abs(rdn)/c;
% test parameter, ratio of probabilities
gamma = logprdn - logprdo;
% acceptance test
if( gamma>0.0 )
rd(k) = rdn;
logprd(k) = logprdn;
else
a = exp(gamma);
r = random('Uniform',0,1);
if( a>r )
rd(k) = rdn;
logprd(k) = logprdn;
else
rd(k) = rdo;
logprd(k) = logprdo;
end
end
end
 
rd = rd(Nburn+1:Niter,1);
logprd = logprd(Nburn+1:Niter,1);
Niter = length(rd);
 
% histogram, converted to a p.d.f.
h = hist(rd,bins)';
norm=Db*sum(h);
h=h/norm;
 
% plot histogram of transformed realizations and true pdf
subplot(1,2,2);
set(gca,'LineWidth',3);
set(gca,'FontSize',14);
hold on;
axis( [dmin, dmax, 0, 0.3 ] );
% improvise bar chart
for i = (1:Nb-1)
tb = [bins(i)-Db/2, bins(i)-Db/2, bins(i)+Db/2, bins(i)+Db/2]';
th = [0, h(i), h(i), 0]';
plot(tb,th,'b-','LineWidth',3);
end
plot(d,pexp,'r-','LineWidth',3);
xlabel('d');
ylabel('p(d)');
fprintf('(Left) Histogram (blue) of ensemble of realizations of a two-sided exponential pdf,\n');
(Left) Histogram (blue) of ensemble of realizations of a two-sided exponential pdf,
fprintf('for ensemble created by transforming uniformly-distributed random numbers.\n');
for ensemble created by transforming uniformly-distributed random numbers.
fprintf('(Right) Histogram (blue) of ensemble of realizations of a two-sided exponential pdf,\n');
(Right) Histogram (blue) of ensemble of realizations of a two-sided exponential pdf,
fprintf('for ensemble created via the Metropolis-Hastings algorithm.\n');
for ensemble created via the Metropolis-Hastings algorithm.