dimanche 11 septembre 2022

Given a list of random variable and an expected value, how to generate probability distribution in python

I have a list of random values x_1,x_2, ... x_n and an expected value X.I wanna write a function that takes these 2 things and randomly generates one of the many probability distributions that meets the above mentioned constraint.

Rephrasing the question as generate a vector p of length n such that

0 <= p_i < =1

| p | = 1

p.x = X

Using information I found here I hacked together the below solution, but it doesn't work as the probabilities are not b/w 0 and 1

def normal_random_distribution(data_array, data_len, X_array, expd_vl):
    e_mat = np.concatenate(([X_array], [np.ones(data_len)]), axis = 0)
    f_mat = np.array([expd_vl, 1])
    v_vec = np.linalg.lstsq(e_mat, f_mat,rcond=None)[0]
    e_null = sla.null_space(e_mat)
    lamda_lower = np.linalg.pinv(e_null) @ (-1 * v_vec)
    lamda_upper = np.linalg.pinv(e_null) @ ( 1 - v_vec)
    lamda = lamda_lower + random.random()*(lamda_upper - lamda_lower)
    sol = v_vec + e_null @ lamda
    return sol

How can I generate p ? The sol has l1 norm 1, and |sol * x| = X. I was hoping interpolating b/w lamda_lower and lamda_upper would make it b/w 0 and 1 but it does not.




Aucun commentaire:

Enregistrer un commentaire