def epsilon_r(x,y,N, size=1., ib=True, seed=False, tildeM=False): ''' Epsilon for radiation model ''' p2, p3, loci, locb = createPops(x,y,N,size,seed) r3 = radiation(p3) # Use definition of m_b with correction for the intra-location flow if tildeM: sizeb = tilde_m(size, r3) # use traditional definition of population mass m_b else: sizeb = 2*size # Insert locations into two-point p2.popDist = np.insert(p2.popDist, 0, np.array([size, sizeb]), 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) r2 = radiation(p2) eps = epsilon(r2, r3, ib=ib) return eps
def radiation_ODM(self, level): ''' Returns the ODM for the radiation 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 else: clustering = self.levels[level - 1] pop = self.cluster_population(clustering) r = radiation(pop) return r.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)