2
* Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project
4
* Contact: cdk-devel@lists.sourceforge.net
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public License
8
* as published by the Free Software Foundation; either version 2.1
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
package org.openscience.cdk.qsar.model.R;
24
* A class that wraps the return value from R function, nnet().
26
* This is an internal class used by R to return the result of
27
* the call to <a href="http://stat.ethz.ch/R-manual/R-patched/library/nnet/html/nnet.html">nnet</a>.
28
* As a result it should not be instantiated by the user. The actual modeling
29
* class, <code>CNNRegressionModel</code>, provides acess to the various
30
* fields of this object.
33
* @author Rajarshi Guha
34
* @cdk.require r-project
39
public class CNNRegressionModelFit {
40
private int noutput; // number of output neurons (== number of classes)
41
private double[] weights;
42
private double[][] fitted;
43
private double[][] residuals;
45
private double[][] hessian = null;
47
private double[][] vectorToMatrix(double[] v, int nrow, int ncol) {
48
double[][] m = new double[nrow][ncol];
49
for (int i = 0; i < ncol; i++) {
50
for (int j = 0; j < nrow; j++) {
51
m[j][i] = v[j + i*nrow];
58
* Constructs an object to contain a CNN regression fit.
60
* This class should not be instantiated directly and is only
63
* @param noutput The number of output neurons (ie the number of predicted variables)
64
* @param nobs The number of observations
65
* @param weights A 1-dimensional array containing the weights and biases
66
* @param fitted A 1-dimensional array containing the fitted values
67
* @param residual A 1-dimensional array containing the residuals of the fitted values
68
* @param value The final value of the cost function
69
* @param hessian A 1-dimensional array containing the Hessian
71
public CNNRegressionModelFit(
75
double[] fitted, double[] residual,
79
// dimensions of hessian = nwt x nwt
80
// dimensions of fitted, residual = nobs x noutput
81
// also note that matrices come in as columnwise 1D arrays
83
this.noutput = noutput;
85
setResiduals(vectorToMatrix(residual, nobs,noutput));
86
setFitted(vectorToMatrix(fitted, nobs,noutput));
88
setHessian(vectorToMatrix(hessian,weights.length,weights.length));
91
* Constructs an object to contain a CNN regression fit.
93
* This class should not be instantiated directly and is only
96
* @param noutput The number of output neurons (ie the number of predicted variables)
97
* @param nobs The number of observations
98
* @param weights A 1-dimensional array containing the weights and biases
99
* @param fitted A 1-dimensional array containing the fitted values
100
* @param residual A 1-dimensional array containing the residuals of the fitted values
101
* @param value The final value of the cost function
103
public CNNRegressionModelFit(
107
double[] fitted, double[] residual,
109
this.noutput = noutput;
111
setResiduals(vectorToMatrix(residual, nobs,noutput));
112
setFitted(vectorToMatrix(fitted, nobs,noutput));
117
* Get the final value of the cost function.
119
* This method should not be called outside this class
121
* @return The final value of the cost function
124
public double getValue() {
128
* Set the final value of the cost function.
130
* This method should not be called outside this class
132
* @param value The value of the cost function at convergence
135
public void setValue(double value) {
140
* Get the Hessian for the final network.
142
* This method should not be called outside this class
144
* @return A 2-dimensional array containing the hessian
147
public double[][] getHessian() { return(this.hessian); }
149
* Set the Hessian for the final network.
151
* This method should not be called outside this class
153
* @param theHessian A 2-dimensional array containing the hessian
156
public void setHessian(double[][] theHessian) {
157
if (theHessian == null) return;
158
this.hessian = new double[theHessian.length][this.noutput];
159
for (int i = 0; i < theHessian.length; i++) {
160
for (int j = 0; j < this.noutput; j++) {
161
this.hessian[i][j] = theHessian[i][j];
167
* Get the weights and biases of the final network.
169
* This method should not be called outside this class
171
* @return A 1-dimensional array of weights and biases
174
public double[] getWeights() { return(this.weights); }
176
* Set the weights and biases of the final network.
178
* This method should not be called outside this class
180
* @param weights A 1-dimensional array of weights and biases
183
public void setWeights(double[] weights) {
184
this.weights = new double[weights.length];
185
for (int i = 0; i < weights.length; i++) {
186
this.weights[i] = weights[i];
190
* Get the residuals of the fit.
192
* This method should not be called outside this class
194
* @return A 2-dimensional array of residuals. The rows contain the
195
* observations and the columns contain the predicted variables
198
public double[][] getResiduals() { return(this.residuals); }
200
* Set the residuals of the fit.
202
* This method should not be called outside this class
204
* @param residuals A 2-dimensional array of residuals. The rows contain the
205
* observations and the columns contain the predicted variables
208
public void setResiduals(double[][] residuals) {
209
this.residuals = new double[residuals.length][this.noutput];
210
for (int i = 0; i < residuals.length; i++) {
211
for (int j = 0; j < this.noutput; j++) {
212
this.residuals[i][j] = residuals[i][j];
217
* Get the fitted values.
219
* This method should not be called outside this class
221
* @return A 2-dimensional array of residuals. The rows contain the
222
* observations and the columns contain the predicted variables
225
public double[][] getFitted() { return(this.fitted); }
227
* Set the fitted values.
229
* This method should not be called outside this class
231
* @param fitted A 2-dimensional array of residuals. The rows contain the
232
* observations and the columns contain the predicted variables
235
public void setFitted(double[][] fitted) {
236
this.fitted = new double[fitted.length][this.noutput];
237
for (int i = 0; i < fitted.length; i++) {
238
for (int j = 0; j < this.noutput; j++) {
239
this.fitted[i][j] = fitted[i][j];