[1] | 1 | package agents.anac.y2015.Phoenix.GP;/* This file is part of the jgpml Project.
|
---|
| 2 | * http://github.com/renzodenardi/jgpml
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2011 Renzo De Nardi and Hugo Gravato-Marques
|
---|
| 5 | *
|
---|
| 6 | * Permission is hereby granted, free of charge, to any person
|
---|
| 7 | * obtaining a copy of this software and associated documentation
|
---|
| 8 | * files (the "Software"), to deal in the Software without
|
---|
| 9 | * restriction, including without limitation the rights to use,
|
---|
| 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 11 | * copies of the Software, and to permit persons to whom the
|
---|
| 12 | * Software is furnished to do so, subject to the following
|
---|
| 13 | * conditions:
|
---|
| 14 | *
|
---|
| 15 | * The above copyright notice and this permission notice shall be
|
---|
| 16 | * included in all copies or substantial portions of the Software.
|
---|
| 17 | *
|
---|
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 25 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | import agents.Jama.Matrix;
|
---|
| 29 |
|
---|
| 30 | /** Composes a covariance function as the sum of other covariance
|
---|
| 31 | * functions. This function doesn't actually compute very much on its own, it
|
---|
| 32 | * merely calls other covariance functions with the right parameters.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | public class CovSum implements CovarianceFunction{
|
---|
| 36 |
|
---|
| 37 | CovarianceFunction[] f;
|
---|
| 38 | int[] idx;
|
---|
| 39 | private int D;
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Create a new <code>PhoenixAlpha.CovarianceFunction</code> as sum of the
|
---|
| 43 | * <code>PhoenixAlpha.CovarianceFunction</code>s passed as input.
|
---|
| 44 | *
|
---|
| 45 | * @param inputDimensions input dimension of the dataset
|
---|
| 46 | * @param f array of <code>PhoenixAlpha.CovarianceFunction</code>
|
---|
| 47 | *
|
---|
| 48 | * @see CovarianceFunction
|
---|
| 49 | */
|
---|
| 50 | public CovSum(int inputDimensions, CovarianceFunction... f){
|
---|
| 51 | this.D = inputDimensions;
|
---|
| 52 | this.f=f;
|
---|
| 53 | idx=new int[f.length+1];
|
---|
| 54 | for(int i=0; i<f.length; i++){
|
---|
| 55 | idx[i+1]=idx[i]+f[i].numParameters();
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Returns the number of hyperparameters of this<code>PhoenixAlpha.CovarianceFunction</code>
|
---|
| 61 | * @return number of hyperparameters
|
---|
| 62 | */
|
---|
| 63 | public int numParameters() {
|
---|
| 64 | return idx[f.length];
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Compute covariance matrix of a dataset X
|
---|
| 69 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
| 70 | * @param X input dataset
|
---|
| 71 | * @return K covariance <code>Matrix</code>
|
---|
| 72 | */
|
---|
| 73 | public Matrix compute(Matrix loghyper, Matrix X){
|
---|
| 74 |
|
---|
| 75 | Matrix K = new Matrix(X.getRowDimension(),X.getRowDimension());
|
---|
| 76 |
|
---|
| 77 | for(int i=0; i<f.length; i++){
|
---|
| 78 | Matrix loghyperi = loghyper.getMatrix(idx[i],idx[i+1]-1,0,0);
|
---|
| 79 | K.plusEquals(f[i].compute(loghyperi,X));
|
---|
| 80 | }
|
---|
| 81 | return K;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Compute compute test set covariances
|
---|
| 86 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
| 87 | * @param X input dataset
|
---|
| 88 | * @param Xstar test set
|
---|
| 89 | * @return [K(Xstar,Xstar) K(X,Xstar)]
|
---|
| 90 | */
|
---|
| 91 | public Matrix[] compute(Matrix loghyper, Matrix X, Matrix Xstar){
|
---|
| 92 |
|
---|
| 93 | Matrix A = new Matrix(Xstar.getRowDimension(),1);
|
---|
| 94 | Matrix B = new Matrix(X.getRowDimension(),Xstar.getRowDimension());
|
---|
| 95 |
|
---|
| 96 | for(int i=0; i<f.length; i++){
|
---|
| 97 | Matrix loghyperi = loghyper.getMatrix(idx[i],idx[i+1]-1,0,0);
|
---|
| 98 | Matrix[] K = f[i].compute(loghyperi,X,Xstar);
|
---|
| 99 | A.plusEquals(K[0]);
|
---|
| 100 | B.plusEquals(K[1]);
|
---|
| 101 | }
|
---|
| 102 | return new Matrix[]{A,B};
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * Coompute the derivatives of this <code>PhoenixAlpha.CovarianceFunction</code> with respect
|
---|
| 107 | * to the hyperparameter with index <code>idx</code>
|
---|
| 108 | *
|
---|
| 109 | * @param loghyper hyperparameters
|
---|
| 110 | * @param X input dataset
|
---|
| 111 | * @param index hyperparameter index
|
---|
| 112 | * @return <code>Matrix</code> of derivatives
|
---|
| 113 | */
|
---|
| 114 | public Matrix computeDerivatives(Matrix loghyper, Matrix X, int index){
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | if(index>numParameters()-1)
|
---|
| 118 | throw new IllegalArgumentException("Wrong hyperparameters index "+index+" it should be smaller or equal to "+(numParameters()-1));
|
---|
| 119 |
|
---|
| 120 | int whichf=0;
|
---|
| 121 | while(index>(idx[whichf+1]-1)) whichf++; // find in which of the covariance this parameter is
|
---|
| 122 |
|
---|
| 123 | Matrix loghyperi = loghyper.getMatrix(idx[whichf],idx[whichf+1]-1,0,0);
|
---|
| 124 | index-=idx[whichf];
|
---|
| 125 | return f[whichf].computeDerivatives(loghyperi,X, index);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | }
|
---|