How to do time series regression without scikit and numpy in Python?

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

Topic implementation regression time-series python

Category Data Science


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.