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:
generate two normal pdf from different mean and standerd devition values.

Subject: generate two normal pdf from different mean and standerd devition values.

From: Amy

Date: 23 Mar, 2012 15:31:17

Message: 1 of 15

How can I generate two pdf curves normal distribution from two sets of random values with a different mean and standerd devition values.
I used rand but it always gives me almost the same mean for the two curves as it generats the values between 0:1. and the plot doesn't look like a bell curve?
please help me. Thank you

Subject: generate two normal pdf from different mean and standerd devition

From: Alan Weiss

Date: 23 Mar, 2012 16:14:39

Message: 2 of 15

On 3/23/2012 11:31 AM, Amy wrote:
> How can I generate two pdf curves normal distribution from two sets of
> random values with a different mean and standerd devition values. I used
> rand but it always gives me almost the same mean for the two curves as
> it generats the values between 0:1. and the plot doesn't look like a
> bell curve?
> please help me. Thank you

If you are trying to generate random samples from normal distributions:
doc randn
Make sure to read the examples.

If, instead, you are trying to fit normal distributions to data:
doc normfit

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: generate two normal pdf from different mean and standerd devition

From: Amy

Date: 23 Mar, 2012 16:55:17

Message: 3 of 15

Thanks for your reply. Here is what I did:
S1=randn(1,100);
Max1=max(S1);
Min1=min(S1);
step1=(Max1-Min1)/1000;
[mu1,sigma1]=normfit(S1)
PDF1=normpdf(Min1:step1:Max1,mu1,sigma1);
S2=randn(1,100);
Max2=max(S2);
Min2=min(S2);
step2=(Max2-Min2)/1000;
[mu2,sigma2]=normfit(S2)
PDF2=normpdf(Min2:step2:Max2,mu2,sigma2);

but I don't want to get the mean value as a negative number, it should be positive? how can I do this? another question: how can I separate these two curvesbso they are not overlapping ?

Alan Weiss <aweiss@mathworks.com> wrote in message <jki7hf$b7i$1@newscl01ah.mathworks.com>...
> On 3/23/2012 11:31 AM, Amy wrote:
> > How can I generate two pdf curves normal distribution from two sets of
> > random values with a different mean and standerd devition values. I used
> > rand but it always gives me almost the same mean for the two curves as
> > it generats the values between 0:1. and the plot doesn't look like a
> > bell curve?
> > please help me. Thank you
>
> If you are trying to generate random samples from normal distributions:
> doc randn
> Make sure to read the examples.
>
> If, instead, you are trying to fit normal distributions to data:
> doc normfit
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Subject: generate two normal pdf from different mean and standerd devition

From: Alan Weiss

Date: 23 Mar, 2012 17:08:56

Message: 4 of 15

On 3/23/2012 12:55 PM, Amy wrote:
> Thanks for your reply. Here is what I did:
> S1=randn(1,100);
> Max1=max(S1);
> Min1=min(S1);
> step1=(Max1-Min1)/1000;
> [mu1,sigma1]=normfit(S1)
> PDF1=normpdf(Min1:step1:Max1,mu1,sigma1);
> S2=randn(1,100);
> Max2=max(S2);
> Min2=min(S2);
> step2=(Max2-Min2)/1000;
> [mu2,sigma2]=normfit(S2)
> PDF2=normpdf(Min2:step2:Max2,mu2,sigma2);
>
> but I don't want to get the mean value as a negative number, it should
> be positive? how can I do this? another question: how can I separate
> these two curvesbso they are not overlapping ?
>
> Alan Weiss <aweiss@mathworks.com> wrote in message
> <jki7hf$b7i$1@newscl01ah.mathworks.com>...
>> On 3/23/2012 11:31 AM, Amy wrote:
>> > How can I generate two pdf curves normal distribution from two sets of
>> > random values with a different mean and standerd devition values. I
>> used
>> > rand but it always gives me almost the same mean for the two curves as
>> > it generats the values between 0:1. and the plot doesn't look like a
>> > bell curve?
>> > please help me. Thank you
>>
>> If you are trying to generate random samples from normal distributions:
>> doc randn
>> Make sure to read the examples.
>>
>> If, instead, you are trying to fit normal distributions to data:
>> doc normfit
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation

Please go back to
doc randn
and read the examples. They will show you how to generate random samples
with the mean and variance you specify. The default mean and variance
are 0 and 1 respectively.

