## Copyright (C) 2012 Marius Hofert, Ivan Kojadinovic, Martin Maechler, and Jun Yan
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.


useDynLib(copula, .registration=TRUE)


## import all we need, but not more..

importFrom("stats", D, approxfun, cor # 'coef': from "bblme"; 'logLik': from "stats4"
	 , deriv, dnorm, dpois, dt, dbeta, ecdf, integrate, .lm.fit, median, model.matrix
         , na.omit, nobs
         , optimize, optim, p.adjust.methods, p.adjust
         , pchisq, pgamma, pnorm, ppois, ppoints, printCoefmat, pt
         , qchisq, qgamma, qlogis, qnorm, qqline, qt, quantile
         , rbeta, rbinom, rcauchy, rexp, rgamma, rmultinom, rnorm, runif
         , setNames, splinefun, uniroot
         , var, vcov
           )

importFrom("utils", head, tail, capture.output, modifyList,
	   str, setTxtProgressBar, txtProgressBar)

importFrom("grDevices", col2rgb, dev.flush, dev.hold, dev.off, pdf, extendrange,
           gray, adjustcolor)
importFrom("graphics", Axis, abline, axis, box, contour, curve
         , layout.show, layout, lcm, lines, mtext
         , par, persp, pairs
         , plot.new, plot.window, points
         , rect, rug, strwidth, text
           )

importMethodsFrom("methods", ## those for which we export new methods:
		  coerce, show)
importFrom("methods", as, is, extends, new
         , callGeneric, existsFunction, hasArg, hasMethod, callNextMethod
         , getClass, getClassDef
         , selectMethod
         , setClass, setClassUnion, setMethod, setOldClass
         , setValidity, validObject, slot, "slot<-", slotNames, .hasSlot
         , signature, representation, prototype)

importFrom("stats4", mle)
importMethodsFrom("stats4",
		  coef, confint, logLik, AIC, BIC,
		  plot, profile, summary)

importFrom("lattice", trellis.par.get, trellis.par.set,
	   splom, wireframe, contourplot, cloud,
           standard.theme, panel.fill, panel.splom, diag.panel.splom,
           panel.3dwire, ltransform3dto3d, lsegments)

importFrom("Matrix", nearPD, invPerm)
## NB: "Dangerous" to not also depend on Matrix; as 'methods' for Matrixclasses are not seen.

importFrom("gsl", debye_1, debye_2, dilog, complex_dilog)

importFrom("ADGofTest", ad.test)# really would want the ad.test.statistic() but that's hidden

importFrom("stabledist", dstable)

## importFrom("bbmle", mle2)


if(FALSE) {## currently do not want to "depend" on Rmpfr's correct installation:
## As we use it only in "numerically hard cases" polyG(), dsumSibuya(),..:
importFrom("Rmpfr",
	   mpfr, chooseMpfr, chooseMpfr.all,
	   getPrec, roundMpfr, sumBinomMpfr)
## currently needed when Rmpfr is detached, as it influences "numeric" (e.g. in examples testing):
importClassesFrom("Rmpfr",
                  mpfr, atomicVector, array_or_vector, Mnumber)
}

importFrom("mvtnorm",
           dmvnorm, pmvnorm, rmvnorm,
           GenzBretz, Miwa, TVPACK, ## <-- import the algorithm functions, too
           dmvt, pmvt, rmvt)

importFrom("pcaPP", cor.fk)# Fast Kendall's tau


## FIXME: we would only want importS3MethodsFrom("pspline", predict)
import("pspline")
## S3method(predict, smooth.Pspline)

## importFrom(sn, dmst)
## importFrom(sn, qsn, dsn, psn, dmsn, pmsn)

importFrom("numDeriv", grad, jacobian)


### What we provide --- Exports --- ##########################################
### -------------------------------

exportMethods(## for "other" (i.e. non copula-package !) generics:
    ## "base"
    "%in%", dim, format,
    ## "methods":
    show,
    ## from "graphics"
    persp, contour, plot,
    ## imported from "stats4" / "bbmle":
    coef, confint, logLik, #-> hence AIC works [BIC: needs 'nobs' fix in bbmle]
    profile, summary
)

