[英]Python Uniform distribution of points on 4 dimensional sphere
I need a uniform distribution of points on a 4 dimensional sphere.我需要在 4 维球面上均匀分布点。 I know this is not as trivial as picking 3 angles and using polar coordinates.我知道这不像选择 3 个角度和使用极坐标那么简单。
In 3 dimensions I use在我使用的 3 个维度中
from random import random
u=random()
costheta = 2*u -1 #for distribution between -1 and 1
theta = acos(costheta)
phi = 2*pi*random
x=costheta
y=sin(theta)*cos(phi)
x=sin(theta)*sin(phi)
This gives a uniform distribution of x, y and z.这给出了 x、y 和 z 的均匀分布。
How can I obtain a similar distribution for 4 dimensions?如何获得 4 个维度的类似分布?
A standard way , though, perhaps not the fastest , is to use Muller's method to generate uniformly distributed points on an N-sphere:然而,一种标准方法,也许不是最快的,是使用穆勒的方法在 N 球面上生成均匀分布的点:
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
N = 600
dim = 3
norm = np.random.normal
normal_deviates = norm(size=(dim, N))
radius = np.sqrt((normal_deviates**2).sum(axis=0))
points = normal_deviates/radius
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
ax.scatter(*points)
ax.set_aspect('equal')
plt.show()
Simply change dim = 3
to dim = 4
to generate points on a 4-sphere.只需将dim = 3
更改为dim = 4
即可在 4 球体上生成点。
Take a point in 4D space whose coordinates are distributed normally, and calculate its unit vector.在4D空间中取一个坐标正态分布的点,计算其单位向量。 This will be on the unit 4-sphere.这将在单位 4 球体上。
from random import random
import math
x=random.normalvariate(0,1)
y=random.normalvariate(0,1)
z=random.normalvariate(0,1)
w=random.normalvariate(0,1)
r=math.sqrt(x*x + y*y + z*z + w*w)
x/=r
y/=r
z/=r
w/=r
print (x,y,z,w)
I like @unutbu's answer if the gaussian sampling really creates an evenly spaced spherical distribution (unlike sampling from a cube), but to avoid sampling on a Gaussian distribution and to have to prove that, there is a simple solution: to sample on a uniform distribution on a sphere (not on a cube).我喜欢@unutbu 的回答,如果高斯采样真的创建了一个均匀分布的球面分布(与从立方体采样不同),但是为了避免对高斯分布进行采样并且必须证明这一点,有一个简单的解决方案:在均匀分布上采样分布在球体上(不是在立方体上)。
This obviously works in an n-dimensional space, since the radius is always the L2-norm in higher dimensions.这显然适用于 n 维空间,因为半径始终是更高维度的 L2 范数。
It is fast so as avoiding a square-root and sampling on a Gaussian distribution, but it's not a vectorized algorithm.它很快,以避免在高斯分布上进行平方根和采样,但它不是矢量化算法。
I found a good solution for sampling from N-dim sphere.我找到了一个从 N-dim 球体采样的好方法。 The main idea is:主要思想是:
If Y is drawn from the uncorrelated multivariate normal distribution, then S = Y / ||Y||如果 Y 来自不相关的多元正态分布,则S = Y / ||Y|| has the uniform distribution on the unit d-sphere.在单位 d 球面上具有均匀分布。 Multiplying S by U 1/d , where U has the uniform distribution on the unit interval (0,1), creates the uniform distribution in the unit d-dimensional ball.将 S 乘以U 1/d ,其中 U 在单位间隔 (0,1) 上具有均匀分布,在单位 d 维球中创建均匀分布。
Here is the python code to do this:这是执行此操作的python代码:
Y = np.random.multivariate_normal(mean=[0], cov=np.eye(1,1), size=(n_dims, n_samples))
Y = np.squeeze(Y, -1)
Y /= np.sqrt(np.sum(Y * sample_isotropic, axis=0))
U = np.random.uniform(low=0, high=1, size=(n_samples)) ** (1/n_dims)
Y *= distr * radius # in my case radius is one
This is what I get for the sphere:这是我得到的球体:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.