#region License Information
/* HeuristicLab
* Copyright (C) 2002-2017 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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Algorithms.DataAnalysis {
//mulitdimensional extension of http://www2.stat.duke.edu/~tjl13/s101/slides/unit6lec3H.pdf
[StorableClass]
internal sealed class PreconstructedLinearModel : RegressionModel, IConfidenceRegressionModel {
[Storable]
public Dictionary Coefficients { get; private set; }
[Storable]
public double Intercept { get; private set; }
[Storable]
private Dictionary Center { get; set; }
[Storable]
private Dictionary Variances { get; set; }
[Storable]
private double ResidualVariance { get; set; }
[Storable]
private int SampleSize { get; set; }
public override IEnumerable VariablesUsedForPrediction {
get { return Coefficients.Keys; }
}
#region HLConstructors
[StorableConstructor]
private PreconstructedLinearModel(bool deserializing) : base(deserializing) { }
private PreconstructedLinearModel(PreconstructedLinearModel original, Cloner cloner) : base(original, cloner) {
if (original.Coefficients != null) Coefficients = original.Coefficients.ToDictionary(x => x.Key, x => x.Value);
Intercept = original.Intercept;
if (original.Center != null) Center = original.Center.ToDictionary(x => x.Key, x => x.Value);
if (original.Variances != null) Variances = original.Variances.ToDictionary(x => x.Key, x => x.Value);
ResidualVariance = original.ResidualVariance;
SampleSize = original.SampleSize;
}
private PreconstructedLinearModel(Dictionary coefficients, double intercept, string targetvariable) : base(targetvariable) {
Coefficients = coefficients.ToDictionary(x => x.Key, x => x.Value);
Intercept = intercept;
Variances = new Dictionary();
ResidualVariance = 0;
SampleSize = 0;
}
public PreconstructedLinearModel(double intercept, string targetvariable) : base(targetvariable) {
Coefficients = new Dictionary();
Intercept = intercept;
Variances = new Dictionary();
ResidualVariance = 0;
SampleSize = 0;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PreconstructedLinearModel(this, cloner);
}
#endregion
public static PreconstructedLinearModel CreateConfidenceLinearModel(IRegressionProblemData pd, out double rmse, out double cvRmse) {
var inputMatrix = pd.Dataset.ToArray(pd.AllowedInputVariables.Concat(new[] {pd.TargetVariable}), pd.AllIndices);
alglib.linearmodel lm;
alglib.lrreport ar;
var nFeatures = inputMatrix.GetLength(1) - 1;
double[] coefficients; // last coefficient is for the constant
int retVal;
alglib.lrbuild(inputMatrix, inputMatrix.GetLength(0), nFeatures, out retVal, out lm, out ar);
if (retVal != 1)
throw new ArgumentException("Error in calculation of linear regression solution");
rmse = ar.rmserror;
cvRmse = ar.cvrmserror;
alglib.lrunpack(lm, out coefficients, out nFeatures);
return new PreconstructedLinearModel(pd.AllowedInputVariables.Zip(coefficients, (s, d) => new {s, d}).ToDictionary(x => x.s, x => x.d), coefficients[nFeatures], pd.TargetVariable);
}
public override IEnumerable GetEstimatedValues(IDataset dataset, IEnumerable rows) {
return rows.Select(row => GetEstimatedValue(dataset, row));
}
public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
return new RegressionSolution(this, problemData);
}
public IEnumerable GetEstimatedVariances(IDataset dataset, IEnumerable rows) {
return rows.Select(i => GetEstimatedVariance(dataset, i));
}
#region helpers
private double GetEstimatedValue(IDataset dataset, int row) {
return Intercept + (Coefficients.Count == 0 ? 0 : Coefficients.Sum(s => s.Value * dataset.GetDoubleValue(s.Key, row)));
}
private double GetEstimatedVariance(IDataset dataset, int row) {
if (SampleSize == 0) return 0.0;
var sum = (from var in Variances let d = dataset.GetDoubleValue(var.Key, row) - Center[var.Key] select d * d / var.Value).Sum();
return ResidualVariance * (1.0 / SampleSize + sum / (SampleSize - 1));
}
#endregion
}
}