require(copula)
set.seed(271)
First, let’s fix some parameters.
0
mu <- 1
sigma <- 3
df <- 10 alpha <-
For the marginals, we will use location scale transformed Student distributions.
function(n, df, mu, sigma) sigma * rt(n,df) + mu
rtls <- function(x, df, mu, sigma) pt((x - mu)/sigma,df)
ptls <- function(u, df, mu, sigma) sigma * qt(u,df) + mu
qtls <- function(u, df, mu, sigma) dt((x - mu)/sigma,df)/sigma dtls <-
Let’s generate some data.
function(n, alpha) {
rclayton <- runif(n+1) # innovations
u <- u
v <-for(i in 2:(n+1))
((u[i]^(-alpha/(1+alpha)) -1)*v[i-1]^(-alpha) +1)^(-1/alpha)
v[i] <-2:(n+1)]
v[
} 200
n <- rclayton(n, alpha = alpha)
u <- qtls(u, df=df, mu=mu, sigma=sigma)
u <- u[-n]
y <- u[-1] x <-
We now estimate the parameters under known marginals
fitCopula(claytonCopula(dim=2),
cbind(ptls(x,df,mu,sigma), ptls(y,df,mu,sigma)))
## Call: fitCopula(claytonCopula(dim = 2), data = cbind(ptls(x, df, mu,
## sigma), ptls(y, df, mu, sigma)))
## Fit based on "maximum pseudo-likelihood" and 199 2-dimensional observations.
## Copula: claytonCopula
## alpha
## 11.08
## The maximized loglikelihood is 226.5
## Optimization converged
## Identical margins
mvdc(claytonCopula(dim=2), c("tls","tls"),
M2tlsI <-rep(list(list(df=NA, mu=NA, sigma=NA)), 2), marginsIdentical= TRUE)
fitMvdc(cbind(x,y), M2tlsI, start=c(3,1,1, 10))) (fit.id.mar <-
## Call: fitMvdc(data = cbind(x, y), mvdc = M2tlsI, start = c(3, 1, 1,
## 10))
## Maximum Likelihood estimation based on 199 2-dimensional observations.
## Copula: claytonCopula
## Identical margins:
## m.df m.mu m.sigma
## 3.9098 0.4095 0.7320
## Copula:
## alpha
## 5.938
## The maximized loglikelihood is -338.2
## Optimization converged
## Not necessarily identical margins
mvdc(claytonCopula(dim=2), c("tls","tls"),
M2tls <-rep(list(list(df=NA, mu=NA, sigma=NA)), 2))
fitMvdc(cbind(x,y), M2tls, start=c(3,1,1, 3,1,1, 10))
## Call: fitMvdc(data = cbind(x, y), mvdc = M2tls, start = c(3, 1, 1,
## 3, 1, 1, 10))
## Maximum Likelihood estimation based on 199 2-dimensional observations.
## Copula: claytonCopula
## Margin 1 :
## m1.df m1.mu m1.sigma
## 3.7851 0.4150 0.7288
## Margin 2 :
## m2.df m2.mu m2.sigma
## 4.0760 0.4046 0.7358
## Copula:
## alpha
## 5.944
## The maximized loglikelihood is -338.1
## Optimization converged
function(z, tau, df, mu, sigma, alpha)
u.cond <-^(-alpha/(1+alpha)) -1) * ptls(z,df,mu,sigma)^(-alpha) + 1) ^ (-1/alpha)
((tau function(z, tau, df, mu, sigma, alpha) {
y.cond <- u.cond(z, tau, df, mu, sigma, alpha)
u <-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))
seq(min(y),max(y),len = 60)
z <-for(i in 1:5) {
i/6
tau <-lines(z, y.cond(z, tau, df,mu,sigma, alpha))
## and compare with estimate:
fit.id.mar@estimate
b <-lines(z, y.cond(z, tau, df=b[1], mu=b[2], sigma=b[3], alpha=b[4]),
col="red")
}