Make a surveillance heatmap¶
Show a set of indicators the way central banks and the IMF do in their surveillance charts: one row per indicator, one column per month, each cell coloured by how unusual that reading is against the indicator's own recent history. Here monthly activity, labour and survey indicators and quarterly GDP growth, ranked over a rolling five-year window, with the regimes k-means finds in the data drawn as a band above the dates.
Before you start¶
- The indicators, as rates or deviations that can be compared over time: growth rates rather than levels, survey
balances as they are. This page uses the example datasets
sample_monthly(industrial production, retail sales, employment and a PMI, January 2000 to March 2025) andsample_macro(quarterly GDP, 2000Q1 to 2024Q4), and makes annualised growth rates from them. With your own data, load the series instead. - Which way is bad for each indicator. Colour means "worse" only if it means it on every row: here higher growth and a higher PMI are good, so all five are flipped and red is weak.
- How to compare: a percentile rank (0 to 100) says where a reading stands among the last n, whatever the series' distribution; a z-score says how many standard deviations it is from the mean; a robust z-score uses the median and the median absolute deviation, which one outlier hardly moves. The window: long enough to hold a cycle (60 months here), or a fixed reference period.
In the app, or in code¶
Follow this step by step in the app
View → Guide → How do I → Make a surveillance heatmap. The Guide panel takes you through these steps one at a time: Open…
opens each step's dialog filled in, Show me runs it, and each step is ticked when its result is there. In
the app's help, the page's Walk me through this opens it. The Guide is in the
pymetrix-guide plugin, which is experimental
(pip install --pre "pymetrix[guide]"; the walkthroughs come with its release after 0.1.0a1).
- File → Load example data…, with Example dataset: sample_macro and Save as: data; again with Example dataset: sample_monthly and Save as: monthly.
- Data → Derived series…, with Data: data, New series name: gy and Formula:
400*dlog(y)(GDP growth, annualised). Again with Data: monthly: New series name: gr, Formula:1200*dlog(retail); New series name: gip, Formula:1200*dlog(ip); New series name: gemp, Formula:1200*dlog(emp). - Estimate → Clusters → Number of clusters (k-means)…, with Series: gip, gr, gemp, pmi, Data: monthly, Most clusters: 6 and Save as: choice. The window shows, for each k, the within-cluster sum of squares (the elbow) and the mean silhouette.
- Estimate → Clusters → K-means clusters…, with Series: gip, gr, gemp, pmi, Data: monthly, Number of clusters: 3 and Save as: regimes. The Summary is each cluster's size, spread and centre; the Graph the cluster of each month.
- Data → Statistics → Surveillance heatmap… (or, in the clusters' window, Next ▸ Surveillance heatmap with these clusters…), with Indicators: gip, gr, gy, gemp, pmi, Data: monthly, Data of another frequency: data, Colour by: Percentile rank (0 to 100), Window (periods): 60, Higher is better for: gip, gr, gy, gemp, pmi, Groups of indicators: activity: gip gr gy; labour: gemp; surveys: pmi, Order of rows: By clustering, Show the last (periods): 36, K-means clusters as a band: regimes and Save as: heatmap.
- The window: the Graph is the heatmap, the clusters in a band above it; Latest is each row's latest reading, its percentile rank and the change over 3, 6 and 12 months; Normalised and Readings are the tables behind the colours.
from pymetrix import Dataset, kmeans, kmeans_k, surveillance_heatmap
data = Dataset.example("sample_macro").derive(gy="400*dlog(y)")
monthly = Dataset.example("sample_monthly").derive(gr="1200*dlog(retail)", gip="1200*dlog(ip)", gemp="1200*dlog(emp)")
indicators = ["gip", "gr", "gemp", "pmi"]
choice = kmeans_k(monthly, indicators, k=range(2, 7)) # silhouette 0.281 at k = 2, 0.223 at 3; within SS falls 140, then 85
regimes = kmeans(monthly, indicators, k=3) # 148, 73 and 81 months; January 2000 left out (no growth rate)
regimes.centres # cluster 2: ip growth 16.0, retail 10.4, PMI 52.1; cluster 3: -3.1, -1.4, 46.6
heatmap = surveillance_heatmap(
[monthly, data], ["gip", "gr", "gy", "gemp", "pmi"], normalise="pctrank", window=60,
higher_is_worse=False, # all five: higher is better, so red is weak
groups={"activity": ["gip", "gr", "gy"], "labour": ["gemp"], "surveys": ["pmi"]},
order="cluster", last=36, regimes=regimes)
heatmap.latest_table() # March 2025: retail 93.3 (growth -9.8), PMI 83.3, composite 64.7; GDP 5.0 in 2024Q4
heatmap.plot()
The settings are kept: the same heatmap on newer data, the clustering behind the order, a fixed reference period, and a report with the cells coloured.
again = heatmap.spec.draw([monthly, data], regimes=regimes) # what File → Refresh data… does with the step
heatmap.dendrogram() # the clustering of the five, by their correlations
from pymetrix.heatmap import normalise
z = normalise(monthly, ["gr"], "zscore", reference=("2010", "2019")) # March 2025: -1.94 against the 2010s
from pymetrix import Report
Report("Surveillance").add(heatmap, sections=["Graph", "Latest"]).save("surveillance.html")
Check the result¶
- Each cell is the indicator's percentile rank among its last 60 months, the month itself included, turned round
where higher is better: retail sales' 93.3 in March 2025 is
100 - movpctrank(gr, 60)there, which a formula in Data → Derived series… gives too. Its growth that month, -9.8% annualised, is lower than in 56 of the 60 months. - GDP is quarterly: each month of a quarter shows the quarter's rank (Lower-frequency readings, under Advanced settings, can show it in the quarter's last month only, or from its release), computed over 20 quarters, the 60 months' worth. Its last figure is 2024Q4, so January to March 2025 are hatched: missing, not average.
- The group rows average their members' ranks, and the composite averages the three groups: 64.7 in March 2025, each group counting once whatever its size. Its highest in the 36 months shown is 92.2, in December 2023.
- The regimes: k-means on the four standardised monthly indicators. Cluster 2 is strong growth, cluster 3 contraction (falling output and retail sales, a PMI below 50), cluster 1 between. The last two months, February and March 2025, are in cluster 3. The mean silhouette, 0.223, says the regimes overlap: they summarise the data, they do not separate it cleanly.
- With scikit-learn installed,
sklearn.cluster.KMeans(3, n_init=10)on the same standardised data finds the same partition, andsklearn.metrics.silhouette_scorethe same silhouette.
Pitfalls¶
- Levels that trend make every recent reading look extreme. A percentile rank of a trending level is near 100 whatever happens; rank growth rates, gaps (Colour by: gap from trend) or ratios instead.
- The window sets what "unusual" means. Against the last five years, a reading is judged by that period's conditions; a long expansion makes ordinary readings look weak. Compare with a fixed reference period (Reference start and Reference end) before drawing conclusions.
- Composites hide disagreement. A middling composite can be one group in red and another in blue: read the group rows. The first-principal-component weights (Composite row, under Advanced settings) follow the series that move together most, not the ones that matter most.
- Monthly growth rates are noisy. A single month in red is often noise; look for runs, or use three-month or twelve-month growth.
- K-means always finds k clusters. It finds them whether or not the data have regimes, and the clusters change with the indicators and their scaling. Check the silhouette and the elbow, and whether the clusters line up with events you know (recessions, the pandemic) before calling them regimes.
See also¶
- Surveillance heatmaps and K-means cluster analysis, in the help.
- Nowcast GDP: the same indicators in a dynamic factor model.
- Make a report: add the heatmap and its tables to a report.
- The user guide's Surveillance heatmaps and clusters.