报告不确定性:给定均值和标准误,仅显示有效数字 [英] Report uncertainty: given a mean and the standard error, show only significant figures

查看:31
本文介绍了报告不确定性:给定均值和标准误,仅显示有效数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目的是展示多次观察的结果,而不是不必要的数字,即显示一个值与给定的有效数字一致的位数不确定性.

The intent is to show the result of several observations without unnecessary digits i.e., to display a value with the number of significant digits that is consistent with a given uncertainty.

例如,如果计算 mean=123.45err=0.0012345 那么预期输出可能看起来像 123450 ± 1.2 (× 10-3) 其中使用以下规则:

For example, if computed mean=123.45 and err=0.0012345 then the expected output may look like 123450 ± 1.2 (× 10-3) where the following rules are used:

  1. 错误总是有一个或两个有效数字.两个如果第一个数字为 1(忽略前导零)
  2. 平均值四舍五入以去掉不确定的数字,除了最后一个(在与第一个显着(非-zero) SEM 中的数字").如有必要,添加尾随零以显示与误差对应的精度.
  1. the error always has one or two significant digits. Two if the first digit is 1 (ignoring the leading zeros)
  2. the mean value is rounded to drop uncertain digits except for the last one ("stop the mean at the same decade as that of the first significant (non-zero) digit in the SEM"). Trailing zeros are added to display the precision corresponding to the error if necessary.

如何在 Python 中使用它:

How it might be used in Python:

import statistics

mean = statistics.mean(measurements)
err = statistics.stdev(measurements, mean) / len(measurements) ** 0.5
print("{} ± {} (×10<sup>{}</sup>)".format(*round_to_uncertainty(mean, err)))

问题是如何实现round_to_uncertainty(value,不确定性) 函数表达上述规则 1 和 2.

The question is how to implement the round_to_uncertainty(value, uncertainty) function expressing the rules 1 and 2 above.

注意:术语错误、不确定性在题.请参阅测量不确定度表达指南 (GUM).这是一个与 R 相关的问题.

Note: the terms error, uncertainty are used loosely in the question. See the Guide to the Expression of Uncertainty in Measurement (GUM). Here's a related question for R.

推荐答案

可以使用decimal模块方便地操作数字的十进制表示:

decimal module could be used to manipulate the decimal representation of the numbers conveniently:

from decimal import Decimal

def round_to_uncertainty(value, uncertainty):
    # round the uncertainty to 1-2 significant digits
    u = Decimal(uncertainty).normalize()
    exponent = u.adjusted()  # find position of the most significant digit
    precision = (u.as_tuple().digits[0] == 1)  # is the first digit 1?
    u = u.scaleb(-exponent).quantize(Decimal(10)**-precision)

    # round the value to remove excess digits
    return round(Decimal(value).scaleb(-exponent).quantize(u)), u, exponent

示例:

for mean, err in [
    (123.45, 0.0012345),    # 123450 ± 1.2 (×10<sup>-3</sup>)
    (8165.666, 338.9741),   # 82 ± 3 (×10<sup>2</sup>)
]: 
    print("{} ± {} (×10<sup>{}</sup>)".format(*round_to_uncertainty(mean, err)))

输入 123.45, 0.0012345 报告为 123450 ± 1.2(×10-3).还有8165.666338.9741根据当前问题的规则,转换为 82 ± 3 (×102).

The input 123.45, 0.0012345 is reported as 123450 ± 1.2 (×10-3). And 8165.666, 338.9741 translates to 82 ± 3 (×102) according to the rules from the current question.

这篇关于报告不确定性:给定均值和标准误,仅显示有效数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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