What does a neural network add once every group has a slope?
Christopher Peters · Bayesian Methods, Part III · November 2026
In the last post I gave ten groups ten different slopes, then asked a neural network to predict all ten with one shared curve. The hierarchical linear model won. I attributed the gap to the cost of carrying extra neural-network parameters. Looking at the equations, I’d change that explanation.
Here’s the detail I missed. The linear model could predict that two groups would cross. The neural network could bend as much as it liked, but its predicted groups had to stay the same distance apart. In the grouped-linear case the scores were 0.31 for the linear model, 0.49 for the ensemble, and 0.84 for the hierarchical network. I also called the network second when it finished third. Part II and its results table
Watch the gap between two groups
Let μg(x) be the expected response for group g at predictor value x. The network in Part II estimated one function f from all the groups. Each group also learned an intercept αg, partially pooled toward a common mean.
μg(x) = αg + f(x)
Suppose group A starts one unit above group B. Subtract their predictions at the same x. The two copies of f(x) cancel. The gap is one unit on the left, in the middle, and on the right. Adding hidden units can change where both curves bend. The group gap stays one.
Now give each group a slope adjustment ug, drawn from a common distribution centered at zero. Groups with little information about slope can borrow strength from the others.
μg(x) = αg + f(x) + ugx
If A’s slope adjustment exceeds B’s by one, their gap becomes 1 + x. The groups meet at x = −1. At x = 0, A leads by one, and at x = 2, A leads by three. The shared curve cancels again. Both groups bend in the same places, even as the distance between them changes.



The third panel illustrates a further extension. Replace ugx with a group-specific function hg(x) and shrink each deviation toward zero. Now one group can have a peak its neighbors lack. These panels are illustrations. The first simulation in Part II generated straight lines, so a varying slope already gives each group the freedom it needs.
Give both models the same group effects
For the rerun, I started with the linear model from Part II. Then I added a single shared neural-network term. The two fits have the same likelihood and the same priors for the group intercepts, group slopes and noise scale.
Linear: μg(x) = αg + βgx
Linear + network: μg(x) = αg + βgx + r(x)
When r(x) is zero, the second equation reduces to the first. I used eight tanh units for r and subtracted each unit’s value at x = 0 so αg still gives the group mean there. The output weights have prior standard deviation 1/√8. This width scaling follows the argument in Neal’s thesis. Centering anchors the intercept, though the shared linear trend and the network can still trade off. The predictions are what I score. Radford Neal, chapter 2
What happened on held-out observations?
I regenerated the original straight-line data with seed 42. Each of ten groups contributed 20 training observations and ten held-out observations, with x drawn uniformly between −2 and 2 and Gaussian noise of standard deviation 0.5. Then I made a paired second dataset by adding 2 sin(1.5x) + 0.5 cos(3x) to every response. That changes the common shape while keeping the group slopes, predictor values, noise draws and train/test split identical.
| Simulated signal | Linear | Linear + network |
|---|---|---|
| Group lines | 0.3123 | 0.3120 |
| Group lines + shared curve | 0.6597 | 0.3163 |
The continuous ranked probability score (CRPS) measures how far each predictive distribution is from the observed outcome, in the units of the response. It rewards forecasts that put their probability near what actually happened. On straight lines the two models both scored 0.31 to two decimal places. Once both could learn the slopes, the large gap from Part II disappeared in this rerun. On the paired curved data, every group supplied information about the same bend. The shared network used those observations to improve predictions across the ten groups.
The new network also uses a different weight scale and anchoring from Part II, so comparing across posts cannot assign an exact fraction of the old gap to the missing slopes. Within the new comparison, the only model change is the shared neural term.
This is one simulated draw, and every test group was represented in training. The small straight-line difference is too little to declare a winner. Predicting an entirely new group would call for a different split.
Download the R script with both Stan models. It generates both datasets and prints the scores and sampling diagnostics. I used four chains per fit with 600 warmup and 600 retained draws per chain. All four fits finished with zero divergences and zero maximum-treedepth hits. Across the held-out predictive means, the largest R̂ was 1.006 or lower.
Which differences should the model learn?
If I expect one clinic to start higher than another, I give them varying intercepts. If I expect their response to increase at different rates, I give them varying slopes. If one clinic might peak at a dose where another keeps climbing, I consider allowing group-specific curvature. That last step requires data across doses within each clinic. For the question I started with, one shared nonlinear term and ten partially pooled slopes were enough.
Earlier in this series · Part I, hierarchical models and neural-network ensembles · Part II, a shared network with group intercepts.