10.1 – Who is X and who is Y?

I hope the previous chapter gave you a basic understanding of linear regression and how one can conduct the linear regression operation on two sets of data, on MS Excel. Remember, we are talking about two variables here – X and Y.

X is defined as the independent variable and Y is the dependent variable. If you’ve spent time thinking about this, then I’m certain you’d have guessed X and Y will eventually be two different stocks.

In fact, let us just go ahead and run a linear regression on two stocks – maybe HDFC Bank and ICICI Bank and see what results we get.

I’m setting ICICI Bank as X and HDFC Bank as Y. A quick note on data before we proceed –

Make sure your data is clean – adjusted for splits, bonuses, and any other corporate actions

Make sure the data matches the exact dates – for instance, the data I have for both the stocks here runs from 4th of Dec 2015 to 4th Dec 2017.

Here is how the data looks –

I’ll run the linear regression on these two stocks (I’ve explained how to do this in the previous chapter), also do note, I’m running this on the stock prices and not really on stock returns –

The result of the linear regression is as follows –

Since ICICI is independent and HDFC is dependent, the equation is –

HDFC = Price of ICICI * 7.613 – 663.677

I’m assuming, you are familiar with the above equation. For those who are not familiar, I’d suggest you to read the previous two chapters. However here is the quick summary – the equation is trying to predict the price of HDFC using the price of ICICI.

Or in other words, we are trying to ‘express’ the price of HDFC in terms of ICICI.

Now, let us reverse this – I will set ICICI as dependent and HDFC as the independent.

Here is how the results look –

The equation is –

ICICI = HDFC * 0.09 + 142.4677

So for the given two stocks, you can regress two ways by reordering which stock is dependent and which one is the independent variable.

However, the question is – how do you decide which one should be marked dependent and which one as independent. Or in other words, which order makes the most sense.

The answer to this depends on three things –

Standard Error

Standard Error of intercept

The ratio of the above two variables.

Remember, the linear equation above, essentially express the variation of price of ICICI in terms of HDFC (refer to the equation above). This expression or explanation of the price variation of one stock by keeping the price of the other stock as a reference can never be 100%. If it was 100%, then there is no play here at all.

Having said so, the equation should be strong enough to explain the variation in price of the dependent variable as much as possible, keeping the independent variable in perspective. The stronger this is, the better it is.

This leads us to the next obvious question – how do we figure out how strong the linear regression equation is? This is where the ratio –

Standard Error of Intercept / Standard Errorcomes into play. To understand this ratio, we need to understand both the numerator and the denominator before talking about the ratio itself.

10.2 – Back to residuals

Here is the linear regression equation of ICICI as independent and HDFC as the dependent –

HDFC = Price of ICICI * 7.613 – 663.677

This essentially means, if I know the price of ICICI, I should be able to predict the price of HDFC. However, in reality, there is a difference between the predicted price of HDFC and the actual price. This difference is called the ‘Residuals’.

Here is the snapshot of the residuals when we try and explain the price of HDFC keeping ICICI as the independent variable –

When I talk about the regression equation and the residuals, usually, I get one common question – what is the use of regression if there is a residual each and every time? Or in other words, how can we rely on an equation, which fails to predict accurately, even once.

This is a fair question. If you look at the residuals above, they vary from a low of -288 to a high of 548, so using this equation to make any sort of prediction one price is futile.

But then, this was never about predicting the price of the dependent stock, given the price of an independent stock. It was always about the residuals!

Let me give you a heads-up here – the residuals display a certain behaviour. If we can understand this behaviour and figure a pattern within it, then we can rework backwards to construct a trade. This trade obviously involves buying and selling the two stocks simultaneously, hence this qualifies as a pair trade.

Over the next few chapter, we will dwell deeper into this. However, for now, let’s talk about the ‘Standard Error’, the denominator in the Standard Error of Intercept / Standard Errorequation.

The standard error is one of the variables which gets reported when you run a linear regression operation. Here is the snapshot showing the same –

The standard error is defined as the standard deviation of the residuals. Remember, the residuals itself is a time series array. So if you were to calculate the standard deviation of the residuals, then you get the standard error.

In fact, let me manually calculate the standard error of the residuals, I’m doing this for X = ICICI and y = HDFC

And excel tells me the standard deviation is 152.665. The standard error as reported in the summary output is 152.819. The minor difference can be ignored.

The ‘Standard Error of the Intercept’, is a little tricky. It does get reported in the regression report, and here is the standard error of the intercept with x = ICICI and y = HDFC

Recall, the regression equation –

y=M*x+ C

Where,

M = Slope

C = Intercept

If you realize, here both M and C are estimates. And how are they estimated? They are estimated based on the historical data provided to the regression algorithm. The data can obviously contain noise components and few outliers. This implies that there is a scope for the estimates can go wrong.

The Standard Error of the Intercept is the measure of the variance of estimated intercept. It helps up understand by what degree the intercept itself can vary. So in a sense, this is somewhat similar to the ‘Standard Error’ itself. To summarize –

Standard Error of Intercept – The variance of the intercept

Standard Error – The variance of the residuals.

