Weight Matrices from Distance Matrix

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

Weight Matrices from Distance Matrix

Postby CharlieEVIEWS » Tue Feb 24, 2015 3:47 am

Dear All,

Please see some code to create 5 commonly used spatial weighting matrices based on distances (k-nearest, radial distances, power distances, exponential distances and double-power distances). The idea is to use this in conjuction with a well specified distance matrix (in the code below, called distance_haversine):

Code: Select all

!rwd_max = 0.5 !pdw_weight=1 !epw_weight=2 !k_nearest=100 !dpd_weight=2 !dpd_max=0.5 matrix(@rows(distance_haversine),@rows(distance_haversine)) knearest_matrix = 0 ' k-Nearest Neighbor Weights. vector(@rows(distance_haversine)) rankvect for !p = 1 to @rows(distance_haversine) rankvect(!p) = !p next for !a = 1 to (@rows(distance_haversine)) vector(@rows(distance_haversine)) sorted_ranks_{!a} sorted_ranks_{!a}=@capplyranks(rankvect,@ranks(@rowextract(distance_haversine,!a) ,"a,","r")) for !k = 2 to !k_nearest+1 knearest_matrix(!a,sorted_ranks_{!a}(!k))=1 next d sorted_ranks_{!a} next matrix(@rows(distance_haversine),@rows(distance_haversine)) rwd_matrix = 0 'Radial Distance Weights. matrix(@rows(distance_haversine),@rows(distance_haversine)) dpdw_matrix = 0 ' Double-Power Distance Weights. matrix(@rows(distance_haversine),@rows(distance_haversine)) pwd_matrix = 0 'Power Distance Weights. matrix(@rows(distance_haversine),@rows(distance_haversine)) ewd_matrix = 0 'Exponential Distance Weights for !a = 1 to @rows(distance_haversine) for !b = !a to @rows(distance_haversine) if !a<>!b then if distance_haversine(!a,!b)<!dpd_max then dpdw_matrix(!a,!b)=(1-(distance_haversine(!a,!b)/!dpd_max)^{!dpd_weight})^{!dpd_weight} dpdw_matrix(!b,!a)=dpdw_matrix(!a,!b) endif if distance_haversine(!a,!b)<!rwd_max then rwd_matrix(!a,!b) = 1 rwd_matrix(!b,!a) = 1 endif if distance_haversine(!a,!b)<>0 then pwd_matrix(!a,!b) = distance_haversine(!a,!b)^(-{!pdw_weight}) pwd_matrix(!b,!a) = pwd_matrix(!a,!b) ewd_matrix(!a,!b) = @exp(-{!epw_weight}*distance_haversine(!a,!b)) ewd_matrix(!b,!a) = ewd_matrix(!a,!b) endif endif next next
If there are any questions or requests, please don't hesitate to ask.

Charlie
Last edited by CharlieEVIEWS on Tue Feb 24, 2015 7:03 am, edited 1 time in total.

trubador
Did you use forum search?
Posts: 1520
Joined: Thu Nov 20, 2008 12:04 pm

Re: Weight Matrices from Distance Matrix

Postby trubador » Tue Feb 24, 2015 6:46 am

Very intuitive and straightforward. Thanks for sharing.

Amber
Posts: 31
Joined: Wed Aug 03, 2011 4:35 am

Re: Weight Matrices from Distance Matrix

Postby Amber » Mon Aug 21, 2017 7:21 am

Dear @CharlieEVIEWS,

Thank you for sharing, this is very useful. Are you happen to know how to implement the spatial autoregressive or spatial error model in Eviews?

Thanks!

Amber

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

Re: Weight Matrices from Distance Matrix

Postby CharlieEVIEWS » Tue Aug 22, 2017 8:00 am

A while back I was building add-ins to estimate these models in EViews using James LeSages matlab toolbox as a guide, but I remember hitting a barrier somewhere and forget exactly what it was. Possibly to do with a matrix decomposition function that EViews didnt provide off the shelf at the time: one that I couldn't quickly figure out how to program efficiently and moved on to something else...

I'll revisit this when I have a bit more time though - perhaps in the next few weeks - thanks for reminding me!

Charlie


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 1 guest