Getting started with blvim: retail centres in Île-de-France
Source:vignettes/tutorial.Rmd
tutorial.RmdThis tutorial walks through a complete analysis with
blvim, from the preparation of the data to the
interpretation of an equilibrium of the Boltzmann–Lotka–Volterra (BLV)
model. It follows the original motivation of the model: given the
residents of a region and the travel times between its cities, where do
retail centres develop, and which areas do they serve?
The introduction of the package (README) presents the
basic functions on a toy example; this tutorial focuses on the practical
choices of an analysis: units and scale of the costs, starting points of
the algorithm, checks of the results and conversion factors. The theory
of the model is presented in vignette("theory") and
vignette("potential"), and the systematic exploration of
the parameter space in vignette("grid").
Data
We consider the 45 cities of Île-de-France (the Paris region)
included in the french_cities data set. In a retail
interpretation of the model:
- the production \(X_i\) of city \(i\) is the spending of its residents, here its population in thousands;
- the attractiveness \(Z_j\) of city \(j\) is its retail capacity (for instance its shopping floor space), in the same unit as the spending;
- the cost \(c_{ij}\) is the travel
time by car from \(i\) to \(j\), taken from the
french_cities_timesdata set. The times are given in seconds and we convert them into minutes.
idf <- french_cities$region == 11
cities <- french_cities[idf, ]
times <- french_cities_times[idf, idf] / 60
rownames(times) <- cities$name
colnames(times) <- cities$name
spending <- cities$population / 1000The region is dominated by Paris, whose population is larger than that of the 44 other cities combined.
head(cities[c("name", "population")])
#> name population
#> 1 Paris 2113705
#> 21 Saint-Denis 148907
#> 32 Boulogne-Billancourt 120205
#> 36 Montreuil 110758
#> 38 Argenteuil 107135
#> 43 Nanterre 98119
sum(cities$population[-1])
#> [1] 3187427Each city is both an origin (its residents) and a destination (its
shops): the model is therefore non-bipartite
(bipartite = FALSE). The travel times are not symmetric,
which is not a problem for the model. A city sends a part of its
spending to itself at no cost, as the diagonal of times is
zero.
The names and the positions of the cities are not used by the model,
but they are needed by most of the representations of the results. They
can be given at creation with the origin_data and
destination_data arguments, which saves setting them on
each model with origin_positions<-() and
destination_positions<-(). For a non-bipartite model,
both must be identical.
The cost scale
The parameter \(\beta\) multiplies
the costs in the model: \(1/\beta\) is
a cost scale, here a duration in minutes. A change of the unit of the
costs must therefore be compensated by a change of \(\beta\), and a range of \(\beta\) that works for one data set has no
reason to work for another. A simple way to calibrate it is to compute
the mean travel time of the static model with uniform attractivenesses,
static_blvim(), for several cost scales. With
flows() and costs(), this is a one-liner.
mean_time <- function(model) {
sum(flows(model) * costs(model)) / sum(flows(model))
}
scales <- c(1, 2, 3, 4, 5, 6, 8, 10, 15)
static_times <- sapply(scales, function(scale) {
mean_time(static_blvim(times, spending, 1.2, 1 / scale,
Z = rep(1, nrow(cities)), bipartite = FALSE
))
})
data.frame(scale = scales, mean_time = round(static_times, 1))
#> scale mean_time
#> 1 1 0.0
#> 2 2 0.3
#> 3 3 1.4
#> 4 4 3.5
#> 5 5 6.2
#> 6 6 8.9
#> 7 8 13.1
#> 8 10 15.9
#> 9 15 19.6With a scale of one minute, the residents shop almost exclusively in their own city. With a scale of 15 minutes, the mean trip lasts more than 20 minutes, close to the median travel time between two cities of the region (28 minutes): the cost barely matters any more. The interesting cost scales are in between.
A first equilibrium
The BLV model lets the retail capacities adapt to the spending they
attract: at equilibrium, the capacity of each city equals its incoming
flow (when it is positive). blvim() computes such an
equilibrium. We use \(\alpha = 1.2\), a
moderate return to scale, and a cost scale of 4 minutes.
model <- blvim(times, spending, 1.2, 1 / 4,
bipartite = FALSE,
origin_data = locations, destination_data = locations
)
model
#> Spatial interaction model with 45 origin locations and 45 destination locations
#> • Model: Wilson's production constrained
#> • Parameters: return to scale (alpha) = 1.2 and inverse cost scale (beta) =
#> 0.25
#> ℹ The BLV model converged after 195 iterations.Without initial attractivenesses, blvim() starts from
the spending of each city (\(Z_j =
X_j\) in the non-bipartite case), a natural guess. Before looking
at the result, we check that the algorithm converged.
sim_converged(model)
#> [1] TRUE
sim_diagnostic(model)
#> BLV model obtained with the "gradient" algorithm.
#> ℹ The algorithm converged after 195 iterations:
#> • Number of potential evaluations: 260.
#> • Duality gap: 0.004535561.
#> • Return status: "converged".The duality gap measures how far the attractivenesses are from an
equilibrium (see vignette("potential")).
Several starting points
The equilibria of the BLV model are the local maxima of a potential
function, and there are in general many of them: the result depends on
the starting point of the algorithm. blvim_initialise()
generates several starting points, blvim() runs the
algorithm from each of them and keeps the result with the largest
potential. Here, we use 50 points sampled by the default generator.
set.seed(10)
random_model <- blvim(times, spending, 1.2, 1 / 4,
Z = blvim_initialise(n = 50),
bipartite = FALSE,
origin_data = locations, destination_data = locations
)
summary(sim_logs(random_model))
#>
#> ── Summary of 50 runs of blvim ──
#>
#> • 50 of 50 runs converged.
#> • Status: converged (50).
#> • Iterations: median 2, range [2, 241].
#> • Evaluations: median 2, range [2, 318].
#> • Largest duality gap of the converged runs: 0.005283.
#>
#> ── Final configurations
#> 4 distinct configurations. The best one (potential 26387.05, 20 active
#> destinations) was reached by 1 of 50 runs.
#> The second best potential is 26036.58 (difference 350.5).
#> configuration potential n_active runs share converged
#> 1 26387.0 20 1 0.02 1
#> 2 26036.6 12 1 0.02 1
#> 3 25695.2 10 1 0.02 1
#> 4 22939.2 1 47 0.94 47
#> ! The best configuration was reached by a single run: more starting
#> points might find a better one.Most of the random starting points end in the configuration where Paris attracts all the spending of the region (a single active destination). The best random run reaches a potential of 26387.0, lower than the one obtained from the spending of the cities (26619.2). A good starting point based on the data can therefore be more useful than many random ones.
Both can be combined with a custom generator, a function of the
context of the model (see blvim_initialise()).
The following one adds the spending of the cities to the points sampled
by the default generator (the centre of the simplex and
n - 1 random points).
with_spending <- function(context, ...) {
random <- blvim_simplex_initialise(context, ...)$Z
rbind(context$X / context$kappa, random)
}
set.seed(10)
model <- blvim(times, spending, 1.2, 1 / 4,
Z = blvim_initialise(with_spending, n = 49),
bipartite = FALSE, keep_runs = TRUE,
origin_data = locations, destination_data = locations
)
logs <- sim_logs(model)
logs$configuration[1:10]
#> [1] 1 5 5 5 5 5 5 5 5 3
which(logs$selected)
#> [1] 1The first run, started from the spending, is selected. With
keep_runs = TRUE, the models of all the runs are kept and
the best model of each distinct final configuration is available with
sim_runs() (and sim_df_runs()), which shows
the variety of the solutions.
configurations <- sim_df_runs(model, unique = TRUE)
configurations[c("configuration", "potential", "n_active", "n_runs")]
#> configuration potential n_active n_runs
#> 1 1 26619.19 21 1
#> 2 2 26036.58 12 1
#> 3 3 25695.16 10 1
#> 4 4 24933.97 5 1
#> 5 5 22939.17 1 46Reading the equilibrium
The attractivenesses at equilibrium show which cities host retail centres.
capacity <- attractiveness(model)
active <- capacity > 1e-6 * sum(capacity)
round(sort(capacity[active], decreasing = TRUE))
#> Paris Drancy Asnières-sur-Seine
#> 2533 656 595
#> Boulogne-Billancourt Saint-Denis Vitry-sur-Seine
#> 161 154 139
#> Nanterre Antony Versailles
#> 119 108 85
#> Argenteuil Créteil Montreuil
#> 79 78 78
#> Évry-Courcouronnes Cergy Champigny-sur-Marne
#> 72 68 61
#> Saint-Maur-des-Fossés Noisy-le-Grand Meaux
#> 61 59 56
#> Corbeil-Essonnes Chelles Rueil-Malmaison
#> 48 47 45Only 21 of the 45 cities keep a retail capacity. Paris attracts 48% of the spending of the region, more than the spending of its own residents (40%). The other centres are spread across the suburbs. The map below shows the flows between the cities (lines, flows smaller than 1 are not shown) and the incoming flows of the destinations (discs), with the names of the active destinations.
autoplot(model,
with_positions = TRUE, show_destination = TRUE, with_names = TRUE,
cut_off = 1
) +
coord_sf(crs = "epsg:4326") +
scale_linewidth(range = c(0, 1.5)) +
labs(x = NULL, y = NULL)
flows_df() returns the flows as a data frame, with the
names of the locations, which is convenient for further analysis. For
instance, we can find the main destination of each city, which defines
the catchment areas of the retail centres.
all_flows <- flows_df(model)
main_flows <- do.call(rbind, lapply(
split(all_flows, all_flows$origin_name),
function(origin_flows) origin_flows[which.max(origin_flows$flow), ]
))
catchments <- split(main_flows$origin_name, main_flows$destination_name)
catchments[order(lengths(catchments), decreasing = TRUE)][1:5]
#> $`Asnières-sur-Seine`
#> [1] "Asnières-sur-Seine" "Clichy" "Colombes"
#> [4] "Courbevoie" "Épinay-sur-Seine" "Gennevilliers"
#> [7] "Levallois-Perret" "Neuilly-sur-Seine" "Saint-Ouen-sur-Seine"
#> [10] "Sartrouville"
#>
#> $Drancy
#> [1] "Aubervilliers" "Aulnay-sous-Bois" "Bobigny"
#> [4] "Bondy" "Drancy" "Fontenay-sous-Bois"
#> [7] "Le Blanc-Mesnil" "Pantin" "Sarcelles"
#> [10] "Sevran"
#>
#> $Paris
#> [1] "Clamart" "Ivry-sur-Seine" "Maisons-Alfort" "Paris"
#> [5] "Villejuif"
#>
#> $Antony
#> [1] "Antony" "Massy"
#>
#> $`Boulogne-Billancourt`
#> [1] "Boulogne-Billancourt" "Issy-les-Moulineaux"
## number of centres by size of their catchment area
table(lengths(catchments))
#>
#> 1 2 5 10
#> 16 2 1 2Paris is the main destination of only a few of its neighbours, although 39 of the 44 other cities send it more than 1% of their spending: the residents of most cities shop mainly in a closer suburban centre, but also partly in Paris. Several centres serve only their own residents.
In the language of central place theory, terminals()
finds the dominant cities, i.e. the cities that receive more than the
city to which they send their largest flow (Nystuen and Dacey’s
definition). In this model, they are exactly the cities that keep a
retail capacity.
Two summaries characterise the concentration of the retail activity:
the diversity of the destinations (see diversity(), the
exponential of the entropy of the incoming flows, which ranges from 1
when a single destination attracts everything to the number of
destinations when the flows are evenly spread), and the mean travel
time.
diversity(model)
#> [1] 7.529913
mean_time(model)
#> [1] 4.731604The concentration of the retail activity in a few centres makes the trips longer than in the static model with uniform attractivenesses (3.5 minutes).
Changing the parameters
grid_blvim() computes the equilibria for all the
combinations of a set of values of \(\alpha\) and \(\beta\). With
shared_start = TRUE, the starting points are generated once
and shared by all the models. sim_df() turns the result
into a data frame with one row per model, to which we add our own
statistics.
set.seed(10)
models <- grid_blvim(times, spending,
alphas = c(1.1, 1.2, 1.3), betas = 1 / c(3, 4, 5, 6),
Z = blvim_initialise(with_spending, n = 49),
bipartite = FALSE, shared_start = TRUE
)
models_df <- sim_df(models)
models_df$scale <- 1 / models_df$beta
models_df$active <- sapply(models, function(m) {
sum(attractiveness(m) > 1e-6 * sum(attractiveness(m)))
})
models_df$paris_share <- sapply(models, function(m) {
attractiveness(m)[1] / sum(attractiveness(m))
})
models_df$mean_time <- sapply(models, mean_time)
format(models_df[c(
"alpha", "scale", "active", "diversity", "paris_share", "mean_time"
)], digits = 2)
#> alpha scale active diversity paris_share mean_time
#> 1 1.1 3 37 14.6 0.42 1.6
#> 2 1.2 3 28 11.7 0.42 2.2
#> 3 1.3 3 28 11.3 0.43 2.3
#> 4 1.1 4 30 10.7 0.45 3.5
#> 5 1.2 4 21 7.5 0.48 4.7
#> 6 1.3 4 7 3.3 0.59 8.1
#> 7 1.1 5 14 5.0 0.54 7.1
#> 8 1.2 5 3 2.4 0.65 10.7
#> 9 1.3 5 1 1.0 1.00 15.6
#> 10 1.1 6 3 2.4 0.66 11.2
#> 11 1.2 6 1 1.0 1.00 15.6
#> 12 1.3 6 1 1.0 1.00 15.6Larger values of \(\alpha\) and of
the cost scale \(1/\beta\) concentrate
the retail activity: fewer centres, a larger share for Paris and longer
trips. Above a cost scale of about 5 minutes, the retail structure
collapses into a single centre, Paris, all the sooner as \(\alpha\) is large. Small changes of the
parameters can therefore lead to completely different equilibria;
vignette("grid") shows how to explore these changes on
larger grids.
Conversion factors
So far, one unit of attractiveness (one unit of retail capacity) was
sustained by one unit of incoming spending in all the cities. In
practice, the cost of maintaining a retail capacity varies, for instance
with the rents. The conversion factors \(\kappa_j\) of blvim() model
this: at equilibrium, the incoming flow of city \(j\) is \(\kappa_j
Z_j\). Let us assume that retail space costs twice as much in
Paris as in the suburbs.
kappa <- ifelse(cities$name == "Paris", 2, 1)
set.seed(10)
expensive_paris <- blvim(times, spending, 1.2, 1 / 5,
Z = blvim_initialise(with_spending, n = 49), kappa = kappa,
bipartite = FALSE,
origin_data = locations, destination_data = locations
)
set.seed(10)
cheap_paris <- blvim(times, spending, 1.2, 1 / 5,
Z = blvim_initialise(with_spending, n = 49),
bipartite = FALSE,
origin_data = locations, destination_data = locations
)
sim_conversion(expensive_paris)[1:3]
#> [1] 2 1 1With a cost scale of 5 minutes, the conversion factor reduces the share of the spending of the region that goes to Paris, and leaves room for more centres in the suburbs. We compare the incoming flows, as the attractivenesses of the two models are no longer in the same unit for Paris.
compare <- function(model) {
flow <- destination_flow(model)
c(
paris_share = flow[[1]] / sum(flow),
active = sum(flow > 1e-6 * sum(flow)),
diversity = diversity(model)
)
}
round(rbind(
cheap = compare(cheap_paris),
expensive = compare(expensive_paris)
), 2)
#> paris_share active diversity
#> cheap 0.65 3 2.44
#> expensive 0.46 9 4.66Note that the generator of starting points takes the conversion
factors into account: the default generator samples attractivenesses
that satisfy \(\sum_j \kappa_j Z_j = \sum_i
X_i\), and with_spending() divides the spending by
the conversion factors.
Where to go next
-
vignette("grid")explores the parameter space systematically, with summaries, clustering and graphical representations of collections of models. -
vignette("potential")explains the potential function and the gradient algorithms ofblvim(), and shows how to monitor their iterations withsim_history(). -
vignette("theory")presents the model of Harris and Wilson and its variants.