Skip to content

Commit

Permalink
v1.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGrund committed Feb 24, 2015
1 parent b953d3d commit be543c6
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 52 deletions.
187 changes: 182 additions & 5 deletions nwequiv.ado
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
capture program drop nwequiv
program nwequiv
syntax [anything(name=netname)] [, type(string) generate(string) mode(string) *]
syntax [anything(name=netname)] [, type(string) iter(integer 3) generate(string) mode(string) *]

if "`generate'" == "" {
local generate "_eqvclass"
}

local name = "equivalent"
local name = "equivalence"
nwvalidate `name'
local name = r(validname)

Expand Down Expand Up @@ -63,27 +63,37 @@ program nwequiv
}
nwdrop __temp_geodesic
}
if ("`type'" == "regular"){
mata: S = regularEquivalence(onenet, `iter')
capture nwdrop _regEquiv
nwset, mat(S) name(`name')
mata: S
mata: S = (S:== 1)
}

nwset, mat(S) name(`name')
nwset, mat(S) name(__equiv)
capture drop `generate'
tempvar comp isol
qui nwgen `isol' = isolates(`netname')
qui nwcomponents `name', generate(`comp')
qui nwcomponents __equiv, generate(`comp')
qui replace `comp' = -1 if `isol' == 1
qui egen `generate' = group(`comp')
label variable `generate' "`generate'"
tab `generate', `options'

capture mata: mata drop S
capture mata: mata drop onenet
capture nwdrop `name'
capture nwdrop __equiv

end


capture mata: mata drop structuralEquivalence()
capture mata: mata drop distanceEquivalence()
capture mata: mata drop getDistribution()
capture mata: mata drop getSimilarity()
capture mata: mata drop getMatch()
capture mata: mata drop regularEquivalence()

mata:
real matrix structuralEquivalence(real matrix net, scalar mode) {
Expand Down Expand Up @@ -167,5 +177,172 @@ real matrix getDistribution(real matrix X, scalar max){
}
return(result)
}
real matrix regularEquivalence(real matrix X, scalar iter) {
Sstart = J(rows(X), cols(X), 1)
for (i = 1 ; i <= iter; i++) {
Sstart = getSimilarity(X, Sstart)
}
return(Sstart)
}
mata:
real matrix getSimilarity(real matrix X,real matrix S) {
S_new = S
for(i=1; i<= rows(X);i++){
for(j=1;j<= rows(X);j++){
S_new[i,j] = getMatch(X,S_new,i,j)
}
}
return(S_new)
}
real scalar getMatch(X,S,a,e) {
points = 0
points_max = 0
nodes = rows(X)
temp = (1::nodes)
a_outties = (editvalue((X[a,.] - X[.,a]'),-1,0))'
a_outties
a_out = select(temp, a_outties)
a_inties = (editvalue((X[.,a]' - X[a,.]),-1,0))'
a_in = select(temp, a_inties)
a_mutualties = (editvalue((X[a,.] + X[.,a]'),1,0))' :== 2
a_mutual = select(temp, a_mutualties)
e_outties = (editvalue((X[e,.] - X[.,e]'),-1,0))'
e_out = select(temp, e_outties)
e_inties = (editvalue((X[.,e]' - X[e,.]),-1,0))'
e_in = select(temp, e_inties)
e_mutualties = (editvalue((X[e,.] + X[.,e]'),1,0))' :== 2
e_mutual = select(temp, e_mutualties)
/////////
// A's PERSPECTIVE
/////////
// go through all out neighbors of a
for (i = 1 ; i <= rows(a_out); i++) {
bestmatch = 0
ii = a_out[i]
// look for a best match among e's neighbors and calculate points
for (j = 1; j<= rows(e_out); j++) {
jj = e_out[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
points = points + bestmatch
}
// go through all in neighbors of a
for (i = 1 ; i <= rows(a_in); i++) {
ii = a_in[i]
bestmatch = 0
// look for a best match among e's neighbors and calculate points
for (j = 1; j<= rows(e_in); j++) {
jj = e_in[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
points = points + bestmatch
}
// go through all mutual neighbors of a
for (i = 1 ; i <= rows(a_mutual); i++) {
ii = a_mutual[i]
bestmatch = 0
// look for a best match among e's neighbors and calculate points
for (j = 1; j<= rows(e_mutual); j++) {
jj = e_mutual[j]
if (S[ii,jj] > bestmatch) {
bestmatch = 2 * S[ii,jj]
}
}
//WILDCARD find best match among e' in /out neighbors
if (bestmatch == 0) {
for (j = 1; j<= rows(e_in); j++) {
jj = e_in[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
for (j = 1; j<= rows(e_out); j++) {
jj = e_out[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
}
points = points + bestmatch
}
/////////
// E's PERSPECTIE
/////////
// go through all out neighbors of e
for (i = 1 ; i <= rows(e_out); i++) {
bestmatch = 0
ii = e_out[i]
// look for a best match among e's neighbors and calculate points
for (j = 1; j<= rows(a_out); j++) {
jj = a_out[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
points = points + bestmatch
}
// go through all in neighbors of e
for (i = 1 ; i <= rows(e_in); i++) {
bestmatch = 0
ii = e_in[i]
// look for a best match among e's neighbors and calculate points
for (j = 1; j<= rows(a_in); j++) {
jj = a_in[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
points = points + bestmatch
}
// go through all mutual neighbors of e
for (i = 1 ; i <= rows(e_mutual); i++) {
bestmatch = 0
ii = e_mutual[i]
// look for a best match among e's neighbors and calculate points
for (j = 1; j<= rows(a_mutual); j++) {
jj = a_mutual[j]
if (S[ii,jj] > bestmatch) {
bestmatch = 2 * S[ii,jj]
}
}
//WILDCARD find best match among a' in /out neighbors
if (bestmatch == 0) {
for (j = 1; j<= rows(a_in); j++) {
jj = a_in[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
for (j = 1; j<= rows(a_out); j++) {
jj = a_out[j]
if (S[ii,jj] > bestmatch) {
bestmatch = S[ii,jj]
}
}
}
points = points + bestmatch
}
points_max = rows(a_out) + rows(a_in) + 2 * rows(a_mutual) + rows(e_out) + rows(e_in) + 2 * rows(e_mutual)
return(points / points_max)
}
end


11 changes: 8 additions & 3 deletions nwgeodesic.ado
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ real matrix distances(real matrix nw) {
found = nw
distance = nw
power = nw
i= 1
while (i<= (nodes / 2) & sum(found)< nodes*nodes - nodes ) {
num_found_previous = -1
num_found = 0
i = 1
while ((i<= (nodes / 2)) & (num_found < (nodes*nodes - nodes))) {
//while ((i<= (nodes / 2)) & (num_found < (nodes*nodes - nodes)) & (num_found > num_found_previous)) {
i = i+1
power = power * nw
power = power:/power
Expand All @@ -149,7 +152,9 @@ real matrix distances(real matrix nw) {
_editvalue(temp, -1, 0)
temp = temp * i
distance = distance + temp
num_found_previous = num_found
found = distance:/distance
num_found = sum(found)
_editmissing(found, 0)
}
_editvalue(distance, 0, -1)
Expand Down
7 changes: 7 additions & 0 deletions nwneighbor.ado
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ program nwneighbor
if temp_mat[1,1] == . {
local temp_rows = 0
}

local neighbors_list1 ""
local neighbors_list2 ""
forvalues j = 1/`temp_rows' {
local temp = temp_mat[`j',1]
local onelab : word `temp' of `labs'
local neighbors_list1 "`neighbors_list1' `temp'"
local neighbors_list2 "`neighbors_list2' `onelab'"
if `uselab' == 1{
di "{res}`onelab'" _continue
}
Expand All @@ -82,6 +87,8 @@ program nwneighbor
di "{txt} , " _continue
}
}
mata: st_global("r(neighbors_list1)", "`neighbors_list1'")
mata: st_global("r(neighbors_list2)", "`neighbors_list2'")
di ""

di "{hline 40}"
Expand Down
Loading

0 comments on commit be543c6

Please sign in to comment.