# Define a Husler-Reiss copula with some asymetry formed by
# simple compositing, and then draw the parent density contours.
para <- list(cop1=HRcop, para1=7, alpha=.4, beta=.16)
pdf("my_lcomoment_test.pdf")
densityCOPplot(cop=composite1COP, para=para, contour.col=8)

# Using 10 testing loops with a color changing for later
# plotting, sample n=820 from the parent, then compute
# Weibull plotting position estimates of the probabilities.
for(i in 1:10) {
  UV <- rCOP(820, cop=composite1COP, para=para)
  UV$U <- lmomco::pp(UV$U, sort=FALSE) # Weibull plotting
  UV$V <- lmomco::pp(UV$V, sort=FALSE) # positions
  # The above pretends that we don't actually know the UVs.
  
  rho <- cor(UV$U, UV$V, method="spearman") # Spearman Rho
  maxtheta <- uniroot(function(r) {
                    rhoCOP(cop=HRcop, para=r) - rho },
                      interval=c(0.5,15))$root
  maxtheta <- 10*maxtheta # The 10 is just a hack and likely
  # could be much lower for practical application.
  # The maxtheta is a helper for a 3D optimization to come for
  # which we do not want pathologically large theta for the 
  # HRcop to form if the optimizing wanders off.
  
  LC <- lmomco::lcomoms2(UV, nmom=4) # Sample L-comoments
  t3.12 <- LC$T3[1,2]; t4.12 <- LC$T4[1,2] # extract the 
  # L-coskew and L-cokurtosis for the U with respect to V.
  # A choice of [2,1] could be made. It is not clear whether
  # in practice that the two L-coskews should be used with
  # abandonment of the L-cokurtosis for parameter estimation.

  "objfunc" <- # create an objective function on sum of squares
     function(par, rho=NA, t3.12=NULL, t4.12=NULL,
                   maxpar1=Inf) {
      if(exp(par[1]) >= maxpar1) return(Inf)
      para <- list(cop1=HRcop, para1=exp(par[1]),
                   alpha=pnorm(par[2]), beta=pnorm(par[3]))
      my.rho <- rhoCOP(cop=composite1COP, para=para)
      my.a <- copBasic:::lcomCOP(cop=composite1COP, para=para)$lcomomUV[3]
      my.b <- copBasic:::lcomCOP(cop=composite1COP, para=para)$lcomomUV[4]
      return((my.rho-rho)^2 + (my.a-t3.12)^2 + (my.b-t4.12)^2)
   }

   par <- optim(c(log(3), 0,0), fn=objfunc,
                rho=rho, t3.12=t3.12, t4.12=t4.12,
                maxpar1=maxtheta)$par
   par[1] <-   exp(par[1]); par[2] <- pnorm(par[2])
                            par[3] <- pnorm(par[3]) 
   fit <- list(cop1=HRcop, para1=par[1], alpha=par[2], beta=par[3])

   densityCOPplot(cop=composite1COP, para=fit, contour.lwd=0.5+2*i/10,
                  contour.col=rgb(i/10,1-i/10,i/10,.2), ploton=FALSE)
   #lcomCOP(cop=composite1COP, para=para, orders=2:4)$lcomomUV
   #lcomCOP(cop=composite1COP, para=fit,  orders=2:4)$lcomomUV
   #print(c(rho, t3.12, t4.12))
   
   # It is useful to watch the maxtheta in comparison to the the
   # theta determined in the 3D optimization. A fitted theta nearly
   # aligning with the maximum, might indicate a solution that should
   # otherwise be rejected. Testing shows that about 70 percent of the
   # solutions kick out to the lower right corner as the parent does.
   print(c(i, maxtheta, fit$para1, fit$alpha, fit$beta))
}
dev.off()