## Part I: Mainly former copula ----------------------------------------

exportClasses(mvdc)
# note: have virtual classes we do *not* export: e.g, dimCopula , Xcopula
exportClasses(Copula, parCopula, xcopula, copula, # virtual classes
	      ellipCopula, archmCopula, evCopula, fhCopula)
exportClasses(normalCopula, tCopula)
exportClasses(claytonCopula, frankCopula, gumbelCopula, amhCopula, joeCopula)
exportClasses(galambosCopula, huslerReissCopula, tawnCopula, tevCopula)
exportClasses(fgmCopula, plackettCopula)
exportClasses(moCopula)
exportClasses(indepCopula)
exportClasses(lowfhCopula, upfhCopula)
exportClasses(empCopula)
# TODO
# exportClasses(parClist, mixWeights) # <--> R/mixCopula.R

## Export Functions  (if they are generic, *also* exports the Methods):

export(mvdc)
export(ellipCopula, archmCopula, evCopula, fhCopula)
export(normalCopula, tCopula)
export(claytonCopula, frankCopula, gumbelCopula, amhCopula, joeCopula)
export(galambosCopula, huslerReissCopula, tawnCopula, tevCopula)
export(fgmCopula, plackettCopula)
export(moCopula)
export(indepCopula)
export(lowfhCopula, upfhCopula)
export(empCopula)
#
export(getIniParam)

export(dCopula, pCopula, rCopula)
export(cCopula)
export(dMvdc, pMvdc, rMvdc)
export(tau, rho, lambda)
export(iTau, iRho)

export(corKendall)

## archmCopula methods
export(psi, iPsi, diPsi)

## evCopula methods
export(A, dAdu, An.biv, An)

## generic utilities:
export(describeCop) # generic & methods
export(margCopula)  # marginal copula for specified margins to keep

## "transformed" copula classes
export(rotCopula)
exportClasses(rotCopula)

export(khoudrajiCopula)
exportClasses(asymCopula, asym2Copula,
              khoudrajiCopula, khoudrajiBivCopula, khoudrajiExplicitCopula)

export(mixCopula)
exportClasses(mixCopula) # , parClist , mixWeights  (TODO, notably 'parClist' ??)

## auxiliary functions mainly used for working with elliptical copulas:
export(p2P, P2p, getSigma, extremePairs)
export(dispstrToep)

## empirical copula
export(F.n, dCn, C.n, toEmpMargins) # .Fn

## fitting copula
export(loglikCopula, loglikMvdc)
export(loglikCopulaMany)
export(fitCopula, fitMvdc, optimMeth)
exportClasses(fittedMV, fitCopula, fitMvdc)

## some parameters can be fixed ( ./R/fixedPar.R , ./man/fixedPar.Rd ) :
export(fixParam, "fixedParam<-")
export(isFree, isFreeP, nParam)# (utilities)

## estimation of tail dependence
export(fitLambda)

## independence tests
export(indepTestSim, indepTest, multIndepTest,
       serialIndepTestSim, serialIndepTest, multSerialIndepTest,
       dependogram)

## gof tests
export(gofCopula, gofMB, gofPB, gofEVCopula)

## cross-validation
export(xvCopula)

## Graphics -- Copula-enhanced Lattice and traditional graphics:
export(cloud2, contourplot2, pairs2, qqplot2, splom2, wireframe2)

## ggraph utils
export(pairwiseCcop, pairwiseIndepTest, pviTest, gpviTest,
       RSpobs, gofBTstat) # R/ggraph-tools.R
export(pairsColList, pairsRosenblatt, .pairsCond) # R/pairsRosenblatt.R

## other nonparametric tests
export(evTestC, evTestA, evTestK)
export(exchEVTest, exchTest)
export(radSymTest)

## variance reduction
export(rAntitheticVariates)
export(rLatinHypercube)

