R/EstHMMGen.R

Defines functions EMStep EstHMMGen

Documented in EstHMMGen

#'#############################################################################
#'@title Estimation of univariate hidden Markov model
#'
#'@description This function estimates the parameters from a univariate hidden Markov model
#'
#'@param y  observations; (n x 1)
#'@param reg    number of regimes
#'@param family  distribution name; run the function distributions() for help
#'@param start  starting parameters for the estimation; (1 x p)
#'@param max_iter  maximum number of iterations of the EM algorithm; suggestion 10000
#'@param eps precision (stopping criteria); suggestion 0.001.
#'@param graph 1 for a graph, 0 otherwise (default); only for continuous distributions
#'@param size additional parameter for some discrete distributions; run the command distributions() for help
#'@param theta0 initial parameters for each regimes; (r x p)
#'
#'@return \item{theta}{ estimated parameters; (r x p)}
#'@return \item{Q}{estimated transition matrix; (r x r)}
#'@return \item{eta}{conditional probabilities of being in regime k at time t given observations up to time t; (n x r) }
#'@return \item{lambda}{conditional probabilities of being in regime k at time t given all observations; (n x r)}
#'@return \item{U}{matrix of Rosenblatt transforms; (n x r)}
#'@return \item{cvm}{cramer-von-Mises statistic for goodness-of-fit}
#'@return \item{W}{pseudo-observations that should be uniformly distributed under the null hypothesis}
#'@return \item{LL}{log-likelihood}
#'@return \item{nu}{stationary distribution}
#'@return \item{AIC}{Akaike information criterion}
#'@return \item{BIC}{Bayesian information criterion}
#'@return \item{CAIC}{consistent Akaike information criterion}
#'@return \item{AICcorrected}{Akaike information criterion corrected}
#'@return \item{HQC}{Hannan-Quinn information criterion}
#'@return \item{stats}{empirical means and standard deviation of each regimes using lambda}
#'@return \item{pred_l}{estimated regime using lambda}
#'@return \item{pred_e}{estimated regime using eta}
#'@return \item{runs_l}{estimated number of runs using lambda}
#'@return \item{runs_e}{estimated number of runs using eta}
#'
#'@export
#'
#'@examples
#'family = "gaussian"
#'Q = matrix(c(0.8, 0.3, 0.2, 0.7), 2, 2) ; theta = matrix(c(-1.5, 1.7, 1, 1),2,2) ;
#'sim = SimHMMGen(Q, family, theta, 10)$SimData ;
#'est = EstHMMGen(y=sim, reg=2, family=family)
#'
#'
#'
#'
#'
#'
EstHMMGen<-function(y, reg, family, start=0, max_iter=10000, eps=10e-4, graph=0, size=0, theta0=0){
  ninit=100   #minimum number of iterations
  n = length(y)

  n0 = floor((n/reg))
  ind0   = 1:n0
  if(is.null(dim(theta0))){
    p=0
  }  else {
    p = dim(theta0)[2]
  }



  if (theta0==0 || p==1){

    switch(family,

           "asymexppower" = {    ## [R+, R+, 01]
             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(1, 5, 1), p3=c(0.1, 0.9, 0.1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)
             } else {
               theta0 = start
             }

             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "asymlaplace" = {    ## [R, R+, R+]
             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1, 1, 0.25), p2=c(1, 10, 1), p3=c(1, 1, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)
             } else {
               theta0=start
             }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "asympower" = {    ## [01, R+, R+]
             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.1, 0.9, 0.1), p2=c(1, 5, 1), p3=c(0.5, 5, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)
             }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "asymt" = {    ## [R+, R+, 01, R]
             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(3, 5, 1), p3=c(0.1, 0.9, 0.1), p4=c(-2,2,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "beard" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "benini" = {     ## [R, R+]
             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (mean(y)>0){
               p1=c(min(y)-0.05, min(y)+0.05, 0.05)
             } else{
               p1=c(max(y)-0.05, max(y)+0.05, 0.05)
             }
             if (length(start) != p){
               params = list(p1=p1, p2=c(0.25, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "bernoulli" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1/0.9), log(0.9/0.1)))$minimum
             }
           } ,



           "beta" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 3, 1), p2=c(1, 3, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betabinomial" = {     ## [N+, R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(size, size, 1), p2=c(1, 5, 1), p3=c(1,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa, size)  ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0[2:3]), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betageometric" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(1, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa)  ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "betanegativebinomial" = {     ## [N+, R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(size, size, 1), p2=c(1, 5, 1), p3=c(1,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa, size)  ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0[2:3]), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "betaburr" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(0.5,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "betaburr7" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(0.5,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betaexponential" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betafrechet" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(0.5,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betagompertz" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 4, 0.5), p2=c(1, 4, 1), p3=c(1,4,1), p4=c(1,4,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "betagumbel" = {     ## [R+, R+, R, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(-2,2,1), p4=c(0.5,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betagumbel2" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 0.5, 1), p3=c(0.5,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "betalognormal" = {     ## [R+, R+, R, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(-1,1,1), p4=c(1,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "betalomax" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(0.5,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,




           "betanormal" = {     ## [R+, R+, R, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(0,0,1), p4=c(1,3,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betaprime" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,3,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "betaweibull" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 0.5), p2=c(1, 5, 1), p3=c(1,5,1), p4=c(1,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "bhattacharjee" = {     ## [R, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 1), p2=c(0.5,10, 1), p3=c(0.5, 5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "birnbaumsaunders" = {     ## [R+, R+, R]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 10, 1), p2=c(0.5,5, 1), p3=c(0, 0, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "binomial" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa, size) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1/0.9), log(0.9/0.1)))$minimum
             }
           } ,


           "boxcox" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5,5, 1), p3=c(0.5, 5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "burr" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 10, 1), p2=c(0.5,5, 1), p3=c(1, 3, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "burr2param" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5,5, 1), p3=c(1, 1, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "cauchy" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 5, 1), p2=c(0.5, 10, 1), p3=c(1, 1, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "chen" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "chi" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(1, 5, 1), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1,1,1),
                           p5=c(1,1,1), p6=c(1,1,1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "chisquared" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(1, 5, 1), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1,1,1),
                           p5=c(1,1,1), p6=c(1,1,1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "clg" = {     ## [R+, R+, R]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5,5, 1), p3=c(-1, 1, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "complementarybeta" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5,5, 1), p3=c(0, 0, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "dagum" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 1, 1), p2=c(0.5,5, 1), p3=c(0.5, 5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "diffzeta" = {     ## [R+, >1]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(1.1,5, 1), p3=c(0.5, 0.5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "discretegamma" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(1,5, 1), p3=c(0.5, 0.5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "discretelaplace" = {     ## [R, 01]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1,1, 1), p2=c(0.1,0.9,0.1), p3=c(0.5, 0.5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "discretenormal" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1,1, 1), p2=c(1,5,1), p3=c(0.5, 0.5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "discreteweibull" = {     ## [01, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.1,0.9, 1), p2=c(1,5,1), p3=c(0.5, 0.5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "doubleweibull" = {     ## [R+, R, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(-1,1, 1), p3=c(0.5, 5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "exponential" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(1, 10, 1), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1,1,1),
                           p5=c(1,1,1), p6=c(1,1,1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "exponentialextension" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "exponentialgeometric" = {     ## [R+, 01]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.1, 0.9, 0.2), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "exponentiallogarithmic" = {     ## [R+, 01]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(0.1, 0.9, 0.2), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "exponentialpoisson" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "exponentialpower" = {     ## [R, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 1), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "ev" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 1), p2=c(0.5, 10, 0.5), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "F" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "fellerpareto" = {     ## [R(mini), R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(min(y)-0.5, min(y)-0.5, 0.5),
                           p2=c(0.5, 5, 1), p3=c(0.5, 5, 1), p4=c(0.5, 5, 1), p5=c(0.5, 5, 1),
                           p6=c(1, 1, 1))
             theta000 = GridSearchS0(family, y, params)
             theta0[1,1:4] = theta000[2:5]
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0[1,]), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "fisk" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5, 5, 0.5), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "foldednormal" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 0.5), p2=c(0.5, 10, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "frechet" = {     ## [R+, R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(0.5, 5, 1), p2=c(min(y)-0.5, min(y)-0.5, 5), p3=c(0.5, 5, 2),
                           p4=c(1, 1, 1), p5=c(1, 1, 1), p6=c(1, 1, 1))
             theta000 = GridSearchS0(family, y, params)
             theta0[1,] = c(theta000[1], theta000[3])
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0[1,]), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "hzeta" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(1, 10))$minimum
             }
           } ,


           "gamma" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "gammapoisson" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(1,5, 1), p3=c(0.5, 0.5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "gaussian" = {     ## [R, R+]
             p = 2 ;
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             for (j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               theta0[j,] = c(mean(x), stats::sd(x))
             }
             alpha0[,1]= theta0[,1]
             alpha0[,2]= log(theta0[,2])
           } ,


           "gev" = {     ## [R, R+, R]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               theta0[j,] = EnvStats::egevd(as.numeric(y))$parameters} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
               alpha0[j,] = theta2alpha(family, theta0[j,])
             }
           } ,


           "geninvbeta" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(0.5, 5, 0.5), p3=c(1, 5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "genlogis" = {     ## [R+, R, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(-1, 1, 1), p3=c(1, 5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "genlogis3" = {     ## [R+, R, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(-1, 1, 1), p3=c(1, 5, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "genlogis4" = {     ## [R+, R+, R, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(1, 5, 1), p3=c(-1, 1, 1), p4=c(1,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "genpowerweibull" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(1, 5, 1), p2=c(1, 5, 1), p3=c(1, 1, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "geometric" = {     ## [01]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1/0.9), log(0.9/0.1)))$minimum
             }
           } ,



           "generalizedhyperbolic" = {     ## [R, R+, R+, R, R]   [mu, delta, alpha, beta, lambda] (avec alpha^2 > beta^2)

             p = 5
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-2, 2, 1), p2=c(0.5, 5, 0.5), p3=c(1, 5, 1), p4=c(0.1, 0.9, 0.1),
                             p5=c(1, 5, 1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "generalizedlambda" = {     ## [R, R+, R, R]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-2, 2, 1), p2=c(0.5, 5, 0.5), p3=c(0, 3, 1), p4=c(0, 3, 1),
                             p5=c(1, 1, 1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "generalizedt" = {     ## [R, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-2, 2, 1), p2=c(0.5, 5, 0.5), p3=c(1, 5, 1), p4=c(1, 5, 1),
                             p5=c(1, 1, 1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "gompertz" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "gpd" = {     ## [R+(min(y)), R+, R]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(min(y), min(y), 1), p2=c(0.5, 5, 0.5), p3=c(0, 0, 1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "gumbel" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 0.5), p2=c(0.5, 10, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "gumbel2" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "halfcauchy" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(0.5, 10, 0.5), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                           p5=c(1, 1, 1), p6=c(1, 1, 1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "halflogistic" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(0.5, 10, 0.5), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                           p5=c(1, 1, 1), p6=c(1, 1, 1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,



           "halfnormal" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(0.5, 10, 0.5), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                           p5=c(1, 1, 1), p6=c(1, 1, 1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "halft" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "hjorth" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(0.5,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "hblaplace" = {     ## [01, R, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.1, 0.9, 0.1), p2=c(-2, 2, 1), p3=c(0.5,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "huber" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1, 1, 1), p2=c(0.5, 10, 0.5), p3=c(1.345, 1.345, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "hyperbolic" = {     ## [R, R+, R+, R]   [mu, delta, alpha, beta] (avec alpha^2 > beta^2)

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1, 1, 1), p2=c(0.5, 5, 0.5), p3=c(1, 2, 1), p4=c(0.1, 0.9, 0.1),
                             p5=c(1, 1, 1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inversebeta" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5, 5, 0.5), p3=c(1, 1, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inverseburr" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5, 5, 0.5), p3=c(1, 1, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inversechisquared" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(0.5, 10, 0.5), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                           p5=c(1, 1, 1), p6=c(1, 1, 1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "inverseexponential" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(0.5, 10, 0.5), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                           p5=c(1, 1, 1), p6=c(1, 1, 1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "inverseexpexponential" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inversegamma" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inverselomax" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inverseparalogistic" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inversepareto" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inversetransformedgamma" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 1), p2=c(0.5, 10, 0.5), p3=c(1, 1, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "inverseweibull" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamy" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamyexponential" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamygamma" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,5,1), p4=c(1,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamygumbel" = {     ## [R+, R+, R, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(-1,1,1), p4=c(1,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamyhalfnormal" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamyloglogistic" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,5,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamynormal" = {     ## [R, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1, 1, 0.5), p2=c(0.5, 5, 1), p3=c(1,5,1), p4=c(1,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "kumaraswamyweibull" = {     ## [R+, R+, R+, R+]

             p = 4
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,5,1), p4=c(1,5,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "laplace" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1, 1, 0.5), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "levy" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1, 1, 0.5), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "linearfailurerate" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "lindley" = {     ## [R+]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             params = list(p1=c(1, 10, 0.5), p2=c(1, 1, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                           p5=c(1, 1, 1), p6=c(1, 1, 1))
             theta0 = GridSearchS0(family, y, params)
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1), log(100)))$minimum
             }
           } ,


           "libbynovickbeta" = {     ## [R+, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1, 5, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "logcauchy" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-1, 1, 1), p2=c(0.5, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "loggamma" = {     ## [R, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 1), p2=c(0.5, 10, 0.5), p3=c(1, 1, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "loggumbel" = {     ## [R, R+,]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 0.5), p2=c(1, 10, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "loglaplace" = {     ## [R, R+, R+]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 1), p2=c(0.5, 10, 0.5), p3=c(1, 5, 0.5), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "loglog" = {     ## [R+, >1]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(1.5, 10, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "loglogistic" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "lognormal" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 0.5), p2=c(1, 10, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "lognormal3" = {     ## [R, R+, R]

             p = 3
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(-2, 2, 0.5), p2=c(1, 10, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "logistic" = {     ## [R, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0, 0, 0.5), p2=c(1, 10, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "logisticexponential" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(1, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,



           "logisticrayleigh" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(1, 5, 1), p3=c(1, 1, 1), p4=c(1, 1, 1),
                             p5=c(1, 1, 1), p6=c(1, 1, 1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optim(par= theta2alpha(family,theta0), funParam0,
                                          method = "Nelder-Mead")$par
             }
           } ,


           "logseries" = {     ## [01]

             p = 1
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             for( j in 1:reg){
               ind = (j-1)*n0+ind0
               x = y[ind]
               funParam0 <- function(thetaa){
                 log_likelihood = (-sum(log(PDF_unc(family, x, thetaa) ) ) )
                 return(log_likelihood)
               }
               alpha0[j,] =  stats::optimize(funParam0, c(log(0.1/0.9), log(0.9/0.1)))$minimum
             }
           } ,


           "lomax" = {     ## [R+, R+]

             p = 2
             theta0 = matrix(0,reg,p)
             alpha0 = theta0
             if (length(start) != p){
               params = list(p1=c(0.5, 5, 0.5), p2=c(0.5, 5, 1), p3=c(1,1,1), p4=c(1,1,1),
                             p5=c(1,1,1), p6=c(1,1,1))
               theta0 = GridSearchS0(family, y, params)} else{
                 theta0 = start
               }