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()
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
Release history Release notifications | RSS feed
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.8.tar.gz
(216.0 kB
view details)
Built Distribution
pypsf-1.0.8-py3-none-any.whl
(11.7 kB
view details)
File details
Details for the file pypsf-1.0.8.tar.gz
.
File metadata
- Download URL: pypsf-1.0.8.tar.gz
- Upload date:
- Size: 216.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ed89113a64ca03bf9da3a2a6047cd606c3500f5721afbab825808ce8c1a2f3c |
|
MD5 | 825f22fdbaf952ed7ac210eee84deeb0 |
|
BLAKE2b-256 | 84d4267032d430388cdcfbc864b5514d5687f8468d44990e201b24015036f691 |
File details
Details for the file pypsf-1.0.8-py3-none-any.whl
.
File metadata
- Download URL: pypsf-1.0.8-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b7278b709d235f453f8265931c7ea70c1ae3a6ef72d0f81c06ae3348d607920 |
|
MD5 | f1736f1449610241e390cd01b0b69c17 |
|
BLAKE2b-256 | 343f7dd9458564a4a992312f029110c17b486f21465d6014038abebf13a4085c |