From: jenya polyakova on
"Wayne King" <wmkingty(a)gmail.com> wrote in message <hbt479$cgo$1(a)fred.mathworks.com>...
> "jenya polyakova" <jenya56(a)yahoo.com> wrote in message <hbt2hf$mhn$1(a)fred.mathworks.com>...
> > "jenya polyakova" <jenya56(a)yahoo.com> wrote in message <hbt1se$a77$1(a)fred.mathworks.com>...
> > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hbt17o$sfp$1(a)fred.mathworks.com>...
> > > > "jenya polyakova" <jenya56(a)yahoo.com> wrote in message <hbsvjo$f61$1(a)fred.mathworks.com>...
> > > > > I have a vector x.
> > > > > I did gamma fitting by:
> > > > > [phat]=gamfit(x);
> > > > > Note now phat has two elements in it.
> > > > > Now I want to plot the histogram of x by
> > > > > hist(x)
> > > > > Then I also want to plot on top that histogram a curve of my gamma distribution >that is specified by parameter phat. How do I do that? THANKS
> > > >
> > > > Hi Jenya,
> > > > one way is to construct a "probability" histogram of your data and then use gampdf() with the estimated alpha and beta parameters and overlay the pdf for that on your data. Here I just generate some gamma-distributed random numbers. Obviously, you would use your data.
> > > >
> >
> >
> > Dear W
> > > > R = gamrnd(2,2,100,1);
> > > > binWidth = 2; %These aren't hard and fast
> > > > binCtrs = 1:binWidth:19; %These aren't hard and fast
> > > > n=length(R);
> > > > counts = hist(R,binCtrs);
> > > > prob = counts / (n * binWidth);
> > > > h = bar(binCtrs,prob,'hist');
> > > > set(h,'FaceColor',[.9 .9 .9]);
> > > > x=0:.01:19; hold on;
> > > > y = gampdf(x,2,2); %gamma pdf
> > > > plot(x,y,'k','linewidth',2);
> > > >
> > > > Hope that helps,
> > > > wayne
> > >
> > >
> > > Dear Wayne,
> > > thanks...but where does my data come in? Thanks
> > > JEnya
> >
> >
> > Dear Wayne,
> > I tried to do it. But what I need is really a vector y of the same size as x but that has estimated values coming from Gamma. THANKS
>
> Hi Jenya, I'm sorry I don't understand. You get an alpha and a beta parameter from your data, x, from the gamfit function. You can use that alpha and beta along with gampdf() to get the appropriate gamma probability density function. Then you make a histogram of your x data as I have done above (you're just substituting your data for the random numbers I've used) and overlay your gamma pdf parametrized by the alpha and beta parameters. Isn't that what you are trying to do? Going back to your question:
> "Now I want to plot the histogram of x by
> " hist(x). Then I also want to plot on top that histogram a curve of my gamma distribution that is specified by parameter phat. How do I do that? THANKS"
>
> wayne

