protected void CalculateTimeSeriesResults()
        {
            OnlineCalculatorError errorState;
            double trainingMean = ProblemData.TrainingIndices.Any() ? ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).Average() : double.NaN;
            var    meanModel    = new ConstantModel(trainingMean, ProblemData.TargetVariable);

            double alpha, beta;
            IEnumerable <double> trainingStartValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();

            OnlineLinearScalingParameterCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
            var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(ProblemData.TargetVariable, new double[] { beta }, alpha);


            #region Calculate training quality measures
            if (ProblemData.TrainingIndices.Any())
            {
                IEnumerable <double> trainingTargetValues         = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
                IEnumerable <double> trainingEstimatedValues      = EstimatedTrainingValues.ToList();
                IEnumerable <double> trainingMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();
                IEnumerable <double> trainingAR1ModelPredictions  = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TrainingIndices).ToList();

                TrainingDirectionalSymmetry         = OnlineDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
                TrainingDirectionalSymmetry         = errorState == OnlineCalculatorError.None ? TrainingDirectionalSymmetry : 0.0;
                TrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingEstimatedValues, out errorState);
                TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TrainingWeightedDirectionalSymmetry : 0.0;
                TrainingTheilsUStatisticAR1         = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState);
                TrainingTheilsUStatisticAR1         = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticAR1 : double.PositiveInfinity;
                TrainingTheilsUStatisticMean        = OnlineTheilsUStatisticCalculator.Calculate(trainingTargetValues.First(), trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState);
                TrainingTheilsUStatisticMean        = errorState == OnlineCalculatorError.None ? TrainingTheilsUStatisticMean : double.PositiveInfinity;
            }
            #endregion

            #region Calculate test quality measures
            if (ProblemData.TestIndices.Any())
            {
                IEnumerable <double> testTargetValues         = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToList();
                IEnumerable <double> testEstimatedValues      = EstimatedTestValues.ToList();
                IEnumerable <double> testMeanModelPredictions = meanModel.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();
                IEnumerable <double> testAR1ModelPredictions  = AR1model.GetEstimatedValues(ProblemData.Dataset, ProblemData.TestIndices).ToList();

                TestDirectionalSymmetry         = OnlineDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
                TestDirectionalSymmetry         = errorState == OnlineCalculatorError.None ? TestDirectionalSymmetry : 0.0;
                TestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testTargetValues.First(), testTargetValues, testEstimatedValues, out errorState);
                TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? TestWeightedDirectionalSymmetry : 0.0;
                TestTheilsUStatisticAR1         = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
                TestTheilsUStatisticAR1         = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticAR1 : double.PositiveInfinity;
                TestTheilsUStatisticMean        = OnlineTheilsUStatisticCalculator.Calculate(testTargetValues.First(), testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
                TestTheilsUStatisticMean        = errorState == OnlineCalculatorError.None ? TestTheilsUStatisticMean : double.PositiveInfinity;
            }
            #endregion
        }
 protected OnlineTheilsUStatisticCalculator(OnlineTheilsUStatisticCalculator original, Cloner cloner)
     : base(original, cloner)
 {
     squaredErrorMeanCalculator      = cloner.Clone(original.squaredErrorMeanCalculator);
     unbiasedEstimatorMeanCalculator = cloner.Clone(original.unbiasedEstimatorMeanCalculator);
 }
        private void CalculateTestPrognosisResults()
        {
            OnlineCalculatorError errorState;
            var problemData = Solution.ProblemData;

            if (!problemData.TestIndices.Any())
            {
                return;
            }
            var model         = Solution.Model;
            var testHorizions = problemData.TestIndices.Select(r => Math.Min(testHorizon, problemData.TestPartition.End - r)).ToList();
            IEnumerable <IEnumerable <double> > testTargetValues    = problemData.TestIndices.Zip(testHorizions, Enumerable.Range).Select(r => problemData.Dataset.GetDoubleValues(problemData.TargetVariable, r)).ToList();
            IEnumerable <IEnumerable <double> > testEstimatedValues = model.GetPrognosedValues(problemData.Dataset, problemData.TestIndices, testHorizions).ToList();
            IEnumerable <double> testStartValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TestIndices.Select(r => r - 1).Where(r => r > 0)).ToList();

            IEnumerable <double> originalTestValues  = testTargetValues.SelectMany(x => x).ToList();
            IEnumerable <double> estimatedTestValues = testEstimatedValues.SelectMany(x => x).ToList();

            double testMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            PrognosisTestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMSE : double.NaN;
            double testMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            PrognosisTestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMAE : double.NaN;
            double testR = OnlinePearsonsRCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            PrognosisTestRSquared = errorState == OnlineCalculatorError.None ? testR * testR : double.NaN;
            double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            PrognosisTestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;
            double testNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            PrognosisTestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNMSE : double.NaN;
            double testME = OnlineMeanErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);

            PrognosisTestMeanError = errorState == OnlineCalculatorError.None ? testME : double.NaN;

            PrognosisTestDirectionalSymmetry         = OnlineDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
            PrognosisTestDirectionalSymmetry         = errorState == OnlineCalculatorError.None ? PrognosisTestDirectionalSymmetry : 0.0;
            PrognosisTestWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(testStartValues, testTargetValues, testEstimatedValues, out errorState);
            PrognosisTestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTestWeightedDirectionalSymmetry : 0.0;


            if (problemData.TrainingIndices.Any())
            {
                //mean model
                double trainingMean = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).Average();
                var    meanModel    = new ConstantModel(trainingMean);

                //AR1 model
                double alpha, beta;
                IEnumerable <double> trainingStartValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices.Select(r => r - 1).Where(r => r > 0)).ToList();
                OnlineLinearScalingParameterCalculator.Calculate(problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices.Where(x => x > 0)), trainingStartValues, out alpha, out beta, out errorState);
                var AR1model = new TimeSeriesPrognosisAutoRegressiveModel(problemData.TargetVariable, new double[] { beta }, alpha);

                IEnumerable <IEnumerable <double> > testMeanModelPredictions = meanModel.GetPrognosedValues(problemData.Dataset, problemData.TestIndices, testHorizions).ToList();
                IEnumerable <IEnumerable <double> > testAR1ModelPredictions  = AR1model.GetPrognosedValues(problemData.Dataset, problemData.TestIndices, testHorizions).ToList();

                PrognosisTestTheilsUStatisticAR1  = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testAR1ModelPredictions, testEstimatedValues, out errorState);
                PrognosisTestTheilsUStatisticAR1  = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticAR1 : double.PositiveInfinity;
                PrognosisTestTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(testStartValues, testTargetValues, testMeanModelPredictions, testEstimatedValues, out errorState);
                PrognosisTestTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? PrognosisTestTheilsUStatisticMean : double.PositiveInfinity;
            }
        }
 public static double Calculate(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> referenceContinuation, IEnumerable<double> predictedContinuation, out OnlineCalculatorError errorState) {
   OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();
   calculator.Add(startValue, actualContinuation, referenceContinuation, predictedContinuation);
   errorState = calculator.ErrorState;
   return calculator.TheilsUStatistic;
 }
    public static double Calculate(IEnumerable<double> startValues, IEnumerable<IEnumerable<double>> actualContinuations, IEnumerable<IEnumerable<double>> referenceContinuations, IEnumerable<IEnumerable<double>> predictedContinuations, out OnlineCalculatorError errorState) {
      IEnumerator<double> startValueEnumerator = startValues.GetEnumerator();
      IEnumerator<IEnumerable<double>> actualContinuationsEnumerator = actualContinuations.GetEnumerator();
      IEnumerator<IEnumerable<double>> referenceContinuationsEnumerator = referenceContinuations.GetEnumerator();
      IEnumerator<IEnumerable<double>> predictedContinuationsEnumerator = predictedContinuations.GetEnumerator();

      OnlineTheilsUStatisticCalculator calculator = new OnlineTheilsUStatisticCalculator();

      // always move forward all enumerators (do not use short-circuit evaluation!)
      while (startValueEnumerator.MoveNext() & actualContinuationsEnumerator.MoveNext() & referenceContinuationsEnumerator.MoveNext() & predictedContinuationsEnumerator.MoveNext()) {
        calculator.Add(startValueEnumerator.Current, actualContinuationsEnumerator.Current, referenceContinuationsEnumerator.Current, predictedContinuationsEnumerator.Current);
        if (calculator.ErrorState != OnlineCalculatorError.None) break;
      }

      // check if all enumerators are at the end to make sure both enumerations have the same length
      if (calculator.ErrorState == OnlineCalculatorError.None &&
          (startValueEnumerator.MoveNext() || actualContinuationsEnumerator.MoveNext() || referenceContinuationsEnumerator.MoveNext() || predictedContinuationsEnumerator.MoveNext())) {
        throw new ArgumentException("Number of elements in startValues, actualContinuations, referenceContinuation and estimatedValues predictedContinuations doesn't match.");
      } else {
        errorState = calculator.ErrorState;
        return calculator.TheilsUStatistic;
      }
    }