Since you can use normfit, you have Statistics Toolbox. Therefore you
can generate your random samples a different way:
doc normrnd
This makes it easier to specify your mean and variance.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: generate two normal pdf from different mean and standerd devition

From: Amy

Date: 23 Mar, 2012 17:20:29

Message: 5 of 15

Alan Weiss <aweiss@mathworks.com> wrote in message <jkian8$ngo$1@newscl01ah.mathworks.com>...
> On 3/23/2012 12:55 PM, Amy wrote:
> > Thanks for your reply. Here is what I did:
> > S1=randn(1,100);
> > Max1=max(S1);
> > Min1=min(S1);
> > step1=(Max1-Min1)/1000;
> > [mu1,sigma1]=normfit(S1)
> > PDF1=normpdf(Min1:step1:Max1,mu1,sigma1);
> > S2=randn(1,100);
> > Max2=max(S2);
> > Min2=min(S2);
> > step2=(Max2-Min2)/1000;
> > [mu2,sigma2]=normfit(S2)
> > PDF2=normpdf(Min2:step2:Max2,mu2,sigma2);
> >
> > but I don't want to get the mean value as a negative number, it should
> > be positive? how can I do this? another question: how can I separate
> > these two curvesbso they are not overlapping ?
> >
> > Alan Weiss <aweiss@mathworks.com> wrote in message
> > <jki7hf$b7i$1@newscl01ah.mathworks.com>...
> >> On 3/23/2012 11:31 AM, Amy wrote:
> >> > How can I generate two pdf curves normal distribution from two sets of
> >> > random values with a different mean and standerd devition values. I
> >> used
> >> > rand but it always gives me almost the same mean for the two curves as
> >> > it generats the values between 0:1. and the plot doesn't look like a
> >> > bell curve?
> >> > please help me. Thank you
> >>
> >> If you are trying to generate random samples from normal distributions:
> >> doc randn
> >> Make sure to read the examples.
> >>
> >> If, instead, you are trying to fit normal distributions to data:
> >> doc normfit
> >>
> >> Alan Weiss
> >> MATLAB mathematical toolbox documentation
>
> Please go back to
> doc randn
> and read the examples. They will show you how to generate random samples
> with the mean and variance you specify. The default mean and variance
> are 0 and 1 respectively.
>
> Since you can use normfit, you have Statistics Toolbox. Therefore you
> can generate your random samples a different way:
> doc normrnd
> This makes it easier to specify your mean and variance.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

But I don't have the mean and variance, I want to calculate them from the genrating values???

Subject: generate two normal pdf from different mean and standerd devition

From: Tom Lane

Date: 23 Mar, 2012 18:04:07

Message: 6 of 15

>> > S1=randn(1,100);
...
>> > but I don't want to get the mean value as a negative number, it should
>> > be positive? how can I do this? another question: how can I separate
>> > these two curvesbso they are not overlapping ?
...
> But I don't have the mean and variance, I want to calculate them from the
> genrating values???

I don't think it's clear what you are trying to do. You're sampling from a
theoretical normal distribution with mean 0 and variance 1. Your sample will
have mean close to 0, but it has as good a chance of being negative as
positive. Can you explain why you want it to be positive, or why you think
it must be positive?

Similarly, if you generate another sample from that same distribution, it is
sensible that the histograms would overlap. In what way to you want to
separate them?

-- Tom

Subject: generate two normal pdf from different mean and standerd devition

From: Amy

Date: 24 Mar, 2012 13:59:13

Message: 7 of 15

"Tom Lane" <tlane@mathworks.com> wrote in message <jkidun$5jd$1@newscl01ah.mathworks.com>...
> >> > S1=randn(1,100);
> ...
> >> > but I don't want to get the mean value as a negative number, it should
> >> > be positive? how can I do this? another question: how can I separate
> >> > these two curvesbso they are not overlapping ?
> ...
> > But I don't have the mean and variance, I want to calculate them from the
> > genrating values???
>
> I don't think it's clear what you are trying to do. You're sampling from a
> theoretical normal distribution with mean 0 and variance 1. Your sample will
> have mean close to 0, but it has as good a chance of being negative as
> positive. Can you explain why you want it to be positive, or why you think
> it must be positive?
>
> Similarly, if you generate another sample from that same distribution, it is
> sensible that the histograms would overlap. In what way to you want to
> separate them?
>
> -- Tom

