Skip to content
SlateGitOrgPublic

About

Gym site selection with a Huff gravity model fitted to members' home zones, forecasting each candidate's sister-club cannibalisation and backtested on a club that has already opened.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

huffsite

CI

huffsite

huffsite chooses gym sites with a Huff gravity model. It is fitted to where a chain's own members live, forecasts how many members a candidate club would take from the chain's other clubs and from competitors, evaluates pairs of candidates jointly, and is backtested on a club that has already opened. The data are synthetic: a seeded generator builds a 40 x 30 km metro with 28 clubs and six candidate sites.

The problem

A regional fitness chain with 16 clubs in one metro area has budget for two new clubs this year and six candidate sites. Its real-estate team ranks sites by adults living within 3 km. That count ignores the 12 competitor clubs nearby and the members a new club would simply move from the chain's own clubs. The site with the biggest radius count can add the fewest new members, and two good sites a kilometre apart look twice as good together as they are.

How it works

flowchart LR
    Z["Zones<br/>adults, income band"] --> M["Market per zone<br/>adults x penetration"]
    C["Clubs<br/>16 own, 12 competitor"] --> G["Distances<br/>0.5 km floor, 20 km choice set"]
    O["Own members<br/>by home zone x club"] --> F
    M --> F["Huff model fit<br/>competitor demand as one<br/>aggregated category"]
    G --> F
    F --> S["Forecast candidates<br/>singly and in pairs<br/>conservation check"]
    S --> B["Backtest on club H<br/>opened 2025-03<br/>versus the radius rule"]
Loading

The utility of club k for a zone is theta . (log floor area, pool, budget, competitor) - beta x log(distance). Members in a zone choose among the clubs within 20 km with probability proportional to exp(utility). The market of a zone, adults x the penetration rate of its income band (rounded to whole members), is fixed, so a new club only redistributes it.

The chain sees its own members by home zone but not competitor membership. The likelihood uses each own club as a separate category and the zone's remaining members (market minus own members) as one aggregated competitor category whose probability is the sum over competitor clubs. Parameters are estimated by L-BFGS-B with the analytic gradient. Standard errors come from the inverse of the Hessian, computed by central differences of that gradient.

A forecast adds the candidate (or two candidates) to the clubs open in a snapshot and recomputes every zone's probabilities with the fitted parameters. The members of the new club split exactly into transfers from each own club and transfers from competitors; net new chain members equal the competitor transfers. Annual contribution is net new members x contribution per member (420 by default) minus the site cost. The radius rule it replaces is adults within 3 km x the chain's observed own members per adult.

Quick start

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"

huffsite generate --seed 17 --out data/
huffsite fit --data data/
huffsite forecast --data data/            # all six candidates
huffsite forecast --site C3 --data data/  # one candidate and the clubs it takes members from
huffsite pairs --data data/ --top 5
huffsite backtest --data data/
huffsite report --data data/ --out out/   # out/report.md and out/map.png

pytest -q

fit, forecast and pairs take --snapshot (default 2025-12) and --contribution.

Example output

From huffsite generate --seed 17 (generate wrote 1,200 zones, 28 clubs, 6 candidates).

huffsite fit

snapshot 2025-12: 28 clubs open, 1200 zones used, 0 excluded (no club within 20 km), log-likelihood -176,570.6, 17 iterations

parameter            estimate  std err  95% CI low  95% CI high   truth  truth in CI
-------------------  --------  -------  ----------  -----------  ------  -----------
log floor area          0.773    0.037       0.700        0.846   0.800          yes
pool                    0.410    0.016       0.379        0.441   0.400          yes
budget format           0.282    0.013       0.256        0.308   0.300          yes
competitor constant    -0.207    0.007      -0.221       -0.193  -0.200          yes
beta (log distance)     1.789    0.005       1.778        1.799   1.800           no

The beta interval misses the truth by 2.2 standard errors on this seed. The test suite checks calibration of the standard errors on twelve other seeds (see Testing).

huffsite forecast

site  members  own transfers  competitor transfers  net new  contribution  radius members  radius contribution
----  -------  -------------  --------------------  -------  ------------  --------------  -------------------
C4      8,012          4,608                 3,405    3,405       829,902           4,120            1,130,288
C3      7,442          4,361                 3,082    3,082       694,259           4,034            1,094,155
C1      5,129          3,218                 1,910    1,910       302,397           2,839              692,517
C2      2,922          1,598                 1,324    1,324       -43,759           2,219              331,965
C6      4,464          2,526                 1,938    1,938      -286,161           6,635            1,686,623
C5      2,058          1,345                   713      713      -300,579             734             -291,835

C6 has the largest radius figure by a wide margin, but its members mostly come from the chain's own clubs and a large competitor 300 m away, and its site cost is the highest of the six (1.1M a year); the model puts it fifth. The site cost is part of that rank: at a 600,000 site cost C6 would still rank fourth, ahead of C2, with a contribution of about 214,000, so the demotion comes from the transfers and the site cost together.

huffsite pairs --top 5

rank   pair  members  net new  contribution  interaction
----  -----  -------  -------  ------------  -----------
1     C1+C4   12,455    5,032     1,013,439     -118,860
2     C3+C4   12,047    5,081       933,855     -590,306
3     C1+C3   11,890    4,716       880,778     -115,877
4     C2+C4   10,921    4,723       783,855       -2,288
5     C2+C3   10,347    4,399       647,393       -3,107

C3 and C4 are the two best singles, but together they lose 590,306 against the sum of their singles (39% of it), and the best pair is C1+C4.

huffsite backtest

fitted on 2024-12; club H predicted for 2025-12; nearest own clubs B and D

