Changeset 4475 for branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators
- Timestamp:
- 09/23/10 13:49:30 (14 years ago)
- Location:
- branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisNormalizedMseEvaluator.cs
r4401 r4475 41 41 [Item("SymbolicTimeSeriesPrognosisNormalizedMseEvaluator", "")] 42 42 [StorableClass] 43 public class SymbolicTimeSeriesPrognosisNormalizedMseEvaluator : SingleSuccessorOperator, ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator { 44 private const string RandomParameterName = "Random"; 45 private const string DataAnalysisProblemDataParameterName = "MultiVariateDataAnalysisProblemData"; 46 private const string TimeSeriesExpressionInterpreterParameterName = "TimeSeriesExpressionInterpreter"; 47 private const string TimeSeriesPrognosisModelParameterName = "TimeSeriesPrognosisModel"; 48 private const string PredictionHorizontParameterName = "PredictionHorizon"; 49 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; 50 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 51 private const string ConditionVariableParameterName = "ConditionVariableName"; 52 private const string SamplesStartParameterName = "SamplesStart"; 53 private const string SamplesEndParameterName = "SamplesEnd"; 54 private const string QualityParameterName = "Quality"; 55 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples"; 56 57 #region parameter properties 58 public ILookupParameter<IRandom> RandomParameter { 59 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 60 } 61 public ILookupParameter<MultiVariateDataAnalysisProblemData> ProblemDataParameter { 62 get { return (ILookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; } 63 } 64 public ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter> TimeSeriesExpressionInterpreterParameter { 65 get { return (ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters[TimeSeriesExpressionInterpreterParameterName]; } 66 } 67 public IValueLookupParameter<IntValue> PredictionHorizonParameter { 68 get { return (IValueLookupParameter<IntValue>)Parameters[PredictionHorizontParameterName]; } 69 } 70 public OptionalValueParameter<StringValue> ConditionVariableNameParameter { 71 get { return (OptionalValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; } 72 } 73 public IValueLookupParameter<IntValue> SamplesStartParameter { 74 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; } 75 } 76 public IValueLookupParameter<IntValue> SamplesEndParameter { 77 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; } 78 } 79 public IValueLookupParameter<DoubleArray> LowerEstimationLimitParameter { 80 get { return (IValueLookupParameter<DoubleArray>)Parameters[LowerEstimationLimitParameterName]; } 81 } 82 public IValueLookupParameter<DoubleArray> UpperEstimationLimitParameter { 83 get { return (IValueLookupParameter<DoubleArray>)Parameters[UpperEstimationLimitParameterName]; } 84 } 85 public ILookupParameter<SymbolicExpressionTree> TimeSeriesPrognosisModelParameter { 86 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[TimeSeriesPrognosisModelParameterName]; } 87 } 88 public ILookupParameter<DoubleValue> QualityParameter { 89 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 90 } 91 public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter { 92 get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; } 93 } 94 #endregion 95 #region 96 public IRandom Random { 97 get { return RandomParameter.ActualValue; } 98 } 99 public MultiVariateDataAnalysisProblemData ProblemData { 100 get { return ProblemDataParameter.ActualValue; } 101 } 102 public ISymbolicTimeSeriesExpressionInterpreter TimeSeriesExpressionInterpreter { 103 get { return TimeSeriesExpressionInterpreterParameter.ActualValue; } 104 } 105 public IntValue PredictionHorizon { 106 get { return PredictionHorizonParameter.ActualValue; } 107 } 108 public StringValue ConditionVariableName { 109 get { return ConditionVariableNameParameter.Value; } 110 } 111 public IntValue SamplesStart { 112 get { return SamplesStartParameter.ActualValue; } 113 } 114 public IntValue SamplesEnd { 115 get { return SamplesEndParameter.ActualValue; } 116 } 117 public DoubleArray LowerEstimationLimit { 118 get { return LowerEstimationLimitParameter.ActualValue; } 119 } 120 public DoubleArray UpperEstimationLimit { 121 get { return UpperEstimationLimitParameter.ActualValue; } 122 } 123 public SymbolicExpressionTree TimeSeriesPrognosisModel { 124 get { return TimeSeriesPrognosisModelParameter.ActualValue; } 125 } 126 public PercentValue RelativeNumberOfEvaluatedSamples { 127 get { return RelativeNumberOfEvaluatedSamplesParameter.Value; } 128 } 129 #endregion 43 public class SymbolicTimeSeriesPrognosisNormalizedMseEvaluator : SymbolicTimeSeriesPrognosisEvaluator { 130 44 131 45 public SymbolicTimeSeriesPrognosisNormalizedMseEvaluator() 132 46 : base() { 133 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "A random number generator."));134 Parameters.Add(new LookupParameter<MultiVariateDataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));135 Parameters.Add(new LookupParameter<ISymbolicTimeSeriesExpressionInterpreter>(TimeSeriesExpressionInterpreterParameterName, "The interpreter that should be used to evaluate the time series model represented as a symbolic expression tree."));136 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition to use for training."));137 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition to use for training."));138 Parameters.Add(new ValueLookupParameter<IntValue>(PredictionHorizontParameterName, "The number of time steps for which to create a forecast."));139 Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower limit for estimated values."));140 Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper limit for estimated values."));141 Parameters.Add(new OptionalValueParameter<StringValue>(ConditionVariableParameterName, "The name of the condition variable indicating if a row should be considered for evaluation or not."));142 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(TimeSeriesPrognosisModelParameterName, "The time series prognosis model encoded as a symbolic expression tree."));143 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the time series prognosis model encoded as a symbolic expression tree."));144 Parameters.Add(new ValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.", new PercentValue(1)));145 47 } 146 48 147 public override IOperation Apply() { 148 double quality; 149 string conditionVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value; 150 int nRows = (int)Math.Ceiling((SamplesEnd.Value - SamplesStart.Value) * RelativeNumberOfEvaluatedSamples.Value); 151 IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(Random.Next(), SamplesStart.Value, SamplesEnd.Value, nRows); 152 153 quality = Evaluate(TimeSeriesPrognosisModel, ProblemData, TimeSeriesExpressionInterpreter, 154 conditionVariableName, rows, PredictionHorizon.Value, LowerEstimationLimit, UpperEstimationLimit); 155 QualityParameter.ActualValue = new DoubleValue(quality); 156 return base.Apply(); 49 public override double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 50 return Calculate(tree, problemData, interpreter, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit); 157 51 } 158 52 159 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 160 ISymbolicTimeSeriesExpressionInterpreter interpreter, 161 IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 162 return Evaluate(tree, problemData, interpreter, null, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit); 163 } 164 165 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 166 ISymbolicTimeSeriesExpressionInterpreter interpreter, string conditionVariableName, 53 public static double Calculate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 54 ISymbolicTimeSeriesExpressionInterpreter interpreter, 167 55 IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 168 56 double[] zeros = new double[problemData.TargetVariables.CheckedItems.Count()]; 169 57 double[] ones = Enumerable.Repeat(1.0, zeros.Length).ToArray(); 170 return SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator. Evaluate(tree, problemData, interpreter, conditionVariableName, rows,58 return SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.CalculateWithScaling(tree, problemData, interpreter, rows, 171 59 predictionHorizon, lowerEstimationLimit, upperEstimationLimit, ones, zeros); 172 60 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.cs
r4460 r4475 37 37 using HeuristicLab.Problems.DataAnalysis.Regression; 38 38 using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces; 39 using HeuristicLab.Problems.DataAnalysis.MultiVariate.Evaluators; 39 40 40 41 namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Evaluators { 41 42 [Item("SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator", "")] 42 43 [StorableClass] 43 public class SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator : SingleSuccessorOperator, ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator { 44 private const string RandomParameterName = "Random"; 45 private const string DataAnalysisProblemDataParameterName = "MultiVariateDataAnalysisProblemData"; 46 private const string TimeSeriesExpressionInterpreterParameterName = "TimeSeriesExpressionInterpreter"; 47 private const string TimeSeriesPrognosisModelParameterName = "TimeSeriesPrognosisModel"; 48 private const string PredictionHorizontParameterName = "PredictionHorizon"; 49 private const string ConditionVariableParameterName = "ConditionVariableName"; 50 private const string SamplesStartParameterName = "SamplesStart"; 51 private const string SamplesEndParameterName = "SamplesEnd"; 52 private const string AlphaParameterName = "Alpha"; 53 private const string BetaParameterName = "Beta"; 54 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; 55 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 56 private const string QualityParameterName = "Quality"; 57 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples"; 58 59 #region parameter properties 60 public ILookupParameter<IRandom> RandomParameter { 61 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 62 } 63 public ILookupParameter<MultiVariateDataAnalysisProblemData> ProblemDataParameter { 64 get { return (ILookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; } 65 } 66 public ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter> TimeSeriesExpressionInterpreterParameter { 67 get { return (ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters[TimeSeriesExpressionInterpreterParameterName]; } 68 } 69 public IValueLookupParameter<IntValue> PredictionHorizonParameter { 70 get { return (IValueLookupParameter<IntValue>)Parameters[PredictionHorizontParameterName]; } 71 } 72 public OptionalValueParameter<StringValue> ConditionVariableNameParameter { 73 get { return (OptionalValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; } 74 } 75 public IValueLookupParameter<IntValue> SamplesStartParameter { 76 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; } 77 } 78 public IValueLookupParameter<IntValue> SamplesEndParameter { 79 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; } 80 } 81 public ILookupParameter<SymbolicExpressionTree> TimeSeriesPrognosisModelParameter { 82 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[TimeSeriesPrognosisModelParameterName]; } 83 } 84 public ILookupParameter<DoubleValue> QualityParameter { 85 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 86 } 87 public ILookupParameter<DoubleArray> AlphaParameter { 88 get { return (ILookupParameter<DoubleArray>)Parameters[AlphaParameterName]; } 89 } 90 public ILookupParameter<DoubleArray> BetaParameter { 91 get { return (ILookupParameter<DoubleArray>)Parameters[BetaParameterName]; } 92 } 93 public IValueLookupParameter<DoubleArray> LowerEstimationLimitParameter { 94 get { return (IValueLookupParameter<DoubleArray>)Parameters[LowerEstimationLimitParameterName]; } 95 } 96 public IValueLookupParameter<DoubleArray> UpperEstimationLimitParameter { 97 get { return (IValueLookupParameter<DoubleArray>)Parameters[UpperEstimationLimitParameterName]; } 98 } 99 public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter { 100 get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; } 101 } 102 103 #endregion 104 #region properties 105 public IRandom Random { 106 get { return RandomParameter.ActualValue; } 107 } 108 public MultiVariateDataAnalysisProblemData ProblemData { 109 get { return ProblemDataParameter.ActualValue; } 110 } 111 public ISymbolicTimeSeriesExpressionInterpreter TimeSeriesExpressionInterpreter { 112 get { return TimeSeriesExpressionInterpreterParameter.ActualValue; } 113 } 114 public IntValue PredictionHorizon { 115 get { return PredictionHorizonParameter.ActualValue; } 116 } 117 public StringValue ConditionVariableName { 118 get { return ConditionVariableNameParameter.Value; } 119 } 120 public IntValue SamplesStart { 121 get { return SamplesStartParameter.ActualValue; } 122 } 123 public IntValue SamplesEnd { 124 get { return SamplesEndParameter.ActualValue; } 125 } 126 public DoubleArray LowerEstimationLimit { 127 get { return LowerEstimationLimitParameter.ActualValue; } 128 } 129 public DoubleArray UpperEstimationLimit { 130 get { return UpperEstimationLimitParameter.ActualValue; } 131 } 132 public SymbolicExpressionTree TimeSeriesPrognosisModel { 133 get { return TimeSeriesPrognosisModelParameter.ActualValue; } 134 } 135 public PercentValue RelativeNumberOfEvaluatedSamples { 136 get { return RelativeNumberOfEvaluatedSamplesParameter.Value; } 137 } 138 #endregion 44 public class SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator : SymbolicTimeSeriesPrognosisEvaluator { 139 45 140 46 public SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator() 141 47 : base() { 142 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "A random number generator."));143 Parameters.Add(new LookupParameter<MultiVariateDataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));144 Parameters.Add(new LookupParameter<ISymbolicTimeSeriesExpressionInterpreter>(TimeSeriesExpressionInterpreterParameterName, "The interpreter that should be used to evaluate the time series model represented as a symbolic expression tree."));145 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition to use for training."));146 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition to use for training."));147 Parameters.Add(new ValueLookupParameter<IntValue>(PredictionHorizontParameterName, "The number of time steps for which to create a forecast."));148 Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower limit for estimated values."));149 Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper limit for estimated values."));150 Parameters.Add(new OptionalValueParameter<StringValue>(ConditionVariableParameterName, "The name of the condition variable indicating if a row should be considered for evaluation or not."));151 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(TimeSeriesPrognosisModelParameterName, "The time series prognosis model encoded as a symbolic expression tree."));152 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the time series prognosis model encoded as a symbolic expression tree."));153 Parameters.Add(new LookupParameter<DoubleArray>(AlphaParameterName, "The alpha parameter for linear scaling based on one step predictions."));154 Parameters.Add(new LookupParameter<DoubleArray>(BetaParameterName, "The beta parameter for linear scaling based on one step predictions."));155 Parameters.Add(new ValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.", new PercentValue(1)));156 48 } 157 49 158 public override IOperation Apply() { 50 public override double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 51 return Calculate(tree, problemData, interpreter, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit); 52 } 53 54 public static double Calculate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 159 55 double[] alpha, beta; 160 56 double quality; 161 string conditionVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value;162 int nRows = (int)Math.Ceiling((SamplesEnd.Value - SamplesStart.Value) * RelativeNumberOfEvaluatedSamples.Value);163 57 164 IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(Random.Next(), SamplesStart.Value, SamplesEnd.Value, nRows); 165 CalculateScalingParameters(TimeSeriesPrognosisModel, ProblemData, TimeSeriesExpressionInterpreter, 166 conditionVariableName, rows, 167 out beta, out alpha); 58 // calculate scaling parameters based on one-step-predictions 59 IEnumerable<string> selectedTargetVariables = from item in problemData.TargetVariables 60 where problemData.TargetVariables.ItemChecked(item) 61 select item.Value; 62 int dimension = selectedTargetVariables.Count(); 168 63 169 quality = Evaluate(TimeSeriesPrognosisModel, ProblemData, TimeSeriesExpressionInterpreter, 170 conditionVariableName, rows, PredictionHorizon.Value, 171 LowerEstimationLimit, UpperEstimationLimit, 64 IEnumerable<double[]> oneStepPredictions = 65 interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1); 66 IEnumerable<double[]> originalValues = from row in rows 67 select (from targetVariable in selectedTargetVariables 68 select problemData.Dataset[targetVariable, row]).ToArray(); 69 alpha = new double[dimension]; 70 beta = new double[dimension]; 71 72 CalculateScalingParameters(originalValues, oneStepPredictions, ref beta, ref alpha); 73 74 // calculate the quality for the full horizon 75 quality = CalculateWithScaling(tree, problemData, interpreter, 76 rows, predictionHorizon, 77 lowerEstimationLimit, upperEstimationLimit, 172 78 beta, alpha); 173 QualityParameter.ActualValue = new DoubleValue(quality); 174 AlphaParameter.ActualValue = new DoubleArray(alpha); 175 BetaParameter.ActualValue = new DoubleArray(beta); 176 return base.Apply(); 79 return quality; 177 80 } 178 81 179 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,82 public static double CalculateWithScaling(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 180 83 ISymbolicTimeSeriesExpressionInterpreter interpreter, 181 IEnumerable<int> rows, int predictionHorizon,182 DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit,183 double[] beta, double[] alpha) {184 return Evaluate(tree, problemData, interpreter, null, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit, beta, alpha);185 }186 187 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,188 ISymbolicTimeSeriesExpressionInterpreter interpreter, string conditionVariableName,189 84 IEnumerable<int> rows, int predictionHorizon, 190 85 DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit, 191 86 double[] beta, double[] alpha) { 192 if (conditionVariableName != null) {193 rows = from row in rows194 where !problemData.Dataset[conditionVariableName, row].IsAlmost(0.0)195 select row;196 }197 87 IEnumerable<string> selectedTargetVariables = from targetVariable in problemData.TargetVariables 198 88 where problemData.TargetVariables.ItemChecked(targetVariable) … … 208 98 select problemData.Dataset[targetVariable, row + step]).ToArray(); 209 99 210 List<OnlineNormalizedMeanSquaredErrorEvaluator> evaluators = new List<OnlineNormalizedMeanSquaredErrorEvaluator>(); 211 foreach (string targetVariable in selectedTargetVariables) 212 evaluators.Add(new OnlineNormalizedMeanSquaredErrorEvaluator()); 100 var evaluator = new OnlineMultiVariateEvaluator<OnlineNormalizedMeanSquaredErrorEvaluator>(); 213 101 214 102 var estimatedValuesEnumerator = estimatedValues.GetEnumerator(); … … 217 105 double[] original = originalValuesEnumerator.Current; 218 106 double[] estimated = estimatedValuesEnumerator.Current; 219 for (int i = 0; i < e valuators.Count; i++) {107 for (int i = 0; i < estimated.Length; i++) { 220 108 if (double.IsNaN(estimated[i])) estimated[i] = upperEstimationLimit[i]; 221 109 else estimated[i] = Math.Min(upperEstimationLimit[i], Math.Max(lowerEstimationLimit[i], estimated[i])); 222 evaluators[i].Add(original[i], estimated[i]);223 110 } 111 evaluator.Add(original, estimated); 224 112 } 225 113 226 double quality = evaluator s.Select(x => x.NormalizedMeanSquaredError).Sum();114 double quality = evaluator.Value; 227 115 return quality; 228 116 } 229 117 230 public static void CalculateScalingParameters(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 231 ISymbolicTimeSeriesExpressionInterpreter interpreter, 232 IEnumerable<int> rows, 233 out double[] betas, out double[] alphas) { 234 CalculateScalingParameters(tree, problemData, interpreter, null, rows, out betas, out alphas); 235 } 236 237 public static void CalculateScalingParameters(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 238 ISymbolicTimeSeriesExpressionInterpreter interpreter, string conditionVariableName, 239 IEnumerable<int> rows, out double[] betas, out double[] alphas) { 240 IEnumerable<string> selectedTargetVariables = from item in problemData.TargetVariables 241 where problemData.TargetVariables.ItemChecked(item) 242 select item.Value; 243 int dimension = selectedTargetVariables.Count(); 244 245 if (conditionVariableName != null) { 246 rows = from row in rows 247 where !problemData.Dataset[conditionVariableName, row].IsAlmost(0.0) 248 select row; 249 } 250 251 IEnumerable<double[]> oneStepPredictions = 252 interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1); 253 IEnumerable<double[]> originalValues = from row in rows 254 select (from targetVariable in selectedTargetVariables 255 select problemData.Dataset[targetVariable, row]).ToArray(); 256 257 alphas = new double[dimension]; 258 betas = new double[dimension]; 259 int[] cnt = new int[dimension]; 118 public static void CalculateScalingParameters(IEnumerable<double[]> originalValues, IEnumerable<double[]> estimatedValues, ref double[] beta, ref double[] alpha) { 260 119 List<OnlineMeanAndVarianceCalculator> estimatedVarianceEvaluators = new List<OnlineMeanAndVarianceCalculator>(); 261 120 List<OnlineCovarianceEvaluator> covarianceEvaluators = new List<OnlineCovarianceEvaluator>(); 262 121 List<OnlineMeanAndVarianceCalculator> originalMeanCalculators = new List<OnlineMeanAndVarianceCalculator>(); 263 foreach (var selectedTargetVariable in selectedTargetVariables) { 264 estimatedVarianceEvaluators.Add(new OnlineMeanAndVarianceCalculator()); 265 covarianceEvaluators.Add(new OnlineCovarianceEvaluator()); 266 originalMeanCalculators.Add(new OnlineMeanAndVarianceCalculator()); 267 } 268 var estimatedEnumerator = oneStepPredictions.GetEnumerator(); 122 int[] cnt = null; 123 124 var estimatedEnumerator = estimatedValues.GetEnumerator(); 269 125 var originalEnumerator = originalValues.GetEnumerator(); 270 126 while (estimatedEnumerator.MoveNext() & originalEnumerator.MoveNext()) { 271 127 double[] original = originalEnumerator.Current; 272 128 double[] estimated = estimatedEnumerator.Current; 273 for (int component = 0; component < dimension; component++) { 274 if (IsValidValue(original[component]) && IsValidValue(estimated[component])) { 275 cnt[component]++; 276 estimatedVarianceEvaluators[component].Add(estimated[component]); 277 covarianceEvaluators[component].Add(original[component], estimated[component]); 278 originalMeanCalculators[component].Add(original[component]); 129 int dimension = original.Length; 130 // initialize 131 if (cnt == null) { 132 cnt = new int[dimension]; 133 for (int i = 0; i < dimension; i++) { 134 estimatedVarianceEvaluators.Add(new OnlineMeanAndVarianceCalculator()); 135 covarianceEvaluators.Add(new OnlineCovarianceEvaluator()); 136 originalMeanCalculators.Add(new OnlineMeanAndVarianceCalculator()); 279 137 } 138 } else if (cnt.Length == dimension) { 139 for (int component = 0; component < dimension; component++) { 140 if (IsValidValue(original[component]) && IsValidValue(estimated[component])) { 141 cnt[component]++; 142 estimatedVarianceEvaluators[component].Add(estimated[component]); 143 covarianceEvaluators[component].Add(original[component], estimated[component]); 144 originalMeanCalculators[component].Add(original[component]); 145 } 146 } 147 } else { 148 throw new ArgumentException("Dimension of input array doesn't match"); 280 149 } 281 150 } 151 282 152 if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) 283 153 throw new InvalidOperationException("Number of elements in estimated and original series doesn't match."); 284 for (int component = 0; component < dimension; component++) { 154 if (cnt == null) throw new ArgumentException("No elements in estimated and original."); 155 156 for (int component = 0; component < cnt.Length; component++) { 285 157 if (cnt[component] < 2) { 286 alpha s[component] = 0;287 beta s[component] = 1;158 alpha[component] = 0; 159 beta[component] = 1; 288 160 } else { 289 161 if (estimatedVarianceEvaluators[component].PopulationVariance.IsAlmost(0.0)) 290 beta s[component] = 1;162 beta[component] = 1; 291 163 else 292 beta s[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance;164 beta[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance; 293 165 294 alpha s[component] = originalMeanCalculators[component].Mean - betas[component] * estimatedVarianceEvaluators[component].Mean;166 alpha[component] = originalMeanCalculators[component].Mean - beta[component] * estimatedVarianceEvaluators[component].Mean; 295 167 } 296 168 }
Note: See TracChangeset
for help on using the changeset viewer.