roberta1899
roberta1899

Reputation: 21

Forecasting.ForecastBySsa with Multiple variables as input

I've got this code to predict a time series. I want to have a prediction based upon a time series of prices and a correlated indicator.

So together with the value to forecast, I want to pass a side value but I cannot understand if this is taken into account because prediction doesn't change with or without it. In which way do I need to tell to the algorithm how to consider these parameters?

public static TimeSeriesForecast PerformTimeSeriesProductForecasting(List<TimeSeriesData> listToForecast)
{
    var mlContext = new MLContext(seed: 1);  //Seed set to any number so you have a deterministic environment
    var productModelPath = $"product_month_timeSeriesSSA.zip";

    if (File.Exists(productModelPath))
    {
        File.Delete(productModelPath);
    }

    IDataView productDataView = mlContext.Data.LoadFromEnumerable<TimeSeriesData>(listToForecast);
    var singleProductDataSeries = mlContext.Data.CreateEnumerable<TimeSeriesData>(productDataView, false).OrderBy(p => p.Date);
    TimeSeriesData lastMonthProductData = singleProductDataSeries.Last();

    const int numSeriesDataPoints = 2500; //The underlying data has a total of 34 months worth of data for each product

    // Create and add the forecast estimator to the pipeline.
    IEstimator<ITransformer> forecastEstimator = mlContext.Forecasting.ForecastBySsa(
        outputColumnName: nameof(TimeSeriesForecast.NextClose),
        inputColumnName: nameof(TimeSeriesData.Close), // This is the column being forecasted.
        windowSize: 22, // Window size is set to the time period represented in the product data cycle; our product cycle is based on 12 months, so this is set to a factor of 12, e.g. 3.
        seriesLength: numSeriesDataPoints, // This parameter specifies the number of data points that are used when performing a forecast.
        trainSize: numSeriesDataPoints, // This parameter specifies the total number of data points in the input time series, starting from the beginning.
        horizon: 5, // Indicates the number of values to forecast; 2 indicates that the next 2 months of product units will be forecasted.
        confidenceLevel: 0.98f, // Indicates the likelihood the real observed value will fall within the specified interval bounds.
        confidenceLowerBoundColumn: nameof(TimeSeriesForecast.ConfidenceLowerBound), //This is the name of the column that will be used to store the lower interval bound for each forecasted value.
        confidenceUpperBoundColumn: nameof(TimeSeriesForecast.ConfidenceUpperBound)); //This is the name of the column that will be used to store the upper interval bound for each forecasted value.

    // Fit the forecasting model to the specified product's data series.
    ITransformer forecastTransformer = forecastEstimator.Fit(productDataView);

    // Create the forecast engine used for creating predictions.
    TimeSeriesPredictionEngine<TimeSeriesData, TimeSeriesForecast> forecastEngine = forecastTransformer.CreateTimeSeriesEngine<TimeSeriesData, TimeSeriesForecast>(mlContext);

    // Save the forecasting model so that it can be loaded within an end-user app.
    forecastEngine.CheckPoint(mlContext, productModelPath);
    ITransformer forecaster;
    using (var file = File.OpenRead(productModelPath))
    {
        forecaster = mlContext.Model.Load(file, out DataViewSchema schema);
    }

    // We must create a new prediction engine from the persisted model.
    TimeSeriesPredictionEngine<TimeSeriesData, TimeSeriesForecast> forecastEngine2 = forecaster.CreateTimeSeriesEngine<TimeSeriesData, TimeSeriesForecast>(mlContext);

    // Get the prediction; this will include the forecasted product units sold for the next 2 months since this the time period specified in the `horizon` parameter when the forecast estimator was originally created.
    prediction = forecastEngine.Predict();
    return prediction;
}

TimeSeriesData has multiple attributes, not only the value of the series that I ant to forecast. Just wonder if they are taken into account when forecasting o not. Is there a better method to forecast this type of series like LMST? Is this method available in ML.NET?

Upvotes: 2

Views: 1499

Answers (1)

GarethReid
GarethReid

Reputation: 418

There is a new ticket for enhancement: Multivariate Time based series forecasting to ML.Net

See ticket: github.com/dotnet/machinelearning/issues/5638

Upvotes: 1

Related Questions