-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalEstimator.java
More file actions
82 lines (77 loc) · 2.15 KB
/
Copy pathNormalEstimator.java
File metadata and controls
82 lines (77 loc) · 2.15 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
82
import java.lang.Math;
public class NormalEstimator extends Estimator{
double mean[],variance[];
int count[];
DataSet dataset;
int index;
int numofclasses;
double temp[], temp2[],tem[] ;
int classIndex;
Double weight[];
NormalEstimator(DataSet dataset, int index, int classIndex, Double w[]) throws Exception
{
this.weight = w;
this.classIndex = classIndex;
//System.out.println("build normal");
this.dataset = dataset;
this.index = index;
numofclasses = dataset.attributes.get(classIndex).getSize();
//System.out.println(numofclasses);
mean = new double[numofclasses];
variance = new double[numofclasses];
count = new int[numofclasses];
//mean
double temp[] = new double[numofclasses];
double temp2[] = new double[numofclasses];
double tem[] = new double[numofclasses];
for(int i=0;i<dataset.examples.size();i++)
{
//for each example
for(int j=0;j<numofclasses;j++)
if(dataset.examples.get(i).get(classIndex).intValue()==j)
{
temp[j] += dataset.examples.get(i).get(index)*weight[i]*dataset.examples.size();
count[j]++;
}
}
for(int i=0;i<numofclasses;i++)
{
mean[i] = temp[i]/count[i];
}
//variance
for(int i=0;i<dataset.examples.size();i++)
{
for(int j=0;j<numofclasses;j++)
if(dataset.examples.get(i).get(classIndex).intValue()==j)
{
tem[j]=dataset.examples.get(i).get(index)*weight[i]*dataset.examples.size()-mean[j];
tem[j]=tem[j]*tem[j];
temp2[j]+=tem[j];
}
}
for(int i=0;i<numofclasses;i++)
variance[i]=temp2[i]/count[i];
}
void addValue(int k, int index)
{
//update statistics
mean[index] = (mean[index]*count[index]+k)/(count[index]+1);
tem[index]=0;
temp[index]=0;
for(int i=0;i<dataset.examples.size();i++)
{
tem[index]=dataset.examples.get(i).get(index)-mean[index];
tem[index]=tem[index]*tem[index];
temp[index]+=tem[index];
}
variance[index]=temp[index]/count[index];
}
Double getProbability(double k,int index)
{
double output=0,expo=0,xishu=0;
xishu=1/(Math.sqrt(variance[index]*2*Math.PI));
expo=-(Math.pow(k-mean[index], 2))/(2*variance[index]);
output=xishu*Math.pow(Math.E, expo);
return output;
}
}