Distance Matrix from Haversines Formula

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

Distance Matrix from Haversines Formula

A program to compute (symmetric) distance matrices (diag=NA) from Haversines formula with two input series of (decimal) latitude and longtitudes:

Code: Select all

if @obs(latitude)<>@obs(longitude) then
@uiprompt("Error: Lat and Long not same size")
endif
matrix(@obs(latitude),@obs(longitude)) distance_haversine
genr lat_rad = latitude*(@acos(-1)/180)
genr lon_rad = longitude*(@acos(-1)/180)
for !b = 1 to @obs(latitude)
for !a = 1 to @obs(latitude)
if !a=!b then
distance_haversine(!b,!a)=NA
else
!temp2 = 2*@atan((({!temp})^0.5)/((1-{!temp})^0.5))
!r=6371 'radius of eath in KM
distance_haversine(!b,!a)=!r*!temp2
endif
next
next

Any questions, please don't hestiate to ask. I intend to post a few more spatial routines in the future (next up - various distance matrices): if anybody has any requests (on distance matrices or spatial estimators), please post below any of the threads or send a PM through this forum.