浮点计算
对于含糊的标题表示歉意。我的问题是这样的:给定一个双精度 w
向量,其条目小于 1,第二个双精度 v
向量,其正条目总和小于 1(用下面的递归),以及一个小于 1 的正数 u
,使用递归扩展 v
w(i) = RandomNumber(); //A random number from (0,1) - not necessarily uniform
v(i) = v(i-1)*w(i)*(1-w(i-1))/w(i-1);
直到 sum(v)>1-u
代码>.问题是 u
可能非常小,并且由于 v(i)
正在(随机)减少,它们也可能变得很小。我们也可能让 w(i)
接近于 1。
实现这一点最安全的方法是什么?准确性要点:)
Apologies for an ambiguous title. My problem is this: Given a vector of doubles w
with entries less than one, a second vector of doubles v
with positive entries that sum to less than one (computed with the recursion below), and a double u
which is positive and less than one, extend v
using the recursion
w(i) = RandomNumber(); //A random number from (0,1) - not necessarily uniform
v(i) = v(i-1)*w(i)*(1-w(i-1))/w(i-1);
until sum(v)>1-u
. The problem is that u
might be quite small, and since the v(i)
's are (stochastically) decreasing they can get tiny too. And we might get w(i)
close to one as well.
What's the safest way to implement this? Points for accuracy :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对浮点数求和而不产生太大误差的一种常见方法是从小到大进行求和。由于
v(i)
的计算仅取决于v(i-1)
,因此您可以将过去的数字保留在排序树中,并在每个节点处保留递归和,以及单独变量中紧邻的前一个值。当您插入新值或重新排列某些节点时,您需要重新计算从这些点沿树向上的总和。每个节点的求和可以是直接加法,也可以是保留更多位的东西,如卡汉求和。One common way to sum floating point numbers without too much error is to sum them from smallest to largest. Since your calculation of
v(i)
only depends onv(i-1)
, you can keep the past numbers in a sorted tree, with recursive sums kept at each node, and the immediately preceding value in a separate variable. When you insert a new value, or rearrange some node, you then need to recalculate the sums going up the tree from those points. The summation at each node could be straight up addition, or something that preserves a few more bits like Kahan summation.也许您不是累加 v(i) 并将其与 1-u 进行比较,而是从 1-u 开始,然后减少每个 v(i) 直到达到负数?越接近 0,准确度就越高。
Maybe instead of accumulating v(i)'s and comparing it to 1-u, you start with 1-u, and decrease each v(i) until you get to a negative number? The accuracy is better the closer you are to 0.