您好,我注意到Apache Maths库(版本2.2)中有一些奇怪的行为,特别是在org.apache.commons.math.distribution.GammaDistributionImpl类中,尽管我认为这可能也适用于其他发行版。
我想从不同的伽马分布中获取样本,如下所示:
public static final double[] gammaSamples(final double[] shapeParameters)
{
double[] samples = new double[shapeParameters.length];
for (int i = 0; i < shapeParameters.length; i++)
{
GammaDistributionImpl gd = new GammaDistributionImpl(shapeParameters[i], 1.0d);
try
{
samples[i] = gd.sample();
}
catch (MathException e)
{
e.printStackTrace();
}
}
return samples;
}然而,在运行代码时,我发现所有示例都可疑地相似,即给定
public static void main(String[] args)
{
System.out.println(Arrays.toString(gammaSamples(new double[] { 2.0d, 2.0d, 2.0d})));
}以下是一些示例输出:
[0.8732612631078758, 0.860967116242789, 0.8676088095186796]
[0.6099133517568643, 0.5960661621756747, 0.5960661621756747]
[2.1266766239021364, 2.209383544840242, 2.209383544840242]
[0.4292184700011395, 0.42083613304362544, 0.42083613304362544]我认为问题是由于默认的随机数生成器对每个分布使用相同/相似的种子,我测试如下:
public static final double[] gammaSamples(final double[] shapeParameters, final Random random)
{
double[] samples = new double[shapeParameters.length];
for (int i = 0; i < shapeParameters.length; i++)
{
GammaDistributionImpl gd = new GammaDistributionImpl(shapeParameters[i], 1.0d);
gd.reseedRandomGenerator(random.nextLong());
try
{
samples[i] = gd.sample();
}
catch (MathException e)
{
e.printStackTrace();
}
}
return samples;
}这似乎解决了问题,即给定
public static void main(String[] args)
{
System.out.println(Arrays.toString(gammaSamples(new double[] { 2.0d, 2.0d, 2.0d }, new Random())));
}以下是一些示例输出:
[2.7506981228470084, 0.49600951917542335, 6.841476090550152]
[1.7571444623500108, 1.941865982739116, 0.2611420777612158]
[6.043421570871683, 0.8852269293415297, 0.6921033738466775]
[1.3859078943455487, 0.8515111736461752, 3.690127105402944]我的问题是:
到底怎么回事?这是一个bug,还是Apache Math发行版打算这样做?
这对我来说似乎很奇怪,如果我创建单独的分布对象,我必须担心它们被赋予了什么种子,并确保它们足够不同。
另一个小麻烦是,我似乎不能将这些分发传递给我自己的Random对象,而是它们只允许通过reseedRandomGenerator(long seed)方法更改种子。如果能够将我自己的Random对象传递给它们,在尝试重现结果时将非常有用。
谢谢你的帮助。
发布于 2013-01-27 05:33:45
通过查看javadoc:
我看到了public double[] sample(int sampleSize) throws MathException方法
从分布中生成一个随机样本。默认实现通过在循环中调用sample()来生成示例。
你试过了吗?
double[] samples = sample(shapeParameters.length);编辑:很抱歉,我看到你每次都用一个新的alpha参数计算一个新的GammaDistributionImpl。我猜这是因为种子值是从有限分辨率的系统时钟中派生出来的,对构造函数的关闭调用将产生相同的结果。看看这个SO question。
以下是一些输入,可以让您进行更深入的调查:
如果可能,您应该更新到lib的v3.1。根据JIRA的规定,some issues have been corrected.
中浏览/订阅/发布
https://stackoverflow.com/questions/14537824
复制相似问题