club             km from new  predicted  actual    error
---------------  -----------  ---------  ------  -------
H (new)                    -      7,276   7,345    -0.9%
H (radius rule)            -      4,087   7,345   -44.4%
B change                 3.2       -545    -525    -3.8%
D change                 4.0       -883    -879    -0.4%
A change                 6.0       -586    -446   -31.4%
G change                 6.0       -472    -520    +9.2%
I change                 8.1       -163     -59  -177.0%
F change                 8.5       -270    -218   -24.0%
C change                11.1       -181    -336   +46.0%
P change                11.5       -123    -144   +14.6%
E change                12.3       -169    -175    +3.7%
M change                14.0       -230    -199   -15.5%
L change                14.8        -72    -174   +58.8%
J change                15.0       -165    -209   +20.9%
K change                17.5        -59     -33   -78.1%
N change                18.6       -126      49  -356.8%
O change                18.6        -48     -55   +13.1%

The model, fitted before H opened, predicts H's members to within 1% and the losses at the two nearest clubs to within 4%. The 44% error of the radius rule is amplified by H being a large club (3,500 m2 with a pool): it draws members from beyond 3 km, which the radius count cannot see. The backtest also holds the market constant between the two snapshots, as the generator does, so it does not test the model against market growth or a changing competitor set. Percentage errors on the small, distant clubs are large because their absolute changes are comparable to the sampling noise of two separate multinomial draws; club N even gains 49 members in the data, and its -356.8% comes from that tiny denominator. A club whose actual change is exactly 0 has no relative error and is printed as n/a.

Design decisions

  • Competitor demand is one aggregated category. The chain does not observe competitor members, so the likelihood cannot give each competitor its own count. Aggregating keeps the fit honest about the data the chain has. The cost is that the competitor constant and competitor attributes are identified only through the total competitor share, so individual competitor effects are not estimated.
  • Fixed market, no outside option. Market members are adults x penetration, and every member picks a club. This makes the conservation invariant exact (the change in members over all clubs is zero in every zone) and means net new chain members equal competitor transfers. It cannot represent a club that grows the market itself, for example by attracting people who would not otherwise join any gym.
  • Euclidean distance with a 0.5 km floor and a 20 km choice set. Simple and reproducible on a grid of 1 km cells; the floor stops a zone centroid on top of a club from getting an infinite weight. Road or travel-time distance would be more realistic but needs data the chain would have to buy. Zones with no club in range are reported and excluded, and stay excluded in forecasts even when a candidate is within 20 km, so every forecast changes the same set of zones.
  • Hessian by central differences of the analytic gradient. The gradient is exact and tested against finite differences; differentiating it numerically gives a Hessian accurate enough for standard errors without a second hand-derived formula. The L-BFGS-B inverse-Hessian estimate is not used because it is only an approximation.
  • Pairs are evaluated jointly. Both candidates are added before probabilities are recomputed, so each one takes members from the other. Interaction is the pair contribution minus the two single contributions; site costs cancel, so it measures cannibalisation only.
  • The radius rule uses the chain's observed own members per adult. This is the strongest simple version of the rule (it already includes penetration and chain share). The rule still ignores competitor proximity, club attributes and the chain's own clubs.

Testing

GITHUB_ACTIONS=true CI=true COLUMNS=80 pytest -q

96 tests, offline. They cover:

  • the hand fixture (probabilities [2/3, 1/3], then [0.4, 0.2, 0.4] with losses 0.2667 and 0.1333 that sum to the new club's 0.4);
  • the analytic gradient against central finite differences at four parameter points, within 1e-6 relative;
  • parameter recovery (beta within 0.15 of 1.8, floor-area coefficient within 0.15 of 0.8) and a log-likelihood at the maximum at least as high as at the true parameters;
  • standard-error calibration across twelve other seeds (60 z-scores): their rms must lie between 0.7 and 1.4, so standard errors half or twice the right size fail, and none may exceed 3.5 in absolute value;
  • conservation of members in every zone for every single candidate and every pair, and new-club members equal to own plus competitor transfers;
  • a zone with no club within 20 km: zero predicted members, counted as excluded, and no effect on the fit;
  • the injected patterns: C3 and C4 are the top two singles, their interaction is at most -20% of the sum of their singles, the best pair is not C3+C4, and C6 ranks first by the radius rule but outside the model's top three;
  • the backtest on H: predicted members within 15% of actual, radius error above 30%, and the two nearest own clubs losing members in prediction and in the data, each predicted loss within 30% of actual;
  • identical rankings across runs, with ties broken by id, and a byte-identical report;
  • the CLI commands, error exits (including --top below 1) and the report files;
  • a snapshot with no competitor clubs, which is reported as a data error, and the fit summary flagging an optimiser that did not converge.

Project layout

src/huffsite/
  geo.py        distances, 0.5 km floor, 20 km choice sets, adults within a radius
  model.py      Huff probabilities, likelihood, analytic gradient, fit, standard errors
  data.py       file schema, loading, snapshots
  generate.py   seeded synthetic metro with the injected patterns
  forecast.py   candidate forecasts, joint pairs, radius rule, ranking
  backtest.py   fit before a club opened, predict, compare with the later snapshot
  analysis.py   dataset to fitted model to forecasting context
  tables.py     plain-text and Markdown tables
  report.py     report.md and map.png
  cli.py        generate, fit, forecast, pairs, backtest, report
tests/          pytest suite

License

MIT. See LICENSE.

About

Gym site selection with a Huff gravity model fitted to members' home zones, forecasting each candidate's sister-club cannibalisation and backtested on a club that has already opened.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages