Python numped random probability random vector with preferred orientation

What is the best way to generate a random vector of biased probability. In other words, a given direction vector "D (dx, dy, dz)", a biased random vector generator will generate random vectors in all directions, but with a higher probability to generate vectors in approximately direction D

import numpy as np
# generate 1000 vectors in all directions
vectors = np.random.random((1000,3))-np.random.random((1000,3))
# generate biased vectors probability
# proba argument gives the biasing intensity or probability to be close to D vector
vectors = biased_proba_random_vectors(directon=(dx,dy,dz), proba=0.7,size=(1000,3))
# biased_proba_random_vectors is a representation, any other implementation is welcomed

should look like this: enter image description here

+4
source share
1 answer

In general, you should study various circular distributions (for example, vonMises, aka "circular normal distribution").

As a quick example:

import numpy as np
import matplotlib.pyplot as plt

# Note higher "kappas" (second arg) result in a _narrower_ distribution
thetas = np.random.vonmises(np.radians(50), 1.5, 100)

# Convert to x, y, dx, dy...
x, y = np.zeros_like(thetas), np.zeros_like(thetas)
dx, dy = np.cos(thetas), np.sin(thetas)

fig, ax = plt.subplots()
ax.quiver(x, y, dx, dy, angles='xy', scale_units='xy', scale=1)
ax.set(xlim=[-1, 1], ylim=[-1, 1], aspect=1)
ax.axis('off')

plt.show()

enter image description here


, , , , .

(, std) . "" .

( 2D , 3D):

import numpy as np
import matplotlib.pyplot as plt

# Input, higher bias results in a _narrower_ distribution
bias, num = 1, 100
direction = np.radians(50)

# Shift the distributions from the center
dx, dy = np.random.normal(0, 0.5, (2, num))
dx += bias * np.cos(direction)
dy += bias * np.sin(direction)

# Plot the results
fig, ax = plt.subplots()
x, y = np.zeros_like(dx), np.zeros_like(dy)
ax.quiver(x, y, dx, dy, angles='xy', scale_units='xy', scale=1)
ax.set(xlim=[-1, 2], ylim=[-1, 2], aspect=1)
ax.axis('off')

plt.show()

enter image description here


, , , vonMises, theta. :

import numpy as np
import matplotlib.pyplot as plt

bias, num = 1.5, 100
direction = np.radians(50)

# Note higher "kappas" (second arg) result in a _narrower_ distribution
thetas = np.random.vonmises(direction, bias, 100)

# Vary magnitude by theta
mag = np.cos(thetas - direction)

# Convert to x, y, dx, dy...
x, y = np.zeros_like(thetas), np.zeros_like(thetas)
dx, dy = mag * np.cos(thetas), mag * np.sin(thetas)

fig, ax = plt.subplots()
ax.quiver(x, y, dx, dy, angles='xy', scale_units='xy', scale=1)
ax.set(xlim=[-0.5, 1], ylim=[-0.5, 1], aspect=1)
ax.axis('off')

plt.show()

enter image description here

+4

All Articles