bayes_classifier <- function (train,trainLabel,test,trueLabel){

# Bayes Classifier
   library(mvtnorm)
   source("./paramperf.r")
   
   
   
   nbLabel=max(trainLabel)
   nTrain = nrow(train)
   nTest = nrow(test)
   
   # learning step
   mat_moy=c()
   mat_cov <- array(0,dim=c(nbLabel,dim(train)[2],dim(train)[2]))
   for (i in 1:nbLabel){
     mat_moy <- rbind(mat_moy,colMeans(train[which(trainLabel==i),]))
     mat_cov[i,,]<- cov(train[which(trainLabel==i),])
   }
   apriori <- table(trainLabel)/length(trainLabel)
   
  
   # decision step
   foundLabel =c()
   matr_sort_classif=matrix(0,nbLabel,nTest)
   for (i in 1:nTest) {
     distri=c()
     for (j in 1:nbLabel){
      distri <- cbind(distri,dmvnorm(test[i,],mat_moy[j,],mat_cov[j,,])*apriori[j])
     }
     foundLabel <- c(foundLabel,which.max(distri)) 
     matr_sort_classif[,i]=c(distri/sum(distri))
   }
   restest=rbind(trueLabel ,foundLabel)
   
   res<-paramperf(restest)
   
   return (list(foundLabel=foundLabel,matr_conf=res[1] ,vect_prob_classif=res[2],vect_prob_erreur=res[3],matr_sort_classif=matr_sort_classif))  
   
 }
 