How do you convert a tiny relative change to log space?

log1p evaluates ln(1+x) in a way that preserves small changes otherwise lost when forming the intermediate sum.

numpy.log1p in practice

For a fractional change near one trillionth, use the dedicated operation to retain detail. In real arithmetic x=-1 leads to log zero and smaller values fall outside the real domain.

import numpy as np
np.log1p([1e-12,0.01])

Result [9.999999999995e-13, 0.009950330853168083]

中文

接近零的相对变化怎样转换成对数变化?

log1p 直接计算 ln(1+x),在 x 很小时能避免先形成 1+x 带来的精度损失。

numpy.log1p 算例

对万亿分之一量级的变化用此函数可保留细节。实数应用要注意 x=-1 时对应对数零,x 小于 -1 超出实对数定义域。

import numpy as np
np.log1p([1e-12,0.01])

结果 [9.999999999995e-13, 0.009950330853168083]

References / 参考来源

Keep reading / 继续阅读