Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How can I find the mean between to points of normal distribution?

Subject: How can I find the mean between to points of normal distribution?

From: BobC Cadenza

Date: 2 Aug, 2010 18:14:20

Message: 1 of 15

Given a normal distribution with mu,sigma. How can I find the mean between two points? So for example I want to find the mean value between -.5 *sigma to +2 sigma?

Thanks

Subject: How can I find the mean between to points of normal distribution?

From: Walter Roberson

Date: 2 Aug, 2010 18:37:35

Message: 2 of 15

BobC Cadenza wrote:
> Given a normal distribution with mu,sigma. How can I find the mean
> between two points? So for example I want to find the mean value
> between -.5 *sigma to +2 sigma?

cdf() to +2 sigma, minus the cdf() to -1/2 sigma.

This is, of course, exactly equivalent to integrating the pdf between
-1/2 sigma and +2 sigma.

Subject: How can I find the mean between to points of normal distribution?

From: Wayne King

Date: 2 Aug, 2010 18:42:27

Message: 3 of 15

"BobC Cadenza" <bobc@mailinator.com> wrote in message <i371ts$ht3$1@fred.mathworks.com>...
> Given a normal distribution with mu,sigma. How can I find the mean between two points? So for example I want to find the mean value between -.5 *sigma to +2 sigma?
>
> Thanks

Bob, Just to clarify, do you want to find the probability that a random variable with a N(mu,\sigma^2) distribution lies in the interval [mu-0.5*sigma, mu+2*sigma]?

