Search This Blog

Sunday, April 08, 2012

R Programming # 6


# 1. Draw the surface plot of bivariate gumbel copula with parameter
# equals to 1.5 and bivariate normal copula with parameter equals to 0.7.

surface=function()
{

library("copula");

gc=gumbelCopula(1.5, dim=2)
persp(gc, dcopula, col="red")

nc=normalCopula(0.7)
persp(nc, dcopula, col="blue")

}
=============================


# 2. Draw the contour plot of the density and cdf of bivariate gumbel
#copula with with parameter 1.4 and marginals N(3,4) and T(3).

cont=function()
{

library("copula");
#Density contour Plot
density = mvdc(copula = archmCopula(family = "gumbel", param = 1.4), margins = c("norm", "t"), paramMargins = list(list(mean = 0, sd = 1), list(df=3, ncp = 3)))

print(density)

contour(density, dmvdc, xlim = c(-2, 8), ylim = c(-2, 10)) ;

# CDF contour Plot

cdf=mvdc(copula = archmCopula(family = "gumbel", param = 1.4), margins = c("norm", "t"), paramMargins = list(list(mean = 0, sd = 1), list(df=3, ncp = 3)))
contour(cdf, pmvdc, xlim = c(-2, 20), ylim = c(-2, 20))

}

==============





# 3. Consider the monthly log returns of Boeing (BA), Abbott Labs (ABT),
# Mo- torola (MOT) and General Motors (GM) from January 1998 to December
# 2007. The log returns are in percentages.
# (a) Calculate the correlation co-efficient for MOT and GM.
# Generate 500 random number from gaussian copula taking parameter as
# this correlation coefficient. Draw contour plot of the empirical
# copula based on the generated sample and superimpose the empirical
# copula based on the data for MOT and GM.

corltn = function()
{

####### --------- Lybraries ------------#####
library("copula")
library("fCopulae")
data_set=read.table("m-ba4c9807.txt",header=TRUE);
BA=data_set[1];
ABT=data_set[2];
MOT=data_set[3];
GM=data_set[4];
MOTGM=data_set[3:4];
BA=t(BA);
ABT=t(ABT);
MOT=t(MOT);
GM=t(GM);
# 4 figures arranged in 2 rows and 2 columns
attach(mtcars)
par(mfrow=c(2,2))
rr=cor(t(MOT),t(GM), use = "everything", method = "pearson")
nc = normalCopula(-0.1231763)
r = rcopula(nc, 500)

# Probablity Empirical Copula:
empg=pempiricalCopula(r, N = 10)
contour(empg, pcopula, main="Probability Empirical contour plot of random gaussian data")

#par(new=TRUE)
empd=pempiricalCopula(MOTGM, N = 10)
contour(empd, pcopula, main="Probability Empirical contour plot of MOT and GM data")

# Density Empirical Copula:
empg=dempiricalCopula(r, N = 10)
contour(empg, dcopula, main="Density Empirical contour plot of random gaussian data")

#par(new=TRUE)
empd=dempiricalCopula(MOTGM, N = 10)
contour(empd, dcopula, main="Density Empirical contour plot of MOT and GM data")

}


===============================



****************** END ******************




No comments:

Post a Comment

Thank you