I want to draw N independent (but not identically distributed) samples from a conditional Bernoulli distribution: y[i] ~ Ber(p[i]), where p[i] = logistic(sum(w .* x[i])). I am currently using the code below, but it seems overly complex. Is there a better way?
Random.seed!(1) D = 5; N = 10; X = randn(N, D) w = randn(D) logits = X*w probs = [StatsFuns.logistic(l) for l in logits] y = zeros(N) ndx = findall(x -> x, rand(N) .> probs) for i in ndx; y[i] = 1; end
Also, what’s the best way to generalize this to more than 2 classes? i.e. how best to sample from a conditional categorical distribution where p[i,:] = softmax(W * x[i])?