-
Notifications
You must be signed in to change notification settings - Fork 256
/
RegressionModelEvaluator.java
377 lines (303 loc) · 11.9 KB
/
RegressionModelEvaluator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (c) 2013 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.evaluator.regression;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.dmg.pmml.FieldRef;
import org.dmg.pmml.OpType;
import org.dmg.pmml.PMML;
import org.dmg.pmml.regression.CategoricalPredictor;
import org.dmg.pmml.regression.NumericPredictor;
import org.dmg.pmml.regression.PMMLAttributes;
import org.dmg.pmml.regression.PredictorTerm;
import org.dmg.pmml.regression.RegressionModel;
import org.dmg.pmml.regression.RegressionTable;
import org.jpmml.evaluator.Classification;
import org.jpmml.evaluator.EvaluationContext;
import org.jpmml.evaluator.ExpressionUtil;
import org.jpmml.evaluator.FieldValue;
import org.jpmml.evaluator.FieldValueUtil;
import org.jpmml.evaluator.ModelEvaluator;
import org.jpmml.evaluator.PMMLUtil;
import org.jpmml.evaluator.TargetField;
import org.jpmml.evaluator.TargetUtil;
import org.jpmml.evaluator.Value;
import org.jpmml.evaluator.ValueFactory;
import org.jpmml.evaluator.ValueMap;
import org.jpmml.model.InvalidAttributeException;
import org.jpmml.model.InvalidElementException;
import org.jpmml.model.InvalidElementListException;
import org.jpmml.model.UnsupportedAttributeException;
public class RegressionModelEvaluator extends ModelEvaluator<RegressionModel> {
private RegressionModelEvaluator(){
}
public RegressionModelEvaluator(PMML pmml){
this(pmml, PMMLUtil.findModel(pmml, RegressionModel.class));
}
public RegressionModelEvaluator(PMML pmml, RegressionModel regressionModel){
super(pmml, regressionModel);
@SuppressWarnings("unused")
List<RegressionTable> regressionTables = regressionModel.requireRegressionTables();
}
@Override
public String getSummary(){
return "Regression";
}
@Override
protected <V extends Number> Map<String, ?> evaluateRegression(ValueFactory<V> valueFactory, EvaluationContext context){
RegressionModel regressionModel = getModel();
TargetField targetField = getTargetField();
String targetFieldName = regressionModel.getTargetField();
if(targetFieldName != null && !Objects.equals(targetField.getFieldName(), targetFieldName)){
throw new InvalidAttributeException(regressionModel, PMMLAttributes.REGRESSIONMODEL_TARGETFIELD, targetFieldName);
}
List<RegressionTable> regressionTables = regressionModel.requireRegressionTables();
if(regressionTables.size() != 1){
throw new InvalidElementListException(regressionTables);
}
RegressionTable regressionTable = regressionTables.get(0);
Value<V> result = evaluateRegressionTable(valueFactory, regressionTable, context);
if(result == null){
return TargetUtil.evaluateRegressionDefault(valueFactory, targetField);
}
RegressionModel.NormalizationMethod normalizationMethod = regressionModel.getNormalizationMethod();
switch(normalizationMethod){
case NONE:
case SOFTMAX:
case LOGIT:
case EXP:
case PROBIT:
case CLOGLOG:
case LOGLOG:
case CAUCHIT:
RegressionModelUtil.normalizeRegressionResult(normalizationMethod, result);
break;
case SIMPLEMAX:
throw new InvalidAttributeException(regressionModel, normalizationMethod);
default:
throw new UnsupportedAttributeException(regressionModel, normalizationMethod);
}
return TargetUtil.evaluateRegression(targetField, result);
}
@Override
protected <V extends Number> Map<String, ? extends Classification<?, V>> evaluateClassification(ValueFactory<V> valueFactory, EvaluationContext context){
RegressionModel regressionModel = getModel();
TargetField targetField = getTargetField();
String targetFieldName = regressionModel.getTargetField();
if(targetFieldName != null && !Objects.equals(targetField.getFieldName(), targetFieldName)){
throw new InvalidAttributeException(regressionModel, PMMLAttributes.REGRESSIONMODEL_TARGETFIELD, targetFieldName);
}
OpType opType = targetField.getOpType();
switch(opType){
case CATEGORICAL:
case ORDINAL:
break;
default:
throw new InvalidElementException(regressionModel);
}
List<RegressionTable> regressionTables = regressionModel.requireRegressionTables();
if(regressionTables.size() < 2){
throw new InvalidElementListException(regressionTables);
}
List<?> targetCategories = targetField.getCategories();
if(targetCategories != null && targetCategories.size() != regressionTables.size()){
throw new InvalidElementListException(regressionTables);
}
ValueMap<Object, V> values = new ValueMap<>(2 * regressionTables.size());
for(int i = 0, max = regressionTables.size(); i < max; i++){
RegressionTable regressionTable = regressionTables.get(i);
Object targetCategory = regressionTable.requireTargetCategory();
if(targetCategories != null && targetCategories.indexOf(targetCategory) < 0){
throw new InvalidAttributeException(regressionTable, PMMLAttributes.REGRESSIONTABLE_TARGETCATEGORY, targetCategory);
}
Value<V> value = evaluateRegressionTable(valueFactory, regressionTable, context);
// "If one or more RegressionTable elements cannot be evaluated, then the predictions are defined by the priorProbability values of the Target element"
if(value == null){
return TargetUtil.evaluateClassificationDefault(valueFactory, targetField);
}
values.put(targetCategory, value);
}
RegressionModel.NormalizationMethod normalizationMethod = regressionModel.getNormalizationMethod();
switch(opType){
case CATEGORICAL:
if(values.size() == 2){
switch(normalizationMethod){
case NONE:
case LOGIT:
case PROBIT:
case CLOGLOG:
case LOGLOG:
case CAUCHIT:
RegressionModelUtil.computeBinomialProbabilities(normalizationMethod, values);
break;
case SIMPLEMAX:
case SOFTMAX:
// XXX: Non-standard behaviour
if(isDefault(regressionTables.get(1)) && (normalizationMethod == RegressionModel.NormalizationMethod.SOFTMAX)){
RegressionModelUtil.computeBinomialProbabilities(RegressionModel.NormalizationMethod.LOGIT, values);
} else
{
RegressionModelUtil.computeMultinomialProbabilities(normalizationMethod, values);
}
break;
case EXP:
throw new InvalidAttributeException(regressionModel, normalizationMethod);
default:
throw new UnsupportedAttributeException(regressionModel, normalizationMethod);
}
} else
{
switch(normalizationMethod){
case NONE:
case SIMPLEMAX:
case SOFTMAX:
RegressionModelUtil.computeMultinomialProbabilities(normalizationMethod, values);
break;
case LOGIT:
case PROBIT:
case CLOGLOG:
case EXP:
case LOGLOG:
case CAUCHIT:
// XXX: Non-standard behaviour
if((RegressionModel.NormalizationMethod.LOGIT).equals(normalizationMethod)){
RegressionModelUtil.computeMultinomialProbabilities(normalizationMethod, values);
break;
}
throw new InvalidAttributeException(regressionModel, normalizationMethod);
default:
throw new UnsupportedAttributeException(regressionModel, normalizationMethod);
}
}
break;
case ORDINAL:
switch(normalizationMethod){
case NONE:
case LOGIT:
case PROBIT:
case CLOGLOG:
case LOGLOG:
case CAUCHIT:
RegressionModelUtil.computeOrdinalProbabilities(normalizationMethod, values);
break;
case SIMPLEMAX:
case SOFTMAX:
case EXP:
throw new InvalidAttributeException(regressionModel, normalizationMethod);
default:
throw new UnsupportedAttributeException(regressionModel, normalizationMethod);
}
break;
default:
throw new InvalidElementException(regressionModel);
}
Classification<?, V> result = createClassification(values);
return TargetUtil.evaluateClassification(targetField, result);
}
private <V extends Number> Value<V> evaluateRegressionTable(ValueFactory<V> valueFactory, RegressionTable regressionTable, EvaluationContext context){
Value<V> result = valueFactory.newValue();
if(regressionTable.hasNumericPredictors()){
List<NumericPredictor> numericPredictors = regressionTable.getNumericPredictors();
for(int i = 0, max = numericPredictors.size(); i < max; i++){
NumericPredictor numericPredictor = numericPredictors.get(i);
FieldValue value = context.evaluate(numericPredictor.requireField());
// "If the input value is missing, then the result evaluates to a missing value"
if(FieldValueUtil.isMissing(value)){
return null;
}
int exponent = numericPredictor.getExponent();
if(exponent != 1){
result.add(numericPredictor.requireCoefficient(), value.asNumber(), exponent);
} else
{
result.add(numericPredictor.requireCoefficient(), value.asNumber());
}
}
} // End if
if(regressionTable.hasCategoricalPredictors()){
List<CategoricalPredictor> categoricalPredictors = regressionTable.getCategoricalPredictors();
// A categorical field is represented by a list of CategoricalPredictor elements.
// The iteration over this list can be terminated right after finding the first and only match
String matchedFieldName = null;
for(int i = 0, max = categoricalPredictors.size(); i < max; i++){
CategoricalPredictor categoricalPredictor = categoricalPredictors.get(i);
String fieldName = categoricalPredictor.requireField();
if(matchedFieldName != null){
if((matchedFieldName).equals(fieldName)){
continue;
}
matchedFieldName = null;
}
FieldValue value = context.evaluate(fieldName);
// "If the input value is missing, then the categorical field is ignored"
if(FieldValueUtil.isMissing(value)){
matchedFieldName = fieldName;
continue;
}
boolean equals = value.equals(categoricalPredictor);
if(equals){
matchedFieldName = fieldName;
result.add(categoricalPredictor.requireCoefficient());
}
}
} // End if
if(regressionTable.hasPredictorTerms()){
List<PredictorTerm> predictorTerms = regressionTable.getPredictorTerms();
List<Number> factors = new ArrayList<>();
for(int i = 0, max = predictorTerms.size(); i < max; i++){
PredictorTerm predictorTerm = predictorTerms.get(i);
factors.clear();
Number coefficient = predictorTerm.requireCoefficient();
List<FieldRef> fieldRefs = predictorTerm.requireFieldRefs();
for(FieldRef fieldRef : fieldRefs){
FieldValue value = ExpressionUtil.evaluate(fieldRef, context);
// "If the input value is missing, then the result evaluates to a missing value"
if(FieldValueUtil.isMissing(value)){
return null;
}
factors.add(value.asNumber());
}
if(factors.size() == 1){
result.add(coefficient, factors.get(0));
} else
if(factors.size() == 2){
result.add(coefficient, factors.get(0), factors.get(1));
} else
{
result.add(coefficient, factors.toArray(new Number[factors.size()]));
}
}
}
Number intercept = regressionTable.requireIntercept();
if(intercept.doubleValue() != 0d){
result.add(intercept);
}
return result;
}
static
private boolean isDefault(RegressionTable regressionTable){
if(regressionTable.hasNumericPredictors() || regressionTable.hasCategoricalPredictors() || regressionTable.hasPredictorTerms()){
return false;
}
Number intercept = regressionTable.requireIntercept();
return (intercept.doubleValue() == 0d);
}
}