#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
using HeuristicLab.Data.Views;
using HeuristicLab.Data;
using HeuristicLab.Problems.DataAnalysis.Evaluators;
using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic;
namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Views {
[Content(typeof(SymbolicTimeSeriesPrognosisSolution), true)]
[View("Time Series Prognosis Results View")]
public partial class ResultsView : AsynchronousContentView {
private List rowNames = new List() {
"Mean squared error",
"Pearson's R²",
"Mean relative error",
"Directional symmetry",
"Weighted directional symmetry",
"Theil's U"
};
public ResultsView() {
InitializeComponent();
}
public new SymbolicTimeSeriesPrognosisSolution Content {
get { return (SymbolicTimeSeriesPrognosisSolution)base.Content; }
set { base.Content = value; }
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ModelChanged += new EventHandler(Content_ModelChanged);
Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.ModelChanged -= new EventHandler(Content_ModelChanged);
Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
}
private void Content_ModelChanged(object sender, EventArgs e) {
UpdateView();
}
private void Content_ProblemDataChanged(object sender, EventArgs e) {
UpdateView();
}
private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
UpdateView();
}
protected override void OnContentChanged() {
base.OnContentChanged();
UpdateView();
}
private void UpdateView() {
if (Content != null) {
matrixView.Content = CalculateMatrix();
} else
matrixView.Content = null;
}
public DoubleMatrix CalculateMatrix() {
List targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value).ToList();
DoubleMatrix matrix = new DoubleMatrix(rowNames.Count, targetVariables.Count() * 2);
matrix.RowNames = rowNames;
matrix.ColumnNames = targetVariables.SelectMany(x => new List() { x + " (training)", x + " (test)" });
matrix.SortableView = false;
int trainingStart = Content.ProblemData.TrainingSamplesStart.Value;
int trainingEnd = Content.ProblemData.TrainingSamplesEnd.Value;
int testStart = Content.ProblemData.TestSamplesStart.Value;
int testEnd = Content.ProblemData.TestSamplesEnd.Value;
// create a list of time series evaluators for each target variable
Dictionary> trainingEvaluators =
new Dictionary>();
Dictionary> testEvaluators =
new Dictionary>();
foreach (string targetVariable in targetVariables) {
trainingEvaluators.Add(targetVariable, new List());
trainingEvaluators[targetVariable].Add(new OnlineMeanSquaredErrorEvaluator());
trainingEvaluators[targetVariable].Add(new OnlinePearsonsRSquaredEvaluator());
trainingEvaluators[targetVariable].Add(new OnlineMeanAbsolutePercentageErrorEvaluator());
trainingEvaluators[targetVariable].Add(new OnlineDirectionalSymmetryEvaluator());
trainingEvaluators[targetVariable].Add(new OnlineWeightedDirectionalSymmetryEvaluator());
trainingEvaluators[targetVariable].Add(new OnlineTheilsUStatisticEvaluator());
testEvaluators.Add(targetVariable, new List());
testEvaluators[targetVariable].Add(new OnlineMeanSquaredErrorEvaluator());
testEvaluators[targetVariable].Add(new OnlinePearsonsRSquaredEvaluator());
testEvaluators[targetVariable].Add(new OnlineMeanAbsolutePercentageErrorEvaluator());
testEvaluators[targetVariable].Add(new OnlineDirectionalSymmetryEvaluator());
testEvaluators[targetVariable].Add(new OnlineWeightedDirectionalSymmetryEvaluator());
testEvaluators[targetVariable].Add(new OnlineTheilsUStatisticEvaluator());
}
Evaluate(trainingStart, trainingEnd, trainingEvaluators);
Evaluate(testStart, testEnd, testEvaluators);
int columnIndex = 0;
foreach (string targetVariable in targetVariables) {
int rowIndex = 0;
// training
foreach (var evaluator in trainingEvaluators[targetVariable]) {
matrix[rowIndex++, columnIndex] = Math.Round(evaluator.Value, 3);
}
columnIndex++;
// test
rowIndex = 0;
foreach (var evaluator in testEvaluators[targetVariable]) {
matrix[rowIndex++, columnIndex] = Math.Round(evaluator.Value, 3);
}
columnIndex++;
}
return matrix;
}
private void Evaluate(int start, int end, Dictionary> evaluators) {
for (int row = start; row < end; row++) {
if (string.IsNullOrEmpty(Content.ConditionalEvaluationVariable) || Content.ProblemData.Dataset[Content.ConditionalEvaluationVariable, row] != 0) {
// prepare evaluators for each target variable for a new prediction window
foreach (var entry in evaluators) {
double referenceOriginalValue = Content.ProblemData.Dataset[entry.Key, row - 1];
foreach (IOnlineTimeSeriesPrognosisEvaluator evaluator in entry.Value.OfType()) {
evaluator.StartNewPredictionWindow(referenceOriginalValue);
}
}
List targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value).ToList();
if (string.IsNullOrEmpty(Content.ConditionalEvaluationVariable) ||
Content.ProblemData.Dataset[Content.ConditionalEvaluationVariable, row] > 0) {
int timestep = 0;
foreach (double[] x in Content.GetPrognosis(row)) {
int targetIndex = 0;
if (row + timestep < Content.ProblemData.Dataset.Rows) {
foreach (var targetVariable in targetVariables) {
double originalValue = Content.ProblemData.Dataset[targetVariable, row + timestep];
double estimatedValue = x[targetIndex];
if (IsValidValue(originalValue) && IsValidValue(estimatedValue)) {
foreach (IOnlineEvaluator evaluator in evaluators[targetVariable]) {
evaluator.Add(originalValue, estimatedValue);
}
}
targetIndex++;
}
}
timestep++;
}
}
}
}
}
private bool IsValidValue(double d) {
return !(double.IsNaN(d) || double.IsInfinity(d));
}
}
}