Search This Blog

Friday, March 16, 2012

R Programming: Part4

1. A random sample of size 100 from mixture of two log-normal distributions with mixture
proportion 0.4. Calculate maximum likelihood estimates.
2. Consider the daily stock return of the Citigroup (tick symbol C) and the Standard
and Poor’s 500 Composite index from January 2001 to December 2008. The data are
simple returns and in the file d-csp0108.txt (three columns with date, C-rtn, SP-rtn).
1
(a) Assume both the companies are holding a long position. Calculate parametric (with
respect to best fitted distribution) and non- parametric 1-day VaR corresponding to
the above data with confidence 0.999, 0.99, 0.95 and 0.9. Also calculate the expected
shortfall. Compare the parameteric and non-parametric result.



Codes

Q no 1>>>>>>>>>>>

em=function()
{
library("MASS");
require(graphics);

lembda=0.4
ez=0
x=lembda*rlnorm(100, meanlog=0,sdlog=0.3) +(1-lembda)*rlnorm(100, meanlog=0, sdlog=0.5)

for( i in 1:length(x) )
{
ez[i]=(lembda*rlnorm(100, meanlog=x[i], sdlog=0.3))/( lembda*rlnorm(100, meanlog=x[i], sdlog=0.3) + (1-lembda)*rlnorm(100, meanlog=x[i], sdlog=0.5) )
}
m1=ez
m2=1-ez

lembdanew=mean(m1)

mu1=sum(m1*x)/(sum(m1))

mu2=sum(m2*x)/(sum(m2))

sigma1=sum(m1*(x-mu1)^2)/(sum(m1))

sigma2=sum(m2*(x-mu2)^2)/(sum(m1))

print(lembdanew)
print(mu1)
print(mu2)
print(sigma1)
print(sigma2)
#print(ez)

}




Q no 2...>>>>>>>>>>>>

var=function(P)
{

####### --------- Lybraries ------------#####
library("MASS");
require(graphics);
require(PerformanceAnalytics)
data_set=read.table("d-csp0108.txt",header=TRUE);
date=data_set[1];
C=data_set[2];
SP=data_set[3];
c_rtn=t(C);
sp_rtn=t(SP);
######### Parametric Approach ############
PVaRC=VaR(c(c_rtn), p=P, method="gaussian", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)
PCVaRC=ES(c(c_rtn), p=P, method="gaussian", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)

PVaRSP=VaR(c(sp_rtn), p=P, method="gaussian", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)
PCVaRSP=ES(c(sp_rtn), p=P, method="gaussian", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)

print(PVaRC)
print(PCVaRC)
print(PVaRSP)
print(PCVaRSP)

########### Non-Parametric Approach ###############
NPVaRC=VaR(c(c_rtn), p=P, method="historical", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)
NPCVaRC=ES(c(c_rtn), p=P, method="historical", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)

NPVaRSP=VaR(c(sp_rtn), p=P, method="historical", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)
NPCVaRSP=ES(c(sp_rtn), p=P, method="historical", clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE)

print(NPVaRC)
print(NPCVaRC)
print(NPVaRSP)
print(NPCVaRSP)
#, clean="none", portfolio_method="single", weight=NULL, mu=NULL, sigma=NULL, m3=NULL, m4=NULL, invert=TRUE


}



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

No comments:

Post a Comment

Thank you