Function to transform empirical distribution to a uniform distribution in Matlab?
Asked Answered
S

3

0

I know the procedure of transforming one distribution to another by the use of CDF. However, I would like to know if there is existing function in Matlab which can perform this task?

My another related question is that I computed CDF of my empirical using ecdf() function in Matlab for a distribution with 10,000 values. However, the output that I get from it contains only 9967 values. How can I get total 10,000 values for my CDF? Thanks.

Shortcake answered 3/7, 2012 at 19:22 Comment(0)
S
1
for t=1:nT 
    [f_CDFTemp,x_CDFTemp]=ecdf(uncon_noise_columndata_all_nModels_diff_t(:,1,t)); % compute CDF of empirical distribution
    f_CDF(1:length(f_CDFTemp),t) = f_CDFTemp; % store the CDF of different distributions with unequal size in a new variable
    x_CDF(1:length(x_CDFTemp),t) = x_CDFTemp;
    b_unifdist=4*t;
    [Noise.N, Noise.X]=hist((a_unifdist+(b_unifdist-a_unifdist).*f_CDF(:,t)),100); % generate the uniform distribution by using the CDF of empirical distribution as the CDF of the uniform distribution
    generatedNoise(:,:,t)=emprand(Noise.X,nRows,nCol); % sample some random numbers from the uniform distribution generated above by using 'emrand' function
end
Shortcake answered 7/7, 2012 at 22:57 Comment(0)
S
1

As you say, all you need is the CDF. The CDF of a normal distribution can be expressed in terms of the erf Matlab function.

Untested example:

C = @(x)(0.5 * (1 + erf(x/sqrt(2))));

x = randn(1,1000);  % Zero-mean, unit variance
y = C(x);           % Approximately uniform
Stanton answered 3/7, 2012 at 20:37 Comment(13)
If I have an empirical CDF which is normal, but not deducted through analytical formula, then how would your code change? Thanks.Shortcake
@S_H: I'm not sure I understand. Either the distribution is normal, or it isn't. Are you asking how it would change for a different mean and variance?Stanton
Let's put it another way. I have an empirical distribution and I want to transform it to uniform distribution. I have CDF of that empirical distribution. In that case how would your answer change? Thanks!Shortcake
@S_H: Replace C with a function that evaluates your CDF.Stanton
How am I transforming it to uniform distribution in that case? x is a random number sampled from normal distribution and the CDF I have is an empirical distribution. How would my final distribution be uniform?Shortcake
@S_H: I don't follow. Your initial question was about transforming normal->uniform, which is what my answer above describes. Are you now talking about transforming arbitrary->uniform, normal->arbitrary, or something else?Stanton
I am talking about arbitrary->uniform. Sorry about the confusion. Thanks.Shortcake
@S_H: Ok, then if you know the CDF of your arbitrary distribution, CDF(x) (where x are your random samples) will approximate a uniform distribution. en.wikipedia.org/wiki/Probability_integral_transformStanton
Where x are random numbers sampled from which distribution?Shortcake
@S_H: Your arbitrary distribution.Stanton
I am sorry but it's not clear. If I use the random number from the arbitrary dist. in the CDF function of the same arbitrary dist., then I am going to get the CDF value of the same arbitrary distribution. I have rephrased my question if you could revise your answer based on the new rephrased question. Thanks.Shortcake
@S_H: No you won't. Please read the Wiki article I linked to: en.wikipedia.org/wiki/Probability_integral_transform.Stanton
I had read this article before posting the question as well, however, I could not totally get how to solve for Y. Can you revise your answer to solve for Y from a known CDF of an empirical distribution? Thanks!Shortcake
S
1
for t=1:nT 
    [f_CDFTemp,x_CDFTemp]=ecdf(uncon_noise_columndata_all_nModels_diff_t(:,1,t)); % compute CDF of empirical distribution
    f_CDF(1:length(f_CDFTemp),t) = f_CDFTemp; % store the CDF of different distributions with unequal size in a new variable
    x_CDF(1:length(x_CDFTemp),t) = x_CDFTemp;
    b_unifdist=4*t;
    [Noise.N, Noise.X]=hist((a_unifdist+(b_unifdist-a_unifdist).*f_CDF(:,t)),100); % generate the uniform distribution by using the CDF of empirical distribution as the CDF of the uniform distribution
    generatedNoise(:,:,t)=emprand(Noise.X,nRows,nCol); % sample some random numbers from the uniform distribution generated above by using 'emrand' function
end
Shortcake answered 7/7, 2012 at 22:57 Comment(0)
M
0

This is not exactly what you are looking for, but it shows how to do the opposite. Reversing it should not be that bad.

Motmot answered 3/7, 2012 at 19:33 Comment(1)
Thanks, but I came across this while searching before asking the question!Shortcake

© 2022 - 2024 — McMap. All rights reserved.