Kash022 asked . 2022-05-23

Mean and 3-sgima for Lognormal distributions

Hello,
 
I am trying to plot the lognormal distribution over 10 iterations and would like to see the mean and 3 sigma outliers. Somehow, doing this for lognormal plots does not look easy. Also, is it possible that I can skip the histogram in my plots and only look at the lognormal curves? This is the code snippet I am using. Thanks!
 
 
figure(113);
for i = 1:10
norm=histfit(Current_c(i,:),10,'lognormal'); %%Current_c is a 10*100 matrix %%
[muHat, sigmaHat] = lognfit(Current_c(i,:));
% Plot bounds at +- 3 * sigma.
lowBound = muHat - 3 * sigmaHat;
highBound = muHat + 3 * sigmaHat;
yl = ylim;
%line([lowBound, lowBound], yl, 'Color', [0, .6, 0], 'LineWidth', 3);
%line([highBound, highBound], yl, 'Color', [0, .6, 0], 'LineWidth', 3);
line([muHat, muHat], yl, 'Color', [0, .6, 0], 'LineWidth', 3);
grid on;
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Line segmentation', 'NumberTitle', 'Off')
hold on;
end

 

#lognormal , #histogram , #plot , #normal , #gaussian , #3sigma

Expert Answer

Prashant Kumar answered . 2024-05-18 02:06:09

to visualize the mean, sigma and 3 sigma on the curve of the probability distribution that approximates your data you have to consider that the bins of the histogram and too wide to directly get accurate marks on the curve, let me explain:
 
If you try
 
find(y==my)
 =
   Empty matrix: 1-by-0

no match

But you can find the histogram bin where the mean is contained in:

find(y>my)
 =
  Columns 1 through 14
     3     4     5     6     7     8     9    10    11    12    13    14    15    16
  Columns 15 through 28
    17    18    19    20    21    22    23    24    25    26    27    28    29    30
  Columns 29 through 31
    31    32    33

find(y<=my)
 =
  Columns 1 through 14
     1     2    34    35    36    37    38    39    40    41    42    43    44    45
  Columns 15 through 28
    46    47    48    49    50    51    52    53    54    55    56    57    58    59
  Columns 29 through 42
    60    61    62    63    64    65    66    67    68    69    70    71    72    73
  Columns 43 through 56
    74    75    76    77    78    79    80    81    82    83    84    85    86    87
  Columns 57 through 69
    88    89    90    91    92    93    94    95    96    97    98    99   100

Let be ny the reference vector of y:

ny=[1:1:length(y)]

then the mean value is somewhere within the interval

[33 34]

So, to accurately plot my and 3*sy you first have to decide how accurate you need be.

While the y step is, and it varies along the curve

abs(y(33)-y(34))
ans =
   0.258998840336178

the x step is constant

abs(x(33)-x(34))
 =
   0.011562155305573

mean(diff(x)) 
=   0.011562155305573

max(diff(x))   
=   0.011562155305573

min(diff(x))    
=   0.011562155305573

So if you decide that 2 decimals precision (on y) is enough, we have to refine x small enough so that at least one point of y interpolated has the value my truncated after 2nd decimal:

3.02xxxx

for 0.2589 go down to 0.0001 the y step has to be fractioned at least

0.2589/dy=0.0001 hence dy=258.9 , let's take dy=259

the angle

alpha=atand(abs(y(33)-y(34))/abs(x(33)-x(34)))
=
 87.443914593184061
lognormal-distributions
since
 
    dx=.0001/atand(alpha)
    =
         1.119259323130124e-06

x_step=abs(x(33)-x(34))/dx

   =
       1.033018449490165e+04

make it

x_step=10331

then

x2=linspace(x(1),x(end),x_step);
y2=interp1(x,y,x2);

overlap, check both y and y2 are the same:

figure(1);plot(x,y,x2,y2);grid on;grid minor