Dear Wayne,
thank you so much for replying. In your code I do not see where use function gamfit(R) to get your alpha and beta? Also, your R vector and vector y of different size, I would like it to be of the same size. Any ideas? Thank you so much
Jenya
From: Wayne King on
"jenya polyakova" <jenya56(a)yahoo.com> wrote in message <hbt594$lec$1(a)fred.mathworks.com>...
> "Wayne King" <wmkingty(a)gmail.com> wrote in message <hbt479$cgo$1(a)fred.mathworks.com>...
> > "jenya polyakova" <jenya56(a)yahoo.com> wrote in message <hbt2hf$mhn$1(a)fred.mathworks.com>...
> > > "jenya polyakova" <jenya56(a)yahoo.com> wrote in message <hbt1se$a77$1(a)fred.mathworks.com>...
> > > > "Wayne King" <wmkingty(a)gmail.com> wrote in message <hbt17o$sfp$1(a)fred.mathworks.com>...
> > > > > "jenya polyakova" <jenya56(a)yahoo.com> wrote in message <hbsvjo$f61$1(a)fred.mathworks.com>...
> > > > > > I have a vector x.
> > > > > > I did gamma fitting by:
> > > > > > [phat]=gamfit(x);
> > > > > > Note now phat has two elements in it.
> > > > > > Now I want to plot the histogram of x by
> > > > > > hist(x)
> > > > > > Then I also want to plot on top that histogram a curve of my gamma distribution >that is specified by parameter phat. How do I do that? THANKS
> > > > >
> > > > > Hi Jenya,
> > > > > one way is to construct a "probability" histogram of your data and then use gampdf() with the estimated alpha and beta parameters and overlay the pdf for that on your data. Here I just generate some gamma-distributed random numbers. Obviously, you would use your data.
> > > > >
> > >
> > >
> > > Dear W
> > > > > R = gamrnd(2,2,100,1);
> > > > > binWidth = 2; %These aren't hard and fast
> > > > > binCtrs = 1:binWidth:19; %These aren't hard and fast
> > > > > n=length(R);
> > > > > counts = hist(R,binCtrs);
> > > > > prob = counts / (n * binWidth);
> > > > > h = bar(binCtrs,prob,'hist');
> > > > > set(h,'FaceColor',[.9 .9 .9]);
> > > > > x=0:.01:19; hold on;
> > > > > y = gampdf(x,2,2); %gamma pdf
> > > > > plot(x,y,'k','linewidth',2);
> > > > >
> > > > > Hope that helps,
> > > > > wayne
> > > >
> > > >
> > > > Dear Wayne,
> > > > thanks...but where does my data come in? Thanks
> > > > JEnya
> > >
> > >
> > > Dear Wayne,
> > > I tried to do it. But what I need is really a vector y of the same size as x but that has estimated values coming from Gamma. THANKS
> >
> > Hi Jenya, I'm sorry I don't understand. You get an alpha and a beta parameter from your data, x, from the gamfit function. You can use that alpha and beta along with gampdf() to get the appropriate gamma probability density function. Then you make a histogram of your x data as I have done above (you're just substituting your data for the random numbers I've used) and overlay your gamma pdf parametrized by the alpha and beta parameters. Isn't that what you are trying to do? Going back to your question:
> > "Now I want to plot the histogram of x by
> > " hist(x). Then I also want to plot on top that histogram a curve of my gamma distribution that is specified by parameter phat. How do I do that? THANKS"
> >
> > wayne
>
> Dear Wayne,
> thank you so much for replying. In your code I do not see where use function gamfit(R) to get your alpha and beta? Also, your R vector and vector y of different size, I would like it to be of the same size. Any ideas? Thank you so much
> Jenya

Dear Jenya, I don't have your data, so I just used random numbers to illustrate the point. But just to be clear: Let's assume that your data is Data. I'm going to simulate this, because I don't have your data.

Data = gamrnd(2,2,1000,1); % pretend the data is 1000x1 it doesn't matter
[phat]=gamfit(Data); % here is where you estimate the parameters
% In this instance I got back phat=[2.0779 1.8674]; %not surprising
Now I'm not sure why you want the output of gampdf to be the same length as the data because when you plot it, the histogram is plotted as counts in bins and the pdf is a continuous function of the independent variable, but if you want to be the same length, okay:

x = 0:(21/1000):21-(21/1000); % now x is 1x1000
binWidth = 2; %These aren't hard and fast
binCtrs = 1:binWidth:19; %These aren't hard and fast
n=length(Data);
counts = hist(Data,binCtrs);
prob = counts / (n * binWidth);
h = bar(binCtrs,prob,'hist');
set(h,'FaceColor',[.9 .9 .9]);
hold on;
y = gampdf(x,2,2); %gamma pdf y is 1 x1000 the same length as your Data
plot(x,y,'k','linewidth',2);
xlabel('Random Variable-X'); ylabel('f(X)');

Hope that helps,
wayne
From: Peter Perkins on
jenya polyakova wrote:
> I have a vector x.
> I did gamma fitting by:
> [phat]=gamfit(x);
> Note now phat has two elements in it.
> Now I want to plot the histogram of x by
> hist(x)
> Then I also want to plot on top that histogram a curve of my gamma distribution that is specified by parameter phat. How do I do that? THANKS

Jenya, you can do this by hand, but I think you'd be happier if you read your data in to DFITTOOL and both fit it and plotted it using the various ployts that are built in to that tool.

Hope this helps.
From: Tom Lane on
>> Then I also want to plot on top that histogram a curve of my gamma
>> distribution that is specified by parameter phat. How do I do that?
>> THANKS
>
> Jenya, you can do this by hand, but I think you'd be happier if you read
> your data in to DFITTOOL and both fit it and plotted it using the various
> ployts that are built in to that tool.

Or in the latest version of matlab, if you just want a quick plot, you can
do something like this:

histfit(x,15,'gamma')

This makes a 15-bin histogram of x and overlays a fitted gamma density.

-- Tom