Fit the hedge ratio of Bank Nifty on Nifty over the previous sixty sessions. On 27 Feb 2026 it reads 0.07. On 31 Jul 2026 it reads 1.67. Same two 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 from its two equations, works one update with a pencil on three dated sessions, tunes its one dial on one stretch of data and tests it on the next, 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
- Write the Kalman filter for a hedge ratio as two equations, work one update by hand on dated closes and read its one dial, q.
- Choose q on one stretch of data, judge a rule on a later one, and say why a smoothed ratio cannot be used in a test.
- Judge a filtered spread by a fixed rule on later days, with lots, statutory charges and the Indian contract rules 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.
Read one more paragraph, 103 words
The word ratio also hides two different numbers. Regress the log of Bank Nifty on the log of Nifty and the slope tells you what counts as the spread: on the whole file of 300 closes it is 0.74. Regress the daily returns instead and the slope tells you how much of one index's daily move the other offsets: on the same file it is 1.13. They answer different questions and they do not agree. The first defines a spread to trade. The second sizes a hedge. I build the filter on the first and then check it on the second.
Two equations: what we see and what we cannot
Everything the filter does follows from two lines. The first says what we observe. The second says how the thing we cannot observe moves.
y(t) = h(t) b(t) + v(t)
b(t) = b(t-1) + w(t)
v ~ N(0, Ve), w ~ N(0, Vw)
Read 2 more paragraphs, 164 words
Here y is the log of the Bank Nifty close. The row h holds the log of Nifty and a one, and the hidden state b holds the slope and the intercept. The noise v is the part of the close the line cannot explain, with variance Ve. The noise w is the step the state may take in one day, with variance Vw. Both are normal and independent of each other. The state follows a random walk because I have no view on which way the ratio will go, only that the next day's is near this day's. That is untrue in detail, since a ratio does not wander forever, and useful as a local description.
What I want each day is a belief about b after the close: a best guess and a variance P. A normal belief stays normal after a normal observation, so those two numbers carry the whole belief. The filter keeps no window of data, only yesterday's belief.
The filter in plain words
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.
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 first line adds the doubt the random walk brings in a day. The next two are the forecast error and its variance: doubt about the state carried through h, plus the noise in the close. The gain K is the doubt that belongs to each state element, divided by the total. The last two lines move the state by a share of the surprise and take the information out of the doubt. I set Vw to q times Ve, so one number, q, decides how freely the ratio moves. 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.
The maths: where the gain comes from
Before the close, the belief about b is normal with mean b and variance P. The close is a line in b plus noise, so b and y are jointly normal. For jointly normal numbers the best guess of one given the other is a regression on the surprise:
E[b, y] = b + C (y - h b)
C = Cov(b, y) / Var(y)
Cov(b, y) = P h'
Var(y) = h P h' + Ve
The regression slope C is the gain K, and the variance left over is P minus K h P. Nothing else is in the filter. Set Vw to zero and the recursion is recursive least squares: it reproduces the ordinary regression on everything seen so far, one day at a time, with each new day counting the same as the first. A Vw above zero is the only thing that lets the filter forget.
The simplest case: how fast does it forget?
Strip the regression down to a single number that drifts, so h is one. The gain then settles to a steady value, and that value is set by q alone. In units of Ve, the doubt after an update plus the step q must equal the doubt before it. That gives K squared over one minus K equal to q.
K^2 + q K - q = 0
K = (sqrt(q^2 + 4q) - q) / 2
window = 2 / K - 1
A q of one hundredth remembers about twenty sessions
Read 2 more paragraphs, 119 words
A steady gain K means each day's close gets a weight K and older days fade by one minus K each day. That is an exponentially weighted average, and it matches a plain average over about two over K minus one sessions. So q has a plain reading. A small q is a long memory and a large q a short one.
A q of 0.01 gives a gain of 0.095 and a window of 20 sessions, close to a rolling month. A q of 0.001 gives about 63, close to the sixty session fit people reach for by habit. The filter has no window length, and in its steady state it behaves as though it had one.
Choosing the two noise variances
Two numbers decide the filter, and the data supply neither. Ve is the noise in the close. I take it from an ordinary fit on the formation window, the first 60 sessions, whose residual standard deviation is 0.0145 in log terms. Vw is the freedom of the ratio. I write it as q times Ve and tune q, in one of three honest ways.
- Likelihood. Every day the filter forecasts the close and states how wrong it expects to be, with variance Q. It can therefore score itself on a stretch of data, and I pick the q that scores highest.
- EM. Alternate between a smoother that looks back over the stretch and a re-estimate of both variances from it, until they stop moving. It is the same likelihood, searched more cleverly, and it can learn Ve and Vw together.
- Diagnostics. Look at the standardised innovations. If the model were right they would have unit variance and no autocorrelation. Neither is a score to maximise. Both are ways to see that the model is wrong.
log L = -1/2 sum over days
of [ ln(2 pi Q) + e^2/Q ]
Read one more paragraph, 56 words
The rule I keep is three stretches that never touch. The first 60 sessions initialise the filter. The next 90 are where q is tuned. The last 150 judge the rule, and nothing learned from them is allowed back into the first two. A dial fitted to the days that judge it has seen the answer.
Scale decides what q means
One thing about q is easy to miss: it is not a pure number. It compares the step of the state with the noise of the data, and the state here is the slope. A step in the slope changes the forecast by x times the step. If the log of Nifty moves only a few hundredths over the stretch, the data barely notice the step, and the surprise is handed to the intercept, the only thing that can soak it up.
The term this section turns on: q
Read one more paragraph, 99 words
So two scalings give two different filters. I subtract the mean of the log of Nifty over the first sixty sessions and divide by its standard deviation, 0.0125. The slope inside the filter is then the ratio times that number, and I divide it back to read the ratio. With the log of Nifty only centred, a q of 0.01 kept the ratio between 1.007 and 1.039 for a year. Standardised, the same q gave a ratio from 0.55 to 1.85. Nothing else changed. Any quoted q is meaningless without the scale of the regressor behind it.
Three 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.
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.
Read 3 more paragraphs, 187 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 good 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.
A rolling regression forgets everything older than its window all at once. The filter forgets gradually, so q does the job of the window length with smoother edges. It needs the same discipline: pick it on one stretch of data and test on another, never on the days that will judge the rule.
Worked example: Bank Nifty on Nifty, a year of closes
Which number is the hedge ratio
The file holds 300 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, 114 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 did not stay in a narrow band. Its ratio went as low as 0.55 on 1 Jul 2026 and as high as 1.85 on 8 May 2026, and in one stretch of twenty one sessions it moved by 1.04. I expected a smoother line than that. The reason is the regression itself. The two indices share most of their weight, so their logs rise and fall together, and the slope is decided by small differences between two large moves. A filter that follows the slope follows those differences.
One update by hand, on three dated sessions
For the hand work I use the returns version, which is the one that sizes a hedge. The observation y is the Bank Nifty's percentage move in a session, x is the Nifty's, and the single hidden state is the ratio. The inputs are model inputs, set near the values of a fit on the first sixty sessions: a ratio of 0.95, a doubt about it of 0.005, noise in a session's Bank Nifty move of 0.08 in squared percent, and a step variance of 0.003 a day. The moves are from the dated closes of 28 Aug to 2 Sep 2026.
| Session | Nifty move, % | Bank Nifty move, % | Forecast, % | Innovation, % | Gain | Ratio after |
|---|---|---|---|---|---|---|
| 31 Aug 2026 | −0.394 | 0.919 | −0.374 | 1.294 | −0.039 | 0.900 |
| 1 Sep 2026 | −0.102 | −1.060 | −0.092 | −0.969 | −0.014 | 0.913 |
| 2 Sep 2026 | −0.588 | −0.414 | −0.537 | 0.123 | −0.096 | 0.901 |
Read 2 more paragraphs, 210 words
Walk the first row. The doubt grows from 0.005 to 0.008 with the day's step. The forecast for Bank Nifty is the ratio times the Nifty move, −0.374. Bank Nifty actually rose 0.919 while Nifty fell, so the innovation is 1.294. Its variance Q is the Nifty move squared times the doubt, plus the noise, which is 0.0812. The gain is the doubt times the Nifty move over Q, −0.039, and the ratio moves by the gain times the innovation to 0.900.
Look at the size of that move. The innovation is about 4.6 noise standard deviations, a day I would circle on a chart, and the ratio shifts by only 0.05. The reason is the gain. The Nifty barely moved, so the day says almost nothing about the slope, and the filter correctly refuses to read much into it. The second session is a small correction, and on the third, with a larger Nifty move, the gain is larger and the innovation is small, so the ratio hardly changes. Three sessions took the ratio from 0.95 to 0.901. The doubt about it rose from 0.005 to 0.0131, because each day adds a step and the updates took back less than that.
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 2 more paragraphs, 166 words
This is the trap in one picture. Across the six settings the leftover goes from a spread that barely forgets to one with no memory at all. Nothing about the Nifty and Bank Nifty changed. Only the dial did. A lower bar is not a better spread.
The standardised innovations say more. If the filter described the data, they would have a standard deviation of one. At q of 0.0001 they come close, at 1.00, and they are almost as persistent as the raw leftover, with an autocorrelation of 0.94. At q of 0.01 their standard deviation is 0.43, and at q of one it is 0.10: the filter claims an error bar ten times wider than the one it actually makes, because the free ratio has already swallowed the surprise. Whiteness and unit variance never arrive together, and a model that fits would deliver both. I read that as the random walk ratio being a convenient description and not the truth.
Tuning q on one stretch
So how do I pick a q without peeking? Score each setting by the likelihood of its forecasts on the tuning stretch, 25 Sep 2025 to 4 Feb 2026, ninety sessions.
On the tuning sessions, a q of one hundredth scores highest
Read one more paragraph, 89 words
The score peaks at q of 0.01. It falls by 13 one step below and by 18 one step above. That is a sharp preference, and it is the setting I carry to the judging stretch without looking at it. Two cautions. The peak is the best of six on a short stretch, and I have not tested it on any other. And a high likelihood tells me which q forecasts the next close best, which is not the same as which q finds a spread that pays.
The test that decides
So I ran the trade as a test on the later 150 sessions, from 5 Feb to 15 Sep 2026, with nothing from them used before. The rule is fixed and simple. At each close the leftover is the day's innovation. I divide it by its standard deviation over the last twenty sessions. The position is minus that number over two, held between minus one and plus one, so a large positive leftover means short the Bank Nifty against the Nifty, a large negative one means long. The hedge is that day's ratio. The profit is the next day's change in the spread, the Bank Nifty's log move less the ratio times the Nifty's. The same rule ran for each setting, and for the frozen first sixty session ratio and the rolling sixty session ratio for comparison. Charges are the statutory ones on the futures legs, set out below.
After statutory charges no setting of the dial left a result apart from zero
Read 2 more paragraphs, 178 words
Before charges the means run from about 0.78 to 3.06 basis points a day, the largest at q of one. The t statistic of that largest mean is 1.41, short of two. After charges every setting is within 1.4 basis points of zero. At q of 0.01, the setting the likelihood chose, the mean is 1.82 basis points before charges, a t of 0.76, and −0.07 after. The frozen ratio and the rolling one do about as well. The filter did not find a trade in this pair. It found a way to describe the pair more smoothly, and I would not tune q until a bar turned positive.
In rupees: one Bank Nifty lot was worth Rs 16,56,012 at the future of 10 Jun 2026, so 1.82 basis points before charges is Rs 301 a day on one lot, and the statutory charges at q of 0.01 took 1.89 basis points, Rs 313 a day. A freer ratio trades more, so the charges grow with q while the gross result does not keep pace.
The same rule with a smoothed ratio
There is a way to make this rule look wonderful, and it is worth seeing once. The filter has a second form, the smoother, which re-estimates yesterday's ratio using the closes that came after it. The Rauch, Tung and Striebel smoother does it with one backward pass. The smoothed ratio describes what the relationship was. A trader could not have held it, because at each date it uses closes that had not happened. Run the same rule with the smoothed leftover and the result changes:
Smoothing with future closes turns a null result into a t of six
Read one more paragraph, 93 words
At q of 0.01 the filtered rule has a t of 0.76. The smoothed rule has a t of 6.28 and a mean of 13.43 basis points a day, which would be Rs 2,224 a day on one Bank Nifty lot. Same data, same rule. Nothing in the pair changed. The ratio at each date simply knew the future. Any filtered spread in a backtest has to be the filter, not the smoother, and a published chart of a hedge ratio with no word on which one it is deserves a question.
What the hedger gets
The test above asks whether the spread pays. A hedger asks something smaller: how much does a Bank Nifty position still move after the Nifty future is sold against it? That needs the returns ratio. For each approach I took the ratio known the evening before, applied it to the next day's Nifty move and measured what was left, over the same judging stretch.
A good filter and a good rolling fit hedge about equally well
Read one more paragraph, 148 words
An unhedged Bank Nifty position moved 126.7 basis points a day. A frozen first sixty session ratio left 56.3. A sixty session rolling fit left 52.6. The filter left 51.2 at its best setting, which is within a hair of the 50.8 that the hindsight ratio would have left. At the largest q it left 66.7, worse than not adapting at all, because the ratio chases noise. On a Bank Nifty book of Rs 8.28 crore, the gap between the frozen ratio and the best filter is Rs 41,885 in one standard deviation of the daily remainder. That is real money for a large book, and small beside what a hedge removes: the filter's remainder is 16% of the variance of the unhedged position. Most of the value is in hedging at all. The choice among sensible ratios moves the remainder by about a tenth.
A break the filter would absorb
Model, not market data. Suppose the true ratio jumps by 0.4 one day and stays there, as it might after an index reconstitution, with no other noise. The filter closes the gap by a fixed share each day, so after n sessions the gap left is the jump times one minus K to the power n. A rolling window closes it in a straight line over its length.
A q of one hundredth takes about a month to notice a new ratio
Read one more paragraph, 92 words
While it is absorbing, the spread looks normal, and the hedge is wrong by the gap that remains. Take a one lot Nifty hedge and a Nifty move of one percent. With a gap of 0.4 in the ratio, the hedge is off by 6,042 rupees on that day, and it stays off by a shrinking share for the weeks the filter takes. A frozen ratio would have been wrong by the full amount from day one, and would at least have shown the break as a spread that never came back.
What a changing ratio means in lots
The ratio moves by fractions, 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. SEBI's circular of 1 Oct 2024 asks for an index derivative contract value of Rs 15 lakh to Rs 20 lakh on the day of review, for new contracts after 20 Nov 2024, and both lots sit in that band. The returns ratio of the filter at its better setting ranged from 0.86 to 1.40, and in its sharpest month it moved by 0.50. For a book of 50 Bank Nifty lots that is about 27 Nifty lots to trade, worth Rs 4.14 crore. If they are sales, STT at 0.05% is Rs 20,700 for the month's rebalance, before exchange charges and the spread. On a one lot book the same move is half a lot, which cannot be traded, and a book of five lots can change its hedge only in steps of 0.18. At small size the ratio is a number on a chart, not a position.
Where this meets India
The legs are index futures. NSE's contract specifications page, updated on 11 Aug 2026, lists three consecutive monthly expiries for the Nifty 50 and Nifty Bank futures. Monthly expiry is the last Tuesday of the month: NSE's circular NSE/FAOP/68747 of 25 Jun 2025 applied it to contracts expiring on or after 1 Sep 2025, following SEBI's circular SEBI/HO/MRD/MRD-TPD-1/P/CIR/2025/76 of 26 May 2025, which limits expiry to Tuesday or Thursday and puts monthly futures in the last week of the month (clause 3.1 and clause 3.3). Both legs of this pair roll on the same day. The tick differs: NSE's circular NSE/FAOP/67135 of 13 Mar 2025 ties the tick of an index future to the index level, reviewed monthly, at Rs 0.10 for an index above 15,000 and up to 30,000 and Rs 0.20 above that. Nifty sat in the first band and Bank Nifty in the second.

