# Copulas estimation and influence of margins

Just a short post to get back on results mentioned at the end of the course. Since copulas are obtained using (univariate) quantile functions in the joint cumulative distribution function, they are – somehow – related to the marginal distribution fitted. In order to illustrate this point, consider an i.i.d. sample  from a Student-t distribution,

```library(mnormt)
r=.5
n=200
X=rmt(n,mean=c(0,0),S=matrix(c(1,r,r,1),2,2),df=4)```

Thus, the true copula is Student-t. Here, with 4 degrees of freedom. Note that we can easily get the (true) value of the copula, on the diagonal

```dg=function(t) pmt(qt(t,df=4),mean=c(0,0),
S=matrix(c(1,r,r,1),2,2),df=4)
DG=Vectorize(dg)```

Four strategies are considered here to define pseudo-copula base variates,

• misfit: consider an invalid marginal estimation: we have assumed that margins were Gaussian, i.e.
• perfect fit: here, we know that margins were Student-t, with 4 degrees of freedom
• standard fit: then, consider the case where we fit marginal distribution, but in the good family this time (e.g. among Student-t distributions),
• ranks: finally, we consider nonparametric estimators for marginal distributions,

Now that we have a sample with margins in the unit square, let us construct the empirical copula,

Let us now compare those four approaches.

• The first one is to illustrate model error, i.e. what’s going on if we fit distributions, but not in the proper family of parametric distributions.
```X0=cbind((X[,1]-mean(X[,1])/sd(X[,1])),
(X[,2]-mean(X[,2])/sd(X[,2])))
Y=pnorm(X0)```

Then, the following code is used to compute the value of the empirical copula, on the diagonal,

```diagonale=function(t,Z) mean((Z[,1]<=t)&(Z[,2]<=t))
diagY=function(t) diagonale(t,Y)
DiagY=Vectorize(diagY)
u=seq(0,1,by=.005)
dY=DiagY(u)```

On the graph below, 1,000 samples of size 200 have been generated. All trajectories are the estimation of the copula on the diagonal. The black plain line is the true value of the copula

Obviously, it is not good at all. Mainly because the distribution of  can’t be a copula, since margins are not even uniform on the unit interval.

• a perfect fit. Here, we use the following code to generate our copula-type sample
`U=pt(X,df=4)`

This time, the fit is much better.

• Using maximum likelihood estimators to fit the best distribution within the Student-t family
```F1=fitdistr(X0[,1],dt,list(df=5),lower = 0.001)
F2=fitdistr(X0[,2],dt,list(df=5),lower = 0.001)
V=cbind(pt(X0[,1],df=F1\$estimate),pt(X0[,2],df=F2\$estimate))```

Here, it is also very good. Even better than before, when the true distribution is considered.

(it is like using Lillie test for goodness of fit, versus Kolmogorov-Smirnov, seehere for instance, in French).

• Finally, let us consider ranks, or nonparametric estimators for marginal distributions,
`R=cbind(rank(X[,1])/(n+1),rank(X[,2])/(n+1))`

Here it is even better then the previous one

If we compare Box-plots of the value of the copula at point (.2,.2), we obtain the following, with on top ranks, then fitting with the good family, then using the true distribution, and finally, using a non-proper distribution.

Just to illustrate one more time a result mentioned in a previous post, “in statistics, having too much information might not be a good thing“.