ML.NET Time Series algorithms to predict future forecasts

Kalyan A 440 Reputation points
2025-02-23T15:10:24.8666667+00:00

I got the suggestion from github copilot , SSA timeseries algorithm from ML.NET

I got NIFTY BANK csv from the below file and tried to run forescast.

https://www.nseindia.com/reports-indices-historical-index-data

The last record is

Last Record: 21-Feb-2025 49306.55

Predicted Nifty High for Day 1: 49243.586

Predicted Nifty High for Day 60: 49510.223

The file has 250 records , I the below parameters

windowSize: 120,

seriesLength:250,

trainSize: 250

Do you have any suggestions on what should be the ideal

windowSize, seriesLength and trainSize

using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.TimeSeries;
using System.Globalization;
public class NiftyData
{
    [LoadColumn(0)]
    public DateTime Date { get; set; }
    [LoadColumn(1)]
    public float Open { get; set; }
    [LoadColumn(2)]
    public float High { get; set; }
    [LoadColumn(3)]
    public float Low { get; set; }
    [LoadColumn(4)]
    public float Close { get; set; }
    [LoadColumn(5)]
    public float SharesTraded { get; set; }
    [LoadColumn(6)]
    public float Turnover { get; set; }
}
public class NiftyPrediction
{
    [ColumnName("ForecastedHigh")]
    public float[] ForecastedHigh { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        // Initialize MLContext
        var context = new MLContext();
        // Specify the full path of the CSV file on the C: drive
        string filePath = @"C:\ML\NIFTYBANK.csv";
        // Load data from the file
        var data = context.Data.LoadFromTextFile<NiftyData>(filePath, separatorChar: ',', hasHeader: true);
        var enumerableData = context.Data.CreateEnumerable<NiftyData>(data, reuseRowObject: false);
        NiftyData lastRecord = enumerableData.LastOrDefault();
        Console.WriteLine("Last Record: " + lastRecord.Date.ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture) + " " + lastRecord.High);
        // Get the last record
        // Convert IDataView to IEnumerable<NiftyData>
        // Define the pipeline to create features and train the time series model
        var pipeline = context.Forecasting.ForecastBySsa(
            outputColumnName: "ForecastedHigh",
            inputColumnName: "High",
            windowSize: 120,
            seriesLength:250,
            trainSize: 250,
            horizon: 60);
        // Train the model
        var model = pipeline.Fit(data);
        // Create a prediction engine
        var forecastingEngine = model.CreateTimeSeriesEngine<NiftyData, NiftyPrediction>(context);
        // Forecast for the next 3 months (typically, 3 months = ~60 trading days)
        var futurePredictions = forecastingEngine.Predict();
        for (int i = 0; i < futurePredictions.ForecastedHigh.Length; i++)
        {
            Console.WriteLine($"Predicted Nifty High for Day {i + 1}: {futurePredictions.ForecastedHigh[i]}");
        }
    }
}
.NET Training
.NET Training
.NET: Microsoft Technologies based on the .NET software framework.Training: Instruction to develop new skills.
21 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pradeep M 6,420 Reputation points Microsoft External Staff
    2025-02-24T04:02:07.23+00:00

    Hi Kalyan A,

    Thank you for reaching out to Microsoft Q & A forum. 

    You're using the SSA (Singular Spectrum Analysis) algorithm in ML.NET to predict future Nifty Bank high prices based on historical data. Your current parameters are: 

    Window Size = 120 (number of past observations used for each prediction) 

    series Length = 250 (total length of the time series considered) 

    train Size = 250 (amount of data used for training) 

    horizon = 60 (number of future days being predicted)  

    Suggested Improvements: 

    1.Window Size (120) – This determines how much past data influences the forecast. A smaller value (e.g., 80–100) may be better for short-term trends, while a larger one (e.g., 150–200) captures long-term patterns. 

    2.Series Length (250) – Ideally, this should be at least 1.5x–3x the window size to capture historical trends effectively. Since your total data is 250, reducing this to around 180–220 could improve performance. 

    3.Training Size (250) – Since you’re using the full dataset for training, testing with a slightly smaller value (200–230) may improve generalization. 

    4.Horizon (60) – Predicting 60 days ahead can be challenging, as accuracy tends to decrease over longer timeframes. You might get better results by first testing a 15–30 day forecast, then gradually increasing it.  

    Recommended Adjustments: 

    Window Size: 80–150 

    Series Length: 180–220 

    Training Size: 200–230 

    Horizon: Start with 15–30 days, then scale up to 60 if results remain accurate 

    Please feel free to contact us if you have any additional questions.     

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.  

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.