knndsinit <- function(trainX, trainLabel){

	# for the parameter init of BeliefKNN and EKNN, obtain the parameter alpha and gamma

	# KNNDSINIT: [para_gamma,para_alpha] = knndsinit(trianX, trainLabel)
	#	
	# 	This function initialises parameter para_gamma and para_alpha of the BPA 
	#	for every class
	# 	para_gamma (q) is the inverse of the mean distance
	#	between two vectors of class q
	#   Input:
	# 	trainX: matrix (N,d) of the training set patterns
	# 	trainLabel: vector (N,1) of the training set labels
    #   Output:
	#	para_gamma: vector (M,1) of parameters of the BPA.
	#	 M is the number of classes.
	#	para_alpha (1,1): also a parameter of the BPA.
	#
	#

    nTrain = nrow(trainX)
    nd = ncol(trainX)
#     browser()
	M = max(trainLabel);
	Dm = rep(0, M)
	for (i in 1:M){
	  ii = which(trainLabel ==i);
	  Nii = length(ii);
	  D = matrix(0, Nii, Nii);
	  for(j in 1:Nii){
       D[j, ] = sqrt(rowSums(((matrix(1, Nii, 1) %*% trainX[ii[j], ]) - trainX[ii, ])^2));
	  }
	   Dm[i] = sum(D) / (Nii * Nii - Nii);
	}

	para_gamma = 1 / Dm;
# 	para_gamma = t(para_gamma);
	para_alpha = 0.95;
	return(list(para_alpha = para_alpha, para_gamma = para_gamma))
}
 
