Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

TrueSight ✨

The TrueSight model is a hybrid forecasting tool that uses statistical forecasting models together with a Deep Neural Network (DNN) to make predictions. The TrueSight Preprocessor class is responsible for getting all the statistical forecasters in one place. It can handle forecasters from packages like statsforecast, scikit-learn, pmdarima, and others. You just need to the ModelWrapper Class to standardize the method calls.

All you need to do before using this package, is create a pandas dataframe with the following structure:

  • unique_id: A string that uniquely identifies each time series in the dataframe
  • ds: A datetime column with the date of each time step. The dates must be in the correct frequency for the date_freq parameter
  • y: The values of the time series

and run the steps in the Usage section. Easy peasy! 😎

Instalation 💻

To install the TrueSight package, just run:

pip install truesight

We also recommend installing the statsforecast package for the statistical forecasters:

pip install statsforecast

Usage 🚀

Import the necessary modules

import tensorflow as tf
from truesight.preprocessing import Preprocessor
from truesight.core import TrueSight
from truesight.metrics import Evaluator, smape, mape, mse, rmse, mae
from truesight.utils import AutoTune, generate_syntetic_data

Load the data

num_time_steps = 60
season_length = 12
forecast_horizon = 12
df = generate_syntetic_data(num_time_steps, season_length, 100)

Create and run the preprocessor class. You can include as many statistical models as you need in the models parameter, just make sure to use the ModelWrapper class. However, keep in mind that more models mean longer processing time. It's important to set a fallback model in case any of the informed models fail to fit.

from statsforecast.models import SeasonalNaive, AutoETS
from sklearn.linear_model import LinearRegression
from truesight.models import AdditiveDecomposition
from truesight.utils import ModelWrapper

models = [
    ModelWrapper(LinearRegression, horizon=forecast_horizon, season_length=season_length, alias="LinearRegression"), 
    ModelWrapper(SeasonalNaive, horizon=forecast_horizon, season_length=season_length), 
    ModelWrapper(AutoETS, horizon=forecast_horizon, season_length=season_length),
    ModelWrapper(AdditiveDecomposition, horizon=forecast_horizon, season_length=season_length)
]

preprocessor = Preprocessor(df)
X_train, Y_train, ids_train, X_val, Y_val, ids_val, models = preprocessor.make_dataset(
    forecast_horizon = 12, 
    season_length = 12,
    date_freq = "MS", 
    models = models, 
    fallback_model = ModelWrapper(SeasonalNaive, horizon=forecast_horizon, season_length=season_length),
    verbose = True
    )

Create the model and automatical automatically find the hyperparameters

optimizer = tf.keras.optimizers.Adam
hparams, optimizer = AutoTune(optimizer=optimizer).tune(X_train, Y_train, n_trials = 20, epochs = 10, batch_size = 32, stats_models = models)
ts = TrueSight(models, forecast_horizon)
ts.set_hparams(hparams)
ts.compile(optimizer=optimizer, loss='mse')

Or set then manually

optimizer = tf.keras.optimizers.Adam
ts = TrueSight(models, forecast_horizon, filter_size = 128, context_size = 512, hidden_size = 1024, dropout_rate = 0.1)
ts.compile(optimizer=optimizer, loss='mse')

Train the model, as the model is built on the tensorflow framework, any tensorflow callback can be used

callbacks = [
    tf.keras.callbacks.EarlyStopping(patience = 100, restore_best_weights = True, monitor = "val_loss"),
    tf.keras.callbacks.ReduceLROnPlateau(monitor = "val_loss", factor = 0.5, patience = 25, verbose = False),
]
ts.fit(
    x = X_train, y = Y_train, 
    validation_data = [X_val, Y_val], 
    batch_size = 128, 
    epochs = 1000, 
    verbose = False, 
    callbacks = callbacks,
)
ts.plot_training_history()

Training Log

Evaluate the results

yhat = ts.predict(X_val, n_repeats = 100, n_quantiles = 15, verbose = False)
evaluator = Evaluator(X_val, Y_val, yhat, ids_val)
evaluator.evaluate_prediction(evaluators = [smape, mape, mse, rmse, mae], return_mean = True)
metric value
smape 0.234369
mape 0.293816
mse 816.238082
rmse 21.218396
mae 15.885432
evaluator.plot_exemple()

Output Exemple

Metadata

Release files for truesight 0.0.3a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for truesight 0.0.3a1
File Size Uploaded Provenance Actions
truesight-0.0.3a1.tar.gz 16.0 kB Without provenance Details for truesight-0.0.3a1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for truesight 0.0.3a1
File Interpreter ABI Platform Actions
truesight-0.0.3a1-py3-none-any.whl Python 3 none any Details for truesight-0.0.3a1-py3-none-any.whl

Total release size: 32.9 kB

Release files / truesight-0.0.3a1.tar.gz

Download URL truesight-0.0.3a1.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4dcc4719ddc861f0c5c4b76f14c9a126dc106804fe5120cbf12d57a12ea72d29
BLAKE2b-256 checksum
How to use checksums
3797781a5150c55ddabc9f65eba4d6ad085e66c0069a12e37d6b1f9eb9f6b98d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / truesight-0.0.3a1-py3-none-any.whl

Download URL truesight-0.0.3a1-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c4bda03446f47bfc9b42f60f3f396fb6756f4a7f44312e2cfca858e927629e5
BLAKE2b-256 checksum
How to use checksums
b2846c5c96afc13e4c9b96d5ee9275bbe6af513c7d802f7a1b971be6294d8670
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for truesight 0.0.3a1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page