Skip to main content

Python implementation of the Pattern Sequence Based Forecasting (PSF) algorithm

Project description

PyPSF

This project provides a python implementation of the Pattern Sequence Based Forecasting (PSF) algorithm. For a detailed description of the PSF algorithm and some of the practical issues I encountered when using it, see this PDF file.

Installation

pip install pypsf

Dependencies

  • scikit-learn
  • numpy

Example Usage

Applying PSF to the classic R "AirPassenger" dataset, which provides monthly totals of a US airline passengers, from 1949 to 1960.

import numpy as np
import matplotlib.pyplot as plt
from pypsf import Psf

plt.style.use("dark_background")

t_series = np.array([112, 118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118,
                     115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114, 140,
                     145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166,
                     171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194,
                     196, 196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201,
                     204, 188, 235, 227, 234, 264, 302, 293, 259, 229, 203, 229,
                     242, 233, 267, 269, 270, 315, 364, 347, 312, 274, 237, 278,
                     284, 277, 317, 313, 318, 374, 413, 405, 355, 306, 271, 306,
                     315, 301, 356, 348, 355, 422, 465, 467, 404, 347, 305, 336,
                     340, 318, 362, 348, 363, 435, 491, 505, 404, 359, 310, 337, 
                     360, 342, 406, 396, 420, 472, 548, 559, 463, 407, 362, 405,
                     417, 391, 419, 461, 472, 535, 622, 606, 508, 461, 390, 432])
train = t_series[:-28]
test = t_series[-28:]

psf = Psf(cycle_length=12, apply_diff=True, diff_periods=12)
psf.fit(train)

pred = psf.predict(len(test))

fig, ax = plt.subplots()
x_train = np.array(range(len(train)))
x_test_pred = np.array(range(len(test))) + x_train[-1]
ax.plot(x_train, train, c="lightblue")
ax.plot(x_test_pred, test, c="lightgreen")
ax.plot(x_test_pred, pred, c="tab:orange")
plt.legend(["Training", "Test", "Prediction"])
plt.tight_layout()
plt.show()

psf_prediction_plot

Parameters

class Psf

  • cycle_length: int
    The cycle length c
  • k: int (optional), default None
    The user-defined number of desired clusters when running K-means on the cycles
  • w: int (optional), default None
    The user-defined window size
  • suppress_warnings: bool (optional), default False
    Suppress all warnings
  • apply_diff: bool (optional), default False
    Apply first order differencing to the time series before applying PSF
  • diff_periods: int (optional), default 1
    Periods to shift for calculating difference, to allow for either ordinary or seasonal differencing. Ignore if apply_diff=False
  • detrend: bool (optional), default False
    Remove a linear trend from the series prior to applying PSF by fitting a simple linear regression model. The trend is subsequently re-added to the predictions.

Psf.fit

  • data:
    The input time series
  • k_values: iterable[int] (optional), default tuple(range(3, 12))
    The set of candidate values of k to test when finding the "best" k number of clusters based on the training data
  • w_values: iterable[int] (optional), default tuple(range(1, 20))
    The set of candidate values of w to test when finding the "best" window size w based on the training data

Psf.predict

  • n_ahead: int
    The number of values to predict

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypsf-1.0.9.tar.gz (215.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypsf-1.0.9-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file pypsf-1.0.9.tar.gz.

File metadata

  • Download URL: pypsf-1.0.9.tar.gz
  • Upload date:
  • Size: 215.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pypsf-1.0.9.tar.gz
Algorithm Hash digest
SHA256 fa9fc8784c43a035e73a4197ec9ed923594b03ac2e3189abdecfcff3f1363198
MD5 3212b791f891eb98fdc4fafee7dbbd54
BLAKE2b-256 ca203bc389090d7d2664aa2c638f1b7f195a560c8cf1509195e1deafbd6d7b71

See more details on using hashes here.

File details

Details for the file pypsf-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: pypsf-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pypsf-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 02ab503a3f6734a0b9313b1fc60bde78c5d8eb877c8d805b51086514388db8c4
MD5 a960bdb5874fd223dfa47412ac80db3a
BLAKE2b-256 5604ebadfa69e10689ffa2ced140adb3523c144e1a15ecdcaa668637c76a8181

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page