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
Metadata
Release files for pypsf 1.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pypsf-1.0.9.tar.gz | 215.9 kB | Without provenance | Details for pypsf-1.0.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pypsf-1.0.9-py3-none-any.whl | Python 3 | none | any | Details for pypsf-1.0.9-py3-none-any.whl |
Total release size: 227.7 kB
Release files / pypsf-1.0.9.tar.gz
| Download URL | pypsf-1.0.9.tar.gz |
|---|---|
| Size | 215.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa9fc8784c43a035e73a4197ec9ed923594b03ac2e3189abdecfcff3f1363198
|
|
BLAKE2b-256 checksum How to use checksums |
ca203bc389090d7d2664aa2c638f1b7f195a560c8cf1509195e1deafbd6d7b71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Release files / pypsf-1.0.9-py3-none-any.whl
| Download URL | pypsf-1.0.9-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
02ab503a3f6734a0b9313b1fc60bde78c5d8eb877c8d805b51086514388db8c4
|
|
BLAKE2b-256 checksum How to use checksums |
5604ebadfa69e10689ffa2ced140adb3523c144e1a15ecdcaa668637c76a8181
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Security for pypsf 1.0.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.