根据概率密度函数 p(x,y,z) 随机填充 3D 网格
Randomly fill a 3D grid according to a probability density function p(x,y,z)
如何按照给定概率密度函数指定的顺序填充 3D 网格?
使用 python,我想以 随机 顺序放置点,但根据该区域的一些指定概率分布,没有重复点.
依次:
- 创建离散的 3D 网格
- 为每个网格点指定一个概率密度函数,pdf(x,y,z)
- 放置一个点 (x0,y0,z0),其随机位置与 pdf(x,y,z)
成正比
- 继续添加点(不重复)直到填满所有位置
期望的结果是网格中所有点的所有点(无重复)的列表,以便它们被填充。
下面没有实现多元高斯绘图:
xi_sorted = np.random.choice(x_grid.ravel(),x_grid.ravel().shape, replace=False, p = pdf.ravel())
yi_sorted = np.random.choice(x_grid.ravel(),x_grid.ravel().shape, replace=False, p = pdf.ravel())
zi_sorted = np.random.choice(x_grid.ravel(),x_grid.ravel().shape, replace=False, p = pdf.ravel())
那是因为p(x)*p(y)*p(z) != p(x,y,z)
除非三个变量是独立的。您可以考虑像 Gibbs sampler 这样的东西,通过从单变量分布中按顺序绘制来从联合分布中绘制。
在多元正态的特定情况下,您可以使用(完整示例)
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D
from math import *
num_points = 4000
sigma = .5;
mean = [0, 0, 0]
cov = [[sigma**2,0,0],[0,sigma**2,0],[0,0,sigma**2]]
x,y,z = np.random.multivariate_normal(mean,cov,num_points).T
svals = 16
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d',aspect='equal')
ax.scatter(x,y,z, s=svals, alpha=.1,cmap=cm.gray)
这是一个示例,使用高斯 pdf(见图表)。此代码很容易适应任何指定的 pdf:
%matplotlib qt
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#number of points to lay down:
n = 4000;
#create meshgrid:
min, max, L = -5, 5, 91;
[x_grid,y_grid,z_grid] = np.meshgrid(np.linspace(min,max,L),np.linspace(min,max,L),np.linspace(min,max,L))
xi,yi,zi = x_grid.ravel(),y_grid.ravel(),z_grid.ravel()
#create normalized pdf (gaussian here):
pdf = np.exp(-(x_grid**2 + y_grid**2 + z_grid**2));
pdf = pdf/np.sum(pdf);
#obtain indices of randomly selected points, as specified by pdf:
randices = np.random.choice(np.arange(x_grid.ravel().shape[0]), n, replace = False,p = pdf.ravel())
#random positions:
x_rand = xi[randices]
y_rand = yi[randices]
z_rand = zi[randices]
fig = plt.figure();
ax = fig.add_subplot(111, projection='3d',aspect='equal')
svals = 16;
ax.scatter(x_rand, y_rand, z_rand, s=svals, alpha=.1)
如何按照给定概率密度函数指定的顺序填充 3D 网格?
使用 python,我想以 随机 顺序放置点,但根据该区域的一些指定概率分布,没有重复点.
依次:
- 创建离散的 3D 网格
- 为每个网格点指定一个概率密度函数,pdf(x,y,z)
- 放置一个点 (x0,y0,z0),其随机位置与 pdf(x,y,z) 成正比
- 继续添加点(不重复)直到填满所有位置
期望的结果是网格中所有点的所有点(无重复)的列表,以便它们被填充。
下面没有实现多元高斯绘图:
xi_sorted = np.random.choice(x_grid.ravel(),x_grid.ravel().shape, replace=False, p = pdf.ravel())
yi_sorted = np.random.choice(x_grid.ravel(),x_grid.ravel().shape, replace=False, p = pdf.ravel())
zi_sorted = np.random.choice(x_grid.ravel(),x_grid.ravel().shape, replace=False, p = pdf.ravel())
那是因为p(x)*p(y)*p(z) != p(x,y,z)
除非三个变量是独立的。您可以考虑像 Gibbs sampler 这样的东西,通过从单变量分布中按顺序绘制来从联合分布中绘制。
在多元正态的特定情况下,您可以使用(完整示例)
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D
from math import *
num_points = 4000
sigma = .5;
mean = [0, 0, 0]
cov = [[sigma**2,0,0],[0,sigma**2,0],[0,0,sigma**2]]
x,y,z = np.random.multivariate_normal(mean,cov,num_points).T
svals = 16
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d',aspect='equal')
ax.scatter(x,y,z, s=svals, alpha=.1,cmap=cm.gray)
这是一个示例,使用高斯 pdf(见图表)。此代码很容易适应任何指定的 pdf:
%matplotlib qt
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#number of points to lay down:
n = 4000;
#create meshgrid:
min, max, L = -5, 5, 91;
[x_grid,y_grid,z_grid] = np.meshgrid(np.linspace(min,max,L),np.linspace(min,max,L),np.linspace(min,max,L))
xi,yi,zi = x_grid.ravel(),y_grid.ravel(),z_grid.ravel()
#create normalized pdf (gaussian here):
pdf = np.exp(-(x_grid**2 + y_grid**2 + z_grid**2));
pdf = pdf/np.sum(pdf);
#obtain indices of randomly selected points, as specified by pdf:
randices = np.random.choice(np.arange(x_grid.ravel().shape[0]), n, replace = False,p = pdf.ravel())
#random positions:
x_rand = xi[randices]
y_rand = yi[randices]
z_rand = zi[randices]
fig = plt.figure();
ax = fig.add_subplot(111, projection='3d',aspect='equal')
svals = 16;
ax.scatter(x_rand, y_rand, z_rand, s=svals, alpha=.1)