How to generate the Weibull's parameters k and c in Matlab? - matlab

Can anyone explain to me how to generate the Weibull distribution parameters k and c, in Matlab?
I have a file of 8000 data of wind speed, and I'd like to do the following:
Generate the Weibull's k and c parameters of those.
Plot the probability density function against the wind speed.
I am new in Matlab and have not yet been able to do this.

If you have the Statistics toolbox, you can use fitdist:
pd = fitdist(x,'Weibull')
where x is your data. I'm guessing it should return the parameters a and b in:
You can then calculate the pdf (and plot it) using the pdf function. There are some examples (albeit for a normal distribution) in the documentation for fitdist.

Related

How to obtain an equation for a line fitted to data

I am trying to obtain an equation for a function fitted to some histogram data, I was thinking of trying to do this by fitting a rational function as the data doesn't resemble any distribution recognisable by myself.
The data is experimental, and I want to be able to generate a random number according to its distribution. Hence I am hoping to be able to fit it to some sort of PDF from which I can obtain a CDF, which can be rearranged to a function into which a uniformly distributed random number between 0 and 1 can be substituted in order to obtain the desired result.
I have attempted to use the histfit function, which has worked but I couldn't figure out how to obtain an equation for the curve it fitted. Is there something stupid I have missed?
Update: I have discovered the function rationalfit, however I am struggling to figure out what the inputs need to be.
Further Update: Upon exploring the histfit command further I have discovered the option to fit it to a kernal, the figure for which looks promising, however I am only able to obtain a set of x and y values for the curve, not its equation as a I wanted.
From the documentation on histfit:
Algorithms
histfit uses fitdist to fit a distribution to data. Use fitdist
to obtain parameters used in fitting.
So the answer to your question is to use fitdist to get the parameters you're after. Here's the example from the documentation:
rng default; % For reproducibility
r = normrnd(10,1,100,1);
histfit(r)
pd = fitdist(r,'Normal')
pd =
NormalDistribution
Normal distribution
mu = 10.1231 [9.89244, 10.3537]
sigma = 1.1624 [1.02059, 1.35033]

What are the differences between different gaussian functions in Matlab?

y = gauss(x,s,m)
Y = normpdf(X,mu,sigma)
R = normrnd(mu,sigma)
What are the basic differences between these three functions?
Y = normpdf(X,mu,sigma) is the probability density function for a normal distribution with mean mu and stdev sigma. Use this if you want to know the relative likelihood at a point X.
R = normrnd(mu,sigma) takes random samples from the same distribution as above. So use this function if you want to simulate something based on the normal distribution.
y = gauss(x,s,m) at first glance looks like the exact same function as normpdf(). But there is a slight difference: Its calculation is
Y = EXP(-(X-M).^2./S.^2)./(sqrt(2*pi).*S)
while normpdf() uses
Y = EXP(-(X-M).^2./(2*S.^2))./(sqrt(2*pi).*S)
This means that the integral of gauss() from -inf to inf is 1/sqrt(2). Therefore it isn't a legit PDF and I have no clue where one could use something like this.
For completeness we also have to mention p = normcdf(x,mu,sigma). This is the normal cumulative distribution function. It gives the probability that a value is between -inf and x.
A few more insights to add to Leander good answer:
When comparing between functions it is good to look at their source or toolbox. gauss is not a function written by Mathworks, so it may be redundant to a function that comes with Matlab.
Also, both normpdf and normrnd are part of the Statistics and Machine Learning Toolbox so users without it cannot use them. However, generating random numbers from a normal distribution is quite a common task, so it should be accessible for users that have only the core Matlab. Hence, there is a redundant function to normrnd which is randn that is part of the core Matlab.

MATLAB: Generate sample from a known density function

I want to write a function that generates samples from a distribution. To do that, I know its continuous density function. The first idea that came into mind was to write a code like this one:
% a and b are know coefficients.
% besselj is a MATLAB function.
x = 0:0.0001:5;
% PDF=density function (PDF=a (1 x 50.000) vector)
PDF = (x/b).*exp(-(x.^2+a^2)./(2*b)).*besselj(1,(x.*a)./b);
% density function has to be normalized
PDF = PDF./sum(PDF)
% y=the vector containing the sample
[temp , y] = histc(rand(1 , N), [0 cumsum(PDF)]);
For the curious ones, this density function represents a Rice distribution ;-).
However, on second thoughts, this algorithm seems bad. Indeed, from a 100%-known density function, I get a partially-known discretized density function (the vector PDF) and I rely on this new density function to obtain samples. I think that some information is lost because of the discretization.
So, my question is the following: Is there a way I can generate sample from a continuous density function without loosing information ?
Moreover, the code above doesn't seem good to me because the magnitudes of the plot(x,PDF) before and after the normalization are absolutely not the same.

How to plot a distribution based on moments

I am wondering how, in Matlab, to plot a continuous pdf with the following information?
mean=-0.3731
standard deviation= 5.6190
skewness=-3.0003
kurtosis=13.1722
or alternative how do I plot a continous pdf that is not normal? (like it is skewness and has kurtosis, etc)
Thanks!
Those parameters don't define a distribution, but normally you would use "makedist" in matlab to generate a probability distribution object and then plot it.
The following thread has some discussion on defining a distribution. How to generate distributions given, mean, SD, skew and kurtosis in R?
Based on your comment below, I think you are looking for something like the following functio that generates a m by n matrix of random values with the following parameters:
r = pearsrnd(mu,sigma,skew,kurt,m,n)

Generate symbols according to PDF (Probability Density Function) in Matlab

In Matlab I've got a binomial PDF already defined where the set of possible events are 0-255. I would like to generate symbols, from 0-255, according to that PDF. How can I do that?
This is the code used in order to generate the PDF:
x=0:255; %range of possible values
m=255;
b=0.06245;
y=binopdf(x,m,b); %generates a binomial distribution
When plotting "y" I can see that most of the times the symbol that the source will generate will be between 9 to 23. Again, how can I design that symbol source? Thank you.
Use
result = binornd(m,b,R,C);
to generate an R x C matrix of random values drawn from an (m,b) binomial distribution.
If you then plot the histogram
hist(result(:),-.5:255.5)
you can check that (for R and/or C large enough) the obtained values follow the desired binomial distribution.