|
@@ -21,16 +21,16 @@ import org.apache.commons.math3.stat.StatUtils;
|
|
|
*/
|
|
*/
|
|
|
public class GaussianProcess {
|
|
public class GaussianProcess {
|
|
|
|
|
|
|
|
- private List<RealVector> X;
|
|
|
|
|
|
|
+ private List<RealVector> listOfCoordinates;
|
|
|
private RealVector weight;
|
|
private RealVector weight;
|
|
|
private double mean;
|
|
private double mean;
|
|
|
private double standardDeviation;
|
|
private double standardDeviation;
|
|
|
private CholeskyDecomposition cholesky;
|
|
private CholeskyDecomposition cholesky;
|
|
|
private RealVector lengthScale;
|
|
private RealVector lengthScale;
|
|
|
|
|
|
|
|
- private GaussianProcess(List<RealVector> X, RealVector weight, double mean, double standardDeviation, CholeskyDecomposition cholesky,
|
|
|
|
|
- RealVector lengthScale) {
|
|
|
|
|
- this.X = X;
|
|
|
|
|
|
|
+ private GaussianProcess(List<RealVector> listOfCoordinates, RealVector weight, double mean, double standardDeviation,
|
|
|
|
|
+ CholeskyDecomposition cholesky, RealVector lengthScale) {
|
|
|
|
|
+ this.listOfCoordinates = listOfCoordinates;
|
|
|
this.weight = weight;
|
|
this.weight = weight;
|
|
|
this.mean = mean;
|
|
this.mean = mean;
|
|
|
this.standardDeviation = standardDeviation;
|
|
this.standardDeviation = standardDeviation;
|
|
@@ -39,15 +39,16 @@ public class GaussianProcess {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @param x coordinate to predict at
|
|
|
|
|
|
|
+ * @param coordinates coordinate to predict at
|
|
|
* @return array containing the predicted [mean, standard deviation] at x.
|
|
* @return array containing the predicted [mean, standard deviation] at x.
|
|
|
*/
|
|
*/
|
|
|
- public double[] predict(RealVector x) {
|
|
|
|
|
- RealVector kernelizedX = maternKernel(X, x, lengthScale);
|
|
|
|
|
- RealVector kernelMatrixTimesX = cholesky.getSolver().solve(kernelizedX);
|
|
|
|
|
|
|
+ public double[] predict(RealVector coordinates) {
|
|
|
|
|
+ RealVector kernelizedCoordinates = maternKernel(listOfCoordinates, coordinates, lengthScale);
|
|
|
|
|
+ RealVector kernelMatrixTimesCoordinates = cholesky.getSolver().solve(kernelizedCoordinates);
|
|
|
|
|
|
|
|
- double predictedMean = weight.dotProduct(kernelizedX);
|
|
|
|
|
- double predictedStandardDeviation = Math.sqrt(maternKernel(x, x, lengthScale) - kernelMatrixTimesX.dotProduct(kernelizedX));
|
|
|
|
|
|
|
+ double predictedMean = weight.dotProduct(kernelizedCoordinates);
|
|
|
|
|
+ double predictedStandardDeviation = Math
|
|
|
|
|
+ .sqrt(maternKernel(coordinates, coordinates, lengthScale) - kernelMatrixTimesCoordinates.dotProduct(kernelizedCoordinates));
|
|
|
|
|
|
|
|
double[] out = new double[2];
|
|
double[] out = new double[2];
|
|
|
out[0] = predictedMean * this.standardDeviation + this.mean;
|
|
out[0] = predictedMean * this.standardDeviation + this.mean;
|
|
@@ -64,61 +65,65 @@ public class GaussianProcess {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Fit Gaussian Process using a matern kernel.
|
|
* Fit Gaussian Process using a matern kernel.
|
|
|
- * @param X
|
|
|
|
|
- * @param Y expected to have zero mean, unit variance if normalize is false
|
|
|
|
|
|
|
+ * @param observedCoordinates
|
|
|
|
|
+ * @param observations expected to have zero mean, unit variance if normalize is false
|
|
|
* @param noise variance of noise in Y
|
|
* @param noise variance of noise in Y
|
|
|
* @param normalize if Y should be normalized
|
|
* @param normalize if Y should be normalized
|
|
|
* @param lengthScale X values are divided by these values before calculating their euclidean distance. If all entries
|
|
* @param lengthScale X values are divided by these values before calculating their euclidean distance. If all entries
|
|
|
* are equal the kernel is isometric.
|
|
* are equal the kernel is isometric.
|
|
|
*/
|
|
*/
|
|
|
- public static GaussianProcess fit(List<RealVector> X, double[] Y, double noise, boolean normalize, double[] lengthScale) {
|
|
|
|
|
- if (X.size() < 1)
|
|
|
|
|
- throw new IllegalArgumentException("X is empty");
|
|
|
|
|
- if (X.size() != Y.length) {
|
|
|
|
|
- throw new IllegalArgumentException(MessageFormat.format("X and Y are of different dimensions {0} and {1}", X.size(), Y.length));
|
|
|
|
|
|
|
+ public static GaussianProcess fit(List<RealVector> observedCoordinates, double[] observations, double noise, boolean normalize,
|
|
|
|
|
+ double[] lengthScale) {
|
|
|
|
|
+ if (observedCoordinates.size() < 1)
|
|
|
|
|
+ throw new IllegalArgumentException("Observed coordinates are empty");
|
|
|
|
|
+ if (observedCoordinates.size() != observations.length) {
|
|
|
|
|
+ throw new IllegalArgumentException(MessageFormat.format("Observed coordinates and observations are of different dimensions {0} and {1}",
|
|
|
|
|
+ observedCoordinates.size(), observations.length));
|
|
|
}
|
|
}
|
|
|
- OptionalInt brokenXIndex = IntStream.range(1, X.size()).filter(i -> X.get(i).getDimension() != X.get(i - 1).getDimension()).findFirst();
|
|
|
|
|
|
|
+ OptionalInt brokenXIndex = IntStream.range(1, observedCoordinates.size())
|
|
|
|
|
+ .filter(i -> observedCoordinates.get(i).getDimension() != observedCoordinates.get(i - 1).getDimension()).findFirst();
|
|
|
brokenXIndex.ifPresent(brokenIndex -> {
|
|
brokenXIndex.ifPresent(brokenIndex -> {
|
|
|
- throw new IllegalArgumentException(MessageFormat.format("X has different dimensions at index {0} ({2}) and index {1} ({3})",
|
|
|
|
|
- brokenIndex - 1, brokenXIndex, X.get(brokenIndex - 1).getDimension(), X.get(brokenIndex).getDimension()));
|
|
|
|
|
|
|
+ throw new IllegalArgumentException(MessageFormat.format(
|
|
|
|
|
+ "Observed coordinates has different dimensions at index {0} ({2}) and index {1} ({3})", brokenIndex - 1, brokenXIndex,
|
|
|
|
|
+ observedCoordinates.get(brokenIndex - 1).getDimension(), observedCoordinates.get(brokenIndex).getDimension()));
|
|
|
});
|
|
});
|
|
|
if (noise <= 0) {
|
|
if (noise <= 0) {
|
|
|
throw new IllegalArgumentException(MessageFormat.format("noise must be strictly positive, got {0}", noise));
|
|
throw new IllegalArgumentException(MessageFormat.format("noise must be strictly positive, got {0}", noise));
|
|
|
}
|
|
}
|
|
|
- if (lengthScale.length != X.get(0).getDimension()) {
|
|
|
|
|
- throw new IllegalArgumentException(
|
|
|
|
|
- MessageFormat.format("lengthScale is of different dimension {0} than X values {1}", lengthScale.length, X.get(0).getDimension()));
|
|
|
|
|
|
|
+ if (lengthScale.length != observedCoordinates.get(0).getDimension()) {
|
|
|
|
|
+ throw new IllegalArgumentException(MessageFormat.format("lengthScale is of different dimension {0} than the coordinates values {1}",
|
|
|
|
|
+ lengthScale.length, observedCoordinates.get(0).getDimension()));
|
|
|
}
|
|
}
|
|
|
double mean = 0;
|
|
double mean = 0;
|
|
|
double standardDeviation = 1;
|
|
double standardDeviation = 1;
|
|
|
if (normalize) {
|
|
if (normalize) {
|
|
|
- mean = StatUtils.mean(Y);
|
|
|
|
|
- standardDeviation = Math.sqrt(StatUtils.variance(Y, mean));
|
|
|
|
|
- for (int i = 0; i < Y.length; i++) {
|
|
|
|
|
- Y[i] = (Y[i] - mean) / standardDeviation;
|
|
|
|
|
|
|
+ mean = StatUtils.mean(observations);
|
|
|
|
|
+ standardDeviation = Math.sqrt(StatUtils.variance(observations, mean));
|
|
|
|
|
+ for (int i = 0; i < observations.length; i++) {
|
|
|
|
|
+ observations[i] = (observations[i] - mean) / standardDeviation;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- RealVector yVector = new ArrayRealVector(Y);
|
|
|
|
|
- RealVector scaleV = new ArrayRealVector(lengthScale);
|
|
|
|
|
|
|
+ RealVector observationVector = new ArrayRealVector(observations);
|
|
|
|
|
+ RealVector lengthScaleVector = new ArrayRealVector(lengthScale);
|
|
|
|
|
|
|
|
- RealMatrix k = maternKernel(X, scaleV);
|
|
|
|
|
- k = k.add(new DiagonalMatrix(DoubleStream.generate(() -> noise).limit(X.size()).toArray(), false));
|
|
|
|
|
|
|
+ RealMatrix k = maternKernel(observedCoordinates, lengthScaleVector);
|
|
|
|
|
+ k = k.add(new DiagonalMatrix(DoubleStream.generate(() -> noise).limit(observedCoordinates.size()).toArray(), false));
|
|
|
|
|
|
|
|
- CholeskyDecomposition choDec = new CholeskyDecomposition(k);
|
|
|
|
|
- RealVector w = choDec.getSolver().solve(yVector);
|
|
|
|
|
|
|
+ CholeskyDecomposition decomposition = new CholeskyDecomposition(k);
|
|
|
|
|
+ RealVector w = decomposition.getSolver().solve(observationVector);
|
|
|
|
|
|
|
|
- return new GaussianProcess(X, w, mean, standardDeviation, choDec, scaleV);
|
|
|
|
|
|
|
+ return new GaussianProcess(observedCoordinates, w, mean, standardDeviation, decomposition, lengthScaleVector);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Matern kernel for nu=2.5 (we get a twice differentiable gp)
|
|
* Matern kernel for nu=2.5 (we get a twice differentiable gp)
|
|
|
*/
|
|
*/
|
|
|
- private static RealMatrix maternKernel(List<RealVector> X, RealVector lengthScale) {
|
|
|
|
|
- RealMatrix k = new Array2DRowRealMatrix(X.size(), X.size());
|
|
|
|
|
- for (int i = 0; i < X.size(); i++) {
|
|
|
|
|
- RealVector left = X.get(i);
|
|
|
|
|
- for (int j = i; j < X.size(); j++) {
|
|
|
|
|
- RealVector right = X.get(j);
|
|
|
|
|
|
|
+ private static RealMatrix maternKernel(List<RealVector> observedCoordinates, RealVector lengthScale) {
|
|
|
|
|
+ RealMatrix k = new Array2DRowRealMatrix(observedCoordinates.size(), observedCoordinates.size());
|
|
|
|
|
+ for (int i = 0; i < observedCoordinates.size(); i++) {
|
|
|
|
|
+ RealVector left = observedCoordinates.get(i);
|
|
|
|
|
+ for (int j = i; j < observedCoordinates.size(); j++) {
|
|
|
|
|
+ RealVector right = observedCoordinates.get(j);
|
|
|
double maternVal = maternKernel(left, right, lengthScale);
|
|
double maternVal = maternKernel(left, right, lengthScale);
|
|
|
k.setEntry(i, j, maternVal);
|
|
k.setEntry(i, j, maternVal);
|
|
|
k.setEntry(j, i, maternVal);
|
|
k.setEntry(j, i, maternVal);
|
|
@@ -127,10 +132,10 @@ public class GaussianProcess {
|
|
|
return k;
|
|
return k;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private static RealVector maternKernel(List<RealVector> X, RealVector v, RealVector lengthScale) {
|
|
|
|
|
- RealVector out = new ArrayRealVector(X.size());
|
|
|
|
|
- for (int i = 0; i < X.size(); i++) {
|
|
|
|
|
- out.setEntry(i, maternKernel(X.get(i), v, lengthScale));
|
|
|
|
|
|
|
+ private static RealVector maternKernel(List<RealVector> observedCoordinates, RealVector vector, RealVector lengthScale) {
|
|
|
|
|
+ RealVector out = new ArrayRealVector(observedCoordinates.size());
|
|
|
|
|
+ for (int i = 0; i < observedCoordinates.size(); i++) {
|
|
|
|
|
+ out.setEntry(i, maternKernel(observedCoordinates.get(i), vector, lengthScale));
|
|
|
}
|
|
}
|
|
|
return out;
|
|
return out;
|
|
|
}
|
|
}
|
|
@@ -155,8 +160,8 @@ public class GaussianProcess {
|
|
|
double[] std = new double[width];
|
|
double[] std = new double[width];
|
|
|
for (int i = 0; i < width; i++) {
|
|
for (int i = 0; i < width; i++) {
|
|
|
double t = (i + 0.5) / width;
|
|
double t = (i + 0.5) / width;
|
|
|
- RealVector x = min.add(max.subtract(min).mapMultiplyToSelf(t));
|
|
|
|
|
- double[] meanStd = predict(x);
|
|
|
|
|
|
|
+ RealVector coordinates = min.add(max.subtract(min).mapMultiplyToSelf(t));
|
|
|
|
|
+ double[] meanStd = predict(coordinates);
|
|
|
mean[i] = meanStd[0];
|
|
mean[i] = meanStd[0];
|
|
|
std[i] = meanStd[1];
|
|
std[i] = meanStd[1];
|
|
|
}
|
|
}
|
|
@@ -170,23 +175,23 @@ public class GaussianProcess {
|
|
|
int[] lowerCharPos = DoubleStream.of(lowerBorder).mapToInt(toCharPos).toArray();
|
|
int[] lowerCharPos = DoubleStream.of(lowerBorder).mapToInt(toCharPos).toArray();
|
|
|
int[] mindCharPos = DoubleStream.of(mean).mapToInt(toCharPos).toArray();
|
|
int[] mindCharPos = DoubleStream.of(mean).mapToInt(toCharPos).toArray();
|
|
|
|
|
|
|
|
- for (int x = 0; x < width; x++) {
|
|
|
|
|
- if (upperCharPos[x] > 0 && upperCharPos[x] < height)
|
|
|
|
|
- out[upperCharPos[x]][x] = '-';
|
|
|
|
|
- if (lowerCharPos[x] > 0 && lowerCharPos[x] < height)
|
|
|
|
|
- out[lowerCharPos[x]][x] = '-';
|
|
|
|
|
- if (mindCharPos[x] > 0 && mindCharPos[x] < height)
|
|
|
|
|
- out[mindCharPos[x]][x] = '+';
|
|
|
|
|
|
|
+ for (int i = 0; i < width; i++) {
|
|
|
|
|
+ if (upperCharPos[i] > 0 && upperCharPos[i] < height)
|
|
|
|
|
+ out[upperCharPos[i]][i] = '-';
|
|
|
|
|
+ if (lowerCharPos[i] > 0 && lowerCharPos[i] < height)
|
|
|
|
|
+ out[lowerCharPos[i]][i] = '-';
|
|
|
|
|
+ if (mindCharPos[i] > 0 && mindCharPos[i] < height)
|
|
|
|
|
+ out[mindCharPos[i]][i] = '+';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
for (int i = 0; i < height; i++) {
|
|
for (int i = 0; i < height; i++) {
|
|
|
- sb.append(out[i]);
|
|
|
|
|
|
|
+ stringBuilder.append(out[i]);
|
|
|
if (i < 98) {
|
|
if (i < 98) {
|
|
|
- sb.append('\n');
|
|
|
|
|
|
|
+ stringBuilder.append('\n');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return sb.toString();
|
|
|
|
|
|
|
+ return stringBuilder.toString();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|