Now that we have defined both these variables, let’s bring back the ‘Error Ratio’. Please note, the term ‘Error Ratio’ is not a standard term, I’ve come up with it for ease of understanding.

Anyway, the error ratio, as we know –

Error Ratio = Standard Error of Intercept / Standard Error

I’m calculated the same for –

ICICI as X and HDFC as y = 0.401

HDFC as X and ICICI as y = 0.227

The decision to designate X and Y to stocks depends on the value of the error ratio. The lower the better. Since HDFC as X and ICICI as y offers the lowest error ratio, we will designate HDFC as the independent variable (X) and ICICI as the dependent variable (Y).

I’d love to explain the reason as to why we are using the error ratio as the key input for designating X and Y, but I guess I will hold back. I’ll revisit this again when I take up pair trade example.

For now, remember to calculate the error ratio and estimate which stock should be dependent and which one will be the independent.

Kite 3 platform has the function to find correlation coefficient ratio between two stocks. For eg. correlation between Banknifty & Yesbank in Excel function Correl() returns a value of .38, Kite 3 shows the ratio as .83. Why the difference?

In Kite 3, in “Chart”, under “Studies” menu, i selected “Correlation Coefficient” to find for stocks between banknifty and yesbank. In Kite 3, its showing all the same as .83 for 3 months, 6 months, 1 year. Strange. In excel, correl() functions, its .38 for 1 year and -.02 for 6 months. How the answers are coming different?

Sir I think I’ve read this mean reverting strategy in a website called Quantopian but I could never understand it before now. Thank you. My question is, do you think its possible to create new mean reverting strategies if one has a firm understanding of statistical methods. You think a retail investor can do that?

Yes, you certainly can. These are not latency critical strategies, meaning, the speed of information does not matter. What matters is the statistical/ quantitative technique. As long as you have a firm understanding of this – there are always opportunities in the market.

Sir,
Can we keep the simple method based on correlation and cointegration only? This chapters going to much deeper now.
First i find 80% or more corelated stocks pair for last 63days (one quarter) .then check cointegration, in which pair cointegration less then 0.5 for last one year..i simple create the trade and its working fine. My favorite pairs are banknifty/nifty, acc/ambuja and tatamotors/tatamtrdvr…
I.cant understand why u taking us in too deep…

Akash, trust me, the method we are discussing here is a unique way to pair trade. You will not find this explanation anywhere else. Learn it, and once you have all the information you can decide to adopt or ignore or even improvise the technique.

Nice to read all your modules
V lucid and easy to understand
illustrations . Great
I have a small query if two stocks are strongly corelated what is the statistical measure to find the magnitude of price movement .
Thanks in advance

Dear team,
we read yours all chapters is it very good information to us.
but 1 suggestion for zerodha team.
Kindly provide its all information in hindi also. bcoz some people are not too much qualified.
& thats why this all information you are provide also with beautiful examples but people cant understand due to language problem.

nice information good keep it up & updating new information for share market.

Karthick, the latest move by SEBI on the clampdown on retail traders trading in F&O limiting them to ITR limit is a dampner to what we are learning here. Could you give your perspective, if this move by SEBI will limit retail traders turnover in F&O ?

Deepu, as you will soon realize, when you enter the pair trade, all you need to do is track the residuals and not really the individual scrips. I’m working on the next chapter, should be ready in a couple of days.

Yes sir I see what you are saying. But I’m asking about the initial selection of stocks to check for correlation. If the stocks are to be positively correlated, we look for stocks in same industry that have similar businesses. I’m asking, what factors would cause negative correlation between two stocks?

As usual lucidly explained and waiting eagerly for the next chapter. I have one question, though. Could this system be applied to commodity pair as well as currency pair? Do let me know your opinion on it.

Kishan, great questions. You will understand this better over the next 2 chapters, but let me try and give you a quick answer. The ratio is basically –

Std Error on intercept / Std Error of Residuals.

If the linear regression equation is to be relied upon, then the standard error of the intercept has to be low. Also, if the trade has to have a decent target and SL range, then the standard error of the residuals should be large (but also adhere to the principles of stationarity, which is explained in the next chapter). This also implies the ratio itself should be low.

Hello sir
Whenever two time series data are rgresseed we get bunch of o/p. But whether the generated o/p are useful depends on the stationarity of the residual data.
Then what is the essence of ‘co-integration’? Is there any parameter(as in correlation) that defines the strength or weakness of co-integration b/w two securities?
Thank you
Varsity student

Suppose the stock is trading in the range of 100 – 120 for over the last 1 year…now today there is 1:2 split, then the stock price drops by half. You can go back in history and correct the last 1 year data to reflect this effect of the split. So the range becomes 50-60 instead of 100 – 120. This is essentially cleaning up the data.

Dear Karthik,
“Make sure your data is clean – adjusted for splits, bonuses, and any other corporate actions.”
which website to refer for this, as different websites are showing different data for same stock? like on investing.com,moneycontrol and nseindia.

The Standard Error of the Intercept is the measure of the variance of estimated intercept. The variance is calculated for a series of observations. My question is what is the series whose variance is being calculated to calculate the Standard Error of the Intercept?

Also, note that regression gives one number for the intercept. What does it mean to have a variance for a single number?