ranksize

City-size distributions: Zipf rank-size regression with the Gabaix-Ibragimov correction, power-law tails by MLE with KS xmin selection and bootstrap goodness of fit, lognormal comparison by Vuong test, primacy and concentration indices, and rank clocks.

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

ranksize

PyPI version Python versions pipeline status License: MIT DOI Downloads

City-size distributions: Zipf, power-law tails, primacy and rank dynamics.

ranksize takes a list of city sizes (population, built-up area, employment, ...) and answers the standard questions about a city system:

It needs only NumPy. A command-line tool reads CSV or JSON and writes JSON or CSV.

Installation

pip install ranksize

Requires Python 3.9 or newer.

Quickstart

import ranksize as rs

sizes = [...]                          # one population per city

z = rs.rank_size(sizes, top=100)      # Gabaix-Ibragimov rank - 1/2 regression
z.zeta, z.se_gi, z.ci                 # exponent, GI standard error, normal 95 % CI
rs.zipf_test(z, 1.0)                  # H0: Zipf's law

pl = rs.fit_power_law(sizes)          # MLE alpha, KS-selected xmin
pl.alpha, pl.xmin, pl.n_tail          # zeta = alpha - 1
rs.power_law_gof(sizes, pl, n_sims=2500, seed=1).p_value
rs.vuong_test(sizes, pl, "lognormal").to_dict()

rs.jefferson_index(sizes), rs.four_city_index(sizes)
rs.concentration(sizes)               # primacy, Gini, HHI, Theil in one dict

Rank dynamics from a long table of (city, period, size):

t = rs.rank_table(city, year, population)
rs.rank_shift(t, top=100)             # mean |rank change|, Kendall tau, exits/entries
rs.top_n_survival(t, 100)             # share of the initial top 100 still there
rs.rank_clock(t, top=30)              # x, y coordinates for a rank-clock plot
rs.zipf_over_time(t, top=100)

See examples/quickstart.py for a runnable script.

Features

Area Functions / classes
Rank-size rank_size -> ZipfFit, zipf_test, rank_size_quadratic, hill_estimator -> HillResult, hill_plot, ranks
Power law fit_power_law -> PowerLawFit, power_law_gof -> GOFResult, ks_distance, sample_power_law
Alternatives fit_lognormal -> LognormalFit, fit_exponential -> ExponentialFit, vuong_test -> Comparison, compare_alternatives
Primacy jefferson_index, four_city_index, primacy_index(k), zipf_benchmark(k), primate_share
Concentration gini, lorenz_curve, hhi, theil, concentration
Dynamics rank_table -> RankTable, rank_clock, rank_shift, kendall_tau, top_n_survival, zipf_over_time
I/O read_table (CSV/JSON), numeric_column, write_csv, write_json

Results have a JSON-safe to_dict(). Sizes must be positive; drop_invalid=True drops missing or non-positive values instead of raising.

Methods and sources

Method Definition Source
Rank-size regression OLS of ln(R - s) on ln S, s = 1/2 (or 0 for the classical form); SE = zeta sqrt(2/n) Gabaix & Ibragimov (2011)
Quadratic rank-size ln(R - s) = a + b ln S + c (ln S)^2 Rosen & Resnick (1980)
Hill estimator zeta = 1 / [(1/k) sum_{i<=k} ln S_(i) - ln S_(k+1)], SE = zeta / sqrt(k) Hill (1975)
Power-law MLE alpha = 1 + n / sum ln(x_i/xmin), SE = (alpha-1)/sqrt(n) Clauset et al. (2009), eqs. 3.1-3.2
xmin value minimising the KS distance between empirical and fitted tail CDFs Clauset et al. (2009), sec. 3.3
Goodness of fit semi-parametric bootstrap; power law ruled out when p <= 0.1 Clauset et al. (2009), sec. 4.1
Model comparison R = sum (ln p_PL - ln p_alt), `p = erfc( R
Lognormal (whole distribution) mu, sigma of ln S Eeckhout (2004)
Two-city index S1 / S2 Jefferson (1939)
Four-city index S1 / (S2 + S3 + S4) standard in the primacy literature (origin credited inconsistently; no single source claimed)
Gini `sum_i sum_j x_i - x_j
HHI sum s_i^2, normalised (HHI - 1/n)/(1 - 1/n) Hirschman (1945); Herfindahl (1950)
Theil (1/n) sum (x/mu) ln(x/mu) Theil (1967)
Rank clock, rank shift radius = rank, time clockwise; `mean r_i(t) - r_i(t-1)
Kendall's tau tau_b with ties Kendall (1945)

Notes:

Command line

ranksize zipf cities.csv --size population --top 100 --hill 50
ranksize powerlaw cities.csv --size population --gof 2500 --seed 1 --scan-csv xmin_scan.csv
ranksize primacy cities.csv --size population --k 2,4,11
ranksize dynamics censuses.csv --size population --city id --period year --top 100 \
    --clock-csv clock.csv --ranks-csv ranks.csv

Inputs are CSV (header row) or JSON (list of records or mapping of columns); output is JSON on stdout or in -o. --drop-invalid drops empty or non-positive sizes. python -m ranksize works too.

References

Academic citation

@software{eminoglu_ranksize,
  author  = {Eminoğlu, Yusuf},
  title   = {ranksize},
  year    = {2026},
  version = {0.1.2},
  doi     = {10.5281/zenodo.24235552},
  url     = {https://gitlab.com/geophilo1/ranksize_sdk}
}

Related Geophilo SDKs

License

MIT, see LICENSE.

Other Geophilo SDKs