#'SMOPlogLikelihood
#' 
#'@author Paola Marson, \email{paola.marson@meteo.fr}
#'
#'@description #takes as input the previous values of tau, beta in the chain MCMC (typically from their posteriors) and the gaussian field x generated from the Gibbs sampling. It gives the value of the loglikelihood.
#'
#'@param param array of length 3 containing 1) numeric previous value of tau; 2) numeric previous value of beta_1, the coefficient associated to the small scale orographic condensation; 3) numeric previous value of beta_2, the coefficient associated to the large-scale non orographic precipitation; 
#'       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
#'       eigenv array numeric of length side containing the eigenvalue of matrix W 
#'       sigmasq numeric value of the variance 
#' 
#'@import Matrix
#'
#'@return numeric value of the log likelihood
#'
#'@example

SMOPlogLikelihood<-function(param, x, eigenv, sigmasq){ #takes as input the previous value of tau FROM ITS POSTERIOR in the chain and the gaussian field x from the Gibbs sampling:
  require(Matrix)
  side<-sqrt(nrow(W))	
  object<-Matrix(In - param[1]*(W), doDiag=TRUE,sparse=TRUE)
  el<-as.matrix(object)%*%x-X%*%param[2:3]
  log_likelihood<-(- (-side^2/2*log(2*pi*sigmasq)+sum(log(1-param[1]*eigenv))-1/(2*sigmasq)*t(el)%*%el) )
  
  return(as.numeric(log_likelihood))
}