Functions to find the loss (or error) given a set of observations and point forecasts.

The loss functions in this module are commonly used to complete a time series analysis, with the following steps:

1. Run an analysis, returning an array of forecast samples
2. Use a function from point_forecast to find the forecast mean, median, or $(-1)-$median
3. Evaluate the point forecast error using a loss function from this module

#### MSE[source]

MSE(y, f)

Mean squared error (MSE).

The mean squared error loss is:

$$L = \frac{1}{n} \sum_{i=1:n} (y_i - f_i)^2$$

The mean is the optimal point forecast for minimizing the MSE. An example below demonstrates how to use the function:

from pybats.shared import load_us_inflation
from pybats.analysis import analysis
from pybats.point_forecast import mean, median, m_one_median
import pandas as pd

forecast_start = '2000-Q1'
forecast_end = '2013-Q4'
Y = data.Inflation.values[1:]

mod, samples = analysis(Y = Y, X=None, family="normal",
k = 4, prior_length = 12,
forecast_start = forecast_start, forecast_end = forecast_end,
dates=data.Date,
ntrend = 2, deltrend=.99,
nsamps = 5000)

# Use the mean, because it is optimal for minimizing the MSE
forecast = mean(samples)

for h in range(4):
start = data[data.Date == forecast_start].index[0] + h
end = data[data.Date == forecast_end].index[0] + h + 1

print(str(h+1) + '-Step Ahead MSE: ' + str(MSE(Y[start:end], forecast[:,h]).round(2)))

beginning forecasting


#### MAD[source]

MAD(y, f)

The mean absolute deviation is:

$$L = \frac{1}{n} \sum_{i=1:n} |y_i - f_i|$$

The median is the optimal point forecast for minimizing the MAD. An example below demonstrates how to use the function:

forecast = median(samples)

for h in range(4):
start = data[data.Date == forecast_start].index[0] + h
end = data[data.Date == forecast_end].index[0] + h + 1


1-Step Ahead MAD: 0.79


#### MAPE[source]

MAPE(y, f)

Mean absolute percent error (MAPE).

The mean absolute percent error is:

$$L = \frac{1}{n} \sum_{i=1:n} \frac{|y_i - f_i|}{y_i}$$

The $(-1)-$median is the optimal point forecast for minimizing the MAPE. An example below demonstrates how to use the function:

forecast = m_one_median(samples)

for h in range(4):
start = data[data.Date == forecast_start].index[0] + h
end = data[data.Date == forecast_end].index[0] + h + 1

print(str(h+1) + '-Step Ahead MAPE: ' + str(MAPE(Y[start:end], forecast[:,h]).round(2)) + "%")

1-Step Ahead MAPE: 81.84%