Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/04/14 11:30:58 (11 years ago)
Author:
gkronber
Message:

#2125: merged r10489:10491 and r10493 from trunk into stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/CovarianceFunctions/CovarianceNeuralNetwork.cs

    r9633 r10530  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Linq.Expressions;
    26 using AutoDiff;
    2724using HeuristicLab.Common;
    2825using HeuristicLab.Core;
     
    4239    public IValueParameter<DoubleValue> LengthParameter {
    4340      get { return (IValueParameter<DoubleValue>)Parameters["Length"]; }
     41    }
     42    private bool HasFixedScaleParameter {
     43      get { return ScaleParameter.Value != null; }
     44    }
     45    private bool HasFixedLengthParameter {
     46      get { return LengthParameter.Value != null; }
    4447    }
    4548
     
    6871    public int GetNumberOfParameters(int numberOfVariables) {
    6972      return
    70         (ScaleParameter.Value != null ? 0 : 1) +
    71         (LengthParameter.Value != null ? 0 : 1);
     73        (HasFixedScaleParameter ? 0 : 1) +
     74        (HasFixedLengthParameter ? 0 : 1);
    7275    }
    7376
     
    8386      // gather parameter values
    8487      int c = 0;
    85       if (LengthParameter.Value != null) {
     88      if (HasFixedLengthParameter) {
    8689        length = LengthParameter.Value.Value;
    8790      } else {
     
    9093      }
    9194
    92       if (ScaleParameter.Value != null) {
     95      if (HasFixedScaleParameter) {
    9396        scale = ScaleParameter.Value.Value;
    9497      } else {
     
    99102    }
    100103
    101 
    102     private static Func<Term, UnaryFunc> asin = UnaryFunc.Factory(
    103         x => Math.Asin(x),      // evaluate
    104         x => 1 / Math.Sqrt(1 - x * x));  // derivative of atan
    105     private static Func<Term, UnaryFunc> sqrt = UnaryFunc.Factory(
    106       x => Math.Sqrt(x),
    107       x => 1 / (2 * Math.Sqrt(x)));
    108 
    109104    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
    110105      double length, scale;
    111106      GetParameterValues(p, out scale, out length);
    112       // create functions
    113       AutoDiff.Variable p0 = new AutoDiff.Variable();
    114       AutoDiff.Variable p1 = new AutoDiff.Variable();
    115       var l = TermBuilder.Exp(2.0 * p0);
    116       var s = TermBuilder.Exp(2.0 * p1);
    117       AutoDiff.Variable[] x1 = new AutoDiff.Variable[columnIndices.Count()];
    118       AutoDiff.Variable[] x2 = new AutoDiff.Variable[columnIndices.Count()];
    119       AutoDiff.Term sx = 1;
    120       AutoDiff.Term s1 = 1;
    121       AutoDiff.Term s2 = 1;
    122       for (int k = 0; k < columnIndices.Count(); k++) {
    123         x1[k] = new AutoDiff.Variable();
    124         x2[k] = new AutoDiff.Variable();
    125         sx += x1[k] * x2[k];
    126         s1 += x1[k] * x1[k];
    127         s2 += x2[k] * x2[k];
    128       }
    129 
    130       var parameter = x1.Concat(x2).Concat(new AutoDiff.Variable[] { p0, p1 }).ToArray();
    131       var values = new double[x1.Length + x2.Length + 2];
    132       var c = (s * asin(sx / (sqrt((l + s1) * (l + s2))))).Compile(parameter);
     107      var fixedLength = HasFixedLengthParameter;
     108      var fixedScale = HasFixedScaleParameter;
    133109
    134110      var cov = new ParameterizedCovarianceFunction();
    135111      cov.Covariance = (x, i, j) => {
    136         int k = 0;
     112        double sx = 1.0;
     113        double s1 = 1.0;
     114        double s2 = 1.0;
    137115        foreach (var col in columnIndices) {
    138           values[k] = x[i, col];
    139           k++;
     116          sx += x[i, col] * x[j, col];
     117          s1 += x[i, col] * x[i, col];
     118          s2 += x[j, col] * x[j, col];
    140119        }
    141         foreach (var col in columnIndices) {
    142           values[k] = x[j, col];
    143           k++;
    144         }
    145         values[k] = Math.Log(Math.Sqrt(length));
    146         values[k + 1] = Math.Log(Math.Sqrt(scale));
    147         return c.Evaluate(values);
     120
     121        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
    148122      };
    149123      cov.CrossCovariance = (x, xt, i, j) => {
    150         int k = 0;
     124        double sx = 1.0;
     125        double s1 = 1.0;
     126        double s2 = 1.0;
    151127        foreach (var col in columnIndices) {
    152           values[k] = x[i, col];
    153           k++;
     128          sx += x[i, col] * xt[j, col];
     129          s1 += x[i, col] * x[i, col];
     130          s2 += xt[j, col] * xt[j, col];
    154131        }
    155         foreach (var col in columnIndices) {
    156           values[k] = xt[j, col];
    157           k++;
    158         }
    159         values[k] = Math.Log(Math.Sqrt(length));
    160         values[k + 1] = Math.Log(Math.Sqrt(scale));
    161         return c.Evaluate(values);
     132
     133        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
    162134      };
    163       cov.CovarianceGradient = (x, i, j) => {
    164         int k = 0;
    165         foreach (var col in columnIndices) {
    166           values[k] = x[i, col];
    167           k++;
    168         }
    169         foreach (var col in columnIndices) {
    170           values[k] = x[j, col];
    171           k++;
    172         }
    173         values[k] = Math.Log(Math.Sqrt(length));
    174         values[k + 1] = Math.Log(Math.Sqrt(scale));
    175         return c.Differentiate(values).Item1.Skip(columnIndices.Count() * 2);
    176       };
     135      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, columnIndices, fixedLength, fixedScale);
    177136      return cov;
    178137    }
    179138
     139    // order of returned gradients must match the order in GetParameterValues!
     140    private static IEnumerable<double> GetGradient(double[,] x, int i, int j, double length, double scale, IEnumerable<int> columnIndices,
     141      bool fixedLength, bool fixedScale) {
     142      {
     143        double sx = 1.0;
     144        double s1 = 1.0;
     145        double s2 = 1.0;
     146        foreach (var col in columnIndices) {
     147          sx += x[i, col] * x[j, col];
     148          s1 += x[i, col] * x[i, col];
     149          s2 += x[j, col] * x[j, col];
     150        }
     151        var h = (length + s1) * (length + s2);
     152        var f = sx / Math.Sqrt(h);
     153        if (!fixedLength) {
     154          yield return -scale / Math.Sqrt(1.0 - f * f) * ((length * sx * (2.0 * length + s1 + s2)) / Math.Pow(h, 3.0 / 2.0));
     155        }
     156        if (!fixedScale) {
     157          yield return 2.0 * scale * Math.Asin(f);
     158        }
     159      }
     160    }
    180161  }
    181162}
Note: See TracChangeset for help on using the changeset viewer.