【问题标题】:How to find out standard deviation of estimated positions against real world position如何找出估计位置与现实世界位置的标准偏差
【发布时间】:2020-10-06 15:36:45
【问题描述】:

我希望标题不会太混乱,但这是我能想到的最好的标题(请随意提出更好的标题!)

我有一个物理传感器放置在房间的固定位置,例如坐标系中的 (1, 1, 1)。该传感器能够估计其在坐标系中的位置。我让传感器在 30 秒的时间段内每秒估计位置 10 次,所以我总共有 300 个位置估计值保存到一个文件中。

现在,为了评估位置估计,我计算了从每个估计到参考点 (1, 1, 1) 的距离,并将所有距离保存到一个列表中。我想找出到参考点 (1, 1, 1) 的距离的标准差。

我对计算标准偏差不是很熟悉,但正如多个解释和教程所建议的那样,我应该 1) 计算所有距离的平均值 2)从每个距离中减去平均值并将其平方 3) 将步骤 2) 中的所有值添加到列表中并计算它们的平均值 4) 取均值的平方根

但是,我认为我不应该在步骤 2) 中使用计算距离的平均值,而是使用 0 的值,因为我不想计算计算距离的标准偏差到它们的平均值,而是到我的参考点 (1, 1, 1)。由于我的参考点与自身的距离显然为 0,因此我认为这可能是正确的方法。

这是我的 python 脚本:

import sys
from math import sqrt, pow

# Returns the amount of samples collected - necessary for mean and standard deviation calculations
def get_sample_count(filename):
    with open(filename) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

def distanceBetweenTwoPoints2D(sample_point, reference_point):
    return sqrt(pow(sample_point[0] - reference_point[0], 2) + pow(sample_point[1] - reference_point[1], 2))

def distanceBetweenTwoPoints3D(sample_point, reference_point):
    return sqrt(pow(sample_point[0] - reference_point[0], 2) + pow(sample_point[1] - reference_point[1], 2) + pow(sample_point[2] - reference_point[2], 2))

def standard_deviation(distances_2D, sample_distance_mean_2D, distances_3D, sample_distance_mean_3D, sample_count):
    squared_distances_2D = []
    squared_distances_3D = []
    for distance in distances_2D:
        squared = pow(distance - 0, 2)
        squared_distances_2D.append(squared)
    for distance in distances_3D:
        squared = pow(distance - 0, 2)
        squared_distances_3D.append(squared)

    std2D = sqrt(sum(squared_distances_2D) / sample_count)
    std3D = sqrt(sum(squared_distances_3D) / sample_count)

    return std2D, std3D

def evaluateData(filename, reference_point):
    sample_x_mean = 0.0
    sample_y_mean = 0.0
    sample_z_mean = 0.0
    distances_2D = []
    distances_3D = []

    sample_count = get_sample_count(filename)

    with open(filename) as file:
        for line in file:
            x = float(line.split(',')[0])
            y = float(line.split(',')[1])
            z = float(line.split(',')[2])

            # Add individual coordinates to means
            sample_x_mean += x
            sample_y_mean += y
            sample_z_mean += z

            # Calculate distance in 2D and 3D and add to distances lists
            sample_point = [x, y, z]
            sample_distance_2D = distanceBetweenTwoPoints2D(sample_point, reference_point)
            sample_distance_3D = distanceBetweenTwoPoints3D(sample_point, reference_point)
            distances_2D.append(sample_distance_2D)
            distances_3D.append(sample_distance_3D)

    sample_x_mean /= sample_count
    sample_y_mean /= sample_count
    sample_z_mean /= sample_count
    sample_distance_mean_2D = sum(distances_2D) / sample_count
    sample_distance_mean_3D = sum(distances_3D) / sample_count
    std2D, std3D = standard_deviation(distances_2D, sample_distance_mean_2D, distances_3D, sample_distance_mean_3D, sample_count)

    return sample_count, sample_x_mean, sample_y_mean, sample_z_mean, sample_distance_mean_2D, sample_distance_mean_3D, std2D, std3D

if __name__ == "__main__":
    filename = sys.argv[1]
    direction = filename.split('(')[0]
    x_reference = float((filename.split('(')[1].split(')')[0].split('_')[0]).replace(',', '.'))
    y_reference = float((filename.split('(')[1].split(')')[0].split('_')[1]).replace(',', '.'))
    z_reference = float((filename.split('(')[1].split(')')[0].split('_')[2]).replace(',', '.'))
    reference_point = [x_reference, y_reference, z_reference]

    print("\n")
    sample_count, x_mean, y_mean, z_mean, distance_mean_2D, distance_mean_3D, std2D, std3D = evaluateData(filename, reference_point)
    print("DIRECTION: {}, SAMPLE COUNT: {}".format(direction, sample_count))
    print("X REFERENCE: {}, Y REFERENCE: {}, Z REFERENCE: {}".format(x_reference, y_reference, z_reference))
    print("X MEAN: {}, Y MEAN: {}, Z MEAN: {}".format(x_mean, y_mean, z_mean))
    print("DISTANCE MEAN 2D: {}, DISTANCE MEAN 3D: {}".format(distance_mean_2D, distance_mean_3D))
    print("STD2D: {}, STD3D: {}".format(std2D, std3D))
    print("\n")

谁能证明我是对还是错?

问候

【问题讨论】:

    标签: python standard-deviation


    【解决方案1】:

    两件事: 1) 如果您已经在 distanceBetweenTwoPoints[23]D() 中计算了参考点和观察点之间的距离,则您不希望使用参考点作为平均值。这已经融入了计算中。

    2) math.stdev() 将为您计算标准差。

    【讨论】:

    • 1) 我不明白你的意思。我在哪里使用 distanceBetweenTwoPoints() 的平均值?而且,我应该改用什么? 2)我知道 math.stdev() 但 math.stdev() 并不能解决我的问题,因为我明确不想计算计算距离的通常标准偏差,但我想知道它们与我的参考点。
    【解决方案2】:

    代码似乎可以正确计算到参考点的距离,然后计算这些距离的均方根。

    (当应用毕达哥拉斯定理仅在 RMS 计算期间再次对距离进行平方时,评估平方根存在一个小效率问题,但对于 300 点,这不值得担心。)

    如果规定的目标是计算距离的标准偏差,那么您确实需要 - 正如您所指出的那样 - 在评估均方根之前减去距离的平均值,因为标准偏差是不是距离本身的均方根,而是与均值偏差的均方根。这不是你计算出来的。

    哪一个更合适取决于您要衡量的内容。例如,假设您的所有估计都与参考点的距离相同(非零)。这些距离的均方根将等于该值。但是距离的标准差将等于零,因为它是相关变量中散布的度量,并且所有值(到参考点的距离)都相等。

    如果您尝试评估测量的准确性,那么均方根距离(而不是标准差)可能具有更大的相关性。

    【讨论】:

    • 是的,你是对的,我想评估准确性。我正在考虑标准偏差,因为我的房间里有多个固定点,我也想在其中收集数据。所以毕竟,我想看看估计位置与参考点的差异最大和最小的位置(并且 不是 与他们自己的平均值相比,它们的差异是因为这些信息对我来说似乎毫无用处,不是吗?)但看起来,我的假设是错误的,像我一样计算标准偏差不是正确的方法,对吧?
    • 或者换句话说,针对一个固定点which is not计算标准差是没有意义的> the mean 样本,因为这是我最初的问题。你能告诉我我可以使用哪些其他方法来评估我的系统的准确性吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-03-04
    • 2019-03-20
    • 2014-02-03
    • 2023-03-13
    • 2020-03-14
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多