Read one more paragraph, 157 words
From 7 Dec 2026, SEBI's circular HO/47/11/11(3)2025-MRD-POD2/I/23843/2026 of 15 Oct 2026 ends trading in expiring index contracts at 3:30 pm and settles them at the volume weighted prices of the last thirty minutes of the cash session (paragraph 4.2.4 and paragraph 4.9.1). A filter that updates on closes has to be told which close: the index close, the last traded price of the future and the settlement price can all differ on an expiry day, and a ratio fitted on one and applied to another leaks. Rebalancing costs are in the cost of an F&O trade: from 1 Apr 2026 STT on the sale of a future is 0.05% (NSE/FATAX/73524 of 31 Mar 2026), and the exchange transaction charge was revised from 1 Mar 2026 (NSE/FA/73061 of 27 Feb 2026). On two stock futures the filter adds delivery on the last day, the ban list and the lot mismatch of the stock futures pair lesson.
What the research says
Kalman published the recursion in 1960 for filtering and predicting a signal seen through noise. In finance the first question was whether a stock's market beta is constant. Bos and Newbold (1984) asked whether the slope of the market model follows a random process, which is the choice made here. Faff, Hillier and Hillier (2000) compared three ways of modelling a time varying beta on daily UK industry returns and found that a Kalman filter with a random walk beta had the lowest mean square error of the three. For India, Das and Barai (2015) estimated industry betas with a regression, the Kalman filter and other approaches, found a dynamic beta, and reported the lower forecasting error for the filter in almost all industries.
Read one more paragraph, 147 words
For pairs, Elliott, Van der Hoek and Malcolm (2005) put the spread itself into state space form, with a mean reverting hidden state seen through noise, which is a different use of the filter from the hidden hedge ratio here. Krauss (2017) surveys the pairs literature in five groups and reviews profitability in light of market frictions. On method, Shumway and Stoffer (1982) set out the EM route to the variances, Mehra (1970) the older problem of identifying them, Rauch, Tung and Striebel (1965) the smoother, and Ljung and Box (1978) the test of whether innovations are white. None of these shows that a filtered hedge ratio makes money in index futures. They show that betas move and that a random walk state tracks them; the choices around it are where the results come from.
Stretch: what to build next
Three extensions follow. Give the state a pull back to a mean, as in the pairs paper above, so the spread is the hidden thing. Estimate Ve and Vw by EM and compare with the likelihood grid. Add a second regressor, such as a sector index. Whatever you build, walk forward testing and backtest overfitting apply to it, and PCA statistical arbitrage carries the idea to a basket. The noise in a daily close is the subject of microstructure models, and the cost of trading the lots is in optimal execution.

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 white noise, while the result of trading it stayed within noise before charges and at zero after them. Before trusting a filtered spread, ask how q was chosen, on which days, what the same rule earns with a fixed ratio, which form of the filter was used, and whether the hedge moves by whole lots.
- Picking q after the results. Of seven settings the highest mean before charges was q of one: 3.06 basis points a day, Rs 506 on one Bank Nifty lot, with a t of 1.41. Chosen after looking, it is a selection and not a finding. After charges it is 0.28 basis points, Rs 46.
- Backtesting with the smoothed ratio. At q of 0.01 that gave Rs 2,224 a day on one lot and a t of 6.28. The honest filter gave Rs 301 and a t of 0.76. The whole difference is information from the future.
- Reading a calm leftover as a reverting spread. The lag one autocorrelation fell from 0.95 to −0.01 as q rose, with the same indices on the same days. A calmer series is the filter absorbing more.
- Hedging in fractions. A monthly ratio move of 0.50 on 50 Bank Nifty lots is 27 Nifty lots and Rs 20,700 of STT on the sales. On one lot it is half a lot, which cannot be traded.
- Trusting it to flag a break. At q of 0.01 the filter takes about 23 sessions to absorb nine tenths of a jump in the true ratio, and the spread looks normal on the way. On a one lot Nifty hedge, a one percent day with the ratio off by 0.4 costs Rs 6,042.
Two terms to hold on to from 18.14
What I'd keep from this
- The filter lets the hedge ratio drift and updates it from each surprise.
- One dial, q, sets how freely the ratio moves, given the regressor scale.
- A freer ratio makes the leftover look more mean reverting, partly by construction.
- Tune q on one stretch and judge on the next; never use the smoothed ratio.
- After statutory charges no setting of q earned more than noise on this pair.
- A ratio that moves by fractions of a lot cannot be followed at small size.
Where this section sits in module 18.14
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