# Housing Market: Auto Correlation Analysis

In this post we take a look at the housing market data which consists of all the transactions registered with the UK Land Registry since 1996. So lets get the copyright out of the way:

The data-set from HM Land Registry has information about all registered property transactions in England and Wales. The data-set used for this post has all transactions till the end of October 2018.

To make things slightly simple and to focus on the price paid and number of transaction metrics I have removed most of the columns from the data-set and aggregated (sum) by month and year of the transaction. This gives us roughly 280 observations with the following data:

{ month, year, total price paid, total number of transactions }

Since this is a simple time-series, it is relatively easy to process. Figure 1 shows this series in a graph. Note the periodic nature of the graph. Figure 1: Total Price Paid aggregated (sum) over a month; time on X axis (month/year) and Total Price Paid on Y axis.

The first thing that one can try is auto-correlation analysis to answer the question: Given the data available (till end-October 2018) how similar have the last N months been to other periods in the series? Once we identify the periods of high similarity, we should get a good idea of current market state.

To predict future market state we can use time-series forecasting methods which I will keep for a different post.

## Auto-correlation

Auto-correlation is correlation (Pearson correlation coefficient) of a given sample (A) from a time series against other available samples (B). Both samples are of the same size.

Correlation value lies between 1 and -1. A value of 1 means perfect correlation between two samples where they are directly proportional (when A increases, B also increases). A value of 0 implies no correlation and a value of -1 implies the two samples are inversely proportional (when A increases, B decreases).

The simplest way to explain this is with an example. Assume:

1. monthly data is available from Jan. 1996 to Oct. 2018
2. we choose a sample size of 12 (months)
3. the sample to be compared is the last 12 months (Oct. 2018 – Nov. 2017)
4. value to be correlated is the Total Price Paid (summed by month).

As the sample size is fixed (12 months) we start generating samples from the series:

Sample to be compared: [Oct. 2018 – Nov. 2017]

Sample 1: [Oct. 2018 – Nov. 2017], this should give correlation value of 1 as both the samples are identical.

Sample 2: [Sep. 2018 – Oct. 2017], the correlation value should start to decrease as we skip back one month.

Sample N: [Dec. 1996 – Jan. 1996], this is the earliest period we can correlate against.

Now we present two graphs for different sample sizes:

1. correlation coefficient visualised going back in time, grouped by Year (scatter and box plot per year) – to show yearly spread
2. correlation coefficient visualised going back in time – to show periods of high correlation

Thing to note in all the graphs is that the starting value (right most) is always 1. That is when we compare the selected sample (last 12 months) with the first sample (last 12 months).

In the ‘back in time’ graph we can see the seasonal fluctuations in the correlation. These are between 1 and -1. This tells us that total price paid has a seasonal aspect to it. This makes sense as we see lots of houses for sale in the summer months than winter as most people prefer to move when the weather is nice!

So if we correlate a 12 month period (like this one) one year apart (e.g. Oct. 2018 – Nov. 2017 and Oct. 2017 – Nov. 2016) one should get positive correlation as the variation of Total Price Paid should have the same shape. This is ‘in phase’ correlation. This can be seen in Figure 2 as the ‘first’ correlation which is in phase (in fact it is perfectly in phase and the values are identical – thus the correlation value of 1).

Similarly, if the comparison is made ‘out of phase’ (e.g. Oct. 2018 – Nov. 2017 and Jul 2018 – Aug. 2017) where variations are opposite then negative correlation will be seen. This is the ‘second’ correlation in Figure 2.

This is exactly what we can see in these figures. Sample sizes are 6 months, 12 months, 18 months and 24 months. There are two figures for each sample size. The first figure is the spread of the auto-correlation coefficient for a given year. The second figure is the time series plot of the auto-correlation coefficient, where we move back in time and correlate against the last N months. The correlation values fluctuating between 1 and -1 in a periodic manner.

## Conclusions

Firstly, if we compare the scatter + box plot figures, especially for 12 months (Figure 4a), we find the correlation coefficients are spread around ‘0’ for most of the years. One period where this is not so and the correlation spread is consistently above ‘0’ is the year 2008, the year that marked the start of the financial crisis. The spread is also ‘tight’ which means all the months of that year saw consistent correlation, for the Total Price Paid, against the last 12 months from October 2018.

