On a recent Hackerrank interview I was faced with the following problem:

Given a set of timestamps (format 2019-11-26 11:00) and their corresponding stock prices (single float value), approximate the missing stock prices for a set of timestamps.

I tried solving it with an SVR model at first, but I had to realize that none of the usual data science libraries were available for this test.

How would you solve this problem without data science libraries in a limited amount of time?

I ended up doing the following: Took the nearest available measurements and solved the y = mx + b equation based on them, so my prediction would be the corresponding y value for my x.

This seemed to solve the problem quite well for the given test case. My solution in Python

  • $\begingroup$ The problem with linear regression and the way you fitted your model is that in time series, the error term is often autocorrelated which makes your results misleading. $\endgroup$
    – Victor Ng
    Nov 26 '19 at 13:33
  • $\begingroup$ Ok, so you mean that if I have some outliers that could seriously ruin the accuracy? $\endgroup$
    – VSZM
    Nov 26 '19 at 16:48
  • $\begingroup$ Given that you are forecasting stock prices, it is not unreasonable to assume the residuals are not independent from each other. The current price most likely relies on the price before that, the day before that on the day two days before, etc. etc. One of the fundamental assumptions of linear regression is that there is no autocorrelation within the residuals. The forecast will still be unbiased, but the prediction interval will be higher than if you had fit it using an appropriate time series regression. See Hyndman's text (otexts.com/fpp2/regression-evaluation.html) $\endgroup$
    – Victor Ng
    Nov 26 '19 at 17:08
  • $\begingroup$ Try an autoregressive model on the time series. $\endgroup$
    – Peter
    Nov 26 '19 at 21:51
  • $\begingroup$ @Peter But in this problem I was not allowed to use any libraries, that is where the difficulty lies. $\endgroup$
    – VSZM
    Nov 27 '19 at 11:21

In case you want to perform a simple time-series regression without using any packages such as Numpy etc, you need to write and solve the model yourself. You can either use gradient descent or least squares to solve the model.

Let's look at a least squares solution. A simple model (omitting sibscripts) would look like:

$$ y = \beta_0 + \beta_1 y_{t-1} + u,$$

where $y$ is explained by a lag $y_{t-1}$ and $u$ is the error term (you can also use more lags if you want). You can "combine" this into a matrix with first column equal to one (the intercept) and the second column containing the lagged $y_{t-1}$. In matrix notation, the model looks like:

$$ y = \beta X + u .$$

Now you can solve by:

$$ \hat{\beta}=(X'X)^{-1} X'y.$$

Now it should be relatively easy (but still some work) to solve the problem without using packages such as numpy.

As an alternative to matrix notation and gradient descent, you can also solve a linear regression by other means, e.g. as demonstrated in this post.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.