Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing probability distribution function in Java

I'm trying to implement a probability distribution function in java where it returns the ith entry in the array with probability:

Fi = 6i(n-i) / (n3 - n)

where n is the array length i.e. for an array length 4:

P1 = 3/10, P2 = 4/10, P3 = 3/10, P4 = 0

Note that this function assumes numbering from 1 to n rather 0 to n-1 as in Java.

At the moment I'm just using the uniform distribution i.e.

 int i = (int)(Math.random()*((arraySize)-1));

with the -1 so it doesn't choose the last element (i.e. Pn = 0 as in the above formula).

Anyone with any ideas or tips on implementing this?

like image 227
user528676 Avatar asked Jul 02 '11 13:07

user528676


People also ask

How do you create a probability in Java?

Java has a class called java. util. Random which can generate random numbers. If you want something to happen with probability 1/25, simply generate a random number between 1 and 25 (or 0 and 24 inclusive) and check whether that number is equal to 1.

What is CDF in Java?

For every probability distribution function f(x), there is a corresponding cumulative distribution function (CDF), denoted by F(x) and defined as: Table 4-3.

What is probability distribution in deep learning?

Probability distribution determines the likelihood of any outcome. The mathematical expression takes a specific value of x and shows the possibility of a random variable with p(x). Some general properties of the probability distribution are – The total of all probabilities for any possible value becomes equal to 1.

What is probability distribution in artificial intelligence?

Probability Distribution for a Random Variable shows how Probabilities are distributed over for different values of the Random Variable.


2 Answers

double rand = Math.random(); // generate a random number in [0,1]
F=0;
// you test if rand is in [F(1)+..+F(i):F(1)+..+F(i)+F(i+1)] it is in this rnge with proba P(i) and therefore if it is in this range you return i
for (int i=1,i<array.size();i++ ){
   F+=F(i);
   if rand < F
       return i;
}
return array.size(); // you went through all the array then rand==1 (this probability is null) and you return n
like image 85
Ricky Bobby Avatar answered Sep 28 '22 07:09

Ricky Bobby


This is essentially what thomson_matt says, but a little more formally: You should perform discrete inverse transform sampling. Pseudocode for your example:

p = [0.3, 0.4, 0.3. 0.0]
c = [0.3, 0.7, 1.0, 1.0] // cumulative sum

generate x uniformly in continuous range [0,1]
find max i such that c[i] < x.
like image 39
YXD Avatar answered Sep 28 '22 08:09

YXD