access.weights.gravity

access.weights.gravity(scale, alpha, min_dist=0)[source]

Create a gravity function from a scale \(s\) and \(\alpha\) parameters as well as an optional minimum distance \(x_\text{min}\). The function is of the form \(f(x) = (\text{max}(x, x_\text{min})/s)^\alpha\). Note that there is no overall normalization.

Parameters:
scalefloat

Scaling value, normalizing the function input.

alphafloat

Power to which the normalized inputs are raised. Note that it is not implicitly negative (i.e., \(x^\alpha\) instead of \(1/x^\alpha\).

min_distfloat

A ‘standard’ issue with gravity model is the infinite potential at 0 distance or time. This can be rectified crudely by specifying a minimum distance, and setting any input exceeding that minimum to the minimum itself. The default threshold is 0.

Returns:
weight_functionfunction

Function returning weight, for input distance or time, x.

Examples

Import the weights:

>>> from access import weights

Create a step function with thresholds at 20, 40, and 60. Travel costs are in minutes here, but the code cannot tell if you mix units!

>>> fn = weights.gravity(scale = 20, alpha = -2, min_dist = 1)
>>> {t : round(fn(t), 2) for t in [0, 1, 2, 20, 40, 60]}
{0: 400.0, 1: 400.0, 2: 100.0, 20: 1.0, 40: 0.25, 60: 0.11}