~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/qsar/model/R/CNNRegressionModelFit.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004-2007  The Chemistry Development Kit (CDK) project
 
3
 *
 
4
 *  Contact: cdk-devel@lists.sourceforge.net
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 */
 
20
 
 
21
package org.openscience.cdk.qsar.model.R;
 
22
 
 
23
/**
 
24
 * A class that wraps the return value from R function, nnet().
 
25
 *
 
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.
 
31
 * 
 
32
 *
 
33
 * @author Rajarshi Guha
 
34
 * @cdk.require r-project
 
35
 * @cdk.module qsar
 
36
 * @deprecated
 
37
 */
 
38
 
 
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;
 
44
    private double value;
 
45
    private double[][] hessian = null;
 
46
 
 
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];
 
52
            }
 
53
        }
 
54
        return(m);
 
55
    }
 
56
    
 
57
    /**
 
58
     * Constructs an object to contain a CNN regression fit.
 
59
     *
 
60
     * This class should not be instantiated directly and is only
 
61
     * required withtin R
 
62
     *
 
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
 
70
     */
 
71
    public CNNRegressionModelFit(
 
72
            int noutput, 
 
73
            int nobs,
 
74
            double[] weights, 
 
75
            double[] fitted, double[] residual, 
 
76
            double value,
 
77
            double[] hessian) {
 
78
 
 
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
 
82
 
 
83
        this.noutput = noutput;
 
84
        setWeights(weights);
 
85
        setResiduals(vectorToMatrix(residual, nobs,noutput));
 
86
        setFitted(vectorToMatrix(fitted, nobs,noutput));
 
87
        setValue(value);
 
88
        setHessian(vectorToMatrix(hessian,weights.length,weights.length));
 
89
    }
 
90
    /**
 
91
     * Constructs an object to contain a CNN regression fit.
 
92
     *
 
93
     * This class should not be instantiated directly and is only
 
94
     * required withtin R
 
95
     *
 
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
 
102
     */
 
103
    public CNNRegressionModelFit(
 
104
            int noutput, 
 
105
            int nobs,
 
106
            double[] weights, 
 
107
            double[] fitted, double[] residual, 
 
108
            double value) {
 
109
        this.noutput = noutput;
 
110
        setWeights(weights);
 
111
        setResiduals(vectorToMatrix(residual, nobs,noutput));
 
112
        setFitted(vectorToMatrix(fitted, nobs,noutput));
 
113
        setValue(value);
 
114
    }
 
115
 
 
116
    /**
 
117
     * Get the final value of the cost function.
 
118
     *
 
119
     * This method should not be called outside this class
 
120
     *
 
121
     * @return The final value of the cost function
 
122
     * @see #setValue
 
123
     */
 
124
    public double getValue() {
 
125
        return(this.value);
 
126
    }
 
127
    /**
 
128
     * Set the final value of the cost function.
 
129
     *
 
130
     * This method should not be called outside this class
 
131
     *
 
132
     * @param value The value of the cost function at convergence
 
133
     * @see #getValue
 
134
     */
 
135
    public void setValue(double value) {
 
136
        this.value = value;
 
137
    }
 
138
 
 
139
    /**
 
140
     * Get the Hessian for the final network.
 
141
     * 
 
142
     * This method should not be called outside this class
 
143
     *
 
144
     * @return A 2-dimensional array containing the hessian
 
145
     * @see #setHessian
 
146
     */
 
147
    public double[][] getHessian() { return(this.hessian); }
 
148
    /**
 
149
     * Set the Hessian for the final network.
 
150
     * 
 
151
     * This method should not be called outside this class
 
152
     *
 
153
     * @param theHessian A 2-dimensional array containing the hessian
 
154
     * @see #getHessian
 
155
     */
 
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];
 
162
            }
 
163
        }
 
164
    }
 
165
 
 
166
    /**
 
167
     * Get the weights and biases of the final network.
 
168
     * 
 
169
     * This method should not be called outside this class
 
170
     *
 
171
     * @return A 1-dimensional array of weights and biases
 
172
     * @see #setWeights
 
173
     */
 
174
    public double[] getWeights() { return(this.weights); }
 
175
    /**
 
176
     * Set the weights and biases of the final network.
 
177
     * 
 
178
     * This method should not be called outside this class
 
179
     *
 
180
     * @param weights A 1-dimensional array of weights and biases
 
181
     * @see #getWeights
 
182
     */
 
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];
 
187
        }
 
188
    }
 
189
    /**
 
190
     * Get the residuals of the fit.
 
191
     * 
 
192
     * This method should not be called outside this class
 
193
     *
 
194
     * @return A 2-dimensional array of residuals. The rows contain the
 
195
     * observations and the columns contain the predicted variables
 
196
     * @see #setResiduals
 
197
     */
 
198
    public double[][] getResiduals() { return(this.residuals); }
 
199
    /**
 
200
     * Set the residuals of the fit.
 
201
     * 
 
202
     * This method should not be called outside this class
 
203
     *
 
204
     * @param residuals A 2-dimensional array of residuals. The rows contain the
 
205
     * observations and the columns contain the predicted variables
 
206
     * @see #getResiduals
 
207
     */
 
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];
 
213
            }
 
214
        }
 
215
    }
 
216
    /**
 
217
     * Get the fitted values.
 
218
     * 
 
219
     * This method should not be called outside this class
 
220
     *
 
221
     * @return A 2-dimensional array of residuals. The rows contain the
 
222
     * observations and the columns contain the predicted variables
 
223
     * @see #setFitted
 
224
     */
 
225
    public double[][] getFitted() { return(this.fitted); }
 
226
    /**
 
227
     * Set the fitted values.
 
228
     * 
 
229
     * This method should not be called outside this class
 
230
     *
 
231
     * @param fitted A 2-dimensional array of residuals. The rows contain the
 
232
     * observations and the columns contain the predicted variables
 
233
     * @see #getFitted
 
234
     */
 
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];
 
240
            }
 
241
        }
 
242
    }
 
243
}
 
244
 
 
245
 
 
246