where in y2 is my located?

find(y2>my)
=
..
..
      3396        3397        3398        3399        3400        3401        3402
  Columns 3228 through 3234
        3403        3404        3405        3406        3407        3408        3409
  Columns 3235 through 3238
        3410        3411        3412        3413

x_mean=max(find(y2>my))

the actual mean value is going to be approximated with y2(3413) = 3.021925056586602

and this error is acceptable

 abs(my-y2(3413))
 =
    1.358549030059386e-04

put what looks like the mean on the curve:

hold on;plot(x2(x_mean),y2(x_mean),'bo')
lognormal-distributions
. . but when checking
 
sum(y)
 =
     3.021789201683596e+02
length(y)
 =
   100
mean(y)
=
   3.021789201683596

and already found mean located between [33 34]

the upper tail cannot even accommodate half single sigma (34.1%)

sum(y([33:end]))/sum(y)*100
 =
  13.483260209259832

Let's find mu summing samples:

pc_target=50
n=2
pc=sum(y([1:n]))/sum(y)*100
while pc

So it turns out mu is within [15 16]

the x boundaries for y~mu

x([15 16])
=
0.201607447590281   0.213169602895854

Using y2 for more detail

pc_target=50
n=2
pc=sum(y2([1:n]))/sum(y2)*100
while pc

for y2, mu is within [1600 1601]

n_mu=1600
and x2 boundaries

x2([1600 1601])
ans =
   0.216920307874458   0.217031116526467

n_mu=1600

the upper y2 boundary for 1 sigma (+34.1%)

pc_target=34.1
n=1
pc=sum(y2([1600:1600+n]))/sum(y2)*100
while pc

the numeral distance for +1sigma is

   n_up_1sigma=1475;

And the location on x2 of +1sigma is:

x2_up_1sigma=x2(1600+n_up_1sigma)
x2_up_1sigma =
   0.380363069587555

the lower y2 boundary for 1 sigma (-34.1%)

pc_target=34.1
n=1
pc=sum(y2([1600-n:1600]))/sum(y2)*100
while pc

the numeral distance for -1sigma is

   n_down_1sigma=842;

And the location on x2 of -1sigma is:

x2_down_1sigma=x2(1600-n_down_1sigma)
 =
   0.123619422882982

Repeating for 3 sigma, understanding that all +- sigma interval cover 89%, then up it is:

pc_target=44.5
n=1
pc=sum(y2([1600:1600+n]))/sum(y2)*100
while pc

and down we go:

pc_target=44.5
n=1
pc=sum(y2([1600-n:1600]))/sum(y2)*100
while pc

and the plot you asked for:

plot(x2,y2,x2(1600),y2(1600),'ro',...
x2(n_mu+n_up_1sigma),y2(n_mu+n_up_1sigma),'rd',x2(n_mu-n_down_1sigma),y2(n_mu-n_down_1sigma),'rd',...
x2(n_mu+n_up_3sigma),y2(n_mu+n_up_3sigma),'gd',x2(n_mu-n_down_3sigma),y2(n_mu-n_down_3sigma),'gd')
grid on;grid minor;
lognormal-distributions

additional comments:

  1. do not mix lognormal mu sigma and normal mu sigma parameters: mind the notation gap. Your x is a reference vector and your y is the actual distribution.

 

Y=lognpdf(X,mu,sigma)

MATLAB function lognpdf calculates the lognormal distribution Y out of the normal distribution X, where X has mean mu and standard variance sigma. In lognpdf help page, both Y and X are random variables, data, each with their respective reference vectors.

As explained in lognpdf help page, the 'mu' and 'sigma' of Y are related to those belonging to X by:

m=exp(mu+sigma^2/2)
v=exp(2*mu+sigma^2)*(exp(sigma^2)-1)

and

mu=log(m^2/(v+m^2)^.5)
sigma=(log(v/m^2)+1)^.5

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.