urbanscaling

Urban scaling laws Y = Y0 N^beta: OLS, RMA, robust and nonlinear fits, bootstrap intervals, tests against linearity, SAMIs, city-definition sensitivity, residual diagnostics and panel scaling.

Version 0.1.2 · Python >=3.9 · MIT licence
pip install urbanscaling
PyPISourceIssuesChangelogCite (DOI)

urbanscaling

PyPI version Python versions pipeline status License: MIT DOI Downloads

Urban scaling laws Y = Y0 N^beta: estimation, inference and diagnostics.

urbanscaling takes one value of an urban indicator per city (GDP, patents, road length, crime, wages, ...) together with the city's population, and estimates how the indicator scales with size:

It needs only NumPy (all distribution functions are implemented in the package). A command-line tool reads CSV or JSON and writes JSON or CSV.

Installation

pip install urbanscaling

Requires Python 3.9 or newer.

Quickstart

import numpy as np
import urbanscaling as us

rng = np.random.default_rng(0)
pop = np.exp(rng.uniform(np.log(5e4), np.log(2e7), 300))
gdp = 2.5e4 * pop**1.12 * np.exp(rng.normal(0, 0.25, 300))

fit = us.fit_ols(pop, gdp)                 # or fit_rma, fit_huber, fit_nls, fit_scaling(method=...)
fit.beta, fit.ci_beta, fit.r_squared       # exponent, 95 % CI, R^2 of the log-log fit
fit.regime()                               # 'superlinear' | 'linear' | 'sublinear'

us.beta_test(fit, 1.0).to_dict()           # t, p-value and verdict for H0: beta = 1
boot = us.bootstrap_fit(pop, gdp, n_boot=2000, seed=1)
boot.ci_bca, boot.ci_percentile

s = us.sami(pop, gdp, names=[f"c{i}" for i in range(300)])
s.top(5)                                   # cities furthest above the scaling line

us.diagnose(fit).to_dict()                 # normality, heteroscedasticity, curvature, outliers
us.threshold_sensitivity(pop, gdp, [5e4, 1e5, 5e5, 1e6]).summary()

Panel data (one row per city and year):

res = us.panel_scaling(city_ids, years, population, indicator, effects="twoway")
res.cross_sectional_records()   # beta per year
res.temporal_summary()          # distribution of per-city time-series exponents
res.within.to_dict()            # pooled fixed-effects exponent, clustered SE

See examples/quickstart.py for a runnable script.

Features

Area Functions / classes
Fitting fit_scaling(method=...), fit_ols(se="classical"|"hc1"), fit_rma, fit_huber(c=1.345), fit_nls(weights=...) -> ScalingFit
Inference bootstrap_fit -> BootstrapResult, beta_test -> BetaTest, compare_betas
SAMIs sami, sami_from_fit -> SAMIResult, sami_correlation, sami_change, sami_persistence
Sensitivity threshold_sensitivity, rank_sensitivity, definition_sensitivity -> SensitivityResult
Diagnostics diagnose -> Diagnostics, jarque_bera, breusch_pagan, curvature_test
Panel make_panel, cross_sectional_fits, temporal_fits, within_fit -> PanelFit, panel_scaling -> PanelResult
Theory theoretical_exponents(dimension, h)
I/O read_table (CSV/JSON), numeric_column, write_csv, write_json

All results have a JSON-safe to_dict(). Logarithms are natural; intercept is ln Y0.

Methods and sources

Method Definition Source
Scaling law, OLS ln Y = ln Y0 + beta ln N by least squares Bettencourt et al. (2007)
HC1 errors n/(n-2) (X'X)^-1 X' diag(e^2) X (X'X)^-1 White (1980); MacKinnon & White (1985)
RMA / SMA slope sign(r) s_y/s_x; CI beta (sqrt(B+1) -/+ sqrt(B)), B = (1-r^2) t^2/(n-2); test of beta0 via cor(lnY - beta0 lnN, lnY + beta0 lnN) Warton et al. (2006); Pitman (1939)
Huber IRLS, scale `median e
NLS min sum w_i (Y_i - Y0 N_i^beta)^2, Levenberg-Marquardt Leitão et al. (2016)
Bootstrap pairs / residual resampling; percentile, basic, normal, BCa (jackknife acceleration) Efron (1987); Efron & Tibshirani (1993)
SAMI xi_i = ln(Y_i / (Y0 N_i^beta)) Bettencourt et al. (2010)
Definition sensitivity refits across size thresholds and delineations Arcaute et al. (2015)
Normality JB = n/6 (S^2 + (K-3)^2/4), chi-square(2) Jarque & Bera (1987)
Heteroscedasticity n R^2 of e^2 on ln N, chi-square(1) Breusch & Pagan (1979); Koenker (1981)
Curvature F test of (ln N)^2 (equal to RESET with squared fitted values) Ramsey (1969)
Influence leverage, studentised residuals, Cook's distance Cook (1977)
Panel per-period and per-city fits; within (one- or two-way) estimator with cluster-robust SE Bettencourt et al. (2020); Arellano (1987)
Model exponents delta = H/(D(D+H)), area D/(D+H), infrastructure 1-delta, socio-economic 1+delta Bettencourt (2013)

Notes:

Command line

urbanscaling fit cities.csv --population pop --indicator gdp --name city \
    --method ols --se hc1 --bootstrap 2000 --seed 1 -o fit.json
urbanscaling sami cities.csv --population pop --indicator gdp --name city --csv sami.csv
urbanscaling sensitivity cities.csv --population pop --indicator gdp --thresholds 5e4,1e5,1e6
urbanscaling sensitivity cities.csv --population pop --indicator gdp --top-k 20,50,100
urbanscaling panel panel.csv --population pop --indicator gdp --city id --period year \
    --effects twoway --csv temporal.csv
urbanscaling theory --dimension 2 --h 1

Inputs are CSV (header row) or JSON (list of records or mapping of columns). Output is JSON on stdout or in -o; --csv writes the per-city table. python -m urbanscaling works too.

References

Academic citation

@software{eminoglu_urbanscaling,
  author  = {Eminoğlu, Yusuf},
  title   = {urbanscaling},
  year    = {2026},
  version = {0.1.2},
  doi     = {10.5281/zenodo.24235550},
  url     = {https://gitlab.com/geophilo1/urbanscaling_sdk}
}

Related Geophilo SDKs

License

MIT, see LICENSE.

Other Geophilo SDKs