Changeset 16448
 Timestamp:
 12/23/18 08:06:56 (13 months ago)
 Location:
 trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs
r16389 r16448 41 41 [StorableClass] 42 42 public sealed class LinearRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> { 43 private const string LinearRegressionModelResultName = "Linear regression solution"; 43 private const string SolutionResultName = "Linear regression solution"; 44 private const string ConfidenceSolutionResultName = "Solution with prediction intervals"; 44 45 45 46 [StorableConstructor] … … 62 63 protected override void Run(CancellationToken cancellationToken) { 63 64 double rmsError, cvRmsError; 65 // produce both solutions, to allow symbolic manipulation of LR solutions as well 66 // as the calculation of prediction intervals. 67 // There is no clean way to implement the new model class for LR as a symbolic model. 64 68 var solution = CreateSolution(Problem.ProblemData, out rmsError, out cvRmsError); 65 Results.Add(new Result(LinearRegressionModelResultName, "The linear regression solution.", solution)); 69 #pragma warning disable 168, 3021 70 var symbolicSolution = CreateLinearRegressionSolution(Problem.ProblemData, out rmsError, out cvRmsError); 71 #pragma warning restore 168, 3021 72 Results.Add(new Result(SolutionResultName, "The linear regression solution.", symbolicSolution)); 73 Results.Add(new Result(ConfidenceSolutionResultName, "Linear regression solution with parameter covariance matrix " + 74 "and calculation of prediction intervals", solution)); 66 75 Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the linear regression solution on the training set.", new DoubleValue(rmsError))); 67 76 Results.Add(new Result("Estimated root mean square error (crossvalidation)", "The estimated root of the mean of squared errors of the linear regression solution via cross validation.", new DoubleValue(cvRmsError))); … … 88 97 double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant 89 98 alglib.lrunpack(lm, out coefficients, out nFeatures); 90 91 int nFactorCoeff = factorVariables.Sum(kvp =>kvp.Value.Count());99 100 int nFactorCoeff = factorVariables.Sum(kvp => kvp.Value.Count()); 92 101 int nVarCoeff = doubleVariables.Count(); 93 102 var tree = LinearModelToTreeConverter.CreateTree(factorVariables, coefficients.Take(nFactorCoeff).ToArray(), … … 132 141 } 133 142 134 private static void PrepareData(IRegressionProblemData problemData, 135 out double[,] inputMatrix, 136 out IEnumerable<string> doubleVariables, 143 private static void PrepareData(IRegressionProblemData problemData, 144 out double[,] inputMatrix, 145 out IEnumerable<string> doubleVariables, 137 146 out IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables) { 138 147 var dataset = problemData.Dataset; 
trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegressionModel.cs
r16415 r16448 49 49 get; private set; 50 50 } 51 51 52 52 public override IEnumerable<string> VariablesUsedForPrediction { 53 53 get { return allowedInputVariables.Union(factorVariables.Select(f => f.Key)); } … … 78 78 this.W = new double[w.Length]; 79 79 Array.Copy(w, W, w.Length); 80 this.C = new double[covariance.GetLength(0), covariance.GetLength(1)];80 this.C = new double[covariance.GetLength(0), covariance.GetLength(1)]; 81 81 Array.Copy(covariance, C, covariance.Length); 82 82 this.NoiseSigma = noiseSigma; … … 123 123 124 124 double[] d = new double[C.GetLength(0)]; 125 125 126 126 for (int row = 0; row < n; row++) { 127 127 for (int column = 0; column < columns; column++) { 128 d[column] = inputData[row, column];128 d[column] = inputData[row, column]; 129 129 } 130 130 d[columns] = 1; 131 131 132 132 double var = 0.0; 133 for (int i=0;i<d.Length;i++) {134 for (int j = 0;j<d.Length;j++) {133 for (int i = 0; i < d.Length; i++) { 134 for (int j = 0; j < d.Length; j++) { 135 135 var += d[i] * C[i, j] * d[j]; 136 136 } 137 137 } 138 yield return var + NoiseSigma *NoiseSigma;138 yield return var + NoiseSigma * NoiseSigma; 139 139 } 140 140 } 141 142 141 143 142 public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
Note: See TracChangeset
for help on using the changeset viewer.