### Install Required Libraries Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/ElectricityLoadForecasting.ipynb Installs the necessary packages for running the electricity load forecasting tutorial. ```bash # !pip install statsforecast "neuralprophet[live]" prophet ``` -------------------------------- ### Install dependencies Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/experiments/Prophet_spark_m5.ipynb Install the necessary libraries for forecasting and distributed execution. ```bash pip install prophet "neuralforecast<1.0.0" "statsforecast[fugue]" ``` -------------------------------- ### Install Mintlify CLI Source: https://github.com/nixtla/statsforecast/blob/main/CONTRIBUTING.md Installs the Mintlify documentation tool globally via npm. ```bash npm i -g mint ``` -------------------------------- ### Install development dependencies Source: https://github.com/nixtla/statsforecast/blob/main/CONTRIBUTING.md Install project dependencies using uv. ```bash uv pip install -r setup.py --extra dev ``` -------------------------------- ### Install datasetsforecast Library Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/experiments/AmazonStatsForecast.ipynb Install the datasetsforecast library, which provides benchmark datasets and evaluation utilities for time series models. ```bash %%capture !pip install datasetsforecast ``` -------------------------------- ### Install Nixtla libraries Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/StatisticalNeuralMethods.ipynb Install the required forecasting libraries and dependencies. ```python %%capture !pip install statsforecast mlforecast neuralforecast datasetforecast s3fs pyarrow ``` -------------------------------- ### Install yfinance Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/GARCH_tutorial.ipynb Command to install the yfinance library. ```bash pip install yfinance ``` -------------------------------- ### Install StatsForecast Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/getting-started/getting_Started_short.ipynb Use this command to install the library if it is not already present in your environment. ```python # uncomment the following line to install the library # %pip install statsforecast ``` -------------------------------- ### Install StatsForecast Source: https://github.com/nixtla/statsforecast/blob/main/experiments/arima_prophet_adapter/README.md Use pip to install the StatsForecast library. ```bash pip install statsforecast ``` -------------------------------- ### Install Statsforecast Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/MFLES.ipynb Install the necessary library to use MFLES. ```python # %pip install statsforecast ``` -------------------------------- ### Install uv Source: https://github.com/nixtla/statsforecast/blob/main/experiments/vn1-competition/README.md Install the uv package manager using pip. This is the first step to setting up the project environment. ```bash pip install uv ``` -------------------------------- ### Preview documentation locally Source: https://github.com/nixtla/statsforecast/blob/main/CONTRIBUTING.md Starts a local server to view the generated documentation. ```bash make preview_docs ``` -------------------------------- ### Setup and Execution Commands Source: https://github.com/nixtla/statsforecast/blob/main/experiments/theta/README.md Commands to initialize the environment, run specific model experiments, and evaluate performance. ```bash conda env create -f environment.yml ``` ```bash conda activate theta ``` ```bash python -m src.theta --dataset M3 --group [group] --model [model] ``` ```bash python -m src.evaluation ``` -------------------------------- ### Install StatsForecast with Extras Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/getting-started/installation.ipynb Install StatsForecast with additional features like polars, plotly, dask, spark, or ray by specifying them in the install command. For example, to install with polars and plotly support, use 'statsforecast[polars,plotly]'. ```shell pip install 'statsforecast[extra1,extra2]' ``` -------------------------------- ### Install StatsForecast and Load Data Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/how-to-guides/Automatic_Forecasting.ipynb Install the library via pip and load the Air Passengers dataset for demonstration. ```python # uncomment the following line to install the library # %pip install statsforecast ``` ```python from statsforecast.utils import AirPassengersDF ``` ```python Y_df = AirPassengersDF ``` -------------------------------- ### Install StatsForecast and s3fs Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/experiments/AmazonStatsForecast.ipynb Installs the StatsForecast library and s3fs for reading data from AWS S3. Ensure StatsForecast is already installed. ```python %%capture !pip install statsforecast s3fs ``` -------------------------------- ### Data Processing and Visualization Setup Source: https://github.com/nixtla/statsforecast/blob/main/scripts/cli.ipynb Initial data manipulation and plotting setup for store transaction analysis. ```python n transactions.set_index(['store_nbr', 'date'], inplace=True) balanced_df.set_index(['store_nbr', 'date'], inplace=True) transactions = balanced_df.merge(transactions, how='left', left_on=['store_nbr', 'date'], right_index=True).reset_index() #check_nans(transactions) transactions = transactions.sort_values(by=['store_nbr', 'date']) trans_values = transactions.transactions.values trans_values = trans_values.reshape(len(filter_stores), len(filter_dates)) trans_values = numpy_ffill(trans_values) trans_values = np.nan_to_num(trans_values) trans_values = trans_values.T trans_values = trans_values > 0 trans_columns = [f'transactions_store_[{x}]' for x in filter_stores] transactions_agg = pd.DataFrame(trans_values, columns=trans_columns) transactions_agg['date'] = filter_dates # for x in STORES: # transactions_agg[f'transactions_store_[{x}]'] = \ # normalizer.fit_transform(transactions_agg[f'transactions_store_[{x}]'].values[:,None]) if verbose: plt.figure(num=1, figsize=(7, 3), dpi=80, facecolor='w') plt.plot(transactions_agg['transactions_store_[45]'], label='transactions_store_[45]') plt.plot(transact ``` -------------------------------- ### Install Development Version from GitHub Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/getting-started/installation.ipynb Install the development version directly from the GitHub repository. This method requires a C++ compiler and is an alternative if nightly wheels are not preferred. ```shell pip install git+https://github.com/Nixtla/statsforecast ``` -------------------------------- ### Install StatsForecast with Ray and other dependencies Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/experiments/ETS_ray_m5.ipynb Installs the necessary libraries for StatsForecast, Ray, NeuralForecast, and data handling. ```python %%capture !pip install "statsforecast[ray]" neuralforecast s3fs pyarrow ``` -------------------------------- ### Initialize Prophet environment Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/experiments/Prophet_spark_m5.ipynb Setup logging and imports for Prophet forecasting on Spark. ```python import logging from time import time import pandas as pd from neuralforecast.data.datasets.m5 import M5, M5Evaluation from prophet import Prophet from pyspark.sql.types import * # disable informational messages from prophet logging.getLogger('py4j').setLevel(logging.ERROR) ``` -------------------------------- ### Setup Conda Environment Source: https://github.com/nixtla/statsforecast/blob/main/experiments/arima_prophet_adapter/README.md Commands to create and activate the environment for running experiments. ```shell conda env create -f environment.yml ``` ```shell conda activate arima_prophet ``` -------------------------------- ### Install StatsForecast Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/ConformalPrediction.ipynb Installs or updates the StatsForecast library using pip. ```python %%capture pip install statsforecast -U ``` -------------------------------- ### Import Libraries and Setup for StatsForecast Source: https://github.com/nixtla/statsforecast/blob/main/experiments/bigquery/src/statsforecast-fugue-liquor-sales.ipynb Imports necessary libraries for StatsForecast, Fugue, and BigQuery integration. Includes setup for notebook environments. ```python import pandas as pd import fugue_bigquery.api as fbqa from fugue_notebook import setup from statsforecast import StatsForecast from statsforecast.distributed.fugue import FugueBackend from statsforecast.models import * setup(is_lab=True) ``` -------------------------------- ### Setup pre-commit hooks Source: https://github.com/nixtla/statsforecast/blob/main/CONTRIBUTING.md Initialize pre-commit hooks to run automated checks before committing. ```bash pre-commit install ``` -------------------------------- ### Sync Dependencies with uv Source: https://github.com/nixtla/statsforecast/blob/main/experiments/vn1-competition/README.md Create a virtual environment and install all project dependencies using uv. This command reads from the project's dependency files. ```bash uv sync ``` -------------------------------- ### Install project dependencies Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/contribute/step-by-step.md Use mamba to update the environment based on the project's configuration file. ```bash mamba env update -f environment.yml ``` ```bash mamba env update -f dev/environment.yml ``` -------------------------------- ### Install MLForecast Dependencies Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/StatisticalNeuralMethods.ipynb Installs the lightgbm and xgboost libraries, which are often used with MLForecast for gradient boosting models. ```bash !pip install lightgbm xgboost ``` -------------------------------- ### Create Conda Environment Source: https://github.com/nixtla/statsforecast/blob/main/experiments/benchmarks_at_scale/README.md Installs the necessary packages for the benchmarks using a conda environment file. Ensure you have conda installed. ```bash conda env create -f environment.yml ``` -------------------------------- ### Launch Jupyter Lab Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/contribute/step-by-step.md Start the development environment using Jupyter Lab. ```bash jupyter lab ``` -------------------------------- ### Minimal StatsForecast Example Source: https://github.com/nixtla/statsforecast/blob/main/README.md A basic example demonstrating how to initialize StatsForecast with an AutoARIMA model, fit it to the AirPassengers dataset, and generate forecasts with confidence intervals. ```python from statsforecast import StatsForecast from statsforecast.models import AutoARIMA from statsforecast.utils import AirPassengersDF df = AirPassengersDF sf = StatsForecast( models=[AutoARIMA(season_length=12)], freq='ME', ) sf.fit(df) sf.predict(h=12, level=[95]) ``` -------------------------------- ### Install StatsForecast Package Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/AnomalyDetection.ipynb Use pip to install or upgrade the statsforecast package. This is the first step before using any of its functionalities. ```python pip install statsforecast -U ``` -------------------------------- ### Install library in editable mode Source: https://github.com/nixtla/statsforecast/blob/main/CONTRIBUTING.md Use this command to link the package to the source code for immediate reflection of changes. ```bash uv pip install --no-build-isolation -e . ``` -------------------------------- ### Install MLFlow and MLFlavors Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/MLFlow.ipynb Installs the required MLFlow and MLFlavors libraries using pip. This is necessary for MLFlow integration. ```bash pip install mlflow mlflavors ``` -------------------------------- ### Configure Environment for Statsforecast Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/AutoTheta.ipynb Initial setup to suppress warnings, set logging levels, and configure pandas display options for cleaner output. ```python #| echo: false # This cell will not be rendered but is to hide warnings and limit the rows shown import warnings warnings.filterwarnings("ignore") import logging logging.getLogger('statsforecast').setLevel(logging.ERROR) import pandas as pd pd.set_option('display.max_rows', 6) ``` -------------------------------- ### Install StatsForecast with Ray Support Source: https://github.com/nixtla/statsforecast/blob/main/experiments/ray/README.md Install StatsForecast with the necessary dependencies for Ray integration. This command ensures all required packages are included for distributed execution. ```bash pip install "statsforecast[ray]" ``` -------------------------------- ### Launch Ray Cluster Source: https://github.com/nixtla/statsforecast/blob/main/experiments/ray/README.md Start a Ray cluster using a provided configuration file. Ensure you obtain the Ray address for subsequent experiment execution. ```bash ray up cluster.yaml ``` -------------------------------- ### Install yfinance Package Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/GARCH_tutorial.ipynb Installs the yfinance package, which is used to download historical stock price data from Yahoo! Finance for the tutorial. ```python %%capture pip install yfinance ``` -------------------------------- ### Configure Multiprocessing Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/IntermittentData.ipynb Set the start method for multiprocessing to spawn. ```python import multiprocessing as mp ``` ```python mp.set_start_method('spawn') ``` -------------------------------- ### MLFlow Integration Setup Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/MLFlow.ipynb Imports necessary libraries for MLFlow integration, including StatsForecast models and metrics calculation. ```python import pandas as pd import mlflow from sklearn.metrics import mean_absolute_error, mean_absolute_percentage_error from statsforecast import StatsForecast from statsforecast.models import AutoARIMA import mlflavors import requests ``` -------------------------------- ### View MLFlow UI Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/MLFlow.ipynb Command to launch the MLFlow UI to view experiments and logged artifacts. Ensure MLFlow is installed. ```bash mlflow ui ``` -------------------------------- ### Import StatsForecast and SESOptimized Model Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/SimpleExponentialOptimized.ipynb Import necessary classes from the statsforecast library. This is the initial setup for using the library. ```python from statsforecast import StatsForecast from statsforecast.models import SimpleExponentialSmoothingOptimized ``` -------------------------------- ### Instantiate MLForecast with Models and Features Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/StatisticalNeuralMethods.ipynb Instantiate the MLForecast object with a list of models, data frequency, lags, lag transformations, and date features. This setup is for daily data with specific lag transformations and date-based regressors. ```python mlf = MLForecast( models=[LGBMRegressor(), XGBRegressor(), LinearRegression()], # List of models for forecasting: LightGBM, XGBoost and Linear Regression freq='D', # Frequency of the data - 'D' for daily frequency lags=list(range(1, 7)), # Specific lags to use as regressors: 1 to 6 days lag_transforms = { 1: [expanding_mean], # Apply expanding mean transformation to the lag of 1 day }, date_features=['year', 'month', 'day', 'dayofweek', 'quarter', 'week'], # Date features to use as regressors ) ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/nixtla/statsforecast/blob/main/experiments/bigquery/src/statsforecast-fugue-citibikes-trips.ipynb Sets up the notebook environment and imports necessary libraries for Fugue and StatsForecast. ```python from typing import Optional import fugue_bigquery.api as fbqa import pandas as pd from fugue_notebook import setup from statsforecast import StatsForecast from statsforecast.distributed.fugue import FugueBackend from statsforecast.models import * setup(is_lab=True) ``` -------------------------------- ### Instantiate Models and Set Parameters Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/IMAPA.ipynb Import and instantiate models, setting parameters like season_length for hourly data and defining the forecast horizon. ```python season_length = 24 # Hourly data horizon = len(test) # number of predictions models = [IMAPA()] ``` -------------------------------- ### Instantiate Forecasting Models Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/getting-started/getting_Started_complete.ipynb Create a list of model instances for training. Specify parameters like season_length where necessary. For example, SeasonalNaive and DOT are instantiated with a season_length of 24. ```python # Create a list of models and instantiation parameters models = [ HoltWinters(), Croston(), SeasonalNaive(season_length=24), HistoricAverage(), DOT(season_length=24) ] ``` -------------------------------- ### Reproduce experimental results via CLI Source: https://github.com/nixtla/statsforecast/blob/main/experiments/arima/README.md Use these commands to initialize the environment, run specific model experiments, and evaluate forecast performance. ```bash make init ``` ```bash make run_module module="python -m src.[model] --dataset M4 --group [group]" ``` ```bash make run_module module="python -m src.data --dataset M4 --group [group]" ``` ```bash make run_module module="Rscript src/arima_r.R [group]" ``` ```bash make run_module module ="python -m src.evaluation" ``` -------------------------------- ### Initialize Spark and StatsForecast backend Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/experiments/Prophet_spark_m5.ipynb Set up the Spark session and the Fugue backend for distributed forecasting. ```python from time import time from neuralforecast.data.datasets.m5 import M5, M5Evaluation from statsforecast.distributed.utils import forecast from statsforecast.distributed.fugue import FugueBackend from statsforecast.models import ETS, SeasonalNaive from statsforecast.core import StatsForecast from pyspark.sql import SparkSession ``` ```python spark = SparkSession.builder.getOrCreate() backend = FugueBackend(spark, {"fugue.spark.use_pandas_udf":True}) ``` -------------------------------- ### Initialize Docker Environment Source: https://github.com/nixtla/statsforecast/blob/main/experiments/arima_xreg/README.md Creates a Docker image containing all necessary dependencies for the experiments. ```bash make init ``` -------------------------------- ### Install Mamba Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/contribute/step-by-step.md Use this command to install Mamba, a faster alternative to Conda for environment management. ```bash conda install mamba -c conda-forge ``` -------------------------------- ### Install Seaborn Library Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/StatisticalNeuralMethods.ipynb Installs the seaborn library, which is used for data visualization. This is a prerequisite for generating plots. ```python %%capture !pip install seaborn ``` -------------------------------- ### StatsForecast Baseline Models Source: https://context7.com/nixtla/statsforecast/llms.txt Demonstrates how to initialize and use basic forecasting models like Naive, SeasonalNaive, HistoricAverage, WindowAverage, and RandomWalkWithDrift for benchmarking. ```python from statsforecast import StatsForecast from statsforecast.models import ( Naive, SeasonalNaive, HistoricAverage, WindowAverage, RandomWalkWithDrift ) from statsforecast.utils import AirPassengersDF df = AirPassengersDF sf = StatsForecast( models=[ Naive(), SeasonalNaive(season_length=12), HistoricAverage(), WindowAverage(window_size=3), RandomWalkWithDrift() ], freq='ME' ) forecasts = sf.forecast(df=df, h=12, level=[95]) # Compare models print(forecasts[['ds', 'Naive', 'SeasonalNaive', 'HistoricAverage', 'WindowAverage', 'RWD']].head()) ``` -------------------------------- ### Install StatsForecast with Conda Source: https://github.com/nixtla/statsforecast/blob/main/README.md Install the StatsForecast library using Conda. This is recommended if you are using the Conda package manager. ```conda conda install -c conda-forge statsforecast ``` -------------------------------- ### Install library in editable mode Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/contribute/step-by-step.md Install the package in editable mode to link source code directly to the Python environment. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/GARCH_tutorial.ipynb Set the NIXTLA_ID_AS_COL environment variable and import necessary libraries. ```python import os os.environ['NIXTLA_ID_AS_COL'] = '1' import yfinance as yf import pandas as pd ``` -------------------------------- ### Navigate to Project Directory Source: https://github.com/nixtla/statsforecast/blob/main/experiments/vn1-competition/README.md Change the current directory to the vn1-competition project folder. This is necessary before synchronizing dependencies. ```bash cd experiments/vn1-competition ``` -------------------------------- ### Install Specific Version of StatsForecast Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/getting-started/installation.ipynb To ensure stability, it is recommended to install a specific version of StatsForecast. Replace '1.0.0' with the desired version number. ```shell pip install statsforecast=="1.0.0" ``` -------------------------------- ### StatsForecast Initialization and Fitting Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/CrostonOptimized.ipynb Demonstrates how to initialize StatsForecast with specified models and frequency, and then fit the models to training data. ```APIDOC ## StatsForecast Initialization and Fitting ### Description Instantiate a StatsForecast object with a list of models, data frequency, and optionally configure parallel processing and fallback models. Then, fit the initialized models to the historical training data. ### Method Python Class Instantiation and Method Call ### Parameters #### StatsForecast Constructor Parameters - **models** (list) - Required - A list of models to be used for forecasting. - **freq** (string) - Required - The frequency of the time series data (e.g., 'h' for hourly). - **n_jobs** (int) - Optional - Number of jobs to use for parallel processing. Use -1 for all available cores. - **fallback_model** (model object) - Optional - A model to use if another model fails during fitting or forecasting. #### fit Method Parameters - **df** (pandas DataFrame) - Required - The historical training data. ### Request Example ```python from statsforecast import StatsForecast from statsforecast.models import CrostonOptimized # Define models and frequency models = [CrostonOptimized()] freq = 'h' # Instantiate StatsForecast sf = StatsForecast(models=models, freq=freq, n_jobs=-1) # Fit the models to the training data sf.fit(df=train) ``` ### Response Example ``` StatsForecast(models=[CrostonOptimized]) ``` ``` -------------------------------- ### Install Development Version from Nightly Wheels Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/getting-started/installation.ipynb Install the latest development version of StatsForecast from nightly wheels. This option is recommended for trying new features before they are released. ```shell pip install --extra-index-url=http://nixtla-packages.s3-website.us-east-2.amazonaws.com --trusted-host nixtla-packages.s3-website.us-east-2.amazonaws.com statsforecast ``` -------------------------------- ### Cell Result Inspection Source: https://github.com/nixtla/statsforecast/blob/main/scripts/cli.ipynb Example output of a notebook cell inspection. ```python {'cell_type': 'code', 'execution_count': None, 'metadata': {}, 'outputs': [], 'source': '#| default_exp favorita', 'idx_': 0} ``` -------------------------------- ### Create and Forecast with Ray Dataset Source: https://context7.com/nixtla/statsforecast/llms.txt Demonstrates how to create a Pandas DataFrame, convert it to a Ray Dataset, and then use StatsForecast to generate forecasts. Ray backend is automatically detected. ```python import pandas as pd import numpy as np import ray from statsforecast import StatsForecast from statsforecast.models import AutoARIMA df = pd.DataFrame({ 'unique_id': np.repeat(range(1000), 100), 'ds': pd.date_range('2020-01-01', periods=100).tolist() * 1000, 'y': np.random.randn(100000) }) # Convert to Ray Dataset ray_df = ray.data.from_pandas(df) sf = StatsForecast( models=[AutoARIMA(season_length=7)], freq='D' ) # Ray backend is automatically detected forecasts = sf.forecast(df=ray_df, h=7) ray.shutdown() ``` -------------------------------- ### Perform Basic Simulation Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/Simulation.ipynb Initialize a model and generate 100 trajectories for a 48-hour horizon using the default Normal distribution. ```python # Initialize the model models = [AutoARIMA(season_length=24)] sf = StatsForecast(models=models, freq=1, n_jobs=1) # Simulate 100 paths for a horizon of 48 hours n_paths = 100 h = 48 # The simulation method handles the fitting (if needed) and the simulation sims = sf.simulate(df=df, h=h, n_paths=n_paths, seed=42) sims.head() ``` -------------------------------- ### Initialize GARCH and ARCH Models Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/GARCH_tutorial.ipynb Import necessary models and define a list of model instances with specific parameters for fitting. ```python from statsforecast.models import ( GARCH, ARCH, Naive ) models = [ARCH(1), ARCH(2), GARCH(1,1), GARCH(1,2), GARCH(2,2), GARCH(2,1), Naive() ] ``` -------------------------------- ### Import Libraries for Plotting Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/StatisticalNeuralMethods.ipynb Imports matplotlib.pyplot and seaborn for creating visualizations. Ensure these libraries are installed before running. ```python import matplotlib.pyplot as plt import seaborn as sns ``` -------------------------------- ### Configure Plotting Environment Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/AutoCES.ipynb Setup for Matplotlib and Seaborn with a custom dark theme for time series visualization. ```python import matplotlib.pyplot as plt import seaborn as sns from statsmodels.graphics.tsaplots import plot_acf from statsmodels.graphics.tsaplots import plot_pacf plt.style.use('fivethirtyeight') plt.rcParams['lines.linewidth'] = 1.5 dark_style = { 'figure.facecolor': '#212946', 'axes.facecolor': '#212946', 'savefig.facecolor':'#212946', 'axes.grid': True, 'axes.grid.which': 'both', 'axes.spines.left': False, 'axes.spines.right': False, 'axes.spines.top': False, 'axes.spines.bottom': False, 'grid.color': '#2A3459', 'grid.linewidth': '1', 'text.color': '0.9', 'axes.labelcolor': '0.9', 'xtick.color': '0.9', 'ytick.color': '0.9', 'font.size': 12 } plt.rcParams.update(dark_style) from pylab import rcParams rcParams['figure.figsize'] = (18,7) ``` -------------------------------- ### Initialize logging and warnings Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/distributed/ray.ipynb Configures logging and warning filters for the environment. ```python #| echo: false import logging import warnings ``` ```python #| echo: false warnings.simplefilter('ignore') logging.getLogger('statsforecast').setLevel(logging.ERROR) ``` -------------------------------- ### Initialize DynamicOptimizedTheta Model Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/DynamicOptimizedTheta.ipynb Import and configure the DynamicOptimizedTheta model for forecasting. ```python from statsforecast import StatsForecast from statsforecast.models import DynamicOptimizedTheta ``` ```python season_length = 12 # Monthly data horizon = len(test) # number of predictions # We call the model that we are going to use models = [DynamicOptimizedTheta(season_length=season_length, decomposition_type="additive")] # multiplicative additive ``` -------------------------------- ### Initialize Dask Client Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/StatisticalNeuralMethods.ipynb Sets up the Dask client and execution engine for distributed processing. ```python client = Client() # without this, dask is not in distributed mode # fugue.dask.dataframe.default.partitions determines the default partitions for a new DaskDataFrame engine = DaskExecutionEngine({"fugue.dask.dataframe.default.partitions": 96}) ``` -------------------------------- ### Configure Logging and Warnings Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/MLFlow.ipynb Sets up logging levels for StatsForecast and MLFlow, and ignores warnings. ```python #| echo: false warnings.simplefilter('ignore') logging.getLogger('statsforecast').setLevel(logging.ERROR) logging.getLogger("mlflow").setLevel(logging.ERROR) ``` -------------------------------- ### Generate Time Series Data Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/tutorials/MLFlow.ipynb Generates synthetic time series data for demonstration purposes. Ensure 'statsforecast' is installed. ```python from statsforecast.utils import generate_series ``` ```python series = generate_series(5, min_length=50, max_length=50, equal_ends=True, n_static_features=1) series.head() ``` -------------------------------- ### Activate Conda Environment Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/contribute/step-by-step.md Activates the 'mlforecast' Conda environment. Ensure this is done before installing packages or running project code. ```bash conda activate mlforecast ``` -------------------------------- ### Download Competition Data Source: https://github.com/nixtla/statsforecast/blob/main/experiments/vn1-competition/README.md Download the necessary datasets for the VN1 competition using the provided make command. Ensure the environment is set up before running this. ```bash make download_data ``` -------------------------------- ### Execute ADF Test on Time Series Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/ARCH.ipynb Example call to the ADF test function using a specific column from a dataframe. ```python Augmented_Dickey_Fuller_Test_func(df["y"],'S&P500') ``` -------------------------------- ### Comparing Multiple Forecasting Models Source: https://github.com/nixtla/statsforecast/blob/main/docs/src/core/models.html.md Demonstrates how to aggregate multiple statistical models to compare their performance on a monthly frequency dataset. ```python from statsforecast import StatsForecast from statsforecast.models import ( AutoARIMA, AutoETS, SeasonalNaive, Theta, HistoricAverage ) # Combine multiple models for comparison models = [ AutoARIMA(season_length=12), AutoETS(season_length=12), SeasonalNaive(season_length=12), Theta(season_length=12), HistoricAverage() ] sf = StatsForecast(models=models, freq='M', n_jobs=-1) forecasts = sf.forecast(df=df, h=12, level=[80, 95]) ``` -------------------------------- ### Retrieve Fitted Model Details Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/CrostonOptimized.ipynb Shows how to access the details of a fitted model, specifically the 'Croston optimized Model' in this example. ```APIDOC ## Retrieve Fitted Model Details ### Description Access the parameters or results of a specific fitted model from the `StatsForecast` object. This example retrieves the `model_` attribute of the first fitted model. ### Method Python Attribute Access ### Parameters - **sf.fitted_**: Accesses the list of fitted models. - **[0,0]**: Indexing to select a specific fitted model (e.g., the first model). - **.model_**: Attribute to retrieve the model's internal state or results. ### Request Example ```python result = sf.fitted_[0,0].model_ print(result) ``` ### Response Example ```json { "mean": [27.41841685] } ``` ``` -------------------------------- ### Import StatsForecast Models Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/getting-started/getting_Started_short.ipynb Import the necessary classes to initialize the forecasting model. ```python from statsforecast import StatsForecast from statsforecast.models import AutoARIMA ``` -------------------------------- ### Basic Forecasting with Pandas DataFrame Source: https://github.com/nixtla/statsforecast/blob/main/docs/src/core/distributed.fugue.html.md Standard StatsForecast usage for cross-validation with a pandas DataFrame. This example does not use distributed backends. ```python from statsforecast import StatsForecast from statsforecast.models import AutoETS from statsforecast.utils import generate_series # Generate data series = generate_series(n_series=4) # Standard usage (pandas/polars) sf = StatsForecast( models=[AutoETS(season_length=7)], freq='D', ) # Forecast with pandas DataFrame sf.cross_validation( df=series, h=7, step_size=24, n_windows=2, level=[90] ).head() ``` -------------------------------- ### Basic Forecasting with StatsForecast Source: https://github.com/nixtla/statsforecast/blob/main/docs/src/core/core.html.md Demonstrates how to instantiate the StatsForecast class and perform efficient forecasting on a panel dataframe. ```python from statsforecast import StatsForecast from statsforecast.models import AutoARIMA, Naive from statsforecast.utils import generate_series # Generate example data panel_df = generate_series(n_series=9, equal_ends=False, engine='pandas') # Instantiate StatsForecast class fcst = StatsForecast( models=[AutoARIMA(), Naive()], freq='D', n_jobs=1, verbose=True ) # Efficiently predict fcsts_df = fcst.forecast(df=panel_df, h=4, fitted=True) ``` -------------------------------- ### Update Conda Environment from File Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/contribute/step-by-step.md Updates the current Conda environment using specifications from 'environment.yml'. This installs all required project dependencies. ```bash mamba env update -f environment.yml ``` -------------------------------- ### Initialize and Forecast with Sklearn Models Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/how-to-guides/sklearn_models.ipynb Initializes StatsForecast with Lasso and Ridge models wrapped in SklearnModel, forecasts future values, calculates conformal prediction intervals, and plots the series. ```python sf = StatsForecast( models=[ SklearnModel(Lasso()), SklearnModel(Ridge()), ], freq=1, ) preds = sf.forecast( df=train_features, h=horizon, X_df=valid_features, prediction_intervals=ConformalIntervals(n_windows=4, h=horizon), level=[95], ) plot_series(train, preds, level=[95], palette='tab20b', max_ids=4) ``` -------------------------------- ### Import Core Libraries Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/models/AutoARIMA.ipynb Imports essential Python libraries for numerical operations, data manipulation, and statistical analysis. Ensure these are installed before running. ```python import numpy as np import pandas as pd import scipy.stats as stats ``` -------------------------------- ### Sync dependencies and build documentation Source: https://github.com/nixtla/statsforecast/blob/main/CONTRIBUTING.md Synchronizes development dependencies and triggers the documentation build process. ```bash uv sync --group dev make all_docs ``` -------------------------------- ### Import M5 Evaluation Utilities Source: https://github.com/nixtla/statsforecast/blob/main/nbs/docs/experiments/AmazonStatsForecast.ipynb Import necessary classes from the datasetsforecast library for M5 dataset loading and evaluation. ```python from datasetsforecast.m5 import M5, M5Evaluation ``` -------------------------------- ### Dask Distributed Setup Source: https://github.com/nixtla/statsforecast/blob/main/docs/src/core/distributed.fugue.html.md Initializes a Dask client and execution engine for distributed computing with StatsForecast. The input DataFrame is converted to a Dask DataFrame. ```python import dask.dataframe as dd from dask.distributed import Client from fugue_dask import DaskExecutionEngine from statsforecast import StatsForecast from statsforecast.models import Naive from statsforecast.utils import generate_series # Generate synthetic panel data df = generate_series(10) df['unique_id'] = df['unique_id'].astype(str) df = dd.from_pandas(df, npartitions=10) # Instantiate Dask client and execution engine dask_client = Client() engine = DaskExecutionEngine(dask_client=dask_client) # Create StatsForecast instance sf = StatsForecast(models=[Naive()], freq='D') ```