private void Forecast_Calc_Fill()
        {//using WekaForcaster to culclate the predicted data for our current signal data.
         //and show it in a chart for the user
            try
            {
                // path to the signal data - found in the project folder ..WekaForecasting\bin\Debug
                String pathToData = ".\\Signal.arff";

                // load the signal data
                Instances data = new Instances(new java.io.BufferedReader(new java.io.FileReader(pathToData)));

                // new forecaster
                WekaForecaster forecaster = new WekaForecaster();

                // set the target we want to forecast - the attribute in our arff file.
                forecaster.setFieldsToForecast("frequency");

                // default underlying classifier is SMOreg (SVM) - we'll use
                // MultilayerPerceptron for regression instead
                forecaster.setBaseForecaster(new MultilayerPerceptron());

                // set the attribute in our arff file that will be our time stamp.
                forecaster.getTSLagMaker().setTimeStampField("time");

                // set the lag creation to the data
                forecaster.getTSLagMaker().setMinLag(1);
                forecaster.getTSLagMaker().setMaxLag(returnBestMaxLagFit(forecaster, data));


                // build the model
                forecaster.buildForecaster(data);

                // prime the forecaster with enough recent historical data
                forecaster.primeForecaster(data);

                //xFactor will help us to calculate the forecast data after the last data piont in the next_data
                int xFactor = next_data.Length;

                // forecast for the number of forecastUnits we set beyond the end of the training data
                int      forecastUnits = Convert.ToInt32(TextBox.Text) + xFactor;
                double[] PredectedList = new double[forecastUnits];
                var      mylist        = forecaster.forecast(forecastUnits);

                //get the forcasted data and place it in the PredectedList
                for (int i = 0; i < forecastUnits; i++)
                {
                    String predict = mylist.get(i).ToString().Split(' ')[2];
                    PredectedList[i] = Convert.ToDouble(predict);
                }

                //lag represent our fixed lag between the time stamp of the predictions
                double lag = forecaster.getTSLagMaker().getDeltaTime();

                //DeltaTime will represent our changing lag between each time stamp at each prediction
                double DeltaTime = 0;

                //getting the last time stamp in our last point in the current data
                double lastValidTime = data.lastInstance().value(0);

                //clear Predicted_Data if alredy have data inside
                Predicted_Data.Clear();
                Negetive_Predicted_Data.Clear();

                // output the predictions with their time stamp in the chart.
                for (int i = 0; i < forecastUnits; i++)
                {
                    if (i < next_data.Length - 1)
                    {//we still in the next data points and not in the predected points the user want to predict
                        Predicted_Data.Add(new ObservablePoint(next_data[i].Item1, PredectedList[i]));

                        //checks if the predected point is negetive if is negetive add the the black points series
                        if (PredectedList[i] < 0)
                        {
                            Negetive_Predicted_Data.Add(new ObservablePoint(next_data[i].Item1, PredectedList[i]));
                        }

                        //get the x from the last point in our next data to be the starting point in the predecred data
                        lastValidTime = next_data[i].Item1;
                    }
                    else
                    {//we are in the range of the predected points the user want to predict
                        //advance the current time to correspond to the forecasted values
                        DeltaTime = DeltaTime + lag;

                        //append the predicted data to the chart
                        Predicted_Data.Add(new ObservablePoint(lastValidTime + DeltaTime, PredectedList[i]));

                        //checks if the predected point is negetive if is negetive add the the black points series
                        if (PredectedList[i] < 0)
                        {
                            Negetive_Predicted_Data.Add(new ObservablePoint(lastValidTime + DeltaTime, PredectedList[i]));
                        }
                    }
                }

                //clear progressBar in the end of the progress
                ProgressBar.Value = 0;

                //checks if their wasn't negetive predected point and add the (0,0) point to the black points series
                //for preserving the chart prespective
                if (Negetive_Predicted_Data.Count == 0)
                {
                    Negetive_Predicted_Data.Add(new ObservablePoint(0, 0));
                }
            }
            catch (Exception ex) { ex.ToString(); }
        }
        private int returnBestMaxLagFit(WekaForecaster forecaster, Instances data)
        {//return max lag that fits the best to the prediction
            //calculate the best fit MaxLag for our predection
            int    bestFit        = 1;
            double sumOfDeferance = 100;

            //Configure the ProgressBar
            ProgressBar.Minimum = 0;
            ProgressBar.Maximum = 100;
            ProgressBar.Value   = 0;

            //Stores the value of the ProgressBar
            double value = 0;

            //Create a new instance of our ProgressBar Delegate that points
            // to the ProgressBar's SetValue method.
            UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar.SetValue);


            //calculate the difference between the prediction points to the actual next data if the next culclation is smaller
            //then befor replace the bestFit value with i;
            for (int i = 1; i <= 24; i++)
            {
                if (ProgressBar.Value != ProgressBar.Maximum)
                {
                    value += 4.16;

                    /*Update the Value of the ProgressBar:
                     *  1) Pass the "updatePbDelegate" delegate
                     *     that points to the ProgressBar1.SetValue method
                     *  2) Set the DispatcherPriority to "Background"
                     *  3) Pass an Object() Array containing the property
                     *     to update (ProgressBar.ValueProperty) and the new value */
                    Dispatcher.Invoke(updatePbDelegate,
                                      System.Windows.Threading.DispatcherPriority.Background,
                                      new object[] { ProgressBar.ValueProperty, value });
                }
                double sum = 0;

                //define the max lag
                forecaster.getTSLagMaker().setMaxLag(i);

                // build the model
                forecaster.buildForecaster(data);

                // prime the forecaster with enough recent historical data
                forecaster.primeForecaster(data);

                //if we dont have at list 10 points in next data we will use all the next data points else we use only 10
                int forecastUnits = 10;
                if (next_data.Length < 10)
                {
                    forecastUnits = next_data.Length;
                }


                // forecast for the number of forecastUnits we set beyond the end of the training data
                var mylist = forecaster.forecast(forecastUnits);

                //get the forcasted data and place it in the PredectedList
                for (int j = 3; j < forecastUnits; j++)
                {
                    String predict = mylist.get(j).ToString().Split(' ')[2];
                    sum = sum + Math.Abs(Convert.ToDouble(predict) - next_data[j - 2].Item2);
                }

                //calculate Standard Error
                sum = sum / forecastUnits;

                if (sum < sumOfDeferance)
                {//save the smallest Standard Error
                    sumOfDeferance = sum;
                    bestFit        = i;
                }
            }

            //show Standard Error in the maim window
            Standard_Error.Content = String.Format("Standard Error: {0:0.00000}", sumOfDeferance);
            return(bestFit);
        }