dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.05k stars 1.88k forks source link

Fitting ForecastBySsa model: 'Function does not accept floating point Not-a-Number values.' #6411

Open mikegoatly opened 2 years ago

mikegoatly commented 2 years ago

System Information (please complete the following information):

Describe the bug When fitting the ForecastBySsa model with certain data sets a System.ArithmeticException is thrown with the message 'Function does not accept floating point Not-a-Number values.'. The call stack is:

   at System.Math.Sign(Double value)
   at Microsoft.ML.Transforms.TimeSeries.TrajectoryMatrix.FftMultiplyTranspose(Single[] vector, Single[] result, Boolean add, Int32 srcIndex, Int32 dstIndex)
   at Microsoft.ML.Transforms.TimeSeries.AdaptiveSingularSpectrumSequenceModelerInternal.ComputeNoiseMoments(Single[] series, Single[] signal, Single[] alpha, Single& observationNoiseVariance, Single& autoregressionNoiseVariance, Single& observationNoiseMean, Single& autoregressionNoiseMean, Int32 startIndex)
   at Microsoft.ML.Transforms.TimeSeries.AdaptiveSingularSpectrumSequenceModelerInternal.TrainCore(Single[] dataArray, Int32 originalSeriesLength)
   at Microsoft.ML.Transforms.TimeSeries.AdaptiveSingularSpectrumSequenceModelerInternal.Train(RoleMappedData data)
   at Microsoft.ML.Transforms.TimeSeries.SsaForecastingEstimator.Fit(IDataView input)
   at TestProject.ValuePredictor.Predict(IList`1 window, DataPointStats stats)

To Reproduce Run this code to reproduce the error:

using Microsoft.ML;
using Microsoft.ML.Transforms.TimeSeries;

using System.Collections.Generic;
using System.Linq;

record TrainingData(float Value);
record OutputData(float Value, float Value_LB, float Value_UB);
var data = new[] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,11 };

var context = new MLContext(seed: 0);
var trainingData = context.Data.LoadFromEnumerable(data.Select(x => new TrainingData(x)));

var pipeline = context.Forecasting.ForecastBySsa(
    windowSize: 28,
    seriesLength: data.Length * 2,
    trainSize: data.Length,
    horizon: 1,
    outputColumnName: nameof(OutputData.Value),
    inputColumnName: nameof(TrainingData.Value),
    confidenceLowerBoundColumn: nameof(OutputData.Value_LB),
    confidenceUpperBoundColumn: nameof(OutputData.Value_UB));

var model = pipeline.Fit(trainingData);

Expected behavior No error should occur when fitting the data.

Screenshots, Code, Sample Projects This is a .NET interactive notebook with the repro code set up and ready to run: Fitting ForecastBySsa ArithmeticException repro.zip

Additional context

michaelgsharp commented 1 year ago

Thanks for finding this. We will take a look.