Secondly conclusion we can draw from the positive correlation between last 12 months (Figure 2b) and the period of the financial crisis is that the variations in the Total Price Paid are similar (weakly correlated) with the time of the financial crisis. This obviously does not guarantee that a new crisis is upon us. But it does mean that the market is slowing down. This is a reasonable conclusion given the double whammy of impending Brexit and on set of winter/Holiday season (which traditionally marks a ‘slow’ time of the year for property transactions).

Code is once again in python and attached below:

`from matplotlib import pyplot as pltfrom pandas import DataFrame as dffrom datetime import datetime as dtfrom matplotlib.dates import YearLocator, MonthLocator, DateFormatterimport pandas as pdimport numpy as npfrom sklearn.cluster import KMeans, MiniBatchKMeans, DBSCANfrom sklearn.mixture import GaussianMixturemonths = MonthLocator(range(1, 13), bymonthday=1, interval=3)year_loc = YearLocator()window_size = 12def is_crisis(year):    if year<2008:        return 0    elif year>2012:        return 2    return 1def is_crisis_start(year):    if year<2008:        return False    elif year>2008:        return False    return Truedef process_timeline(do_plot=False):    col = "Count"    y = []    x = []    x_d = []    box_d = []    year_d = []    year = 0    years_pos = []    crisis_corr = []    for i in range(0, size - window_size):        try:            if year != df_dates["Year"][size-1-i]:                if year > 0:                    box_d.append(year_d)                    years_pos.append(year)                year_d = []                year = df_dates["Year"][size-1-i]            corr = np.corrcoef(df_dates[col][size -i - window_size: size - i].values, current[col].values)            year_d.append(corr[0, 1])            y.append(corr[0, 1])            if is_crisis_start(year):                crisis_corr.append(corr[0, 1])            x.append(year)            month = df_dates["Month"][size - 1 - i]            x_d.append(dt(year, month, 15))        except Exception as e:            print(e)    box_d.append(year_d)    years_pos.append(year)    corr_np = np.array(crisis_corr)    corr_mean = corr_np.mean()    corr_std = corr_np.std()    print("Crisis year correlation: mean and std.: {} / {} ".format(corr_mean, corr_std))    if do_plot:        fig, sp = plt.subplots()        sp.scatter(x, y)        sp.boxplot(box_d, positions=years_pos)        plt.show()        fig, ax = plt.subplots()        ax.plot(x_d, y,'-o')        ax.grid(True)        ax.xaxis.set_major_locator(year_loc)        ax.xaxis.set_minor_locator(months)        plt.show()    return corr_mean, corr_stdcsv = "c:\\ML Stats\\housing_oct_18_no_partial_mnth_cnt_sum.csv"full_csv = "c:\\ML Stats\\housing_oct_18.csv_mnth_cnt_sum.csv"df = pd.read_csv(full_csv)mnth = {    1: "Jan",    2: "Feb",    3: "Mar",    4: "Apr",    5: "May",    6: "Jun",    7: "Jul",    8: "Aug",    9: "Sep",    10: "Oct",    11: "Nov",    12: "Dec"}dates = list(map(lambda r: dt(int(r["Year"]), int(r["Month"]), 15), df.iterrows()))crisis = list(map(lambda r: is_crisis(int(r["Year"])), df.iterrows()))df_dates = pd.DataFrame({"Date": dates, "Count": df.Count, "Sum": df.Sum, "Year": df.Year, "Month": df.Month, "Crisis": crisis})df_dates = df_dates.sort_values(["Date"])df_dates = df_dates.set_index("Date")plt.plot(df_dates["Sum"],'-o')plt.ylim(ymin=0)plt.show()size = len(df_dates["Count"])corr_mean_arr = []corr_std_arr = []corr_rat = []idx = []for i in range(0, size-window_size):    end = size - i    current = df_dates[end-window_size:end]    print("Length of current: {}, window size: {}".format(len(current), window_size))    ret = process_timeline(do_plot=True)    break #Exit early`

# Recurrent Neural Networks to Predict Pricing Trends in UK Housing Market

Recurrent Neural Networks (RNN):

RNNs are used when temporal relationships have to be learnt. Some common examples include time series data (e.g. stock prices), sequence of words (e.g. predictive text) and so on.

The basic concept of RNNs is that we train an additional set of weights (along with the standard input – output pair) that associate past state (time: t-1) with the current state (time: t). This can then be used to predict the future state (time: t+1) given the current state (time: t). In other words RNNs are NNs with state!