All what I'm trying to do is to generate two sets of values ( all are positive should be) then obtain the mean and sigma for these two sets (should be different). then plot these two pdf curves.
It should be very simple but I just started using matlab so that's why I stucked. "rand" gave me very similler values of m1 and m2 , like 0.49 and 0.50
I appreciate your help and suggestions.

Subject: generate two normal pdf from different mean and standerd devition

From: Tom Lane

Date: 24 Mar, 2012 15:27:18

Message: 8 of 15

> All what I'm trying to do is to generate two sets of values ( all are positive should be) then obtain the mean and sigma for these two sets (should be different). then plot these two pdf curves.
> It should be very simple but I just started using matlab so that's why I stucked. "rand" gave me very similler values of m1 and m2 , like 0.49 and 0.50
> I appreciate your help and suggestions.

Okay, if you use randn to generate

    x = 88+5*randn(100,1)

then it will have roughly a normal distribution with mean 88 and
variance 5^2=25. Then you can generate y in a similar way choosing a
different mean and variance so the two do not overlap and so that a
value less than 0 is very unlikely.

-- Tom

Subject: generate two normal pdf from different mean and standerd devition

From: dpb

Date: 24 Mar, 2012 15:31:41

Message: 9 of 15

On 3/24/2012 8:59 AM, Amy wrote:
...

> All what I'm trying to do is to generate two sets of values ( all are
> positive should be) then obtain the mean and sigma for these two sets
> (should be different). then plot these two pdf curves.

Well, what characteristics do you expect these two nebulously defined
sets of values to have? You have tried two different things as near as
I can tell; sampling from a standard normal (mean zero, std dev one) and
another case from a uniform distribution (range zero to one; ergo mean
0.5). The (pseudo)random number generators will create sets of values
each of which will approximate closely the underlying characteristics of
the theoretical distribution they model (what else would you expect;
they would be of very limited value for their stated purpose if did
anything else) so naturally, your results are:

a) very similar from one sample set to another,
b) not at all surprising that a set from a uniform distribution doesn't
much look like the bell curve from a normal, and
c) have means very near 0.0 for the normal and 0.5 for the uniform but
are, naturally, scattered somewhat around that theoretical value.

> It should be very simple but I just started using matlab so that's why I
> stucked. "rand" gave me very similler values of m1 and m2 , like 0.49
> and 0.50
> I appreciate your help and suggestions.

The first thing is to define clearly and unambiguously what it is you're
really after. If, you're just trying to demonstrate two pdf's of a pair
of normal distributions that are somewhat separated in means and perhaps
also w/ different spreads, then you need to scale the results of the two
sample sets obtained from the standardized RNG before you compute the
pdf and statistics. You could consider picking those values via another
random selection if you don't want to set them specifically. But,
unless and until you do that (or something very similar) you'll continue
to get sample sets that essentially overlap.

--

Subject: generate two normal pdf from different mean and standerd devition

From: Amy

Date: 24 Mar, 2012 15:51:16

Message: 10 of 15

Tom Lane <tplane@gmail.com> wrote in message <e1bb236b-06ff-4f8e-825c-dcc67f67d7b0@er9g2000vbb.googlegroups.com>...
> > All what I'm trying to do is to generate two sets of values ( all are positive should be) then obtain the mean and sigma for these two sets (should be different). then plot these two pdf curves.
> > It should be very simple but I just started using matlab so that's why I stucked. "rand" gave me very similler values of m1 and m2 , like 0.49 and 0.50
> > I appreciate your help and suggestions.
>
> Okay, if you use randn to generate
>
> x = 88+5*randn(100,1)
>
> then it will have roughly a normal distribution with mean 88 and
> variance 5^2=25. Then you can generate y in a similar way choosing a
> different mean and variance so the two do not overlap and so that a
> value less than 0 is very unlikely.
>
> -- Tom

Thank you Tom, I really appriciate your help, I did it and change the S1 and S2 in my previous code to the one you mentioned, is it still the same way how I optain the pdf and plot it? then why I get the mean (from the plot statestics) diffrent than the mu values?

Subject: generate two normal pdf from different mean and standerd devition

From: dpb

Date: 24 Mar, 2012 16:07:18

Message: 11 of 15

On 3/24/2012 10:51 AM, Amy wrote:
...

> Thank you Tom, I really appriciate your help, I did it and change the S1
> and S2 in my previous code to the one you mentioned, is it still the
> same way how I optain the pdf and plot it? then why I get the mean (from
> the plot statestics) diffrent than the mu values?

S1 and S2 from your above postings were the two vectors from returned by
randn()

