Accuracy Metrics#

Accuracy metrics measure how well the index predicts actual property values. We will demonstrate how to calculate and visualize accuracy metrics.

Basic Setup#

First, we will import the necessary modules, load some sales data, and create an index:

>>> from hpipy.datasets import load_seattle_sales
>>> from hpipy.price_index import RepeatTransactionIndex

>>> df = load_seattle_sales()

>>> hpi = RepeatTransactionIndex.create_index(
...     trans_data=df,
...     prop_id="pinx",
...     trans_id="sale_id",
...     price="sale_price",
...     date="sale_date",
...     periodicity="M",
...     estimator="robust",
...     log_dep=True,
...     smooth=True,
... )

Calculating Accuracy#

To calculate the accuracy of the index, you can use the accuracy function:

>>> from hpipy.utils.metrics import accuracy
>>> from hpipy.utils.plotting import plot_index_accuracy

>>> acc = accuracy(hpi)
>>> acc.round(5).head()
   pair_id  rt_price    pred_price    error  log_error  pred_period
0        1    577200  555423.08556 -0.03773   -0.03846           75
1        2    488737  458143.88008 -0.06260   -0.06464           63
2        3    570000  582619.73327  0.02214    0.02190           77
3        4    402500  416336.17537  0.03438    0.03380           63
4        5    385000  429625.48835  0.11591    0.10967           69

>>> plot_index_accuracy(acc).properties(
...     title={"text": "Accuracy Metrics", "anchor": "middle"}
... )
alt.VConcatChart(...)

Rolling Window Analysis#

You can also analyze accuracy over different time windows:

>>> rolling_acc = accuracy(hpi, window=12)
>>> rolling_acc.round(5).head()
   pair_id  rt_price    pred_price    error  log_error  pred_period
0        1    577200  555423.08556 -0.03773   -0.03846           75
1        2    488737  458143.88008 -0.06260   -0.06464           63
2        3    570000  582619.73327  0.02214    0.02190           77
3        4    402500  416336.17537  0.03438    0.03380           63
4        5    385000  429625.48835  0.11591    0.10967           69

The window parameter specifies the number of periods to use for the rolling calculation.