Let say I have my own custom distribution numpy
vector called p
.
Then p
satisfy the next: np.ndim(p) == 1 & np.sum(p) == 1 & np.all(p >= 0)
With that vector I can easily sample a number in 0 - p.shape
with np.random.choice(np.arange(len(p)), p=p)
In a case I have many such p
s, I have a matrix (with dim 2) P
that satisfies:
np.sum(P[:,i]) == 1 # for all i in P.shape[1]
np.all(P >= 0)
Then I wish to sample P.shape[1]
numbers in the range 0 to P.shape[0]
with probability P.
For example the next code:
P = np.array([[0.2, 0.3],
[0.5, 0.7],
[0.3, 0]])
x = np.random.choice(np.arange(P.shape[0], P[:,0]))
y = np.random.choice(np.arange(P.shape[0], P[:,1]))
will produce my will (x=0 in 0.2, x=1 in 0.5 and x=2 in 0.3 and y=0 in 0.3, y=1 in 0.7).
In my case P
has many columns and I wish to sample all in one shot.
Of course I can do it in a for loop, for example:
random_values = np.empty(P.shape[1])
arange_arr = np.arange(P.shape[0])
for i in range(P.shape[1]):
random_values[i] = np.random.choice(arnge_arr, p=P[:,i])
Trying to find some nupmy-scipy elegant way to do it.
Thanks
Aucun commentaire:
Enregistrer un commentaire