Fit the hedge ratio of Bank Nifty on Nifty over the previous sixty sessions and read it on the last day of February 2026. It is close to zero. Read the same fit on 31 Jul 2026 and it is above one and a half. Same indices, same method: from almost no hedge to more than one and a half for one. A fixed ratio is too stiff for that, and a rolling one is too jumpy. The Kalman filter is the third answer: let the ratio drift, and let each day's surprise decide how far. This lesson builds the filter in plain steps, shows what it does to the Nifty and Bank Nifty ratio on a year of dated closes, and asks whether a moving ratio finds a spread worth trading or only makes an old spread look calm. Dated NSE data, nothing live, nothing a view.
3By the end you can
- Build the predict, forecast, compare and update loop of a Kalman hedge ratio and read its one dial, q.
- Explain why a more mobile ratio makes the leftover spread look more mean reverting without proving it is.
- Judge a filtered spread by a fixed rule on later days, with lots and costs in the picture.

The idea, plainly
The problem a fixed ratio hides
The cointegration toolkit fitted one ratio on a formation window and froze it. If the relationship between the two prices moves, the frozen ratio goes stale and the spread drifts for a reason that is not mean reversion. The usual patch is to refit on a rolling window. That swaps one choice for two: the window length, and the fact that every day's refit is a fresh draw from a short sample. The ratio then jumps about, and a pair that needs a stable hedge gets an unstable one.
The filter in plain words
The Kalman filter treats the ratio as a hidden number that follows a random walk: the next day's ratio is the last one plus a small unknown step. Each day it does the same four things.
- Predict. The ratio is what it was, and the doubt about it grows a little.
- Forecast. Use the predicted ratio to say where the Bank Nifty should close given the Nifty.
- Compare. The gap between the forecast and the actual close is the innovation, the day's surprise.
- Update. Move the ratio by a share of the surprise. That share is the gain: large when the doubt about the ratio is large next to the noise in the data, small otherwise.
Predict, forecast, compare, update: the ratio moves by a share of each surprise
The maths: the recursion
Let y be the log of Bank Nifty and x the log of Nifty. The state b holds the slope and the intercept, h is the row of x and one.
b(t) = b(t-1) + w
y(t) = h(t) b(t) + v
P = P + Vw
e(t) = y(t)-h b
Q = h P h' + Ve
K = P h' / Q
b = b + K e(t)
P = P-K h P
The noise in w is Vw and in v is Ve. I set Vw to q times Ve, so one number, q, decides how freely the ratio moves. A very small q gives a filter that behaves like a regression over everything seen so far. A large q gives one that chases yesterday's close. The standardised innovation is e divided by the square root of Q, and if the model were right it would have unit variance and no pattern.
Two things the filter does not do
It does not create mean reversion. The innovation is the part of today's close that the ratio did not predict, and the filter updates the ratio from it. Raise q and the ratio absorbs more of every deviation, so the leftover looks calmer and less persistent. That looks like reversion and is partly a property of the method.
Read 2 more paragraphs, 134 words
It does not choose q either. The dial sets how stiff the ratio is, and no part of the data says what the right stiffness is. If I pick q to make the spread look best on the same days I will trade, I have fitted the dial to the answer.
And it does not know a break from noise. If the link between the two indices changes for good, because index weights moved or a rule changed, the filter adapts a few days later and the old spread vanishes without a mark. A frozen ratio would have shown the break as a spread that never returns. That makes the filter a smoother, not a detector, and a trader who wants to know that a relationship has gone has to look at something else.
q is a window length in disguise
A rolling regression forgets everything older than its window all at once. The filter forgets gradually: each new day counts a little more than the one before it. A larger q means a shorter memory, a smaller q a longer one. So the dial does the job that the window length does in a rolling fit, with smoother edges. That is useful, and it is also why q needs the same discipline: pick it on one stretch of data and test on another, and never on the days that will judge the rule.
Appendix: one update by hand
Take a ratio of one, a doubt about it of one hundredth, noise in the close of four hundredths, and a regressor of one. The doubt over the doubt plus the noise gives a gain of one fifth. If the close comes in a quarter above the forecast, the innovation is a quarter, the ratio moves up by a twentieth to one and five hundredths, and the doubt falls from one hundredth to eight thousandths. The next morning the filter adds a little doubt back, since the ratio is allowed to wander, and the cycle repeats. The quiz works the same numbers.
Worked example: Bank Nifty on Nifty, a year of closes

