-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path160.java
More file actions
81 lines (65 loc) · 2.66 KB
/
Copy path160.java
File metadata and controls
81 lines (65 loc) · 2.66 KB
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
/*
* Copyright (c) 2015 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;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.dmg.pmml.ContinuousDistribution;
import org.dmg.pmml.DataType;
import org.dmg.pmml.GaussianDistribution;
import org.dmg.pmml.PoissonDistribution;
public class DistributionUtil {
private DistributionUtil(){
}
/**
* <p>
* Calculates the value of the specified probability function at the specified point.
* </p>
*/
static
public double probability(ContinuousDistribution distribution, Number x){
if(distribution instanceof GaussianDistribution){
return probability((GaussianDistribution)distribution, x);
} else
if(distribution instanceof PoissonDistribution){
return probability((PoissonDistribution)distribution, x);
}
throw new UnsupportedElementException(distribution);
}
static
public double probability(GaussianDistribution gaussianDistribution, Number x){
NormalDistribution distribution = new NormalDistribution(gaussianDistribution.getMean(), Math.sqrt(gaussianDistribution.getVariance()));
return distribution.density(x.doubleValue());
}
static
public double probability(PoissonDistribution poissonDistribution, Number x){
org.apache.commons.math3.distribution.PoissonDistribution distribution = new org.apache.commons.math3.distribution.PoissonDistribution(null, poissonDistribution.getMean(), org.apache.commons.math3.distribution.PoissonDistribution.DEFAULT_EPSILON, org.apache.commons.math3.distribution.PoissonDistribution.DEFAULT_MAX_ITERATIONS);
x = (Number)TypeUtil.cast(DataType.INTEGER, x);
return distribution.probability(x.intValue());
}
static
public boolean isNoOp(ContinuousDistribution distribution){
if(distribution instanceof GaussianDistribution){
return isNoOp((GaussianDistribution)distribution);
}
return true;
}
static
public boolean isNoOp(GaussianDistribution gaussianDistribution){
return (gaussianDistribution.getVariance() <= 0d);
}
}