You can't change them directly, you scale/shift the results from randn
as Tom showed--

pick_a_mean_value=85;
now_a_std_dev=5;
S1=pick_a_mean_value + now_a_std_dev*randn(100,1);

Then choose a different set of values next time. After that you'll get
a set of means and variances close to the values chosen.

--

Subject: generate two normal pdf from different mean and standerd devition

From: Amy

Date: 27 Mar, 2012 16:04:13

Message: 12 of 15

Is it right to calculate the mean and veriance for the set S1 and S2 from "normfit" then using the "normpdf" to get the data for plotting???? I was wondering why when I do this I get differnt mean and sigma on the plot??
dpb <none@non.net> wrote in message <jkkrfl$fs9$1@speranza.aioe.org>...
> On 3/24/2012 10:51 AM, Amy wrote:
> ...
>
> > Thank you Tom, I really appriciate your help, I did it and change the S1
> > and S2 in my previous code to the one you mentioned, is it still the
> > same way how I optain the pdf and plot it? then why I get the mean (from
> > the plot statestics) diffrent than the mu values?
>
> S1 and S2 from your above postings were the two vectors from returned by
> randn()
>
> You can't change them directly, you scale/shift the results from randn
> as Tom showed--
>
> pick_a_mean_value=85;
> now_a_std_dev=5;
> S1=pick_a_mean_value + now_a_std_dev*randn(100,1);
>
> Then choose a different set of values next time. After that you'll get
> a set of means and variances close to the values chosen.
>
> --
>
>

Subject: generate two normal pdf from different mean and standerd devition

From: dpb

Date: 27 Mar, 2012 20:14:15

Message: 13 of 15

On 3/27/2012 11:04 AM, Amy wrote:
> Is it right to calculate the mean and veriance for the set S1 and S2
> from "normfit" then using the "normpdf" to get the data for plotting????
> I was wondering why when I do this I get differnt mean and sigma on the
> plot??

...

It appears using normfit and normpdf would be appropriate, yes (not
having toolbox, all I can do is look at the online doc, but certainly
seems reasonable).

Well, presumably you get different values for the mean and std-dev
because you didn't input the same data for S1 and S2.

You haven't shown your work; how can we tell what, specifically, you
have ended up doing?

BTW, please don't toppost in replying--makes conversation follow hard.

--

Subject: generate two normal pdf from different mean and standerd devition

From: carlos lopez

Date: 27 Mar, 2012 21:20:11

Message: 14 of 15

"Amy" wrote in message <jki9tl$ivj$1@newscl01ah.mathworks.com>...
> Thanks for your reply. Here is what I did:
> S1=randn(1,100);
> Max1=max(S1);
> Min1=min(S1);
> step1=(Max1-Min1)/1000;
> [mu1,sigma1]=normfit(S1)
> PDF1=normpdf(Min1:step1:Max1,mu1,sigma1);
> S2=randn(1,100);
> Max2=max(S2);
> Min2=min(S2);
> step2=(Max2-Min2)/1000;
> [mu2,sigma2]=normfit(S2)
> PDF2=normpdf(Min2:step2:Max2,mu2,sigma2);
>
> but I don't want to get the mean value as a negative number, it should be positive? how can I do this? another question: how can I separate these two curvesbso they are not overlapping ?

Hello Amy:
After reading your posting it is yet unclear what is your goal. If you have a population of K numbers which follow N(mu,sigma) and you have it contaminated with P (P<<K) numbers which follow N(mu',sigma'), and if both pdf's do not overlap too much, then you can search for "high breakdown methods" and/or the LIBRA toolbox.
Such methods/code considers the population of P numbers as outliers, and are able to distinguish from the larger population.
In your example (above) you have set exactly K=P=100, so the proposed trick will not work.
Hope this helps
Regards
Carlos

Subject: generate two normal pdf from different mean and standerd devition

From: Tom Lane

Date: 28 Mar, 2012 14:11:56

Message: 15 of 15

> Is it right to calculate the mean and veriance for the set S1 and S2 from
> "normfit" then using the "normpdf" to get the data for plotting???? I was
> wondering why when I do this I get differnt mean and sigma on the plot??

It is fine to do that. For example, after the code you posted before you
could run

 x1 = Min1:step1:Max1;
 x2 = Min2:step2:Max2;
 plot(x1,PDF1,'b-', x2,PDF2,'r-')

I just ran your code and got:
 

Tags for this Thread

No tags are associated with 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