When used to standard time series prediction the input and output values are taken from the same time series (usually a scalar value). This is a degenerate case of single valued inputs and outputs. Thus we need to learn the relationship between x(t-1) and x(t) so that we can predict the value of x(t+1) given x(t). This is what I did for this post.

Time series can be made more complicated by making the input a vector of different parameters, the output may still remain a scalar value which is a component of x or be a vector. One reason this is done is to add all the factors that may impact the value to be predicted (e.g. x(t+1)). In our example of average house prices – we may want to add factors such as time of the year, interest rates, salary levels, inflation etc. to provide some more “independent” variables in the input.

Two final points:

• Use-cases for RNNs: Speech to Text, Predictive Text, Music Tagging, Machine Translation
• RNNs include the additional complexity of training in Time as well as Space therefore our standard Back-Propagation becomes Back-Propagation Through Time

RNN Structure for Predicting House Prices:

The basic time series problem is that we have a sequence of numbers – the average price of houses for a given month and year (e.g. given: X(1), X(2), … X(t-1), X(t) ) with a regular step size and our task is to predict the next number in the sequence (i.e. predict: X(t+1)). In our problem the avg price is calculated for every month since January 1995 (thus step size is 1 month). As a first step we need to define a fixed sequence size that we are going to use for training the RNN. For the input data we will select a sub-sequence of a given length equal to the number of inputs (in the diagram above there are three inputs). For training output we will select a sub-sequence of the same length as the input but the values will be shifted one step in the future.

Thus if input sub-sequence is: X(3), X(4) and X(5) then the output sub-sequence must be: X(4), X(5) and X(6). In general if input sub-sequence spans time step to where b > a and b-a = sub-sequence length, then the output sub-sequence must span a+1 to b+1.

Once the training has been completed if we provide the last sub-sequence as input we will get the next number in the series as the output. We can see how well the RNN is able to replicate the signal by starting with a sub-sequence in the middle and movie ahead in time steps and plotting actual vs predicted values for the next number in the sequence.

Remember to NORMALISE the data!

The parameters are as below:

```n_steps = 36 # Number of time steps (thus a = 0 and b = 35, total of 36 months)

n_inputs = 1 # Number of inputs per step (the avg. price for the current month)

n_neurons = 1000 # Number of neurons in the middle layer

n_outputs = 1 # Number of outputs per step (the avg. price for the next month)

learning_rate = 0.0001 # Learning Rate

n_iter = 2000 # Number of iterations

batch_size = 50 # Batch size```

I am using TensorFlow’s BasicRNNCell (complete code at the end of the post) but the basic setup is:

```X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_steps, n_outputs])

cell = tf.contrib.rnn.OutputProjectionWrapper(tf.contrib.rnn.BasicRNNCell(num_units = n_neurons, activation = tf.nn.relu), output_size=n_outputs)

outputs, states = tf.nn.dynamic_rnn(cell, X, dtype = tf.float32)

loss = tf.reduce_mean(tf.square(outputs-y))
training = opt.minimize(loss)

saver = tf.train.Saver()

init = tf.global_variables_initializer()```

Results:

A sample of 3 runs, using Mean Squared Error threshold of 1e-4 we get the following values for Error:

1. 8.6831e-05
2. 9.05436e-05
3. 9.86998e-05

Run 3 fitting and predictions are shown below:

Orange dots represent the prediction by the RNN and Blue dots represent the actual data

Then we start from October 2017 (Month 24 in figure below) and forecast ahead to October 2018. This predicts a rise in average prices which start to plateau 3rd quarter of 2018. Given that average house prices across a country like UK are determined by a large number of noisy factors, we should take this prediction with a pinch of salt. Run 3 Forecasting from Month 24 (October 2017 for the year ahead till October 2018)

A sample of 3 runs, using Mean Squared Error threshold of 1e-3 we get the following values for Error:

1. 3.4365e-04
2. 4.1512e-04
3. 2.1874e-04

With a higher Error Threshold we find when comparing against actual data (Runs 2 and 3 below) the predicted values have a lot less overlap with the actual values. This is expected as we have traded accuracy for reduction in training time.

Projections in this case are lot different. We see a linearly decreasing avg price in 2018.

Next Steps:

I would like to add more parameters to the input – but it is difficult to get correlated data for different things such as interest rates, inflation etc.

I would also like to try other types of networks (e.g. LSTM) but I am not sure if that would be the equivalent of using a canon to kill a mosquito.

Finally if anyone has any ideas on this I would be happy to collaborate with them on this!

Source code can be found here: housing_tf