mie-lab / trackintel

trackintel is a framework for spatio-temporal analysis of movement trajectory and mobility data.
MIT License
198 stars 50 forks source link

BUG: `point_haversine_dist` is not precise enough #593

Closed bifbof closed 8 months ago

bifbof commented 8 months ago

I am currently rewriting #592 and there a test on the haversine distance keeps failing because our implementation of the haversine function is not precise enough. If we compare it to the sklearn implementation: (distance Buenos Aires to Paris)

import trackintel as ti
from sklearn.metrics.pairwise import haversine_distances
from math import radians
bsas = [-34.83333, -58.5166646]
paris = [49.0083899664, 2.53844117956]
bsas_in_radians = [radians(_) for _ in bsas]
paris_in_radians = [radians(_) for _ in paris]
result = (haversine_distances([bsas_in_radians, paris_in_radians]) * 6371000)[0, 1]
test = ti.geogr.point_haversine_dist(*bsas, *paris)[0]
print(result)
print(test)
print(test/result)

Result:

11099540.35581966
9891586.042330354
0.8911707805219199

For smaller distances e.g. in a city that is not really observable but over bigger distances it is quite dramatic. Here our result is 11% smaller than the sklearn implementation.

hongyeehh commented 8 months ago

It's great that you find it out! I have always used this function, and it would be great to ensure its precision...

bifbof commented 8 months ago

It isn't a bug. I just entered the wrong input arguments. point_haversine_dist takes longitude, latitude as inputs. While haversine_distances takes it reverse; latitude, longitude as input.