Matlab Gumbel Distribution (Extreme Maximum case) - matlab

The default MATLAB 'Extreme Value' distribution (also called a Gumbel distribution) is used for the extreme MIN case.
Given the mean and standard deviation of Gumbel distributed random variables for the extreme MAX case, I can get the location and scale parameter using the following equations from this website:
My question is how do I transform the MATLAB 'Extreme Value' distribution from the MIN case to MAX case (MATLAB says "using the negative of the original values").
I would like to use MATLAB's icdf function, thus do I need to negate the location and scale parameters of the inputs?

Judging by the last paragraph of your question you want the inverse CDF of the maximum Gumbel distribution. Given that Matlab offers the inverse CDF of the Gumbel min distribution as follows:
X = evinv(P,mu,sigma);
You can get the inverse CDF of the Gumbel max by:
X = -evinv(1-P, -mu, sigma);
Note that for computing the PDF or CDF different expressions hold (that can be similarly worked out based on the definition of the two distributions).

I have been working on the same problem and this is what I've concluded:
To create the probability distribution function of extreme value type I or gumbel for the maximum case in matlab using mu and sigma, or location and scale parameter, you can use the makedist function, use generalized extreme value function and set the k parameter equal to zero. This will create a mirror image of the ev, or extreme value function minimum which is used for gumbel in matlab. The mirror of the minimum case of gumbel is the maximum case of gumbel.
pd = makedist('GeneralizedExtremeValue','k',0,'sigma',sigma,'mu',mu);
so using the above command all you have to do is replace sigma and mu with the values you've got.
I am a student and this is my understanding of this problem.

Related

Can I use the in-built function 'mle' for Maximum Likelihood Estimate that maximizes the log-likelihood of a vector in MATLAB for this purpose?

