25 bool ANBC_Model::train(UINT classLabel,MatrixDouble &trainingData,VectorDouble &weightsVector){
28 if( trainingData.getNumCols() != weightsVector.size() ){
33 UINT M = trainingData.getNumRows();
34 N = trainingData.getNumCols();
35 this->classLabel = classLabel;
38 weights = weightsVector;
45 for(UINT j=0; j<N; j++){
48 for(UINT i=0; i<M; i++){
49 mu[j] += trainingData[i][j];
60 for(UINT j=0; j<N; j++){
63 for(UINT i=0; i<M; i++){
64 sigma[j] += SQR( trainingData[i][j]-mu[j] );
67 sigma[j] = sqrt( sigma[j]/
double(M-1) );
75 double meanPrediction = 0.0;
76 VectorDouble predictions(M);
77 for(UINT i=0; i<M; i++){
79 vector<double> testData(N);
80 for(UINT j=0; j<N; j++) {
81 testData[j] = trainingData[i][j];
84 predictions[i] = predict(testData);
85 meanPrediction += predictions[i];
89 meanPrediction /= double(M);
93 for(UINT i=0; i<M; i++) {
94 stdDev += SQR( predictions[i]-meanPrediction );
96 stdDev = sqrt( stdDev / (
double(M)-1.0) );
98 threshold = meanPrediction-(stdDev*gamma);
101 trainingMu = meanPrediction;
102 trainingSigma = stdDev;
107 double ANBC_Model::predict(
const VectorDouble &x){
108 double prediction = 0.0;
109 for(UINT j=0; j<N; j++){
111 prediction += log(gauss(x[j],mu[j],sigma[j]) * weights[j]);
116 double ANBC_Model::predictUnnormed(
const VectorDouble &x){
117 double prediction = 0.0;
118 for(UINT j=0; j<N; j++){
120 prediction += log(unnormedGauss(x[j],mu[j],sigma[j]) * weights[j]);
125 inline double ANBC_Model::gauss(
const double x,
const double mu,
const double sigma){
126 return ( 1.0/(sigma*sqrt(TWO_PI)) ) * exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
129 inline double ANBC_Model::unnormedGauss(
const double x,
const double mu,
const double sigma){
130 return exp( - ( ((x-mu)*(x-mu))/(2*(sigma*sigma)) ) );
133 void ANBC_Model::recomputeThresholdValue(
const double gamma){
135 threshold = trainingMu-(trainingSigma*gamma);
This class implements a container for an ANBC model.