Given a set of discrete locations (for example, “sites”) that are pairwise connected in some categorical ways (for example, overall proximity) and contain local level data (for example, population size), I want to efficiently calculate the average correlation coefficients between local level data in paired locations that are characterized by the same relationship.
For example, I assumed 100 sites and randomized their pairwise relationships using values from 1 to 25, getting a triangular matrix relations:
import numpy as np
sites = 100
categ = 25
relations = np.random.randint(low=1, high=categ+1, size=(sites, sites))
relations = np.triu(relations)
np.fill_diagonal(relations, 0)
I also have 5,000 replicas of simulation results on each site:
sims = 5000
res = np.round(np.random.rand(sites, sims),1)
, i rho[j] res j, i:
rho_list = np.ones(categ)*99
for i in range(1, categ+1):
idr = np.transpose(np.where(relations == i))
comp = np.vstack([res[idr[:,0]].ravel(), res[idr[:,1]].ravel()])
comp_uniq = np.reshape(comp.T, (len(idr), res.shape[1], -1))
rho = np.ones(len(idr))*99
for j in range(len(idr)):
comp_uniq_s = comp_uniq[j][np.all(comp_uniq!=0, axis=2)[j]].T
rho[j] = np.corrcoef(comp_uniq_s[0], comp_uniq_s[1])[0,1]
rho_list[i-1] = np.nanmean(rho)
script , sites = 400, 6 , . ? ?