A Simple Rent-to-Price ML Estimator by Keras in Colab
Housing rents and prices are well known to be highly correlated and the rent-to-price ratio is commonly coined as the rental yield rate. In schools, students are learned how to apply the knowledge to estimate housing price from rent. However, a machine can learn by itself to identify the rule (relationship) without being told — that is machine learning (ML).
For example, Figure 1 shows a strongly positive association between the year-on-year changes of housing prices and rents in the past decades. Even though their magnitudes are not exactly the same, the ups and downs are almost always in tandem. The phenomenon clearly implies that housing rent is a good candidate for estimating housing price and vice versa. They become very good metrics to judge whether the house price or rent is reasonable at that location and at that time.
However, it can be tedious for us to keep track of their relationship, as the yield rate is changing over time and over location. Machine learning can be a good tool to help identify their latest association and can help lay-users to get price and rent information from mobile apps without learning the jargons and technical knowledge of real estate finance, such as Gordon Growth Model and Discounted Cash Flow Model.
This article aims to show the simplest ML estimator of house price by just providing house rent. Since it just involves one input variable — house rent and one output — house price, it just requires a simplest neural network: one input neuron, one hidden layer neuron, and one output neuron (Figure 2). In this example, the 20 pairs of data can be considered as weekly asking rents (x) in NZ$ of houses in Auckland and their corresponding capital values (y, a proxy of market prices) to train the machine for the estimation of house prices from rents.
In Colab, it just requires 5 routines:
- import libraries — from tensorflow import keras to run machine learning:
#import libraries
import tensorflow as tf
import numpy as np
from tensorflow import keras
2. define the NN model — use Sequential model with 1 input and 1 layer
#define the Neural Network model as Sequential with one input variable, one hidden layer with one neuron, and one output layer with one numeric outputmodel = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
print(model.summary())
Print the Model Summary is as follows:
Model: “sequential_1” _________________________________________________________________ Layer (type) Output Shape Param # ======================================== dense_1 (Dense) (None, 1) 2 ======================================== Total params: 2
Trainable params: 2
Non-trainable params: 0 _________________________________________________________________ None
3. Compile the NN model by SQD and MSE:
#compile NN model using STOCHASTIC GRADIENT DESCENT (sgd) for the optimizer. with loss function defined by mean squared errormodel.compile(optimizer='sgd', loss='mean_squared_error')
4. Data Input — here I type in 20 pairs of x, y as data input. You can refer to my previous article (Yiu, 2021a) to try other data input methods
#data routine, here x is weekly asking rent, y is market price (proxied by the capital value) collected from the Auckland market in 2021 as shown.xs1 = np.array([370, 420, 500, 470, 600, 520, 630, 440, 550, 520, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750], dtype=int)ys1 = np.array([769600, 1040000, 928571, 977600, 1155556, 932414, 1424348, 1089524, 1430000, 901333, 624000, 827273, 717241, 780000, 1083333, 922581, 1155556, 1469565, 1582609, 1950000], dtype=int)#normalise by dividing 1000, ie value in $kxs = xs1/1000
ys = ys1/1000
print (xs,ys)
Printing x,y are as follows (in NZ$’000):
[0.37 0.42 0.5 0.47 0.6 0.52 0.63 0.44 0.55 0.52 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75] [ 769.6 1040. 928.571 977.6 1155.556 932.414 1424.348 1089.524 1430. 901.333 624. 827.273 717.241 780. 1083.333 922.581 1155.556 1469.565 1582.609 1950. ]
5. Train the machine (fit the model) by 500 epochs
#model training x,y by 500 training epochsmodel.fit(xs, ys, epochs=500)
6. Now, we can test the ML estimator by inputting any weekly rent (x_mkt):
#report the predicted y when x=670x_mkt = 670
print(model.predict([x_mkt/1000]))
And the result shows [[1200.4675]] — NZ$1,200,467.5, which is equivalent to a yield rate of 2.9%.
I have uploaded the codes of “ML estimator of house price by rent” at the following Github: https://github.com/Chung-collab/great/blob/master/ML_Estimator_of_House_Price_by_Rent.ipynb
I also produce a Youtube at TBA (Yiu, 2021b) to explain in more details.
References
Yiu, C.Y. (2021a) Getting Data from API in Colab, Medium, June 14. https://ecyy.medium.com/getting-data-from-api-in-colab-57b9a445c791
Yiu, C.Y. (2021b) A Simple Rent-to-Price ML Estimator, Youtube, June 23. https://youtu.be/Gc-tYZxjsKE