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.

3 Answers3

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
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • If I have an empirical CDF which is normal, but not deducted through analytical formula, then how would your code change? Thanks. –  Jul 03 '12 at 20:58
  • @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? – Oliver Charlesworth Jul 03 '12 at 20:59
  • 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! –  Jul 03 '12 at 21:02
  • @S_H: Replace C with a function that evaluates your CDF. – Oliver Charlesworth Jul 03 '12 at 21:03
  • 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? –  Jul 03 '12 at 21:11
  • @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? – Oliver Charlesworth Jul 03 '12 at 21:15
  • I am talking about arbitrary->uniform. Sorry about the confusion. Thanks. –  Jul 03 '12 at 21:25
  • @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. http://en.wikipedia.org/wiki/Probability_integral_transform – Oliver Charlesworth Jul 03 '12 at 21:26
  • Where `x` are random numbers sampled from which distribution? –  Jul 03 '12 at 21:34
  • @S_H: Your arbitrary distribution. – Oliver Charlesworth Jul 03 '12 at 21:34
  • 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. –  Jul 03 '12 at 22:25
  • @S_H: No you won't. Please read the Wiki article I linked to: http://en.wikipedia.org/wiki/Probability_integral_transform. – Oliver Charlesworth Jul 03 '12 at 22:34
  • 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! –  Jul 04 '12 at 02:04
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
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.

John Kane
  • 4,383
  • 1
  • 24
  • 42
  • Thanks, but I came across this while searching before asking the question! –  Jul 03 '12 at 19:36