In my work I have a vector of particular length 'L'. I want to maximize the log-likelihood estimate of the vector using maximum likelihood estimation. I have followed the following procedure:
x=[vector(1:L)];
pd = fitdist(x,'Normal');
z=log(normpdf(x,pd(1),pd(2)); %for finding log-likelihood
However, I do not know whether the above program maximizes the log-likelihood. I am getting a scalar value in 'z', can I use that scalar value to plot back the desired signal? The distribution is assumed to be Normal distribution. Is there any other way to do the problem?

Draw random numbers from the Gumbel distribution in Matlab

Question: I would like your help to draw random numbers from the Gumbel distribution with scale mu and location beta in Matlab.
I want to use the definition of the Gumbel distribution provided by Wikipedia (see the PDF and CDF definitions on the right of the page).
Notice: The package evrnd in Matlab, described here, cannot be used (or maybe can be used with some modifications?) because it considers flipped signs.
Let me explain better this last point.
Let's fix the scale to 0 and the location to 1.
Now, following Wikipedia and other textbooks (for example, here p.42) the Gumbel PDF is
exp(-x)*exp(-exp(-x))
In Matlab though it seems that evrnd considers random draws from the following PDF:
exp(x)*exp(-exp(x))
You can see that in Matlab -x is replaced with x.
Any idea on what is the best way to proceed?
According to the Wikipedia, the inverse cumulative distribution function is
Q(p) = mu - beta*log(-log(p))
From this function, the inverse transformation method can be applied. Thus
sz = [1 1e6]; % desired size for result array
mu = 1; % location parameter
beta = 2.5; % scale parameter
result = mu - beta*log(-log(rand(sz)))
gives result with i.i.d. Gumbel-distributed numbers. Plotting the histogram for these example values gives
>> histogram(result, 51)
If you want to use the evrnd function (Statistics Toolbox), you only need to change the sign of the output. According to the documentation,
R = evrnd(mu,sigma,[m,n,...])
The version used here is suitable for modeling minima; the mirror image of this distribution can be used to model maxima by negating R.
Thus, use
result = -evrnd(mu, beta, sz);

Sample multinomial distribution in Matlab without using mnrnd

I know for a random variable x that P(x=i) for each i=1,2,...,100. Then how may I sample x by a multinomial distribution, based on the given P(x=i) in Matlab?
I am allowed to use the Matlab built-in commands rand and randi, but not mnrnd.
In general, you can sample numbers from any 1 dimensional probability distribution X using a uniform random number generator and the inverse cumulative distribution function of X. This is known as inverse transform sampling.
random_x = xcdf_inverse(rand())
How does this apply here? If you have your vector p of probabilities defining your multinomial distribution, F = cumsum(p) gives you a vector that defines the CDF. You can then generate a uniform random number on [0,1] using temp = rand() and then find the first row in F greater than temp. This is basically using the inverse CDF of the multinomial distribution.
Be aware though that for some distributions (eg. gamma distribution), this turns out to be an inefficient way to generate random draws because evaluating the inverse CDF is so slow (if the CDF cannot expressed analytically, slower numerical methods must be used).

Python and Matlab compute variances differently. Am I using the correct functions? [duplicate]

I try to convert matlab code to numpy and figured out that numpy has a different result with the std function.
in matlab
std([1,3,4,6])
ans = 2.0817
in numpy
np.std([1,3,4,6])
1.8027756377319946
Is this normal? And how should I handle this?
The NumPy function np.std takes an optional parameter ddof: "Delta Degrees of Freedom". By default, this is 0. Set it to 1 to get the MATLAB result:
>>> np.std([1,3,4,6], ddof=1)
2.0816659994661326
To add a little more context, in the calculation of the variance (of which the standard deviation is the square root) we typically divide by the number of values we have.
But if we select a random sample of N elements from a larger distribution and calculate the variance, division by N can lead to an underestimate of the actual variance. To fix this, we can lower the number we divide by (the degrees of freedom) to a number less than N (usually N-1). The ddof parameter allows us change the divisor by the amount we specify.
Unless told otherwise, NumPy will calculate the biased estimator for the variance (ddof=0, dividing by N). This is what you want if you are working with the entire distribution (and not a subset of values which have been randomly picked from a larger distribution). If the ddof parameter is given, NumPy divides by N - ddof instead.
The default behaviour of MATLAB's std is to correct the bias for sample variance by dividing by N-1. This gets rid of some of (but probably not all of) of the bias in the standard deviation. This is likely to be what you want if you're using the function on a random sample of a larger distribution.
The nice answer by #hbaderts gives further mathematical details.
The standard deviation is the square root of the variance. The variance of a random variable X is defined as
An estimator for the variance would therefore be
where denotes the sample mean. For randomly selected , it can be shown that this estimator does not converge to the real variance, but to
If you randomly select samples and estimate the sample mean and variance, you will have to use a corrected (unbiased) estimator
which will converge to . The correction term is also called Bessel's correction.
Now by default, MATLABs std calculates the unbiased estimator with the correction term n-1. NumPy however (as #ajcr explained) calculates the biased estimator with no correction term by default. The parameter ddof allows to set any correction term n-ddof. By setting it to 1 you get the same result as in MATLAB.
Similarly, MATLAB allows to add a second parameter w, which specifies the "weighing scheme". The default, w=0, results in the correction term n-1 (unbiased estimator), while for w=1, only n is used as correction term (biased estimator).
For people who aren't great with statistics, a simplistic guide is:
Include ddof=1 if you're calculating np.std() for a sample taken from your full dataset.
Ensure ddof=0 if you're calculating np.std() for the full population
The DDOF is included for samples in order to counterbalance bias that can occur in the numbers.

How To Fit Multivariate Normal Distribution To Data In MATLAB?

I'm trying to fit a multivariate normal distribution to data that I collected, in order to take samples from it.
I know how to fit a (univariate) normal distribution, using the fitdist function (with the 'Normal' option).
How can I do something similar for a multivariate normal distribution?
Doesn't using fitdist on every dimension separately assumes the variables are uncorrelated?
There isn't any need for a specialized fitting function; the maximum likelihood estimates for the mean and variance of the distribution are just the sample mean and sample variance. I.e., compute the sample mean and sample variance and you're done.
Estimate the mean with mean and the variance-covariance matrix with cov.
Then you can generate random numbers with mvnrnd.
It is also possible to use fitmgdist, but for just a multivariate normal distribution mean and cov are enough.
Yes, using fitdist on every dimension separately assumes the variables are uncorrelated and it's not what you want.
You can use [sigma,mu] = robustcov(X) function, where X is your multivariate data, i.e. X = [x1 x2 ... xn] and xi is a column vector data.
Then you can use Y = mvnpdf(X,mu,sigma) to get the values of the estimated normal probability density function.
https://www.mathworks.com/help/stats/normfit.html
https://www.mathworks.com/help/stats/mvnpdf.html