## For some, have S3 print() and S4 show() .. MM is not convinced
S3method(print, parCopula)
S3method(print, tCopula)
S3method(print, normalCopula)
S3method(print, nacopula, printNacopula)
S3method(print, fitCopula)
##
S3method(print, mvdc)
S3method(print, indepTest)

S3method(summary, fitCopula)
S3method(print, fitMvdc)
S3method(summary, fitMvdc)
S3method(print, summary.fitCopula, printSummary.fittedMV)
S3method(print, summary.fitMvdc,   printSummary.fittedMV)
##
S3method(coef, fittedMV)
S3method(nobs, fittedMV)
S3method(vcov, fittedMV)
S3method(logLik, fittedMV)

##_TESTING_
## export(dCdtheta, dcdtheta, dTau, dRho)
## export(dCdu)
## export(ChiPlot, KPlot)
##
## export(fitCopula.repar)
## export(multcomp) ---> R/mult.R


## Part II: Mainly former nacopula -------------------------------------

exportClasses("acopula",
              "nacopula", "outer_nacopula",
              "interval", "maybeInterval"
              )

## Generics, functions and acopula - family objects defined in this package
export("copAMH", "copClayton", "copFrank", "copGumbel", "copJoe"
       , .ac.shortNames, .ac.longNames, .ac.objNames, .ac.classNames
       ,"getAcop", "getAname"
       ,"dnacopula", "pnacopula", "rnacopula", "rnchild"
       ## marginal transformations
       ,"pobs"
       ## estimation
       ,"coeffG" ## , "polyG", "polyJ", "signFF"
       ,"initOpt"
       ,"beta.", "betan"
       ,"debye1", "debye2" # <- since we extend the gsl ones
       ,"ebeta", "etau", "emde", "dDiag", "edmle", "emle", "enacopula"
       ## goodness-of-fit
       ,"gofT2stat" ,"gofTstat", "htrafo"
       ## distribution of the radial part for Archimedean copulas
       ,"pacR", "qacR"
       ## Kendall distribution
       ,"Kn", "pK", "qK", "dK", "rK"
       ## misc
       ,"onacopula", "onacopulaL", "nac2list", "nesdepth", "rnacModel", "nacPairthetas"
       ,"polynEval"
       ,"printNacopula"
       ,"interval"
       ,"opower"
       ,"prob"
       ,"nacFrail.time"
       ## Should we provide these in a separate (very small) R package ?
       ,"sinc", "A..Z"
       ,"rstable1"
       ,"retstable", "retstableR"
       ,"dSibuya", "pSibuya", "dsumSibuya", "rSibuya", "rSibuyaR"
       ,"rlog", "rlogR", "rF01Frank", "rF01Joe", "rFFrank", "rFJoe"
       ,"log1mexp" ,"log1pexp"
       ,"polylog", "Bernoulli", "Bernoulli.all"
       ,"Eulerian", "Stirling1", "Stirling2"
       ,"Eulerian.all", "Stirling1.all", "Stirling2.all"
       ,"getTheta", "setTheta"
       ,"tauAMH", "tauJoe"
       ,"allComp" # <- maybe keep private ?
       ,"safeUroot"
       )




## Deprecated (old names) --- move to 'Defunct', i.e., replace .Deprecated(.) by .Defunct()
export(
    ## 2012-08-27 :
    cacopula
    ## 2013-01-08 :
  , Cn
    ## 2014-08-03 :
  , gnacopula
    ## 2016-04-22 :
  , gofMB, gofPB
    ## 2016-06-04 :
  , rtrafo
    ## in CRAN's copula 0.999-14 till mid-2016
  , tailIndex
  , asymCopula
  , asymExplicitCopula
)

## Defunct (from copula 0.999-15,  mid-2016):
export(
    K
  , absdPsiMC
  , beta.hat
  , calibKendallsTau
  , calibSpearmansRho
  , kendallsTau
  , spearmansRho
  , genInv
  , genFun, genFunDer1, genFunDer2
  , Afun
  , AfunDer
  , Anfun
  , dcopula, pcopula, rcopula
  , dmvdc, pmvdc, rmvdc
)
