dimanche 17 juillet 2022

Sample numbers from a custom distribution of probabilities

I need to sample numbers from a distribution, but the probabilities of all numbers must be something I can control.

For example, say I have 5 nodes (a, b, c, d, e) on a graph, and each node has an "attachment probability" that determines how likely a new node added to the graph will "stick" to it.

For example, the attachment probabilities of my 5 nodes might be:

{
    a: 0.1
    b: 0.1
    c: 0.2
    d: 0.1
    e: 0.5
}

When I add a new node, it should attach to node "e" most of the time (since it has the highest probability) but of course this should not be all the time, since these are probabilities.

I could manually create a sample of say 1000 numbers, whose occurrences follow the above probabilities. So the array would have 100 letter a, 100 letter b, 200 letter c, 100 letter d and 500 letter e. Then I could do a random sample from this array, which would be the same as drawing from a distribution with the above mentioned probabilities.

Is there any other (less manual) way of doing this in javascript? Does the Math or random API have a way to specify the probabilities that underly the sampling?




Aucun commentaire:

Enregistrer un commentaire