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 static agents.anac.y2015.Phoenix.GP.MatrixOperations.*;
|
---|
29 |
|
---|
30 | import java.util.Arrays;
|
---|
31 |
|
---|
32 | import agents.Jama.Matrix;
|
---|
33 |
|
---|
34 |
|
---|
35 | /** Squared Exponential covariance function with Automatic Relevance Detemination
|
---|
36 | * (ARD) distance measure. The covariance function is parameterized as:
|
---|
37 | * <p>
|
---|
38 | * k(x^p,x^q) = sf2 * exp(-(x^p - x^q)'*inv(P)*(x^p - x^q)/2)
|
---|
39 | * <p>
|
---|
40 | * where the P matrix is diagonal with ARD parameters ell_1^2,...,ell_D^2, where
|
---|
41 | * D is the dimension of the input space and sf2 is the signal variance. The
|
---|
42 | * hyperparameters are:
|
---|
43 | * <p>
|
---|
44 | * [ log(ell_1)
|
---|
45 | * log(ell_2)
|
---|
46 | * .
|
---|
47 | * log(ell_D)
|
---|
48 | * log(sqrt(sf2))]
|
---|
49 | */
|
---|
50 | public class CovSEard implements CovarianceFunction {
|
---|
51 |
|
---|
52 | private int D;
|
---|
53 | private int numParameters;
|
---|
54 | private Matrix K=null;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Creates a new <code>PhoenixAlpha.CovSEard PhoenixAlpha.CovarianceFunction<code>
|
---|
58 | * @param inputDimension muber of dimension of the input
|
---|
59 | */
|
---|
60 | public CovSEard(int inputDimension){
|
---|
61 | this.D = inputDimension;
|
---|
62 | numParameters = D+1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Returns the number of hyperparameters of <code>PhoenixAlpha.CovSEard</code>
|
---|
67 | * @return number of hyperparameters
|
---|
68 | */
|
---|
69 | public int numParameters() {
|
---|
70 | return numParameters;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Compute covariance matrix of a dataset X
|
---|
75 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
76 | * @param X input dataset
|
---|
77 | * @return K covariance <code>Matrix</code>
|
---|
78 | */
|
---|
79 | public Matrix compute(Matrix loghyper, Matrix X) {
|
---|
80 |
|
---|
81 | if(X.getColumnDimension()!=D)
|
---|
82 | throw new IllegalArgumentException("The number of dimensions specified on the covariance function "+D+" must agree with the size of the input vector"+X.getColumnDimension());
|
---|
83 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters)
|
---|
84 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters);
|
---|
85 |
|
---|
86 | final Matrix ell = exp(loghyper.getMatrix(0,D-1,0,0)); // characteristic length scales
|
---|
87 | final double sf2 = Math.exp(2*loghyper.get(D,0)); // signal variance
|
---|
88 |
|
---|
89 | Matrix diag = new Matrix(D,D);
|
---|
90 | for(int i=0; i<D; i++)
|
---|
91 | diag.set(i,i,1/ell.get(i,0));
|
---|
92 |
|
---|
93 | K = exp(squareDist(diag.times(X.transpose())).times(-0.5)).times(sf2); // SE covariance
|
---|
94 |
|
---|
95 | return K;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Compute compute test set covariances
|
---|
100 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
101 | * @param X input dataset
|
---|
102 | * @param Xstar test set
|
---|
103 | * @return [K(Xstar,Xstar) K(X,Xstar)]
|
---|
104 | */
|
---|
105 | public Matrix[] compute(Matrix loghyper, Matrix X, Matrix Xstar) {
|
---|
106 |
|
---|
107 | if(X.getColumnDimension()!=D)
|
---|
108 | throw new IllegalArgumentException("The number of dimensions specified on the covariance function "+D+" must agree with the size of the input vector"+X.getColumnDimension());
|
---|
109 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters)
|
---|
110 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters);
|
---|
111 |
|
---|
112 | final Matrix ell = exp(loghyper.getMatrix(0,D-1,0,0)); // characteristic length scales
|
---|
113 | final double sf2 = Math.exp(2*loghyper.get(D,0)); // signal variance
|
---|
114 |
|
---|
115 | double[] a = new double[Xstar.getRowDimension()];
|
---|
116 | Arrays.fill(a,sf2);
|
---|
117 | Matrix A = new Matrix(a,Xstar.getRowDimension());
|
---|
118 |
|
---|
119 | Matrix diag = new Matrix(D,D);
|
---|
120 | for(int i=0; i<D; i++)
|
---|
121 | diag.set(i,i,1/ell.get(i,0));
|
---|
122 |
|
---|
123 | Matrix B = exp(squareDist(diag.times(X.transpose()),diag.times(Xstar.transpose())).times(-0.5)).times(sf2);
|
---|
124 |
|
---|
125 | return new Matrix[]{A,B};
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Coompute the derivatives of this <code>PhoenixAlpha.CovarianceFunction</code> with respect
|
---|
131 | * to the hyperparameter with index <code>idx</code>
|
---|
132 | *
|
---|
133 | * @param loghyper hyperparameters
|
---|
134 | * @param X input dataset
|
---|
135 | * @param index hyperparameter index
|
---|
136 | * @return <code>Matrix</code> of derivatives
|
---|
137 | */
|
---|
138 | public Matrix computeDerivatives(Matrix loghyper, Matrix X, int index){
|
---|
139 |
|
---|
140 | if(X.getColumnDimension()!=D)
|
---|
141 | throw new IllegalArgumentException("The number of dimensions specified on the covariance function "+D+" must agree with the size of the input vector"+X.getColumnDimension());
|
---|
142 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters)
|
---|
143 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters);
|
---|
144 | if(index>numParameters()-1)
|
---|
145 | throw new IllegalArgumentException("Wrong hyperparameters index "+index+" it should be smaller or equal to "+(numParameters()-1));
|
---|
146 |
|
---|
147 | Matrix A=null;
|
---|
148 |
|
---|
149 | final Matrix ell = exp(loghyper.getMatrix(0,D-1,0,0)); // characteristic length scales
|
---|
150 | final double sf2 = Math.exp(2*loghyper.get(D,0)); // signal variance
|
---|
151 | // noise variance
|
---|
152 |
|
---|
153 | if(K.getRowDimension()!=X.getRowDimension() || K.getColumnDimension()!=X.getRowDimension()){
|
---|
154 | Matrix diag = new Matrix(D,D);
|
---|
155 | for(int i=0; i<D; i++)
|
---|
156 | diag.set(i,i,1/ell.get(i,0));
|
---|
157 |
|
---|
158 | K = exp(squareDist(diag.times(X.transpose())).times(-0.5)).times(sf2); // SE covariance
|
---|
159 | }
|
---|
160 |
|
---|
161 | if(index<D){ //length scale parameters
|
---|
162 | Matrix col = squareDist(X.getMatrix(0,X.getRowDimension()-1,index,index).transpose().times(1/ell.get(index,0)));
|
---|
163 |
|
---|
164 | A = K.arrayTimes(col);
|
---|
165 | } else { // magnitude parameter
|
---|
166 | A=K.times(2);
|
---|
167 | K = null;
|
---|
168 | }
|
---|
169 |
|
---|
170 | return A;
|
---|
171 | }
|
---|
172 |
|
---|
173 | private static Matrix squareDist(Matrix a){
|
---|
174 | return squareDist(a,a);
|
---|
175 | }
|
---|
176 |
|
---|
177 | private static Matrix squareDist(Matrix a, Matrix b){
|
---|
178 | Matrix C = new Matrix(a.getColumnDimension(),b.getColumnDimension());
|
---|
179 | final int m = a.getColumnDimension();
|
---|
180 | final int n = b.getColumnDimension();
|
---|
181 | final int d = a.getRowDimension();
|
---|
182 |
|
---|
183 | for (int i=0; i<m; i++){
|
---|
184 | for (int j=0; j<n; j++) {
|
---|
185 | double z = 0.0;
|
---|
186 | for (int k=0; k<d; k++) { double t = a.get(k,i) - b.get(k,j); z += t*t; }
|
---|
187 | C.set(i,j,z);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | return C;
|
---|
192 | }
|
---|
193 |
|
---|
194 | }
|
---|