Which number is the hedge ratio
The file holds three hundred daily closes of both indices from 1 Jul 2025 to 15 Sep 2026. The chart lines up the estimates a trader could have held. The filter starts from a fit to the first sixty sessions, and I judge everything from 25 Sep 2025 on.
Four ways to fit the ratio, four different answers
Read one more paragraph, 79 words
The whole file gives about three quarters, the first sixty sessions about one, and a rolling fit anything from nothing to almost two. The filter stayed in a narrow band around one, and moved slowly inside it. Its ratio was above one from 12 Mar 2026 and drifted down after late Jul. That is the case for the filter: it shows the relationship moving without the jumps. It is not yet a case that the movement is worth following.
One dial, two readings
Now turn the dial. At the smallest settings the filter behaves like a regression over everything it has seen, and its innovations are strongly persistent: the spread wanders. At the largest the ratio chases every close, and the leftover forgets most of yesterday.
The more the ratio moves, the less the leftover remembers
Read one more paragraph, 47 words
This is the trap in one picture. Across the six settings the leftover goes from a spread that barely forgets to one that forgets most of yesterday. Nothing about the Nifty and Bank Nifty changed. Only the dial did. A lower bar is not a better spread.
The test that decides
So I ran the trade as a test on past data. Each day the rule was long the leftover when it stood low against its recent size and short when it stood high, up to a fixed limit, hedged with that day's ratio, and it collected the next day's change in the spread. The same rule ran for each setting, and for the frozen and rolling fits for comparison.
No setting of the dial made money, and the fixed ratios did no better
Read one more paragraph, 97 words
Every bar is below zero before costs, and none is far enough from zero to mean anything: the t statistics run from about minus one and a half to zero, for a daily noise of about fifty to seventy basis points on the filtered rules. The frozen and rolling ratios did about as well. The filter did not find a trade in this pair. It found a way to describe the pair more smoothly. I would take that as the honest output of the method here, and I would not tune q until a bar turned positive.
What a changing ratio means in lots
The filter's ratio moved by less than a fifth in any month, and a trader can only hold whole lots. On 10 Jun 2026 one lot of the near Nifty future was worth Rs 15,10,607 and one lot of Bank Nifty Rs 16,56,012, so the lots are close in value. SEBI's circular of 1 Oct 2024 asks for lot sizes that keep the value on the day of review between 15 and 20 lakh rupees, and both lots sit inside that band. A move in the ratio from the lowest to the highest value of the filter's band changes the Bank Nifty needed against a hundred Nifty lots by about twenty lots. Against one lot the same move is a fifth of a lot, which cannot be traded. At small size the ratio is a number on a chart, not a position. Each rebalance also pays the statutory charges, with STT of 0.05% on the sale of a future.
Appendix: why I centre the Nifty series
The log of Nifty sits near ten and barely moves, so the slope and the intercept of the regression are almost interchangeable. The filter then gives a wrong share of each surprise to each of them, and the ratio swings for no reason. Subtracting the average of the first sixty sessions from the log of Nifty makes the intercept the level of Bank Nifty at the average Nifty, and the slope the ratio alone. The forecasts do not change, but the ratio becomes readable.
Where this meets India
The legs are index futures, which expire monthly, settle in cash and need a roll by the last day. Each lot is a fixed number of units, set by NSE and revised from time to time, and an index future moves in ticks that depend on the level of the index. A filter that wants a ratio change of a few hundredths to a sixth in a month has to be sized so that the change is a whole lot, and at that size the costs of changing it count. The same filter on two stock futures adds delivery on the last day, the ban list and the lot mismatch of the stock futures pair lesson. A smooth hedge ratio does not remove any of that.
Two terms to hold on to from 18.14
The common mistake
The claim: A Kalman filter adapts the hedge ratio, so the pair never breaks down.
The filter follows a ratio that moves. It does not make a spread revert, and it can absorb a break so quietly that nothing looks wrong. In this file the setting of the dial changed how calm the leftover looked, from persistent to short lived, while the result of trading it stayed below zero in every case. Before trusting a filtered spread, ask how q was chosen, on which days, what the same rule earns with a fixed ratio, and whether the hedge moves by whole lots.
Key takeaways
- The filter lets the hedge ratio drift and updates it from each surprise.
- One dial, q, sets how freely the ratio moves.
- A freer ratio makes the leftover look more mean reverting, partly by construction.
- No setting of the dial made the Bank Nifty and Nifty rule positive here.
- A ratio that moves by fractions of a lot cannot be followed at small size.
The term this section turns on: q
Before you trade. Derivatives trading carries a high risk of loss. SEBI's study "Profitability of Individual Traders in the Equity Derivatives Segment (FY25 to FY26)" (August 2026) found that 87.7% of individual traders in equity F&O made net losses in FY26, with aggregate net losses of Rs 91,685 crore. Read the SEBI study