#'SMOPposteriorTau
#' 
#'@author Paola Marson, \email{paola.marson@meteo.fr}
#'
#'@description takes as input the current values of Beta (typically from its posteriors) and the gaussian field x generated from the Gibbs sampling. It generates the current values of parameter tau. 
#'
#'@param tau   numeric previous value of tau; 
#'       x     array numeric of lenght mesh size containing the values of the latent gaussian field generated by the Gibbs Sampling;
#'       W     squared matrix of expected dimension side*side, where side is the number of spatial points in the side of the mesh, containing the spatial neighbours wights
#'       X    elements managed inside the wrap function - more accurate desc will follow
#'       sigmasq numeric value of the variance 
#' 
#'@import Matrix
#'
#'@return numeric the generated value of tau
#'
#'@example

SMOPposteriorTau<-function(tau, beta_current){
  side<-sqrt(nrow(W))
  object<-Matrix(In - tau*(W), doDiag=TRUE,sparse=TRUE)
  
  el<-as.matrix(object)%*%x-X%*%beta_current
  posterior<-( (-side^2/2*log(2*pi*sigmasq)+sum(log(1-param*eigenv))-1/(2*sigmasq)*t(el)%*%el) ) + log(1/(6000-400))  ####attenzione, numeri da togliere + eigenv non viene creato dentro la funzione!!!
  
  return(as.numeric(posterior))
  
}