在球形体积内采样均匀分布的随机点 [英] Sampling uniformly distributed random points inside a spherical volume

查看:759
本文介绍了在球形体积内采样均匀分布的随机点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够生成随机均匀的粒子位置样本,这些样本属于球形体积。

下面的图片(由给出。我似乎无法破译或理解他们做了什么。

 函数X = randsphere(m,n,r)

%这个函数返回一个m n数组X,其中
%中的每一行具有n个笛卡尔坐标
%均匀分布在具有$的n维超球的
%内部的随机点b $ b%半径r并且在原点处居中。函数
%'randn'最初用于生成m组具有独立多元
%正态分布的n
%随机变量,均值为0,方差为1.
%Then不完全伽马函数'gammainc',
%被用于径向映射这些点,以适应具有统一的%空间分布的有限半径r的
%超球面。
%Roger Stafford - 12/23/05

X = randn(m,n);
s2 = sum(X. ^ 2,2);
X = X. * repmat(r *(gammainc(s2 / 2,n / 2)。^(1 / n))./ sqrt(s2),1,n)。

我非常感谢在Python中从球形体生成真正统一样本的任何建议。 p>

似乎有很多示例显示如何从统一的球形壳中进行采样,但似乎更容易解决问题。这个问题与缩放有关 - 在0.1的半径处应该有比在1.0的半径处更少的粒子以从球体的体积产生均匀的样本。



< 编辑 :修正并删除了我通常询问的事实,而我的意思是统一的。

解决方案

尽管我更喜欢球体的丢弃方法,但为了完整性

在球形坐标系中,利用 stackoverflow.com/questions/2106503/pseudorandom-number-generator-exponential-distribution/2106568#2106568\">抽样规则

 <$随机(0,1)

theta = arccos(costheta)
r = R * cuberoot(u)

现在你有一个(r,theta,phi)组,它可以通常的方式转换为(x,y,z)

  x = r * sin(theta)* cos(phi)
y = r * sin(theta)* sin(phi)
z = r * cos(theta)


I am looking to be able to generate a random uniform sample of particle locations that fall within a spherical volume.

The image below (courtesy of http://nojhan.free.fr/metah/) shows what I am looking for. This is a slice through the sphere, showing a uniform distribution of points:

This is what I am currently getting:

You can see that there is a cluster of points at the center due to the conversion between spherical and Cartesian coordinates.

The code I am using is:

def new_positions_spherical_coordinates(self):
   radius = numpy.random.uniform(0.0,1.0, (self.number_of_particles,1)) 
   theta = numpy.random.uniform(0.,1.,(self.number_of_particles,1))*pi
   phi = numpy.arccos(1-2*numpy.random.uniform(0.0,1.,(self.number_of_particles,1)))
   x = radius * numpy.sin( theta ) * numpy.cos( phi )
   y = radius * numpy.sin( theta ) * numpy.sin( phi )
   z = radius * numpy.cos( theta )
   return (x,y,z)

Below is some MATLAB code that supposedly creates a uniform spherical sample, which is similar to the equation given by http://nojhan.free.fr/metah. I just can't seem to decipher it or understand what they did.

function X = randsphere(m,n,r)

% This function returns an m by n array, X, in which 
% each of the m rows has the n Cartesian coordinates 
% of a random point uniformly-distributed over the 
% interior of an n-dimensional hypersphere with 
% radius r and center at the origin.  The function 
% 'randn' is initially used to generate m sets of n 
% random variables with independent multivariate 
% normal distribution, with mean 0 and variance 1.
% Then the incomplete gamma function, 'gammainc', 
% is used to map these points radially to fit in the 
% hypersphere of finite radius r with a uniform % spatial distribution.
% Roger Stafford - 12/23/05

X = randn(m,n);
s2 = sum(X.^2,2);
X = X.*repmat(r*(gammainc(s2/2,n/2).^(1/n))./sqrt(s2),1,n);

I would greatly appreciate any suggestions on generating a truly uniform sample from a spherical volume in Python.

There seem to be plenty of examples showing how to sample from a uniform spherical shell, but that seems to be easier an easier problem. The issue has to do with the scaling - there should be fewer particles at a radius of 0.1 than at a radius of 1.0 to generate a uniform sample from the volume of the sphere.

Edit: Fixed and removed the fact I asked for normally and I meant uniform.

解决方案

While I prefer the discarding method for spheres, for completeness I offer the exact solution.

In spherical coordinates, taking advantage of the sampling rule:

phi = random(0,2pi)
costheta = random(-1,1)
u = random(0,1)

theta = arccos( costheta )
r = R * cuberoot( u )

now you have a (r, theta, phi) group which can be transformed to (x, y, z) in the usual way

x = r * sin( theta) * cos( phi )
y = r * sin( theta) * sin( phi )
z = r * cos( theta )

这篇关于在球形体积内采样均匀分布的随机点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