title: How to find out standard deviation of estimated positions against real world position
body: <p>I hope the title isn't too confusing but it's the best I could think of (feel free to suggest better titles!)</p>
  
  <p>I have a physical sensor placed at a fixed location in a room, say (1, 1, 1) in a coordinate system. This sensor is able to estimate its position within the coordinate system. I let the sensor estimate the position 10 times a second for a time period of 30 seconds, so in total I have 300 position estimations which are saved to a file.</p>
  
  <p>Now, in order to evaluate the position estimations, I calculated the distance from every estimation to the reference point (1, 1, 1) and saved all distances to a list. I'd like to find out the standard deviation of the distances to the reference point (1, 1, 1).</p>
  
  <p>I am not that familiar with calculating standard deviations but as multiple explanations and tutorials suggested, I should
  1) calculate the mean of all distances
  2) substract the mean from every single distance and square it
  3) add all values from step 2) to a list and calculate their mean
  4) take the square root of the mean</p>
  
  <p>But, I think I shouldn't use the mean of the calculated distances in step 2) but instead use the value of 0 because I don't want to calculate the standard deviation of the calculated distances to their mean but to my reference point (1, 1, 1). Since my reference point obviously has a distance of 0 to itself, I thought that this might be the right approach.</p>
  
  <p>Here's my python script:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>Can anybody prove me right or wrong please?</p>
  
  <p>Regards</p>