### Kalman Filter Example Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt This example demonstrates a basic Kalman filter implementation. Ensure you have the necessary libraries installed. ```python from darts.models import KalmanFilter from darts.timeseries import TimeSeries import numpy as np # Generate some dummy data np.random.seed(42) series = TimeSeries.from_values(np.random.randn(100, 1)) # Initialize and fit the Kalman Filter model kalman_filter = KalmanFilter() kalman_filter.fit(series) # Make a prediction prediction = kalman_filter.predict(n=10) print("Prediction:") print(prediction.values()) ``` -------------------------------- ### Basic RNN Model Example Source: https://unit8co.github.io/darts/_sources/examples/04-RNN-examples.ipynb.txt Demonstrates the instantiation and basic usage of a simple RNN model. Ensure you have the 'torch' library installed. ```python from darts.models import RNNModel from darts.utils.timeseries_generation import linear_timeseries # Generate a simple time series series = linear_timeseries(length=100) # Instantiate a basic RNN model model = RNNModel(model='RNN', hidden_dim=25, dropout=0.1, num_layers=2, optimizer='adam', loss='mse', epochs=50, model_kwargs={'batch_first': True}) # Train the model model.fit(series, verbose=True) # Make a prediction pred = model.predict(n=10) print(pred) ``` -------------------------------- ### Train TFT Model with Default Parameters Source: https://unit8co.github.io/darts/_sources/examples/13-TFT-examples.ipynb.txt Trains a TFT model using default parameters. This is a basic example for getting started. ```python from darts.models import TFTModel from darts.datasets import AirPassengersDataset # Load the dataset past_series = AirPassengersDataset().load().astype(np.float32) # Initialize and train the TFT model with default parameters tft = TFTModel() tft.fit(series=past_series) print("TFT model trained with default parameters.") ``` -------------------------------- ### Create a Kalmanర్జాతీయ Model Source: https://unit8co.github.io/darts/_sources/examples/02-data-processing.ipynb.txt This example demonstrates the creation and fitting of a Kalmanర్జాతీయ model. It's useful for state-space modeling. ```python from darts.models import Kalmanర్జాతీయ model = Kalmanర్జాతీయ() model.fit(train_df) ``` -------------------------------- ### Install NeuralForecast Source: https://unit8co.github.io/darts/_sources/examples/26-NeuralForecast-examples.ipynb.txt Install the NeuralForecast library using pip. This is a prerequisite for using any of the examples. ```bash pip install neuralforecast ``` -------------------------------- ### Example Usage with Data Source: https://unit8co.github.io/darts/_sources/examples/16-hierarchical-reconciliation.ipynb.txt A complete example demonstrating the creation, training, and forecasting process with sample data. ```python from darts.utils.utils import HierarchicalUtils from darts.models import ExponentialSmoothing from darts.timeseries import TimeSeries import pandas as pd import numpy as np # Define hierarchy hierarchy = { "Total": ["A", "B"], "A": ["A1", "A2"], "B": ["B1", "B2"], } hierarchical_utils = HierarchicalUtils(hierarchy) # Generate sample data dates = pd.date_range('2023-01-01', periods=100) def generate_series(base_value, trend, noise_level, dates): values = base_value + np.arange(len(dates)) * trend + np.random.randn(len(dates)) * noise_level return TimeSeries.from_times_and_values(dates, values) train_series = {} train_series["A1"] = generate_series(10, 0.1, 1, dates) train_series["A2"] = generate_series(15, 0.15, 1.5, dates) train_series["B1"] = generate_series(20, 0.2, 2, dates) train_series["B2"] = generate_series(25, 0.25, 2.5, dates) # Reconcile training data to ensure consistency train_series = hierarchical_utils.reconcile_series(train_series) # Create and train hierarchical model base_model = ExponentialSmoothing() hierarchical_model = hierarchical_utils.create_hierarchical_model(base_model) hierarchical_model.fit(train_series) # Generate forecasts n = 30 hierarchical_forecasts = hierarchical_model.predict(n=n) # Print forecasts for 'Total' print(f"Forecast for Total:\n{hierarchical_forecasts['Total'].pd_series()}") ``` -------------------------------- ### Setup Python Path and Initialize JavaScript Source: https://unit8co.github.io/darts/examples/20-SKLearnModel-examples.html Ensures the correct Python path for local development and initializes JavaScript for interactive visualizations. ```python from utils import fix_pythonpath_if_working_locally fix_pythonpath_if_working_locally() # activate javascript from shap import initjs() initjs() ``` -------------------------------- ### Univariate Kalman Filter Example Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt This example shows a basic univariate Kalman filter implementation. Ensure you have the 'darts' library installed. ```python from darts.models import KalmanForecaster from darts.utils.timeseries_generation import linear_timeseries # Generate a simple time series series = linear_timeseries(start_time=0, length=100, freq='D') # Initialize and fit the KalmanForecaster forecaster = KalmanForecaster() forecaster.fit(series) # Make a prediction prediction = forecaster.predict(n=10) print(prediction) ``` -------------------------------- ### Create a Kalmanర్జాతీయ Model with parameters Source: https://unit8co.github.io/darts/_sources/examples/02-data-processing.ipynb.txt Demonstrates creating a Kalmanర్జాతీయ model with custom parameters. This allows for more control over the state-space estimation. ```python from darts.models import Kalmanర్జాతీయ model = Kalmanర్జాతీయ(dim_x=10) model.fit(train_df) ``` -------------------------------- ### Initialize and Run a Model Source: https://unit8co.github.io/darts/_sources/examples/02-data-processing.ipynb.txt This snippet demonstrates how to initialize a model and run it. It's useful for setting up and executing machine learning models. ```python from darts.models import ExponentialSmoothing model = ExponentialSmoothing() model.fit(train_df) prediction = model.predict(l=10) ``` -------------------------------- ### SequentialEncoder Configuration Example Source: https://unit8co.github.io/darts/generated_api/darts.dataprocessing.encoders.encoders.html Example of configuring SequentialEncoder with custom past encoders and a transformer. This setup defines how past covariates are processed. ```python { 'custom': {'past': [lambda idx: (idx.year - 1950) / 50]}, 'transformer': Scaler(), 'tz': 'CET', } ``` -------------------------------- ### BlockRNNModel Configuration Example Source: https://unit8co.github.io/darts/_modules/darts/models/forecasting/block_rnn_model.html Example of how to configure the BlockRNNModel with custom encoders and a transformer. This setup allows for automatic generation of covariates and their transformation. ```python def encode_year(idx): return (idx.year - 1950) / 50 add_encoders={ 'cyclic': {'future': ['month']}, 'datetime_attribute': {'future': ['hour', 'dayofweek']}, 'position': {'past': ['relative'], 'future': ['relative']}, 'custom': {'past': [encode_year]}, 'transformer': Scaler(), 'tz': 'CET' } ``` -------------------------------- ### Kalman Filter with Different Initialization Strategies Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt This example shows how to use different strategies for initializing the Kalman filter's state. This can impact the filter's convergence and accuracy, especially in the early stages. ```python from darts.models import KalmanFilter from darts.timeseries import TimeSeries # Example usage with different initialization strategies: # kf = KalmanFilter(init_strategy='estimate') # Estimate initial state from data # kf.fit(series) ``` -------------------------------- ### Install Darts and Conformal Prediction Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Install the necessary libraries, Darts and its conformal prediction module, using pip. This is a prerequisite for running the examples. ```bash pip install darts[conformal-prediction] ``` -------------------------------- ### Set up Training Arguments Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Configures training parameters such as output directory, learning rate, batch size, and number of epochs using Hugging Face's TrainingArguments. Adjust these based on your hardware and dataset size. ```python from transformers import TrainingArguments training_args = TrainingArguments( output_dir="./results", overwrite_output_dir=True, num_train_epochs=3, per_device_train_batch_size=4, save_steps=10_000, save_total_limit=2, prediction_loss_only=True, learning_rate=5e-5, fp16=True, # Use mixed precision if supported ) ``` -------------------------------- ### Kalman Filter with Darts Library Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt Example of using the Darts library for Kalman filtering. Ensure Darts is installed (`pip install darts`). ```python from darts.models import KalmanForecastingModel from darts.timeseries import TimeSeries import numpy as np # Assuming 'series' is a Darts TimeSeries object # model = KalmanForecastingModel() # model.fit(series) # prediction = model.predict(n=10) # Example with synthetic data np.random.seed(42) sig = 0.3 x = np.arange(100) y = np.sin(x / 10) + np.random.normal(0, sig, 100) t = TimeSeries.from_values(y) model = KalmanForecastingModel() model.fit(t) # Predict next 10 steps prediction = model.predict(n=10) # You can also access the filtered series # filtered_series = model.filter(t) ``` -------------------------------- ### Kalman Filter with State Initialization Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt This example shows how to provide an initial state and covariance for the Kalman filter, which can be useful if you have prior knowledge about the system's starting conditions. Requires 'numpy' and 'darts'. ```python import numpy as np from darts.models import KalmanFilter # Initial state vector (x0) initial_state = np.array([0.0, 0.0]) # Initial state covariance matrix (P0) initial_covariance = np.eye(2) * 1.0 model = KalmanFilter(initial_state_mean=initial_state, initial_state_covariance=initial_covariance) # Fit and predict # model.fit(series) # forecast = model.predict(n=10) ``` -------------------------------- ### KalmanForecaster Initialization and Usage Example Source: https://unit8co.github.io/darts/_modules/darts/models/forecasting/kalman_forecaster.html Demonstrates how to initialize and use the KalmanForecaster with future covariates. It shows fitting the model and generating predictions. ```python from darts.datasets import AirPassengersDataset from darts.models import KalmanForecaster from darts.utils.timeseries_generation import datetime_attribute_timeseries series = AirPassengersDataset().load() # optionally, use some future covariates; e.g. the value of the month encoded as a sine and cosine series future_cov = datetime_attribute_timeseries(series, "month", cyclic=True, add_length=6) # increasing the size of the state vector model = KalmanForecaster(dim_x=12) model.fit(series, future_covariates=future_cov) pred = model.predict(6, future_covariates=future_cov) print(pred.values()) ``` -------------------------------- ### Basic TiDE Usage Example Source: https://unit8co.github.io/darts/_sources/examples/18-TiDE-examples.ipynb.txt This snippet shows a fundamental example of using TiDE. It's a good starting point for understanding the basic structure and operations. ```python from darts.models import TiDE from darts.datasets import AirPassengersDataset # Load dataset pd_air = AirPassengersDataset().load().pd_val() # Create and train the model model = TiDE( input_chunk_length=24, output_chunk_length=12, n_epochs=50, random_state=42, ) model.fit(pd_air) # Make predictions pred = model.predict(n=24) # Save the model model.save_model("tide_model") # Load the model loaded_model = TiDE.load_model("tide_model") print("TiDE model trained, predicted, saved, and loaded successfully.") ``` -------------------------------- ### DTW with Example Time Series Source: https://unit8co.github.io/darts/_sources/examples/12-Dynamic-Time-Warping-example.ipynb.txt This snippet demonstrates a basic Dynamic Time Warping calculation between two example time series. Ensure you have the 'darts' library installed. ```python from darts.metrics import dtw_metric from darts.timeseries import TimeSeries import numpy as np time_series_1 = TimeSeries.from_values(np.array([1, 2, 3, 4, 5])) time_series_2 = TimeSeries.from_values(np.array([1, 1, 2, 3, 4, 5])) dtw_distance = dtw_metric(time_series_1, time_series_2) print(f"DTW distance: {dtw_distance}") ``` -------------------------------- ### Initialize Trainer and Start Fine-Tuning Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Initializes the Trainer with the model, training arguments, and dataset, then starts the fine-tuning process. ```python from transformers import Trainer trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, # Assuming train_dataset is an instance of TextDataset ) trainer.train() ``` -------------------------------- ### Kalman Filter with Initial State Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt This example demonstrates how to provide an initial state and covariance for the Kalman filter. This is useful when you have prior knowledge about the system's starting condition. ```python initial_state = np.array([0, 0]) initial_covariance = np.array([[1, 0], [0, 1]]) kf_init = KalmanFilter(initial_state=initial_state, initial_covariance=initial_covariance) kf_init.fit(series) estimated_init = kf_init.predict(series) ``` -------------------------------- ### Basic Array Operations in NumPy Source: https://unit8co.github.io/darts/_sources/examples/02-data-processing.ipynb.txt Provides examples of basic array creation and manipulation using the NumPy library. Ensure NumPy is installed (`pip install numpy`). ```python import numpy as np # Create a NumPy array arr = np.array([1, 2, 3, 4, 5]) print(arr) # Perform element-wise operation print(arr * 2) # Create an array of zeros zeros_arr = np.zeros((2, 3)) print(zeros_arr) ``` -------------------------------- ### Basic NeuralForecast Setup Source: https://unit8co.github.io/darts/_sources/examples/26-NeuralForecast-examples.ipynb.txt This snippet shows the basic setup for using NeuralForecast, including necessary imports and data preparation. It's a starting point for most forecasting tasks. ```python from neuralforecast import NeuralForecast from neuralforecast.models import NBEATS import pandas as pd import numpy as np # Sample data creation data = { 'unique_id': ['1'] * 100, 'ds': pd.to_date_range(start='2020-01-01', periods=100, freq='D'), 'y': np.random.rand(100) * 100 } df = pd.DataFrame(data) # Initialize the model model = NBEATS( input_size=12, output_size=7, stack_n=2, n_blocks=1, mlp_hidden_mults=(4, 4), max_steps=1000, learning_rate=0.001, batch_size=128, valid_batch_size=128, early_stopping=True, patience=10, random_seed=1, num_workers=0 ) # Initialize NeuralForecast nf = NeuralForecast( models=[model], freq='D' ) # Fit the model fitted_nf = nf.fit(df=df) # Predict predictions = nf.predict(df=df) print(predictions.head()) ``` -------------------------------- ### Set up Training Arguments Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Configures training parameters for fine-tuning. This includes output directory, learning rate, number of epochs, and batch size. Adjust these based on your specific task and hardware. ```python from transformers import TrainingArguments training_args = TrainingArguments( output_dir="./fine-tuned-model", per_device_train_batch_size=8, num_train_epochs=3, learning_rate=2e-5, logging_dir="./logs", logging_steps=10, save_steps=500, evaluation_strategy="epoch" ) ``` -------------------------------- ### Basic TCN Model Setup Source: https://unit8co.github.io/darts/_sources/examples/05-TCN-examples.ipynb.txt This snippet shows the fundamental setup for a TCN model, including necessary imports and model instantiation. Ensure you have the 'darts' library installed. ```python from darts.models import TCNModel from darts.datasets import AirPassengersDataset # Load the dataset series = AirPassengersDataset().load() # Split the data into training and validation sets train, val = series.split_after(pd.Timestamp('19590101')) # Initialize the TCN model # You can customize parameters like input_chunk_length, output_chunk_length, n_filters, etc. model = TCNModel(input_chunk_length=24, output_chunk_length=12, n_filters=8, n_resid_blocks=1, kernel_size=3, random_state=42) # Train the model model.fit(train, epochs=50, validation_split=0.2) # Make predictions pred = model.predict(n=24) # Evaluate the model (optional) from darts.metrics import mape, smape print("MAPE: ", mape(series, pred)) print("SMAPE: ", smape(series, pred)) ``` -------------------------------- ### Start Fine-Tuning Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Initiates the fine-tuning process using the configured Trainer. This will train the model on the provided dataset. ```python trainer.train() ``` -------------------------------- ### n_steps_between Source: https://unit8co.github.io/darts/generated_api/darts.utils.utils.html Get the number of time steps with a given frequency freq between end and start. ```APIDOC ## Function darts.utils.utils.n_steps_between Get the number of time steps with a given frequency freq between end and start. Works for both integers and time stamps. ### Parameters - **end**: The end point. - **start**: The start point. - **freq**: The frequency. ``` -------------------------------- ### TiDE Model Training Example Source: https://unit8co.github.io/darts/_sources/examples/18-TiDE-examples.ipynb.txt This example demonstrates how to train a TiDE model. It loads the AirPassengers dataset and trains the model for a specified number of epochs. Ensure you have 'darts' and 'pytorch' installed. ```python from darts.models import TiDEModel from darts.datasets import AirPassengersDataset # Load the dataset series = AirPassengersDataset().load() # Split the data into training and validation sets len_series = len(series) train, val = series[:int(len_series * 0.8)], series[int(len_series * 0.8):] # Initialize and train the TiDE model model = TiDEModel( input_chunk_length=12, output_chunk_length=12, n_epochs=100, random_state=42, ) model.fit(train, val_series=val, verbose=True) # Save the trained model model.save_model("/mnt/models/tide/air_passengers/tide_air_passengers_v1.0.0.ckpt") ``` -------------------------------- ### Set up Training Arguments Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Configures training parameters for fine-tuning. Adjust output directory and other hyperparameters as needed. ```python from transformers import TrainingArguments training_args = TrainingArguments( output_dir="./results", evaluation_strategy="epoch", learning_rate=2e-5, per_device_train_batch_size=8, per_device_eval_batch_size=8, num_train_epochs=3, weight_decay=0.01, save_strategy="epoch", load_best_model_at_end=True, ) ``` -------------------------------- ### Fine-tune the Model Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Sets up the training arguments and initializes the Trainer for fine-tuning. This example uses a small learning rate and a few epochs. ```python from transformers import Trainer, TrainingArguments training_args = TrainingArguments( output_dir="./results", num_train_epochs=3, per_device_train_batch_size=1, save_steps=10_000, save_total_limit=2, learning_rate=5e-5, warmup_steps=500, weight_decay=0.01, logging_dir='./logs', logging_steps=100, fp16=True # Use mixed precision if available ) trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_dataset, # Assuming tokenized_dataset is prepared tokenizer=tokenizer ) trainer.train() ``` -------------------------------- ### Using Different NeuralForecast Models (Example: NHITS) Source: https://unit8co.github.io/darts/_sources/examples/26-NeuralForecast-examples.ipynb.txt This example demonstrates how to use a different model architecture, NHITS, within the NeuralForecast framework. The setup and prediction process are similar to other models. ```python from neuralforecast.models import NHITS # Initialize NHITS model hits_model = NHITS( input_size=24, output_size=7, n_epochs=50, random_seed=1 ) # Initialize NeuralForecast with NHITS nf_hits = NeuralForecast( models=[hits_model], freq='D' ) # Train and predict (assuming 'data' is already defined) nf_hits.fit(train_df=data) hits_predictions = nf_hits.predict(h=7) print(hits_predictions.head()) ``` -------------------------------- ### Foundation Model with CatBoost Source: https://unit8co.github.io/darts/_sources/examples/25-FoundationModel-examples.ipynb.txt Sets up a foundation model using CatBoost. This example specifies `input_chunk_length`, `output_chunk_length`, and training epochs. ```python from darts.models.forecasting.foundationmodels import FoundationModel model = FoundationModel(model="catboost", input_chunk_length=128, output_chunk_length=24, n_epochs=50, optimizer_kwargs={"lr": 1e-4}, random_state=42) ``` -------------------------------- ### SKLearnModel Example Source: https://unit8co.github.io/darts/_sources/examples/20-SKLearnModel-examples.ipynb.txt This snippet shows a basic example of using a scikit-learn model with Darts. It involves creating a Darts Transformer from a scikit-learn model and applying it to a time series. Ensure you have scikit-learn installed. ```python from darts.models import SKLearnModel from sklearn.svm import LinearSVR from darts.timeseries import TimeSeries # Create a dummy time series time_series = TimeSeries.from_values( [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] ) # Create a scikit-learn model sklearn_model = LinearSVR() # Wrap the scikit-learn model in a Darts SKLearnModel model = SKLearnModel(model=sklearn_model) # Fit the model to the time series model.fit(time_series) # Predict using the fitted model prediction = model.predict(n=3) print(prediction.values()) ``` -------------------------------- ### Basic NeuralForecast Model Setup Source: https://unit8co.github.io/darts/_sources/examples/26-NeuralForecast-examples.ipynb.txt This snippet shows the basic setup for a NeuralForecast model, including importing necessary libraries and defining model parameters. It's a starting point for most forecasting tasks. ```python from neuralforecast import NeuralForecast from neuralforecast.models import NBEATS import pandas as pd import numpy as np # Sample data (replace with your actual data) data = { 'unique_id': ['1'] * 100, 'ds': pd.to_datetime(pd.date_range(start='2023-01-01', periods=100, freq='D')), 'y': np.random.rand(100) * 100 } df = pd.DataFrame(data) # Define the model model = NBEATS( input_size=24, # Lookback window h=12, # Forecast horizon # Add other NBEATS specific parameters if needed ) # Initialize NeuralForecast nf = NeuralForecast( models=[model], freq='D' # Frequency of the data ) ``` -------------------------------- ### Set up Training Arguments Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Configures training parameters such as output directory, learning rate, batch size, and number of epochs using Hugging Face's TrainingArguments. ```python from transformers import TrainingArguments training_args = TrainingArguments( output_dir="./results", overwrite_output_dir=True, num_train_epochs=3, per_device_train_batch_size=4, save_steps=10_000, save_total_limit=2, learning_rate=5e-5, warmup_steps=500, logging_dir='./logs', logging_steps=50, ) ``` -------------------------------- ### Basic Conformal Prediction Setup Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt This snippet shows the basic setup for conformal prediction, including necessary imports and data preparation. It's a starting point for applying conformal prediction to any regression problem. ```python from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.datasets import make_regression from darts.utils.conformal_prediction import ConformalPrediction X, y = make_regression(n_samples=1000, n_features=10, noise=10, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train a base model model = LinearRegression() model.fit(X_train, y_train) ``` -------------------------------- ### Set up Training Arguments Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Configures training parameters such as output directory, learning rate, and number of epochs using Hugging Face's TrainingArguments. This is essential for controlling the fine-tuning process. ```python from transformers import TrainingArguments training_args = TrainingArguments( output_dir="./results", overwrite_output_dir=True, num_train_epochs=1, per_device_train_batch_size=2, save_steps=10_000, save_total_limit=2, ) ``` -------------------------------- ### Early Stopping Callback Example Setup Source: https://unit8co.github.io/darts/userguide/torch_forecasting_models.html Set up an `EarlyStopping` callback from PyTorch Lightning to prevent overfitting and reduce training time. This example also includes data loading, scaling, and model initialization. ```python import pandas as pd from pytorch_lightning.callbacks import EarlyStopping from torchmetrics import MeanAbsolutePercentageError from darts.dataprocessing.transformers import Scaler from darts.datasets import AirPassengersDataset from darts.models import NBEATSModel # read data series = AirPassengersDataset().load() # create training and validation sets: train, val = series.split_after(pd.Timestamp(year=1957, month=12, day=1)) # normalize the time series transformer = Scaler() train = transformer.fit_transform(train) val = transformer.transform(val) # Example of initializing a model and callback (fit method not shown here) # model = NBEATSModel(...) # callbacks = [EarlyStopping(monitor="val_loss", patience=5, mode="min")] # model.fit(train, val_series=val, callbacks=callbacks) ``` -------------------------------- ### Kalman Filter with All Custom Parameters Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt Provides an example of initializing the KalmanFilter with all customizable parameters: state transition matrix, control input matrix, measurement matrix, and various covariances. This offers maximum flexibility in defining the system dynamics and noise characteristics. ```python import numpy as np from darts.models import KalmanFilter # Define all custom parameters state_transition = np.array([[1.0, 1.0], [0.0, 1.0]]) control_input = np.array([[1.0]]) measurement = np.array([[1.0, 0.0]]) initial_state_cov = np.array([[1.0, 0.0], [0.0, 1.0]]) process_noise = np.array([[0.1, 0.0], [0.0, 0.1]]) observation_noise = np.array([[0.5]]) # Initialize Kalman filter with all custom parameters kf = KalmanFilter( state_transition_matrix=state_transition, control_input_matrix=control_input, measurement_matrix=measurement, initial_state_covariance=initial_state_cov, process_noise_covariance=process_noise, observation_noise_covariance=observation_noise ) # Fit and predict as usual # kf.fit(series) # forecast = kf.predict(n=10) ``` -------------------------------- ### Set up Training Arguments Source: https://unit8co.github.io/darts/_sources/examples/27-Torch-and-Foundation-Model-Fine-Tuning-examples.ipynb.txt Configures training arguments for fine-tuning, including output directory, learning rate, and number of epochs. ```python from transformers import TrainingArguments training_args = TrainingArguments( output_dir="./results", learning_rate=2e-5, per_device_train_batch_size=16, per_device_eval_batch_size=16, num_train_epochs=3, weight_decay=0.01, evaluation_strategy="epoch" ) ``` -------------------------------- ### Conformal Classification with RandomForest Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Example of using RandomForest for conformal classification. Requires installation of `darts` and `scikit-learn`. ```python from darts.models import RandomForestModel from darts.datasets import HeartRateDataset # Load data series = HeartRateDataset().load() # Split data train, val = series.split_before(0.8) # Model training model = RandomForestModel( lags=10, output_chunk_length=1, n_estimators=100, random_state=42, ) model.fit(train) # Conformal prediction for classification # For classification, conformal prediction generates prediction sets with guaranteed coverage. # The `conformal_predict` method requires a calibration set and a significance level (alpha). # The calibration set is used to estimate the quantile for the prediction sets. # The significance level (alpha) determines the desired coverage probability (1 - alpha). # For example, alpha=0.1 means we want 90% prediction sets. # Let's use the validation set as calibration set # Note: In a real-world scenario, you might want a separate calibration set. calibration_series = val alpha = 0.1 # Generate prediction sets # The `conformal_predict` method returns a `ConformalizedModel` object # which can then be used to predict sets. # The `method` parameter can be set to 'classification' for classification models. # The `coverage` parameter is 1 - alpha. # For RandomForest, we need to specify the `method` as 'classification' # and the `coverage` as 1 - alpha. # The `conformal_predict` method will return a new model that can predict sets. # The `conformal_predict` method for RandomForest requires the calibration set and the coverage level. # It returns a model that can predict sets. # The coverage is set to 1 - alpha. # For classification models like RandomForest, the `conformal_predict` method is used to obtain prediction sets. # It requires a calibration set and a coverage level (1 - alpha). # The method returns a model that can predict sets. # Example of generating prediction sets: # prediction_sets = model.conformal_predict(calibration_series=calibration_series, alpha=alpha) # The above line is commented out as it requires a full run and might be slow. # In practice, you would use it like this: # prediction_sets = model.conformal_predict(calibration_series=val, alpha=0.1) # To demonstrate the concept, let's simulate the output structure: # prediction_sets = model.predict(n=10) # This would be a standard prediction # For conformal prediction, the output would be a PredictionSet object. # The following is a placeholder to show the intended usage: # prediction_sets = model.conformal_predict(calibration_series=val, alpha=0.1) # print(prediction_sets.values()) # Since we cannot run the full prediction here, we will skip the actual prediction and printing. # The key takeaway is the usage of `conformal_predict` with `calibration_series` and `alpha`. print("Conformal classification with RandomForest example setup complete.") print("To generate prediction sets, use: model.conformal_predict(calibration_series=val, alpha=0.1)") ``` -------------------------------- ### Conformal Regression with NormalNets Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Example of using NormalNets for conformal regression. Requires installation of `darts` and `torch`. ```python from darts.models import NormalNets from darts.datasets import AirPassengersDataset # Load data series = AirPassengersDataset().load() # Split data series = series[:120] train, val = series.split_before(0.8) # Model training model = NormalNets( input_chunk_length=12, output_chunk_length=1, n_epochs=50, model_name="normalnets_regression", ) model.fit(train, val_series=val, epochs=50, verbose=0) # Conformal prediction # For regression, we can use the `conformal_predict` method to get prediction intervals. # This method requires a calibration set and a significance level (alpha). # The calibration set is used to estimate the quantile for the prediction intervals. # The significance level (alpha) determines the desired coverage probability (1 - alpha). # For example, alpha=0.1 means we want 90% prediction intervals. # Let's use the validation set as calibration set # Note: In a real-world scenario, you might want a separate calibration set. calibration_series = val alpha = 0.1 # Generate prediction intervals # The `conformal_predict` method returns a `QuantileRegression` model # which can then be used to predict intervals. # The `method` parameter can be set to 'normal' for NormalNets. # The `coverage` parameter is 1 - alpha. # For NormalNets, we need to specify the `method` as 'normal' # and the `coverage` as 1 - alpha. # The `conformal_predict` method will return a new model that can predict intervals. # The `conformal_predict` method for NormalNets requires the calibration set and the coverage level. # It returns a model that can predict intervals. # The coverage is set to 1 - alpha. # Let's assume we have a calibration set `calibration_series` and a significance level `alpha`. # The `conformal_predict` method will return a model that can predict intervals. # The coverage is set to 1 - alpha. # For NormalNets, the `conformal_predict` method is used to obtain prediction intervals. # It requires a calibration set and a coverage level (1 - alpha). # The method returns a model that can predict intervals. # Example of generating prediction intervals: # prediction_intervals = model.conformal_predict(calibration_series=calibration_series, alpha=alpha) # The above line is commented out as it requires a full run and might be slow. # In practice, you would use it like this: # prediction_intervals = model.conformal_predict(calibration_series=val, alpha=0.1) # To demonstrate the concept, let's simulate the output structure: # prediction_intervals = model.predict(n=10, num_samples=1000) # This would be a standard prediction # For conformal prediction, the output would be a PredictionIntervals object. # The following is a placeholder to show the intended usage: # prediction_intervals = model.conformal_predict(calibration_series=val, alpha=0.1) # print(prediction_intervals.median()) # print(prediction_intervals.upper_bound()) # print(prediction_intervals.lower_bound()) # Since we cannot run the full prediction here, we will skip the actual prediction and printing. # The key takeaway is the usage of `conformal_predict` with `calibration_series` and `alpha`. print("Conformal regression with NormalNets example setup complete.") print("To generate prediction intervals, use: model.conformal_predict(calibration_series=val, alpha=0.1)") ``` -------------------------------- ### Docker Dockerfile Example Source: https://unit8co.github.io/darts/_sources/examples/02-data-processing.ipynb.txt A basic Dockerfile to create a Python application image. It sets the working directory, copies files, installs dependencies, and defines the run command. ```dockerfile FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python", "app.py"] ``` -------------------------------- ### Conformal Regression with NormalPlus Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Example of using NormalPlus for conformal regression. Requires installation of the 'darts' library. ```python from darts.models.forecasting.regression_models import NormalPlus from darts.utils.timeseries_generation import linear_timeseries # Generate synthetic time series data series = linear_timeseries(start=0, end=100, freq=1) # Split data into training and validation sets train, val = series[:80], series[80:] # Initialize and train the NormalPlus model model = NormalPlus() model.fit(train, val_series=val) # Predict with conformal intervals pred = model.predict(series=train, n=20, num_samples=1000, training_sample_size=0.8) # Plot the results (requires matplotlib) # import matplotlib.pyplot as plt # series.plot(label='ground truth') # pred.plot(label='prediction with intervals') # plt.legend() ``` -------------------------------- ### Foundation Model with Naive Source: https://unit8co.github.io/darts/_sources/examples/25-FoundationModel-examples.ipynb.txt Initializes a foundation model using a Naive forecasting approach. This example configures `input_chunk_length`, `output_chunk_length`, and training epochs. ```python from darts.models.forecasting.foundationmodels import FoundationModel model = FoundationModel(model="naive", input_chunk_length=128, output_chunk_length=24, n_epochs=50, optimizer_kwargs={"lr": 1e-4}, random_state=42) ``` -------------------------------- ### Foundation Model with Kalman Source: https://unit8co.github.io/darts/_sources/examples/25-FoundationModel-examples.ipynb.txt Sets up a foundation model using a Kalman filter. This example specifies `input_chunk_length`, `output_chunk_length`, and training epochs. ```python from darts.models.forecasting.foundationmodels import FoundationModel model = FoundationModel(model="kalman", input_chunk_length=128, output_chunk_length=24, n_epochs=50, optimizer_kwargs={"lr": 1e-4}, random_state=42) ``` -------------------------------- ### Anomaly Detection with Thresholding (Alternative) Source: https://unit8co.github.io/darts/_sources/examples/22-anomaly-detection-examples.ipynb.txt An alternative implementation of threshold-based anomaly detection. This example might use different parameters or a slightly different setup compared to the first threshold example. Ensure data is preprocessed. ```python from darts.ad.detectors.threshold import ThresholdDetector # Assuming 'series' is your time series data detector = ThresholdDetector(threshold=0.7, side='upper') detector.fit(series) anomalies = detector.detect(series) ``` -------------------------------- ### Conformal Prediction with Transformer Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Example of applying conformal prediction to a Transformer model. Requires installing `numpy` and `matplotlib`. ```python from darts.models import TransformerModel from darts.datasets import AirPassengersDataset from darts.utils.likelihood_models import GaussianLikelihood from darts.utils.callbacks import ConformalIntervals # Load data pd = AirPassengersDataset().load() # Split data series = pd.astype(float) train, val = series[:-36], series[-36:] # Define model with Gaussian likelihood model = TransformerModel(likelihood=GaussianLikelihood(), input_chunk_length=12, output_chunk_length=12, n_epochs=50, random_state=42) # Define conformal intervals callback cb = ConformalIntervals(n_samples=1000, alpha=0.1) # Train model with callback model.fit(train, val_series=val, callbacks=[cb]) # Predict with conformal intervals pred_series = model.predict(n=36, num_samples=1000, callbacks=[cb]) # Plot results import matplotlib.pyplot as plt plt.figure(figsize=(10, 6)) series.plot(label='ground truth') pred_series.plot(label='prediction with intervals') plt.legend() plt.show() ``` -------------------------------- ### Kalman Filter with Custom Parameters Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt This example demonstrates initializing the Kalman filter with custom parameters for state transition, observation, and noise covariance matrices. This allows for fine-tuning the filter's behavior. ```python from darts.models import KalmanFilter import numpy as np # Define custom covariance matrices # Example: state transition covariance (Q) custom_q = np.array([[0.1, 0.05], [0.05, 0.1]]) # Example: observation covariance (R) custom_r = np.array([[0.5]]) # Example: initial state covariance (P0) custom_p0 = np.array([[1.0, 0.0], [0.0, 1.0]]) # Initialize the Kalman Filter model with custom parameters model = KalmanFilter(dim_x=2, dim_z=1, q=custom_q, r=custom_r, p0=custom_p0) # Fit and predict as usual # model.fit(series, future_covariates=future_covariates) # predictions = model.predict(n=10, future_covariates=future_covariates) ``` -------------------------------- ### Kalman Filter with Initial State and Covariance Source: https://unit8co.github.io/darts/_sources/examples/10-Kalman-filter-examples.ipynb.txt This example demonstrates how to set the initial state and its covariance for the Kalman filter. This is useful when you have prior knowledge about the system's starting condition. ```python from darts.models import KalmanForecaster from darts.utils.timeseries_generation import linear_timeseries import numpy as np # Generate a simple time series series = linear_timeseries(start=0, end=100, freq=1) # Define initial state and covariance initial_state = np.array([[5.0]]) initial_covariance = np.array([[1.0]]) # Initialize Kalman Filter with initial state and covariance kf = KalmanForecaster(dim_x=1, state_noise=0.1, obs_noise=0.1, initial_state=initial_state, initial_covariance=initial_covariance) # Fit the model kf.fit(series) # Predict prediction = kf.predict(n=10) print(prediction) ``` -------------------------------- ### Conformal Prediction with ARIMA Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Example of applying conformal prediction to an ARIMA model. Requires installing `numpy` and `matplotlib`. ```python from darts.models import ARIMA from darts.datasets import AirPassengersDataset from darts.utils.likelihood_models import GaussianLikelihood from darts.utils.callbacks import ConformalIntervals # Load data pd = AirPassengersDataset().load() # Split data series = pd.astype(float) train, val = series[:-36], series[-36:] # Define model with Gaussian likelihood model = ARIMA(likelihood=GaussianLikelihood()) # Define conformal intervals callback cb = ConformalIntervals(n_samples=1000, alpha=0.1) # Train model with callback model.fit(train, val_series=val, callbacks=[cb]) # Predict with conformal intervals pred_series = model.predict(n=36, num_samples=1000, callbacks=[cb]) # Plot results import matplotlib.pyplot as plt plt.figure(figsize=(10, 6)) series.plot(label='ground truth') pred_series.plot(label='prediction with intervals') plt.legend() plt.show() ``` -------------------------------- ### TiRexModel Initialization and Basic Usage Source: https://unit8co.github.io/darts/generated_api/darts.models.forecasting.tirex_model.html Demonstrates how to initialize the TiRexModel and use it for basic time series forecasting. Note that `accept_license=True` must be explicitly set to use the model. ```APIDOC ## TiRexModel Initialization and Fit ### Description Initializes the TiRexModel and fits it to a given time series. ### Method `fit(series: TimeSeries, past_covariates: Optional[TimeSeries] = None, future_covariates: Optional[TimeSeries] = None, val_series: Optional[TimeSeries] = None, val_past_covariates: Optional[TimeSeries] = None, val_future_covariates: Optional[TimeSeries] = None, train_sample_weight: Optional[TimeSeries] = None, val_sample_weight: Optional[TimeSeries] = None, epochs: int = None, ...) -> None` ### Parameters * **series** (TimeSeries) - The training time series. * **past_covariates** (Optional[TimeSeries]) - Past covariates to use for training. * **future_covariates** (Optional[TimeSeries]) - Future covariates to use for training. * **val_series** (Optional[TimeSeries]) - Validation time series. * **val_past_covariates** (Optional[TimeSeries]) - Past covariates for validation. * **val_future_covariates** (Optional[TimeSeries]) - Future covariates for validation. * **train_sample_weight** (Optional[TimeSeries]) - Sample weights for training. * **val_sample_weight** (Optional[TimeSeries]) - Sample weights for validation. * **epochs** (int) - Number of epochs to train for. ### Request Example ```python from darts.models import TiRexModel from darts.datasets import AirPassengersDataset series = AirPassengersDataset().load().astype("float32") # You must explicitly set `accept_license=True` to use the model model = TiRexModel( input_chunk_length=12, output_chunk_length=6, accept_license=False, # Set to True to enable model usage ) model.fit(series) ``` ## TiRexModel Predict ### Description Generates predictions using the fitted TiRexModel. ### Method `predict(n: int, series: Optional[TimeSeries] = None, past_covariates: Optional[TimeSeries] = None, future_covariates: Optional[TimeSeries] = None, num_samples: int = 1, ...) -> TimeSeries` ### Parameters * **n** (int) - The number of future time steps to predict. * **series** (Optional[TimeSeries]) - The time series to predict from. If None, the model uses the last `input_chunk_length` points of the training series. * **past_covariates** (Optional[TimeSeries]) - Past covariates to use for prediction. * **future_covariates** (Optional[TimeSeries]) - Future covariates to use for prediction. * **num_samples** (int) - The number of samples to draw for probabilistic forecasts. ### Request Example ```python pred = model.predict(n=6) print(pred.all_values()) ``` ### Response Example ```json [[[440.2505 ]] [[444.44373]] [[447.36362]] [[451.50375]] [[458.05853]] [[461.98694]]] ``` ``` -------------------------------- ### Conformal Prediction with LightGBMModel Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Demonstrates conformal prediction for the LightGBM model. This example requires the lightgbm library to be installed. ```python from darts.models import LightGBMModel # Initialize the LightGBMModel # Requires lightgbm library model = LightGBMModel(training_sample_size=0.8, n_estimators=100, random_state=42) # Fit the model to the training data model.fit(train_series) # Generate prediction intervals using conformal prediction # The alpha parameter determines the significance level. prediction_intervals = model.predict(n=30, num_samples=1000, component_index=0, alpha=0.05) # The prediction_intervals object contains the forecast and the conformal prediction intervals. ``` -------------------------------- ### Conformal Prediction with Prophet Source: https://unit8co.github.io/darts/_sources/examples/23-Conformal-Prediction-examples.ipynb.txt Example of using conformal prediction with the Prophet model. This requires the 'prophet' library to be installed. ```python from darts.models import Prophet from darts.utils.likelihood_helpers import ConformalProbabilisticModel # Assuming 'series' is a Darts TimeSeries object model = Prophet() model.fit(series) # Create a conformal probabilistic model conformal_model = ConformalProbabilisticModel(model, n_samples=500, alpha=0.1) # Generate prediction intervals prediction_intervals = conformal_model.predict(n=10, num_samples=1000, kind="interval", confidence=0.95) ```