← Notes · December 2026
Why combining models helps, how to weight them, why random forests average well, when a calibration transfers to another city, and how to keep two pollutants consistent, with proofs.
Machine learning calibration of low-cost sensors often combines several models, for example a neural network and a random forest, and is expected to work across cities, sensor types and pollutants. Behind these choices sit a few clean mathematical facts: combining models trades accuracy against diversity, random forests reduce variance by averaging correlated trees, transfer to a new city depends on what changes between cities, and joint predictions should respect physical constraints. These notes state and prove them.
Let \(f_1,\dots,f_K\) be predictions of a target \(y\), and \(\bar f = \sum_k \alpha_k f_k\) a convex combination (\(\alpha_k\ge0\), \(\sum_k\alpha_k = 1\)).
Proof. Write \(f_k - y = (f_k - \bar f) + (\bar f - y)\), square, and take the \(\alpha\)-weighted sum. The cross term is \(2(\bar f - y)\sum_k\alpha_k(f_k-\bar f) = 0\), since \(\sum_k\alpha_k f_k = \bar f\) and \(\sum_k \alpha_k = 1\). \(\square\)
So a hybrid gains from diversity: models that make different errors, such as a smooth neural network and a piecewise-constant random forest, have a larger second term.
Proof. The error is \(g(\alpha) = \mathbb E(\alpha e_1 + (1-\alpha)e_2)^2 = \alpha^2\sigma_1^2 + (1-\alpha)^2\sigma_2^2 + 2\alpha(1-\alpha)c\), a convex quadratic. Setting \(g'(\alpha)=0\) gives \(\alpha^\star\), and substituting gives the minimum. Since \(\alpha = 1\) and \(\alpha = 0\) are allowed, the minimum is at most \(\min(\sigma_1^2,\sigma_2^2)\). \(\square\)
The weight should be estimated on held-out data, not on the training data, where flexible models look better than they are. If \(\alpha^\star\) falls outside \([0,1]\), the errors are so strongly correlated that one model should simply be dropped. When the two models are equally accurate and their errors uncorrelated, \(\alpha^\star = 1/2\) and the error halves (Bates and Granger, 1969).
Proof. The variance of the sum is \(B\sigma^2 + B(B-1)\rho\sigma^2\). Divide by \(B^2\). \(\square\)
Adding trees removes the second term but not the first. Random forests reduce \(\rho\) by growing each tree on a bootstrap sample and considering a random subset of features at each split (Breiman, 2001). This is why they need little tuning: more trees never hurt, and decorrelation does the rest. The same formula explains why averaging forecasts from very similar models gains little.
Let a calibration model be trained in a source city, where inputs \(x\) (raw sensor reading, humidity, temperature) have density \(p_S(x)\), and used in a target city with density \(p_T(x)\). Write \(R_T(f) = \mathbb E_T\,\ell(f(x),y)\) for its risk in the target city.
Proof. \(R_T(f) = \int\!\!\int \ell(f(x),y)\,p(y\mid x)\,p_T(x)\,dy\,dx\). Multiply and divide by \(p_S(x)\): the integrand becomes \(w(x)\,\ell(f(x),y)\,p(y\mid x)\,p_S(x)\), which is the source expectation of \(w\ell\). \(\square\)
So if cities differ only in their conditions (one is more humid, another more polluted), a model can be made to target the new city by reweighting source data, provided the source city covers the target's conditions. Two warnings follow from the proof:
Heavily uneven weights also mean few effective observations: the effective sample size \((\sum_i w_i)^2/\sum_i w_i^2\) from the GWR note measures exactly this.
Physically, \(\mathrm{PM}_{2.5} \le \mathrm{PM}_{10}\), because PM10 includes all particles up to 10 µm. Calibrating the two separately can violate this. The fix is a projection, and it can only help.
Proof. If \(\hat a > \hat b\), the closest point of \(C\) lies on the line \(a = b\), and minimising \((\hat a - m)^2 + (\hat b - m)^2\) over \(m\) gives \(m = (\hat a+\hat b)/2\). For the inequality, \(C\) is closed and convex, so the projection satisfies \(\langle z - P(z),\, t - P(z)\rangle \le 0\) for all \(t\in C\). Then \(\|z-t\|^2 = \|z-P(z)\|^2 + \|P(z)-t\|^2 - 2\langle z-P(z), t-P(z)\rangle \ge \|P(z)-t\|^2\). \(\square\)
So enforcing \(\mathrm{PM}_{2.5}\le\mathrm{PM}_{10}\) after separate calibrations never increases the joint squared error, and it removes predictions that would be impossible to explain to users of the data.
A tested Python sketch: fit a random forest and a neural network, choose the hybrid weight on validation data by Proposition 2, and enforce the PM ordering by Proposition 5. On synthetic sensor data, the hybrid beat both models and the ambiguity identity of Proposition 1 held to machine precision.
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
def fit_hybrid(Xtr, ytr, Xva, yva):
rf = RandomForestRegressor(n_estimators=300, min_samples_leaf=5, random_state=0).fit(Xtr, ytr)
nn = make_pipeline(StandardScaler(),
MLPRegressor(hidden_layer_sizes=(32, 32), max_iter=2000, random_state=0)).fit(Xtr, ytr)
e1, e2 = rf.predict(Xva) - yva, nn.predict(Xva) - yva
s1, s2, c = e1.var(), e2.var(), np.cov(e1, e2)[0, 1]
a = np.clip((s2 - c) / (s1 + s2 - 2 * c), 0, 1) # Proposition 2
return lambda X: a * rf.predict(X) + (1 - a) * nn.predict(X)
def enforce_order(pm25, pm10):
"""Project onto {pm25 <= pm10} (Proposition 5)."""
m = (pm25 + pm10) / 2
bad = pm25 > pm10
return np.where(bad, m, pm25), np.where(bad, m, pm10)
With my Ph.D. student Irfan Yaqoob and colleagues, we developed a hybrid of a neural network and a random forest that calibrates two sensor types and two pollutants, PM2.5 and PM10, across three European cities. Details are in Yaqoob et al. (2026). It builds on our earlier work on machine learning calibration of PM2.5 data (Yaqoob et al., 2024), calibration during wildfire smoke events (Yaqoob et al., 2025), and how the distance to and number of co-located sensors affect correction models (Kumar et al., 2025). The same questions, how to combine models and when a calibration transfers, guide our sensor network in Karachi.
With \(c=0\): \(\alpha^\star = 9/13\approx0.69\) on the better model, and error \(36/13 \approx 2.77\), better than 4. With correlation 0.5, \(c = 0.5\cdot2\cdot3 = 3\): \(\alpha^\star = (9-3)/(4+9-6) = 6/7\approx0.86\), and error \((36-9)/7 = 27/7\approx3.86\). The gain over the better model shrinks from 31% to 4%: correlated errors leave little to gain from combining.
By Proposition 3: \(0.4 + 0.6/10 = 0.46\), \(0.4 + 0.6/100 = 0.406\), and \(0.4\) in the limit. More trees cannot go below \(\rho\sigma^2 = 0.4\). To lower it, decorrelate the trees, for example by using fewer candidate features per split or smaller bootstrap samples, which usually raises each tree's own variance a little but lowers \(\rho\) more.
On \([50,80]\), the weight is \(w = (1/45)/(1/60) = 4/3\), and below 50% it is 0: source data in dry conditions is irrelevant to the target. Above 80%, \(p_S = 0\) while \(p_T>0\), so the condition of Proposition 4 fails: one third of the target's humidity range has no source data. That is exactly where low-cost optical sensors are least reliable, because particles absorb water. Co-locating at least one sensor with a reference monitor in the target city, during humid periods, is the remedy.