Or are you really asking for the mean value of the pdf between [mu-0.5*sigma, mu+2*sigma]? (Not sure what the use case is for this... so that's why I'm hesitant to conclude this is really what you're looking for)

In either case, I'm assuming you want to include the mean in the expression because depending on the value of mu and sigma, you may not have any probability in the interval [-0.5*sigma, 2*sigma], or equivalently any nonzero value for the pdf.

To find the probability in the interval [mu-0.5*sigma, mu+2*sigma], if you have the Statistics Toolbox, enter

% for example
mu = 10;
sigma = 2;
normcdf(mu+2*sigma,mu,sigma)-normcdf(mu-0.5*sigma,mu,sigma)

% Note this is the same as
normcdf(2,0,1)-normcdf(-0.5,0,1)
% for a N(0,1) distribution
% or just normcdf(2)-normcdf(-0.5)

Write back if you really did not want the probability.

Wayne

Subject: How can I find the mean between to points of normal distribution?

From: BobC Cadenza

Date: 2 Aug, 2010 19:07:09

Message: 4 of 15

"Wayne King" <wmkingty@gmail.com> wrote in message <i373ij$428$1@fred.mathworks.com>...
> "BobC Cadenza" <bobc@mailinator.com> wrote in message <i371ts$ht3$1@fred.mathworks.com>...
> > Given a normal distribution with mu,sigma. How can I find the mean between two points? So for example I want to find the mean value between -.5 *sigma to +2 sigma?
> >
> > Thanks
>
> Bob, Just to clarify, do you want to find the probability that a random variable with a N(mu,\sigma^2) distribution lies in the interval [mu-0.5*sigma, mu+2*sigma]?
>
> Or are you really asking for the mean value of the pdf between [mu-0.5*sigma, mu+2*sigma]? (Not sure what the use case is for this... so that's why I'm hesitant to conclude this is really what you're looking for)
>
> In either case, I'm assuming you want to include the mean in the expression because depending on the value of mu and sigma, you may not have any probability in the interval [-0.5*sigma, 2*sigma], or equivalently any nonzero value for the pdf.
>
> To find the probability in the interval [mu-0.5*sigma, mu+2*sigma], if you have the Statistics Toolbox, enter
>
> % for example
> mu = 10;
> sigma = 2;
> normcdf(mu+2*sigma,mu,sigma)-normcdf(mu-0.5*sigma,mu,sigma)
>
> % Note this is the same as
> normcdf(2,0,1)-normcdf(-0.5,0,1)
> % for a N(0,1) distribution
> % or just normcdf(2)-normcdf(-0.5)
>
> Write back if you really did not want the probability.
>
> Wayne

Yes I actually want mean. If the values represent equity returns, the mean is the average return. The above finds the median value, which is useful also, but I am trying to find the mean. Do you know how to calculate this?

Thanks.

Subject: How can I find the mean between to points of normal distribution?

From: Roger Stafford

Date: 2 Aug, 2010 19:41:21

Message: 5 of 15

"BobC Cadenza" <bobc@mailinator.com> wrote in message <i371ts$ht3$1@fred.mathworks.com>...
> Given a normal distribution with mu,sigma. How can I find the mean between two points? So for example I want to find the mean value between -.5 *sigma to +2 sigma?
>
> Thanks
- - - - - - - - - -
  If p(x) is the pdf of the original normal distribution with mu and sigma from -infinity to +infinity, but you want the mean value of that distribution restricted to some finite range, [a,b], then you can do the following.

 mean value = int(x*p(x),'x',a,b)/int(p(x),'x',a,b)

The denominator is just the cdf difference between its value at b from that at a. In the case of the numerator you can write

 int(x*p(x),'x',a,b) = int((x-mu)*p(x),'x',a,b) + mu*int(p(x),'x',a,b)

Since this is the normal distribution, the first of these terms is easy to integrate and gives sigma^2*(p(a)-p(b)), while the second term is again obtained from the difference between cdf's.

Roger Stafford

Subject: How can I find the mean between to points of normal distribution?

From: Bruno Luong

Date: 2 Aug, 2010 21:05:24

Message: 6 of 15

"BobC Cadenza" <bobc@mailinator.com> wrote in message <i371ts$ht3$1@fred.mathworks.com>...
> Given a normal distribution with mu,sigma. How can I find the mean between two points? So for example I want to find the mean value between -.5 *sigma to +2 sigma?
>
> Thanks

Take a look at my FEX submission:

http://www.mathworks.com/matlabcentral/fileexchange/23832-truncated-gaussian

sigma=2;
[trash meaneff sigmaeff]=TruncatedGaussian(-sigma,[-0.5*sigma 2*sigma]);
disp(meaneff)
% The mean is 0.8915

Bruno

Subject: How can I find the mean between to points of normal distribution?

From: BobC Cadenza

Date: 2 Aug, 2010 21:22:06

Message: 7 of 15

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i37711$fqu$1@fred.mathworks.com>...
> "BobC Cadenza" <bobc@mailinator.com> wrote in message <i371ts$ht3$1@fred.mathworks.com>...
> > Given a normal distribution with mu,sigma. How can I find the mean between two points? So for example I want to find the mean value between -.5 *sigma to +2 sigma?
> >
> > Thanks
> - - - - - - - - - -
> If p(x) is the pdf of the original normal distribution with mu and sigma from -infinity to +infinity, but you want the mean value of that distribution restricted to some finite range, [a,b], then you can do the following.
>
> mean value = int(x*p(x),'x',a,b)/int(p(x),'x',a,b)
>
> The denominator is just the cdf difference between its value at b from that at a. In the case of the numerator you can write
>
> int(x*p(x),'x',a,b) = int((x-mu)*p(x),'x',a,b) + mu*int(p(x),'x',a,b)
>
> Since this is the normal distribution, the first of these terms is easy to integrate and gives sigma^2*(p(a)-p(b)), while the second term is again obtained from the difference between cdf's.
>
> Roger Stafford

Thanks Roger, that works perfectly. Since that worked so nice, how about adding in a wrinkle. How would I include in the mean the values, that are outside a and b limited to a and b. For example p(x<a)=p(a) and p(x>b)=p(b)

Subject: How can I find the mean between to points of normal distribution?

From: Roger Stafford

Date: 2 Aug, 2010 22:39:04

Message: 8 of 15

"BobC Cadenza" <bobc@mailinator.com> wrote in message <i37ctu$1ta$1@fred.mathworks.com>...
> Thanks Roger, that works perfectly. Since that worked so nice, how about adding in a wrinkle. How would I include in the mean the values, that are outside a and b limited to a and b. For example p(x<a)=p(a) and p(x>b)=p(b)
- - - - - - - - - - -
  Well that alters the assumption I was making. When you write

 int(x*p(x),'x',a,b)/int(p(x),'x',a,b)

as I did, that implies that the probability P2(a<=x & x<=b) is one, so that P2(x<a) and P2(x>b) are therefore each equal to zero (where P2 means the revised probability.) In other words, by that former assumption x can't be outside [a,b].

  It sounds to me as though you are now saying that the probability density between a and b is to remain that same as in the original normal density, but that all x's below a are now to be concentrated in the single point x=a and similarly for x>b at the point x=b. In that case there is no normalization to be done. The mean would be the straightforward expression:

 int(a*p(x),'x',-inf,a) + int(x*p(x),'x',a,b) + int(b*p(x),'x',b,inf)

where again p(x) is the original pdf for mu and sigma. The first and last of these integrals just involves the cdf for p(x). The middle term is evaluated by the same trick I described earlier.

  I hope I have understood correctly what you are asking.

Roger Stafford

Subject: How can I find the mean between to points of normal distribution?

From: BobC Cadenza

Date: 3 Aug, 2010 13:08:04

Message: 9 of 15

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i37he8$cmp$1@fred.mathworks.com>...
> "BobC Cadenza" <bobc@mailinator.com> wrote in message <i37ctu$1ta$1@fred.mathworks.com>...
> > Thanks Roger, that works perfectly. Since that worked so nice, how about adding in a wrinkle. How would I include in the mean the values, that are outside a and b limited to a and b. For example p(x<a)=p(a) and p(x>b)=p(b)
> - - - - - - - - - - -
> Well that alters the assumption I was making. When you write
>
> int(x*p(x),'x',a,b)/int(p(x),'x',a,b)
>
> as I did, that implies that the probability P2(a<=x & x<=b) is one, so that P2(x<a) and P2(x>b) are therefore each equal to zero (where P2 means the revised probability.) In other words, by that former assumption x can't be outside [a,b].
>
> It sounds to me as though you are now saying that the probability density between a and b is to remain that same as in the original normal density, but that all x's below a are now to be concentrated in the single point x=a and similarly for x>b at the point x=b. In that case there is no normalization to be done. The mean would be the straightforward expression:
>
> int(a*p(x),'x',-inf,a) + int(x*p(x),'x',a,b) + int(b*p(x),'x',b,inf)
>
> where again p(x) is the original pdf for mu and sigma. The first and last of these integrals just involves the cdf for p(x). The middle term is evaluated by the same trick I described earlier.
>
> I hope I have understood correctly what you are asking.
>
> Roger Stafford

Thanks,
This worked perfectly also. I owe you a drink.

Subject: How can I find the mean between to points of normal distribution?

From: Roger Stafford

Date: 3 Aug, 2010 18:24:07

Message: 10 of 15

"BobC Cadenza" <bobc@mailinator.com> wrote in message <i394bk$5c$1@fred.mathworks.com>...
> Thanks,
> This worked perfectly also. I owe you a drink.
- - - - - - - - - -
  You don't owe me anything, BobC. I get pleasure (sometimes) out of answering these questions. The "thanks" are appreciated, however.

Roger Stafford

Subject: How can I find the mean between to points of normal distribution?

From: BobC Cadenza

Date: 15 Aug, 2010 19:50:04

Message: 11 of 15

After my first tests I thought everything was working correctly, but it seems that it is not. I have attached a test script whereas it calculates the expected value with a brute
force way, and a way using the formula you stated in your reply. It calculates these 2 values for varying values of mu. You can see from the plot, that the green line which represents your formula, goes outside the min/max. I probably just have a sign incorrect or
something, so if you could take a look I would really appreciate it.

function [ ] = testExpectedValue()

    a = 25;
    b = 26;
    sigma = 1;
    
    mean1(1) = 0;
    mean2(1) = 0;
    
    for mu=20:.1:30
    
        x = random('normal',mu,sigma,1000000,1);
        
        mean1(end+1) = mean(max(a,min(b,x)));
        
        mean2(end+1) = a*normcdf(a,mu,sigma) + sigma^2*(normpdf(a)-normpdf(b)) + ...
            mu*(normcdf(b,mu,sigma) - normcdf(a,mu,sigma)) + ...
            b*(1-normcdf(b,mu,sigma));
       
    
    end
    
    close all;
    plot(mean1(2:end),20:.1:30);hold on; plot(mean2(2:end),20:.1:30,'g');

end

Subject: How can I find the mean between to points of normal distribution?

From: Bruno Luong

Date: 15 Aug, 2010 21:30:21

Message: 12 of 15

The distribution you generate by clipping the normal distribution looks very artificial, I'm not sure where it can find a meaningful interpretation in practice. But any way here is the answer for the question (I'm not sure you derive the formula in your script):

a = 25;
b = 26;
sigma = 1;

mean1(1) = 0;
mean2(1) = 0;

for mu=20:.1:30
    
    x = mu + sigma*randn(1000000,1);
    
    mean1(end+1) = mean(max(a,min(b,x)));
    
    aa = (a-mu)/(sqrt(2)*sigma);
    bb = (b-mu)/(sqrt(2)*sigma);
    c = ((-exp(-bb^2)+exp(-aa^2))/sqrt(pi) + ...
        aa*(1+erf(aa)) + bb*erfc(bb))/sqrt(2);
    mean2(end+1) = mu + sigma.*c;
    
end

close all;
plot(mean1(2:end),20:.1:30,'b',mean2(2:end),20:.1:30,'g');

% Bruno

Subject: How can I find the mean between to points of normal distribution?

From: Roger Stafford

Date: 16 Aug, 2010 00:57:03

Message: 13 of 15

"BobC Cadenza" <bobc@mailinator.com> wrote in message <i49gdc$7fj$1@fred.mathworks.com>...
> After my first tests I thought everything was working correctly, but it seems that it is not. I have attached a test script whereas it calculates the expected value with a brute
> force way, and a way using the formula you stated in your reply. It calculates these 2 values for varying values of mu. You can see from the plot, that the green line which represents your formula, goes outside the min/max. I probably just have a sign incorrect or
> something, so if you could take a look I would really appreciate it.
>
> function [ ] = testExpectedValue()
>
> a = 25;
> b = 26;
> sigma = 1;
>
> mean1(1) = 0;
> mean2(1) = 0;
>
> for mu=20:.1:30
>
> x = random('normal',mu,sigma,1000000,1);
>
> mean1(end+1) = mean(max(a,min(b,x)));
>
> mean2(end+1) = a*normcdf(a,mu,sigma) + sigma^2*(normpdf(a)-normpdf(b)) + ...
> mu*(normcdf(b,mu,sigma) - normcdf(a,mu,sigma)) + ...
> b*(1-normcdf(b,mu,sigma));
>
>
> end
>
> close all;
> plot(mean1(2:end),20:.1:30);hold on; plot(mean2(2:end),20:.1:30,'g');
>
> end
- - - - - - - - - - - -
  I see an error in the term "sigma^2*(normpdf(a)-normpdf(b))" for mean2. You didn't include the mu and sigma arguments. Though the documentation doesn't say, I suspect they default to the standard normal with mu = 0 and sigma = 1 and that would make your result incorrect.

  In my opinion Mathworks should either state that default in their documentation or else produce an error if the arguments are missing.

Roger Stafford

Subject: How can I find the mean between to points of normal distribution?

From: BobC Cadenza

Date: 16 Aug, 2010 11:54:06

Message: 14 of 15

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i4a2cv$86e$1@fred.mathworks.com>...
> "BobC Cadenza" <bobc@mailinator.com> wrote in message <i49gdc$7fj$1@fred.mathworks.com>...
> > After my first tests I thought everything was working correctly, but it seems that it is not. I have attached a test script whereas it calculates the expected value with a brute
> > force way, and a way using the formula you stated in your reply. It calculates these 2 values for varying values of mu. You can see from the plot, that the green line which represents your formula, goes outside the min/max. I probably just have a sign incorrect or
> > something, so if you could take a look I would really appreciate it.
> >
> > function [ ] = testExpectedValue()
> >
> > a = 25;
> > b = 26;
> > sigma = 1;
> >
> > mean1(1) = 0;
> > mean2(1) = 0;
> >
> > for mu=20:.1:30
> >
> > x = random('normal',mu,sigma,1000000,1);
> >
> > mean1(end+1) = mean(max(a,min(b,x)));
> >
> > mean2(end+1) = a*normcdf(a,mu,sigma) + sigma^2*(normpdf(a)-normpdf(b)) + ...
> > mu*(normcdf(b,mu,sigma) - normcdf(a,mu,sigma)) + ...
> > b*(1-normcdf(b,mu,sigma));
> >
> >
> > end
> >
> > close all;
> > plot(mean1(2:end),20:.1:30);hold on; plot(mean2(2:end),20:.1:30,'g');
> >
> > end
> - - - - - - - - - - - -
> I see an error in the term "sigma^2*(normpdf(a)-normpdf(b))" for mean2. You didn't include the mu and sigma arguments. Though the documentation doesn't say, I suspect they default to the standard normal with mu = 0 and sigma = 1 and that would make your result incorrect.
>
> In my opinion Mathworks should either state that default in their documentation or else produce an error if the arguments are missing.
>
> Roger Stafford

Thanks for catching that, and also thanks Bruno for the alternative solution.

Subject: How can I find the mean between to points of normal distribution?

From: Steven_Lord

Date: 16 Aug, 2010 13:46:24

Message: 15 of 15



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:i4a2cv$86e$1@fred.mathworks.com...
> "BobC Cadenza" <bobc@mailinator.com> wrote in message
> <i49gdc$7fj$1@fred.mathworks.com>...

*snip*

> I see an error in the term "sigma^2*(normpdf(a)-normpdf(b))" for mean2.
> You didn't include the mu and sigma arguments. Though the documentation
> doesn't say, I suspect they default to the standard normal with mu = 0 and
> sigma = 1 and that would make your result incorrect.
>
> In my opinion Mathworks should either state that default in their
> documentation or else produce an error if the arguments are missing.

I've forwarded that suggestion to the documentation staff for Statistics
Toolbox.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us