This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Calculate the probability that the given input is in the positive class | |
public double probability(double in[]){ | |
double relativepositive=0,relativenegative=0; | |
for(int j=0; j<in.length; j++){ | |
relativepositive += (in[j]-posmean[j])*(in[j]-posmean[j]) / posvariance[j] ; | |
relativenegative += (in[j]-negmean[j])*(in[j]-negmean[j]) / negvariance[j] ; | |
} | |
relativepositive = positives*Math.exp(0.5*relativepositive) ; | |
relativenegative = negatives*Math.exp(0.5*relativenegative) ; | |
return relativepositive / (relativepositive + relativenegative) ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double ProbabilityOfInputIfPositive(double in[]){ | |
double prob = 1/Math.sqrt(2 * Math.PI) ; | |
for(int j=0; j<in.length;j++){ | |
prob*= Math.exp(- (in[j]-posmean[j])*(in[j]-posmean[j]) / (2*posvariance[j]) ) ; | |
} | |
return prob ; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//constructs a naive Bayes binary classifier | |
public NaiveBayes(double in[][], boolean out[]){ | |
int inputs = in[0].length ; | |
//initialize sums and sums of squares for each class | |
double[] poss = new double[inputs], poss2 = new double[inputs]; | |
double[] negs = new double[inputs], negs2 = new double[inputs]; | |
//calculate amount of each class, sums, and sums of squares | |
for(int k=0;k<in.length;k++){//for each data point | |
if(out[k]){ | |
positives++;//keep track of total positives |
NewerOlder