def epsilon_g(x,y,N, size=1., ib=True, exp=True, seed=False, tildeM=2, gamma=20): ''' Takes the x and y displacements defined in the tripoint problem and returns the error between treating the satellite locations as one and as separate seed = False --> use completely random pop dist seed = int --> recreate a previous population distribution ''' p2, p3, loci, locb = createPops(x,y,N,size,seed) g3 = gravity(p3, alpha=1, beta=1, gamma=gamma, exp=exp) # Insert locations into two-point p2.popDist = np.insert(p2.popDist, 0, np.array([size, tildeM]), axis=0) p2.locCoords = np.insert(p2.locCoords, 0, np.array([loci, locb]), axis=0) # re-initialise parameters p2.DM, p2.size = p2.distance_matrix(), len(p2.locCoords) g2 = gravity(p2, 1, 1, gamma, exp=exp) eps = epsilon(g2, g3, ib=ib) return eps
def gravity_ODM(self, level, gamma=False, exp=False): """ Returns the ODM for the gravity model at a specific level of clustering. If level == 0 (no clustering), a dataframe needs to be specified so that a population object (the original) can be created from it. """ if level == 0: pop = self.levels[0].pop if type(gamma ) == bool: # pass explicit gamma instead of using the area S = np.mean(self.pop.locArea) # mean population unit area else: clustering = self.levels[level - 1] pop = self.cluster_population(clustering) if type(gamma) == bool: S = np.mean( self.levels[level - 1].clustered_area) # mean population unit area # So we can pass explicit gamma argument if we'd like if type(gamma) == bool: gam = gamma_est( S, exp=exp ) # calculate the gamma exponent with the average population unit area else: gam = gamma g = gravity(pop, 1, 1, gam, exp=exp) return g.ODM()
def k_ratio(r_ib, r_jk, N, gamma=0.68726, exp=True): ''' Returns the ratio of K values for tri and two-point distributions with certain parameter values ''' size = 1 tildeM = 2 p2, p3, loci, locb = createPops(r_ib, r_jk, N, size, seed=False) g3 = gravity(p3, alpha=1, beta=1, gamma=gamma, exp=exp) # Insert locations into two-point p2.popDist = np.insert(p2.popDist, 0, np.array([size, tildeM]), axis=0) p2.locCoords = np.insert(p2.locCoords, 0, np.array([loci, locb]), axis=0) g2 = gravity(p2, 1, 1, gamma, exp=exp) k3 = g3.K[0] k2 = g2.K[0] return k2 / k3
def gravity_ODM(clusters_list, level, gamma): """ Returns the ODM for the gravity model at a specific level of clustering. If level = 0 (no clustering), a dataframe needs to be specified so that a population object can be created from it. """ pop = clusters_list[0].pop if level > 0: clustering = clusters_list[level - 1] pop = cluster_population(clustering) g = gravity(pop, 1, 1, gamma) return g.ODM()
def plot_flow(population, model='all', alpha=1, beta=1, gamma=0.2): ''' Takes a population object and a mobility model and plots the flow probability as a function of the (scaled) distance between two locations TODO: pass instance of mob_model as second argument ''' distance = [] flux_gravity = [] flux_radiation = [] flux_opportunities = [] p = population g = gravity(p, alpha, beta, gamma) r = radiation(p) o = opportunities(p, gamma) for i in range(p.size): for j in range(p.size): if i != j: distance.append(p.r(i, j) * np.sqrt(p.size)) if model == 'all': flux_gravity.append(g.flux(i, j)) flux_radiation.append(r.flux(i, j)) flux_opportunities.append(o.flux(i, j)) if isinstance(model, gravity) == True: flux_gravity.append(g.flux(i, j)) if isinstance(model, radiation) == True: flux_radiation.append(r.flux(i, j)) if isinstance(model, opportunities) == True: flux_opportunities.append(o.flux(i, j)) plt.loglog(distance, flux_gravity, '.', label='gravity') #plt.loglog(distance, flux_radiation, '.', label = 'radiation') #plt.loglog(distance, flux_opportunities, '.', label = 'opportunities') plt.xlabel(r'$ \~r$') plt.ylabel('$p_{ij}$') plt.legend() plt.title('Log-log plot') plt.show()
def epsilon(p, i, model, exp=True, tilde = False): ''' Returns epsilon for a given target location i. ''' epsValues = [] for n in neighbours(p)[1]: if n[0] != i and n[1] != i: j, k = n[0], n[1] p2 = copy.deepcopy(p) if tilde == True: # use m tilde correction p2.popDist[j] = p2.popDist[k] + p2.popDist[j] - model.flux(j, k) - model.flux(k, j) if tilde == False: # merge two populations p2.popDist[j] = p2.popDist[k] + p2.popDist[j] # move j to midpoint p2.locCoords[j][0] = 0.5*(p2.locCoords[j][0]+ p2.locCoords[k][0]) p2.locCoords[j][1] = 0.5*(p2.locCoords[j][1]+ p2.locCoords[k][1]) p2.DM = p2.distance_matrix() p2.popDist[k] = 0. #remove k b = j #rename j if isinstance(model, gravity): alpha = model.alpha beta = model.beta gamma = model.gamma g2 = gravity(p2, alpha, beta, gamma, exp=exp) flow_ib = g2.flux(i, b) eps = (flow_ib - (model.flux(i, j)+model.flux(i, k)))/(flow_ib) if isinstance(model, radiation): r2 = radiation(p2) flow_ib = r2.flux(i, b) eps = (flow_ib - (model.flux(i, j)+model.flux(i, k)))/(flow_ib) epsValues.append(abs(eps)) return np.array(epsValues)
plt.tight_layout() if isinstance(model, gravity): mod = "gravity" else: mod = "radiation" title = mod + "_exp=" + str(exp) + "_" + x_value plt.savefig(title) ### Run this to plot: from hm.pop_models.pop_random import random as pop_random from hm.hm_models.gravity import gravity from hm.hm_models.radiation import radiation N = 100 alpha, beta = 1, 1 S = 1 / N # exponential gamma = 0.3 * (S)**(-0.18) # power law #gamma = 1.4 * (S)**(0.11) p = pop_random(N) g = gravity(p, alpha, beta, gamma, exp=False) r = radiation(p) print(np.mean(r_jk(p, 1))) r_plot(p, r, "r_jk", exp=False) #r_ib_plot(p, r, 0.1)
import importlib from hm.hm_models.gravity import gravity from hm.hm_models.radiation import radiation from hm.hm_models.opportunities import opportunities from hm.pop_models.pop_random import random as pop_random from hm.pop_models.pop_explicit import explicit as pop_explicit popDist = [3, 4, 7, 5, 6] locCoords = [[2, 3], [3, 2], [-5, 9], [0, 1], [1, -8]] alpha, beta = 1, 1 gamma = 0.2 N = 20 p = pop_random(N) p1 = pop_explicit(locCoords, popDist) g = gravity(p, alpha, beta, gamma) r = radiation(p) o = opportunities( p, gamma ) # TODO seems a little slow, probably just the nature of the algorithm