# MLE and Quantile Evaluation for a Clayton AR(1) Model with Student Marginals

#### Original demo from Roeger Koenker; by Marius Hofert, Ivan Kojadinovic, Martin Mächler, Jun Yan

#### 2015-10-26

    require(copula)
    set.seed(271)

First, let’s fix some parameters.

    mu <- 0
    sigma <- 1
    df <- 3
    alpha <- 10

For the marginals, we will use location scale transformed Student distributions.

    rtls <- function(n, df, mu, sigma) sigma * rt(n,df) + mu
    ptls <- function(x, df, mu, sigma) pt((x - mu)/sigma,df)
    qtls <- function(u, df, mu, sigma) sigma * qt(u,df) + mu
    dtls <- function(u, df, mu, sigma) dt((x - mu)/sigma,df)/sigma

## Generating the data

Let’s generate some data.

    rclayton <- function(n, alpha) {
        u <- runif(n+1) # innovations
        v <- u
        for(i in 2:(n+1))
                v[i] <- ((u[i]^(-alpha/(1+alpha)) -1)*v[i-1]^(-alpha) +1)^(-1/alpha)
        v[2:(n+1)]
    }
    n <- 200
    u <- rclayton(n, alpha = alpha)
    u <- qtls(u, df=df, mu=mu, sigma=sigma)
    y <- u[-n]
    x <- u[-1]

We now estimate the parameters under known marginals

    fitCopula(claytonCopula(dim=2),
              cbind(ptls(x,df,mu,sigma), ptls(y,df,mu,sigma)))

    ## fitCopula() estimation based on 'maximum pseudo-likelihood'
    ## and a sample of size 199.
    ##       Estimate Std. Error z value Pr(>|z|)
    ## param  11.0802     0.8533   12.98   <2e-16 ***
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ## The maximized loglikelihood is  226.5
    ## Optimization converged
    ## Number of loglikelihood evaluations:
    ## function gradient
    ##       17        6

## Estimation under unknown marginal parameters

    ## Identical margins
    M2tlsI <- mvdc(claytonCopula(dim=2), c("tls","tls"),
                   rep(list(list(df=NA, mu=NA, sigma=NA)), 2), marginsIdentical= TRUE)
    (fit.id.mar <- fitMvdc(cbind(x,y), M2tlsI, start=c(3,1,1, 10)))

    ## The Maximum Likelihood estimation is based on 199 observations.
    ## Identical margins:
    ##         Estimate Std. Error z value Pr(>|z|)
    ## m.df     3.90978    0.88657   4.410 1.03e-05 ***
    ## m.mu     0.40952    0.05586   7.331 2.29e-13 ***
    ## m.sigma  0.73199    0.05238  13.974  < 2e-16 ***
    ## Copula:
    ##       Estimate Std. Error z value Pr(>|z|)
    ## param   5.9379     0.5904   10.06   <2e-16 ***
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ## The maximized loglikelihood is -338.2248
    ## Optimization converged
    ## Number of loglikelihood evaluations:
    ## function gradient
    ##       46       15

    ## Not necessarily identical margins
    M2tls <- mvdc(claytonCopula(dim=2), c("tls","tls"),
                  rep(list(list(df=NA, mu=NA, sigma=NA)), 2))
    fitMvdc(cbind(x,y), M2tls, start=c(3,1,1, 3,1,1, 10))

    ## The Maximum Likelihood estimation is based on 199 observations.
    ## Margin 1 :
    ##          Estimate Std. Error
    ## m1.df      3.7851      1.033
    ## m1.mu      0.4150      0.057
    ## m1.sigma   0.7288      0.059
    ## Margin 2 :
    ##          Estimate Std. Error
    ## m2.df      4.0760      1.198
    ## m2.mu      0.4046      0.058
    ## m2.sigma   0.7358      0.060
    ## Copula:
    ##       Estimate Std. Error
    ## param    5.944      0.591
    ## The maximized loglikelihood is -338.0503
    ## Optimization converged
    ## Number of loglikelihood evaluations:
    ## function gradient
    ##       63       18

## Plot some true and estimated conditional quantile functions

    u.cond <- function(z, tau, df, mu, sigma, alpha)
        ((tau^(-alpha/(1+alpha)) -1) * ptls(z,df,mu,sigma)^(-alpha) + 1) ^ (-1/alpha)
    y.cond <- function(z, tau, df, mu, sigma, alpha) {
        u <- u.cond(z, tau, df, mu, sigma, alpha)
        qtls(u, df=df, mu=mu, sigma=sigma)
    }
    plot(x, y)
    title("True and estimated conditional quantile functions")
    mtext(quote("for" ~~  tau == (1:5)/6))
    z <- seq(min(y),max(y),len = 60)
    for(i in 1:5) {
        tau <- i/6
        lines(z, y.cond(z, tau, df,mu,sigma, alpha))
        ## and compare with estimate:
        b <- fit.id.mar@estimate
        lines(z, y.cond(z, tau, df=b[1], mu=b[2], sigma=b[3], alpha=b[4]),
              col="red")
    }