Mean and 3-sigma in Lognormal plots

18 次查看(过去 30 天)
Kash022
Kash022 2016-6-15
回答: John BG 2017-3-12
Hi,
I have a lognormal distribution in which I want to mark clearly the mean and 3 sigma ranges. I amusing the below code snippet but it doesn't seem to work.
pd = fitdist(A(i,:)','LogNormal');
y = pdf(pd,A(i,:)');
figure(22); plot(A(i,:)',y,'LineStyle','None','Marker','x');
lowBound = pd.mu - 3 * pd.sigma;
highBound = pd.mu + 3 * pd.sigma;
yl = ylim;
x1 = xlim;
line([lowBound, lowBound], yl, 'Color', [0, .6, 0], 'LineWidth', 1);
line([highBound, highBound], yl, 'Color', [0, .6, 0], 'LineWidth', 1);
line([pd.mu, pd.mu], yl, 'Color', [0, .6, 0], 'LineWidth', 1);
grid on;
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Line segmentation', 'NumberTitle', 'Off')
hold on;
I have attached the lognormal plot in which I would like to view the mean and sigma. I have also attached a sample image of how it should look (from a different simulator). Thanks!

回答(1 个)

John BG
John BG 2017-3-12
Hi Kash022
1.
The attached function lognormal_pdf_123sigma_locations.m calculates the non symmetric locations of +-sigma1 +-sigma2 +-sigma3, along with the mean value and also returns the interpolated curve of the pdf to achieve 1 decimal accuracy, on the pdf, not the data.
mu0: mean
sm1: sigma 1 minus
sp1: sigma 1 plus
sm2: sigma 2 minus
sp2: sigma2 plus
sm3: sigma 3 minus
sp3: sigma 3 plus
call example, replace b with data:
b = betarnd(3,10,100,1); % replace betarand() with data
[mu0,sm1,sp1,sm2,sp2,sm3,sp3,y2,ny2]=lognormal_pdf_123sigma_locations(b)
2.
lognormal_pdf_123sigma_locations.m also returns a couple figures showing data, pdf approximation, and the location of the requested points:
.
.
.
3.
Copy of the function
function [mu0,sm1,sp1,sm2,sp2,sm3,sp3,y2,ny2]=lognormal_pdf_123sigma_locations(b)
% mu0: mean
% sm1: sigma 1 minus
% sp1: sigma 1 plus
% sm2: sigma 2 minus
% sp2: sigma2 plus
% sm3: sigma 3 minus
% sp3: sigma 3 plus
% author: John Bofarull Guix, jgb2012@sky.com
% b = betarnd(3,10,100,1); % simulated data, replace betarand() with real data
figure(1);
% h1=histfit(b,20,'lognormal') % test
h1=histfit(b,20,'lognormal')
b_histogram=h1(1);grid on;
b_pdf_lognormal_fit=h1(2)
y=b_pdf_lognormal_fit.YData;
sumy=sum(y);y=y/sumy;
ny=b_pdf_lognormal_fit.XData;
N=100;
y2=interp(y,N);y2=y2/sum(y2);
ny2=[1:1:numel(y)]*N;
pc_target=50; % mu: distribution mean value
n=2
pc=sum(y2([1:n])) *100
while pc<pc_target
n=n+1;pc=sum(y2([1:n])) *100;
end
mu=n;
figure(2);plot(y2);hold all
figure(2);plot(mu,y2(n),'bo');grid on
pc_target=34; [s1_min,s1_plus]=go_get_it(pc_target,mu,y2); % +- sigma
figure(2);plot(s1_min,y2(s1_min),'ro');
figure(2);plot(s1_plus,y2(s1_plus),'ro');
pc_target=47.5; [s2_min,s2_plus]=go_get_it(pc_target,mu,y2); % +-2*sigma
figure(2);plot(s2_min,y2(s2_min),'ro')
figure(2);plot(s2_plus,y2(s2_plus),'ro');
pc_target=49.7;[s3_min,s3_plus]=go_get_it(pc_target,mu,y2); % +-3*sigma
figure(2);plot(s3_min,y2(s3_min),'ro');
figure(2);plot(s3_plus,y2(s3_plus),'ro');
mu0=mu;
sm1=s1_min;
sp1=s1_plus;
sm2=s2_min;
sp2=s2_plus;
sm3=s3_min;
sp3=s3_plus;
function [sdown,sup]=go_get_it(pc_target,mu,y2)
% support function finds t1 t2 such sum(y([t1:mu]))=sum(y([mu:t2]))
n=1;pc=sum(y2([mu-n:mu])) *100; % search - s
while pc<pc_target
n=n+1;pc=sum(y2([mu-n:mu])) *100;
end
sdown=mu-n;
% figure(h);
% plot(h,sdown,y2(sdown),'ro');hold all;
n=1;pc=sum(y2([mu:mu+n])) *100; % search + s
while pc<pc_target
n=n+1;pc=sum(y2([mu:mu+n])) *100 ;
end
sup=n+mu;
% figure(2);
% plot(h,sup,y2(sup),'ro');
end
end
4.
checks
sum(y2([mu0:sp1]))
sum(y2([sm1:mu0]))
sum(y2([mu0:sp2]))
sum(y2([sm2:mu0]))
sum(y2([mu0:sp3]))
sum(y2([sm3:mu0]))
% upper tail
sum(y2([sp3:end]))
% lower tail
sum(y2([1:sm3]))
=
0.340091218398777
=
0.340121503890558
=
0.475010605933624
=
0.475120352505811
=
0.497000712621476
=
0.497028227492323
=
0.003110773668274
ans =
0.003254677216922
5.
lognormal pdf is normal pdf skewed righ, the sigma1,2,3 locations are no longer symmetric.
.
.
Kash022
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by