Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/26/17 11:06:51 (7 years ago)
Author:
bwerth
Message:

#2699 reworked kenel functions (beta is always a scaling factor now), added LU-Decomposition as a fall-back if Cholesky-decomposition fails

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/KernelRidgeRegression/KernelRidgeRegressionModel.cs

    r14888 r14891  
    3232  [Item("KernelRidgeRegressionModel", "A kernel ridge regression model")]
    3333  public sealed class KernelRidgeRegressionModel : RegressionModel {
    34     public override IEnumerable<string> VariablesUsedForPrediction {
     34    public override IEnumerable<string> VariablesUsedForPrediction
     35    {
    3536      get { return allowedInputVariables; }
    3637    }
     
    3839    [Storable]
    3940    private readonly string[] allowedInputVariables;
    40     public string[] AllowedInputVariables {
     41    public string[] AllowedInputVariables
     42    {
    4143      get { return allowedInputVariables; }
    4244    }
     
    114116        var l = new double[n, n]; Array.Copy(gram, l, l.Length);
    115117
     118        double[,] invG;
    116119        // cholesky decomposition
    117120        var res = alglib.trfac.spdmatrixcholesky(ref l, n, false);
    118         if (res == false) throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?");
    119 
    120         alglib.spdmatrixcholeskysolve(l, n, false, y, out info, out denseSolveRep, out alpha);
    121         if (info != 1) throw new ArgumentException("Could not create model.");
    122 
    123 
    124         {
     121        if (res == false) { //throw new ArgumentException("Could not decompose matrix. Is it quadratic symmetric positive definite?");
     122          int[] pivots;
     123          var lua = new double[n, n];
     124          Array.Copy(gram, lua, lua.Length);
     125          alglib.rmatrixlu(ref lua, n, n, out pivots);
     126          alglib.rmatrixlusolve(lua, pivots, n, y, out info, out denseSolveRep, out alpha);
     127          if (info != 1) throw new ArgumentException("Could not create model.");
     128          alglib.matinvreport rep;
     129          invG = lua;  // rename
     130          alglib.rmatrixluinverse(ref invG, pivots, n, out info, out rep);
     131          if (info != 1) throw new ArgumentException("Could not invert Gram matrix.");
     132        } else {
     133          alglib.spdmatrixcholeskysolve(l, n, false, y, out info, out denseSolveRep, out alpha);
     134          if (info != 1) throw new ArgumentException("Could not create model.");
    125135          // for LOO-CV we need to build the inverse of the gram matrix
    126136          alglib.matinvreport rep;
    127           var invG = l;   // rename
    128           alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep);         
     137          invG = l;   // rename
     138          alglib.spdmatrixcholeskyinverse(ref invG, n, false, out info, out rep);
    129139          if (info != 1) throw new ArgumentException("Could not invert Gram matrix.");
    130           var ssqLooError = 0.0;
    131           for (int i = 0; i < n; i++) {
    132             var pred_i = Util.ScalarProd(Util.GetRow(gram, i).ToArray(), alpha);
    133             var looPred_i = pred_i - alpha[i] / invG[i, i];
    134             var error = (y[i] - looPred_i) / yScale;
    135             ssqLooError += error * error;
    136           }
    137           LooCvRMSE = Math.Sqrt(ssqLooError / n);
    138         }
    139       } catch (alglib.alglibexception ae) {
     140        }
     141
     142        var ssqLooError = 0.0;
     143        for (int i = 0; i < n; i++) {
     144          var pred_i = Util.ScalarProd(Util.GetRow(gram, i).ToArray(), alpha);
     145          var looPred_i = pred_i - alpha[i] / invG[i, i];
     146          var error = (y[i] - looPred_i) / yScale;
     147          ssqLooError += error * error;
     148        }
     149        LooCvRMSE = Math.Sqrt(ssqLooError / n);
     150      }
     151      catch (alglib.alglibexception ae) {
    140152        // wrap exception so that calling code doesn't have to know about alglib implementation
    141153        throw new ArgumentException("There was a problem in the calculation of the kernel ridge regression model", ae);
Note: See TracChangeset for help on using the changeset viewer.