How to generate a random probability distribution julia

graphtheory12345678

I have a list of keys in a dictionary, and I would like to assign a number between 0 and 1 to each item in such a way that the assigned numbers sum to 1. How would one do this? I tried something with rand(), but that didn't work. Any suggestions?

Eric

You can generate a "probability vector", i.e. a vector of length d whose entries are all non-negative and sum to 1, uniformly at random by:

using Random

rand_probvec(d) = rand_probvec(Random.GLOBAL_RNG, d)

function rand_probvec(rng, d)
    unif = rand(rng, d - 1)
    T = eltype(unif)
    w = zeros(T, d + 1)
    w[2:d] .= sort(unif)
    w[d+1] = one(T)
    return diff(w)
end

following http://www.cs.cmu.edu/~nasmith/papers/smith+tromble.tr04.pdf.

If you don't care if your distribution is drawn uniformly at random, you could just draw d random numbers and divide by their sum, e.g.

function rand_probvec2(d)
    v = rand(d)
    return v / sum(v)
end

Either way, once you can generate random probability vectors, if you have a list of elements you wish to assign the probabilities to, you can do something like

my_list = ["a", "b", "c"]
prob_vec = rand_probvec(length(my_list))
my_dist = Dict(k => prob_vec[i] for (i, k) in enumerate(my_list))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to generate random numbers with predefined probability distribution?

How to generate random categorical data in python according to a probability distribution?

Generate random variables from a probability distribution

Matlab: generate random numbers from normal distribution with given probability

How to generate random int with given probability in python?

How to generate random number to express the probability

How to generate a random date in Julia?

Generate Random Boolean Probability

How to compare Numpy.random's probability distribution functions

How to sample random variable from a different sized probability distribution

Random index with custom probability distribution

How to generate a random normal distribution of integers

How to generate a random weighted distribution of elements

How to generate random alphanumeric string in julia?

How to generate a random hexadecimal string in Julia

How to generate random positive real number in Julia?

How to generate random strings that matches a Regex in Julia?

How to generate random integers by group in julia

How to make a random function in fortran to generate the same random distribution into array?

How to generate pseudo random string based on percentage probability?

Flutter: How to generate random boolean value with my own probability?

How to generate a random sequence given a probability matrix of transitions?

How can I generate a random boolean with probability (1 to 100) in C?

How to generate random numbers when given the value of a probability density function?

Generate random number with given probability

Generate random graph with probability p

Make a probability distribution from two distributions in Julia

Estimating a probability distribution and sampling from it in Julia

Create random numbers with left skewed probability distribution