kats.models.harmonic_regression module¶

class kats.models.harmonic_regression.HarmonicRegressionModel(data: kats.consts.TimeSeriesData, params: kats.models.harmonic_regression.HarmonicRegressionParams)[source]¶

Bases: Generic[kats.models.model.ParamsType]

fit()None[source]¶

Fits harmonic regression to the time series. See fit_harmonics for details.

fit_harmonics(period: float, fourier_order: int)Tuple[numpy.ndarray, numpy.ndarray][source]¶

Performs harmonic regression. Harmonic regression fits cosines amplitude*cos(freq*t + phase). Using double angle identity formulas, we have: beta1*cos(freq*t) + beta2*sin(freq*t). Thus, we can fit two coefficients, which will take care of the amplitude and the phase. If we generate the raw cos(freq*t) and sin(freq*t) for each freq we want to have, it becomes a linear regression. Since we ignore intercept, we demean the time series before fitting.

Since the regression takes care of the phase, we can pick time 0 wherever we want, we just have to use the same for training, test, validation, and prediction. We pick that as the epoch; so when we generate the raw cos and sin values for the test set, and apply the parameters from the training, it will have the right phase.

Parameters
  • period (float; seasonality in hours; e.g. 24 for daily) –

  • fourier_order (int; number of harmonics for the given frequency) –

  • harms (externally computed harmonics) –

  • Returns – params: coefficients harms: feature matrix the generated raw cos and sin; for each fourier_order, there is one cos-sin pair. Number of colums: fourier_order*2

static fourier_series(dates: pandas.core.series.Series, period: float, series_order: int)numpy.ndarray[source]¶

Provides Fourier series components with the specified frequency and order. The starting time is always the epoch. :param dates: :type dates: pd.Series containing timestamps. :param period: :type period: Number of hours of the period. :param series_order: :type series_order: Number of components.

Returns

Return type

Matrix with seasonality features.

static make_harm_eval(harmonics: numpy.ndarray)Callable[source]¶

Defines evaluation function for the optimizer :param harmonics: :type harmonics: the harmonics to fit

Returns

Return type

The evaluation function for the optimizer

plot()plotly.graph_objs._figure.Figure[source]¶
Demeans the time series, fits the harmonics,

returns the plot and error metrics.

Parameters

Returns – Plot of the original time series and the fitted harmonics Dataframe with mean square error and absolute error

predict(dates: pandas.core.series.Series)pandas.core.frame.DataFrame[source]¶
Predicts with harmonic regression values.

Call fit before calling this function.

Parameters

dates (dates to compute the predictions for) –

Returns

  • Pandas DataFrame with the dates (time) and the

  • forecast values (fcst)

setup_data()[source]¶

abstract method to set up dataset

This is a declaration for setup data method

validate_inputs()[source]¶

abstract method to validate the inputs

This is a declaration for validate_inputs method

class kats.models.harmonic_regression.HarmonicRegressionParams(period: float, fourier_order: int)[source]¶

Bases: kats.consts.Params