# What model is best for the price change relationship between two stocks? python data analytics

I am kind of stuck with some stock price change investigation. My brain is about to be over-heated...

What I want to know is, say there are two stocks in a market. Their price change in a minute wise seems to be strongly related. I want to compare the two stocks' price up and down and see whether they are truly related. Also, I want to know if the price of both change simultaneously or one of them follows the other in a certain time interval.

I first tried correlation coefficient, and linear regression. But that's not good for my study because what I am interested in is not the correlation between their prices but the relationship between the two's price change.

In other words, I just want to see if one stock price changes in the same direction as or after the other's price changes. The price in itself is not a matter of interest. In addition, I don't think their prices have a linear relationship because the degree of price change in each minute varies from each other.

Another problem I faced was that even if one's change follows the other, there can be a time delay in the one's responding to the other's change. I could not think of any algorithm to recognize this delay and consider it as one's following the other.

To sum up,

1) Whether the price change of two stocks is strongly related in a minute wise.

2) if related, the change takes place at the same time or one is followed by the other.

3) if one follows the other, calculate the overall time delay for one's responding to the other.

What statistical model is best for the above?

• Why not create two new series that are the change in the respective stocks from minute to minute (ie: $\frac{y_t - y_{t-1}}{y_{t-1}}$) and conduct similar analyses on those series?
– jros
Sep 7, 2021 at 20:26
• Useful search keywords are "differencing," "cross-correlation," and "DTW."
– whuber
Sep 7, 2021 at 20:32
• Hi: Based on what you described, it sounds like you want to investigate whether the log prices of the two stocks are cointegrated. cointegration is a somewhat complex topic but there are a lot of discussions about it on the net. Also, Hamilton's text and Ender's text do a nice job covering the details. My experience is that the theory is interesting and beautiful ( which is part of the reason E and G got the Nobel Prize ) but, in practice, it's tough to apply it because of the instability ( over time ) of the estimated coefficients. Sep 8, 2021 at 1:14
• @whuber Thanks a lot for the search keywords. It's really helpful for a newbie like me. I am learning a lot from articles related to those keywords and getting some ideas how to do with my research. Sep 9, 2021 at 21:24

My thoughts on how possibly to obtain some statistically valid insights.

First, randomly divide the data in half where the first half is used in data analysis to postulate a process and associated model. The second data set is saved an out-of-sample validation of any proposed models for which I would argue should also have some theoretical (as in economics) basis. For example, both stocks may have exposure to unexpected increases in raw materials required in manufacturing.

In the 1st data set, selected a reasonable response time (albeit, positive or negative depending on which stock is leading or lagging or neither) which you will vary to ascertain the strongest response. Also, based on an examination of historical price changes, judgmentally select a magnitude of absolute change (which you will also vary) above which the data point is included in the analysis (otherwise excluded).

For data accepted to the modeling stage, convert positive changes to +1 and negative to -1, respectively. So why delete data? Because stock fluctuations relate to many causes, most of which are not large or necessarily related to, possible changes in a fundamental instrumental variable(s). The latter may impact both stocks (but not necessarily equally or timely) and, in my assessment of the presented question, is a possible driver of change here.

Calculate the correlation of this new data set across a range of values specified above and select the best (and justifiable) results for the verification stage.

Test for significance of obtaining by chance the magnitude of the associated non-zero result for the 2nd database based on the prior developed best practices.

• This procedure you recommend will deceive its user into believing their modeling is much better than it really is. Random partitioning of time-series data does not work as one would expect, because the held-out data are likely to be highly correlated with the training data.
– whuber
Sep 8, 2021 at 16:07
• Agree, increased correlation especially if one is partitioning the time-series data by, say, selecting every other time data value for each partitioned group. However, is there a randomization technique that is less likely subject to this issue? Possibly based on performing selection based on perhaps a more likely independent auxiliary variable. Even so, this is a case where a "too good" result is suspect, and if one gets a result that is weak response, not an issue! Sep 17, 2021 at 20:07
• People have developed ways of holding out entire sections, or "blocks," of data for this purpose as well as for related purposes, such as bootstrapping. For instance, a search of our site for block bootstrap time series turns up some relevant posts. Including "cross validation" in the search might give you some solutions that directly address this issue.
– whuber
Sep 17, 2021 at 21:06