/// <summary> /// Calculates alpha and beta parameters to linearly scale elements of original to the scale and location of target /// original[i] * beta + alpha /// </summary> /// <param name="original">Values that should be scaled</param> /// <param name="target">Target values to which the original values should be scaled</param> /// <param name="alpha">Additive constant for the linear scaling</param> /// <param name="beta">Multiplicative factor for the linear scaling</param> /// <param name="errorState">Flag that indicates if errors occurred in the calculation of the linea scaling parameters.</param> public static void Calculate(IEnumerable <double> original, IEnumerable <double> target, out double alpha, out double beta, out OnlineCalculatorError errorState) { OnlineLinearScalingParameterCalculator calculator = new OnlineLinearScalingParameterCalculator(); IEnumerator <double> originalEnumerator = original.GetEnumerator(); IEnumerator <double> targetEnumerator = target.GetEnumerator(); // always move forward both enumerators (do not use short-circuit evaluation!) while (originalEnumerator.MoveNext() & targetEnumerator.MoveNext()) { double originalElement = originalEnumerator.Current; double targetElement = targetEnumerator.Current; calculator.Add(originalElement, targetElement); if (calculator.ErrorState != OnlineCalculatorError.None) { break; } } // check if both enumerators are at the end to make sure both enumerations have the same length if (calculator.ErrorState == OnlineCalculatorError.None && (originalEnumerator.MoveNext() || targetEnumerator.MoveNext())) { throw new ArgumentException("Number of elements in original and target enumeration do not match."); } else { errorState = calculator.ErrorState; alpha = calculator.Alpha; beta = calculator.Beta; } }
protected OnlineLinearScalingParameterCalculator(OnlineLinearScalingParameterCalculator original, Cloner cloner) : base(original, cloner) { targetMeanCalculator = cloner.Clone(original.targetMeanCalculator); originalMeanAndVarianceCalculator = cloner.Clone(original.originalMeanAndVarianceCalculator); originalTargetCovarianceCalculator = cloner.Clone(original.originalTargetCovarianceCalculator); // do not reset the calculators here }
private void CalculateTrainingPrognosisResults() { OnlineCalculatorError errorState; var problemData = Solution.ProblemData; if (!problemData.TrainingIndices.Any()) { return; } var model = Solution.Model; //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); var trainingHorizions = problemData.TrainingIndices.Select(r => Math.Min(trainingHorizon, problemData.TrainingPartition.End - r)).ToList(); IEnumerable <IEnumerable <double> > trainingTargetValues = problemData.TrainingIndices.Zip(trainingHorizions, Enumerable.Range).Select(r => problemData.Dataset.GetDoubleValues(problemData.TargetVariable, r)).ToList(); IEnumerable <IEnumerable <double> > trainingEstimatedValues = model.GetPrognosedValues(problemData.Dataset, problemData.TrainingIndices, trainingHorizions).ToList(); IEnumerable <IEnumerable <double> > trainingMeanModelPredictions = meanModel.GetPrognosedValues(problemData.Dataset, problemData.TrainingIndices, trainingHorizions).ToList(); IEnumerable <IEnumerable <double> > trainingAR1ModelPredictions = AR1model.GetPrognosedValues(problemData.Dataset, problemData.TrainingIndices, trainingHorizions).ToList(); IEnumerable <double> originalTrainingValues = trainingTargetValues.SelectMany(x => x).ToList(); IEnumerable <double> estimatedTrainingValues = trainingEstimatedValues.SelectMany(x => x).ToList(); double trainingMSE = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); PrognosisTrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMSE : double.NaN; double trainingMAE = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); PrognosisTrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMAE : double.NaN; double trainingR = OnlinePearsonsRCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); PrognosisTrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR * trainingR : double.NaN; double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); PrognosisTrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN; double trainingNMSE = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); PrognosisTrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNMSE : double.NaN; double trainingME = OnlineMeanErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); PrognosisTrainingMeanError = errorState == OnlineCalculatorError.None ? trainingME : double.NaN; PrognosisTrainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState); PrognosisTrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTrainingDirectionalSymmetry : 0.0; PrognosisTrainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingEstimatedValues, out errorState); PrognosisTrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? PrognosisTrainingWeightedDirectionalSymmetry : 0.0; PrognosisTrainingTheilsUStatisticAR1 = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingAR1ModelPredictions, trainingEstimatedValues, out errorState); PrognosisTrainingTheilsUStatisticAR1 = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticAR1 : double.PositiveInfinity; PrognosisTrainingTheilsUStatisticMean = OnlineTheilsUStatisticCalculator.Calculate(trainingStartValues, trainingTargetValues, trainingMeanModelPredictions, trainingEstimatedValues, out errorState); PrognosisTrainingTheilsUStatisticMean = errorState == OnlineCalculatorError.None ? PrognosisTrainingTheilsUStatisticMean : double.PositiveInfinity; }
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); 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 }
/// <summary> /// Calculates alpha and beta parameters to linearly scale elements of original to the scale and location of target /// original[i] * beta + alpha /// </summary> /// <param name="original">Values that should be scaled</param> /// <param name="target">Target values to which the original values should be scaled</param> /// <param name="alpha">Additive constant for the linear scaling</param> /// <param name="beta">Multiplicative factor for the linear scaling</param> /// <param name="errorState">Flag that indicates if errors occurred in the calculation of the linea scaling parameters.</param> public static void Calculate(IEnumerable<double> original, IEnumerable<double> target, out double alpha, out double beta, out OnlineCalculatorError errorState) { OnlineLinearScalingParameterCalculator calculator = new OnlineLinearScalingParameterCalculator(); IEnumerator<double> originalEnumerator = original.GetEnumerator(); IEnumerator<double> targetEnumerator = target.GetEnumerator(); // always move forward both enumerators (do not use short-circuit evaluation!) while (originalEnumerator.MoveNext() & targetEnumerator.MoveNext()) { double originalElement = originalEnumerator.Current; double targetElement = targetEnumerator.Current; calculator.Add(originalElement, targetElement); if (calculator.ErrorState != OnlineCalculatorError.None) break; } // check if both enumerators are at the end to make sure both enumerations have the same length if (calculator.ErrorState == OnlineCalculatorError.None && (originalEnumerator.MoveNext() || targetEnumerator.MoveNext())) { throw new ArgumentException("Number of elements in original and target enumeration do not match."); } else { errorState = calculator.ErrorState; alpha = calculator.Alpha; beta = calculator.Beta; } }