2

I am varying the signal strength for synthetic images. I need the signal to vary between 0 and 0.1, but I need to do this with a gamma distribution so that more of them fall around the .01/.02 range. The problem is that I am using the 2010 version of Matlab without the Statistics Toolbox that doesn't have the gamrnd function a part of its library.

Any and all help is greatly appreciated.

EBH
  • 10,350
  • 3
  • 34
  • 59
Kelsey
  • 21
  • 3

1 Answers1

5

You can use the Inverse transform sampling method to convert a uniform distribution to any other distribution:

P = rand(1000);
X = gaminv(P(:),2,2); % with k = 2 and theta = 2

Here is a litle demonstration:

for k = [1 3 9]
    for theta = [0.5 1 2]
        X = gaminv(P(:),k,theta);
        histogram(X,50)
        hold on
    end
end

Which gives:

gamma dist


Edit:

Without the statistics toolbox, you can use the Marsaglia's simple transformation-rejection method to generate random numbers from gamma distribution with rand and randn:

N = 10000; % no. of tries
% distribution parameters:
a = 0.5;
b = 0.1;
% Marsaglia's simple transformation-rejection:
d = a - 1/3;
x = randn(N,1);
U = rand(N,1);
v = (1+x./sqrt(9*d)).^3;
accept = log(U)<(0.5*x.^2+d-d*v+d*log(v));
Y = d*(v(accept)).*b;

Now Y is distributed like gamma(a,b). We can test the result using the gamrnd function:

n = size(Y,1);
X = gamrnd(a,b,n,1);

And the histograms of Y, and X are:

gamma dist 2

However, keep in mind that gamma distribution might not fit your needs because it has no specific upper bound (i.e. goes to infinity). So you may want to use another (bounded) distribution, like beta divided by 10.

EBH
  • 10,350
  • 3
  • 34
  • 59
  • This version of matlab does not know what gaminv means. – Kelsey Jan 31 '17 at 21:26
  • @Kelsey do you have the **Statistics Toolbox**? – EBH Jan 31 '17 at 21:33
  • Yeah it is weird that it doesn't recognize it. I don't have the toolbox. I have found the distribution on wikipedia, but I am not sure how to use it to create random numbers between 0 - 0.1. https://en.wikipedia.org/wiki/Gamma_distribution#Generating_gamma-distributed_random_variables – Kelsey Jan 31 '17 at 21:39
  • `gaminv` is part of the toolbox, so you won't be able to use it. – EBH Jan 31 '17 at 21:43
  • Thank you so much. I will try that. We also just went over the gamma distribution in my stats class. I realize this isn't the best way to do this, but I am doing it for work. And the boss really wants the gamma distribution. But I will talk to him about other possibilities. Again, thank you. – Kelsey Feb 02 '17 at 16:18
  • This actually ended up not being what I needed. My statistics professor was able to help. Excel has a gamma function and I used the gamma inverse function to create the data points I needed and imported that data into Matlab. But thank you for the quick replies and all of the help. – Kelsey Feb 09 '17 at 15:08