[R-SIG-Finance] [Fwd: CAPM homework]

Brian G. Peterson brian at braverock.com
Thu Feb 9 13:57:00 CET 2012


to the list


-------- Forwarded Message --------
From: Baranzan Alhamdu Vayin <BAVayin at uclan.ac.uk>
To: Brian G. Peterson <brian at braverock.com>
Subject: CAPM homework
Date: Thu, 9 Feb 2012 12:45:06 +0000

many thanks brian. so sorry for violating your procedure. it was not intentional.i am not aware. please pardon me. i just posted the complete question with my R.code (the code i wrote), before your response came. my company is Whitbread PLC. the ticker symbol is "WBT". 
A table detailing company data allocation for each student has been attached with this
assignment. All these companies are included in the FTSE100 index. Download daily data for
the last five years (subject to availability) for the particular stock, allocated to you. You need
to identify the particular ticker (symbol) for the company you are given. Pay particular
attention to the fact that some companies can be traded on different markets. You need the
British shares of any such companies. Up to 5 points can be rewarded if you explain why this
is the case in this assignment.
It is up to you how you download this data (but please note the source and mention it in your
assignment). Download data for the same period for FTSE all shares (^FTAS) as well as the
gilts index BG06.L. Using the last two as proxies for the overall market return and the riskfree
return, calculate the appropriate continuously compounded (logarithmic) returns.
1. Provide the appropriate time series regression tests for the CAPM.
2. Estimate the Single Index model for your stock and discuss the differences to the CAPM
estimates.
3. Provide a rolling analysis of the time series CAPM regression. Select appropriate window
and briefly justify your choice. Draw appropriate conclusions from your analysis
Presentation and marking guidelines:
This assignment involves four (1, 2, 3) practical tasks that carry equal weighting. For each of
the above you need to present a computer output demonstrating you have accomplished the
task. This should consist of both computer code and output from the execution of the code.
Please provide details and explanations for any relevant estimation procedures and fully
explain them. No marks will be rewarded if you provide correct results but fail to explain
them (since you may copy the answers from someone else without properly understanding it).
It should be clear to a third party reader, what you have done and more importantly why you
have done it. Aim to provide self-contained explanations that contain enough detail that
allows one to replicate your results (provided they use different estimation software).
Furthermore if they are calculation mistakes, it should be clear of whether you got the
principles right and simply obtained the wrong results because of typing errors, or did not
understood the task. If details are missing, or they are insufficient, the latter will be assumed
resulting in lower marks.
When explaining, refer to background theory, but do not replicate textbooks material. This
assignment is designed to test understanding (alongside practical skills) rather than ability to
rephrase teaching material.
below is my R.code. i do not really really understand the question, so not too sure of what i have done. i shall be most grateful, if you respond swiftly like you did. please, help me with a clearer explanation. many thanks for your quick response, vayin. i equally attached my R.code to this mail.
library("TSA")
library("tseries")
library("zoo")

AvivaDailyPrices = get.hist.quote(instrument="AV.L", start="2007-01-22",
                             end="2012-01-20", quote="AdjClose",
                             provider="yahoo", origin="1970-01-01",
                             compression="d", retclass="zoo")
class(AvivaDailyPrices)
colnames(AvivaDailyPrices)
start(AvivaDailyPrices)
end(AvivaDailyPrices)
nrow(AvivaDailyPrices)
colnames(AvivaDailyPrices) = "AVIVA"

FTSEDailyPrices = get.hist.quote(instrument="^ftas", start="2007-01-22",
                             end="2012-01-20", quote="AdjClose",
                             provider="yahoo", origin="1970-01-01",
                             compression="d", retclass="zoo")
colnames(FTSEDailyPrices) = "FTSE"

GiltDailyPrices = get.hist.quote(instrument="BG06.L", start="2007-01-22",
                             end="2012-01-20", quote="AdjClose",
                             provider="yahoo", origin="1970-01-01",
                             compression="d", retclass="zoo")
colnames(GiltDailyPrices) = "GILT"
# compute daily cc returns
#

AvivaDailyReturns = diff(log(AvivaDailyPrices))
Aviva.df = as.data.frame(coredata(AvivaDailyPrices))
FTSEDailyReturns = diff(log(FTSEDailyPrices))
FTSE.df = as.data.frame(coredata(AvivaDailyPrices))
GILTDailyReturns = diff(log(GiltDailyPrices))
GILT.df = as.data.frame(coredata(GILTDailyPrices))
DailyReturns = merge(AvivaDailyReturns, FTSEDailyReturns,GILTDailyReturns)
DailyReturns = merge(AvivaDailyReturns, FTSEDailyReturns,GILTDailyReturns, by=”date”, all=T)
# create matrix data 
AvivaDailyReturns.mat = coredata(AvivaDailyReturns)
FTSEDailyReturns.mat = coredata(FTSEDailyReturns)
GILTDailyReturns.mat = coredata(GILTDailyReturns)
DailyReturns.mat = coredata(DailyReturns)

# create excess returns by subtracting off risk free rate
# note: coredata() function extracts data from zoo object
DailyReturns.mat = as.matrix(coredata(DailyReturns))
excessReturns.mat = DailyReturns.mat - DailyReturns.mat[,"GILT"]
excessReturns.df = as.data.frame(excessReturns.mat)

# CAPM regression for Aviva (AVIVA) using 5 years of data
capm.fit = lm(AVIVA~FTSE,data=excessReturns.df)
capm.fit = lm(AVIVA~FTSE,data=excessReturns.df)
summary(capm.fit)

# plot data and regression line
plot(excessReturns.df$FTSE,excessReturns.df$AVIVA,
main="CAPM regression for AVIVA",
ylab="Excess returns on AVIVA",
xlab="Excess returns on FTSE")
abline(capm.fit)					# plot regression line
abline(h=0,v=0)					# plot horizontal and vertical lines at 0

 
________________________________________
From: r-sig-finance-bounces at r-project.org [r-sig-finance-bounces at r-project.org] on behalf of r-sig-finance-request at r-project.org [r-sig-finance-request at r-project.org]
Sent: 09 February 2012 11:00
To: r-sig-finance at r-project.org
Subject: R-SIG-Finance Digest, Vol 93, Issue 7

Send R-SIG-Finance mailing list submissions to
        r-sig-finance at r-project.org

To subscribe or unsubscribe via the World Wide Web, visit
        https://stat.ethz.ch/mailman/listinfo/r-sig-finance
or, via email, send a message with subject or body 'help' to
        r-sig-finance-request at r-project.org

You can reach the person managing the list at
        r-sig-finance-owner at r-project.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of R-SIG-Finance digest..."


Today's Topics:

   1. rbloomberg includeConditionCodes (Jonny So)
   2. Hessian in GARCH-Models (rugarch-package) (Lin23)
   3. assistance please (Baranzan Alhamdu Vayin)


----------------------------------------------------------------------

Message: 1
Date: Wed, 8 Feb 2012 17:22:31 +0000
From: Jonny So <jonny.so at gmail.com>
To: r-sig-finance at r-project.org
Subject: [R-SIG-Finance] rbloomberg includeConditionCodes
Message-ID:
        <CAMEiyUGZ-tFSaPPy26En11k31dj4B4gtzzQLSMenOokMYgw2PQ at mail.gmail.com>
Content-Type: text/plain

I'm trying to get trade tick data with condition codes but the
includeConditionCodes option doesn't seem to do anything.

tick(con, "BP/ LN Equity", "TRADE", "2012-01-19 16:29:50.000", "2012-01-19
16:29:59.000", option_names="includeConditionCodes", option_values="TRUE")

Clearly I'm missing something - can anyone tell me what it is?

        [[alternative HTML version deleted]]



------------------------------

Message: 2
Date: Wed, 8 Feb 2012 14:30:00 -0800 (PST)
From: Lin23 <linusholtermann at gmx.de>
To: r-sig-finance at r-project.org
Subject: [R-SIG-Finance] Hessian in GARCH-Models (rugarch-package)
Message-ID: <1328740200776-4371133.post at n4.nabble.com>
Content-Type: text/plain; charset=us-ascii

Hello,

i wrote a code for a GJR-GARCH-Model. With my current data and model there
is something wrong with the robust standard errors and the p-value. I use
maxLik-package with BHHH-algorithm. The coefficients in my model are equal
to those in the rugarch-package or EViews. But the p-value for the intercept
in the variance-equation is way too high in my model. When i estimate my
model with non-robust standard errors (-crossprod(fit$gradientObs))
everything is all right. So I think there is something wrong with the
hessian that is computed by the maxLik-package. I want to compute my own
hessian. I used the code for the hessian from the rugarch-package
(rugarch-numderiv.R). But I failed.  Maybe someone can help me with my
problem. I would appreciate it very much.

My GARCH-code:

garch<-function(y,Di,Mi,Do,Fr,x1,x2,x3,x4,Dum,backcast){

      n<-length(y)

        reg <- lm(y ~ Di + Mi + Do + Fr + x1 + x2 + x3 + x4+(Dum:x3)+(Dum:x4))
#Start Values

        exp.smooth <- function(lambda){
        smooth <- as.vector(1:n-1)
        init.hh <- lambda^n*mean(reg$res^2)+(1-lambda)*sum(lambda^smooth*reg$res^2)
# Backcast initial variance
        return(init.hh)
        }
        init.hh <- exp.smooth(lambda=backcast)


LLH <- function(par){                                                                                   # Log.Likelihood
     a <-par[1]
     di <- par[2]
     mi <- par[3]
     do <- par[4]
     fr <- par[5]
     b1 <- par[6]
     b2 <- par[7]
     b3 <- par[8]
     b4 <- par[9]
     b5 <- par[10]
     b6 <- par[11]
     omega <- par[12]
     alpha <- par[13]
     beta <- par[14]
     gjr <- par[15]
     dummy <- par[16]

     res<-array(n)
     hh<-array(n)
     ll<-numeric(n)

     for (i in 1:n){

res[i]<-y[i]-a-di*Di[i]-mi*Mi[i]-do*Do[i]-fr*Fr[i]-b1*x1[i]-b2*x2[i]-b3*x3[i]-b4*x4[i]-b5*Dum[i]*x3[i]-b6*Dum[i]*x4[i]
#Mean-Equation
     }

     hh[1] <- init.hh
     for(i in 2:n){
     hh[i] <-
(omega+alpha*res[i-1]^2+beta*(hh[i-1]-omega)+ifelse(res[i-1]<0,
gjr*res[i-1]^2, 0))*(1+dummy*Dum[i])            #Variance-Equation
     }

       for (i in 1:n){
     ll[i] <- -1/2*log(2*pi*hh[i]) - 1/2*res[i]^2/hh[i]                                                                                                 #
Log.Likelihood
      }
        ll

        }

        param <- c(reg$coef, omega = 0.1*mean(reg$res^2),alpha = 0.15, beta =
0.6,gjr=0.05,dummy=0)
        fit<-maxLik(start=param,
logLik=LLH,method="BHHH",finalHessian=TRUE,iterlim=2500)                                                        # fit with
maxLik-package


        grad <- fit$gradientObs
      robust.vcov <-
solve(fit$hessian)%*%t(grad)%*%grad%*%solve(fit$hessian)
        se.coef <- sqrt(diag(robust.vcov))                                                                                                                      # robust standard errors
        tval <- fit$estimate/se.coef
        matcoef <- cbind(fit$estimate, se.coef, tval, 2*(1-pnorm(abs(tval))))
        dimnames(matcoef) <- list(names(tval), c("Estimate", "Std. Error","t
value", "Pr(>|t|)"))


cat("\n---------------------------------------------------------------------\n")
#Output
        cat("\nKoeffizienten\n")

cat("\n---------------------------------------------------------------------\n")
        printCoefmat(matcoef, digits = 6, signif.stars=TRUE)
}
EST1 <-
garch(y=dat2$r_csi,Di=dat2$Di,Mi=dat2$Mi,Do=dat2$Do,Fr=dat2$Fr,x1=dat2$r_t,x2=dat2$r_sp_l,x3=dat2$r_csi_l,x4=dat2$r_csi_l_3,Dum=dat2$f,backcast=0.7)

See below for the hessian-code from the rugarch-package. I dont know what I
to enter for x and for ... in my case. I hope someone can help.

hessian-code from rugarch-package:

.hessian2sided = function(f, x, ...)
{
        n = length(x)
        fx = f(x, ...)
        eps = .Machine$double.eps

        # Compute the stepsize (h)
        h = eps^(1/3)*apply(as.data.frame(x), 1,FUN = function(z) max(abs(z),
1e-2))
        xh = x+h
        h = xh-x
        if(length(h) == 1) ee = matrix(h, ncol = 1, nrow = 1) else ee =
as.matrix(diag(h))

        # Compute forward and backward steps
        gp = vector(mode = "numeric", length = n)
        gp = apply(ee, 2, FUN = function(z) f(x+z, ...))
        gm = vector(mode="numeric",length=n)
        gm = apply(ee, 2, FUN = function(z) f(x-z, ...))
        H = h%*%t(h)
        Hm = H
        Hp = H
        # Compute "double" forward and backward steps
        for(i in 1:n){
                for(j in  i:n){
                        Hp[i,j] = f(x+ee[,i]+ee[,j], ...)
                        Hp[j,i] = Hp[i,j]
                        Hm[i,j] = f(x-ee[,i]-ee[,j], ...)
                        Hm[j,i] = Hm[i,j]
                }
        }
        #Compute the hessian
        for(i in 1:n){
                for(j in  i:n){
                        H[i,j] = ( (Hp[i,j]-gp[i]-gp[j]+fx+fx-gm[i]-gm[j]+Hm[i,j]) /H[i,j] )/2
                        H[j,i] = H[i,j]
                }
        }
        return(H)
}

Thank you all!!!

--
View this message in context: http://r.789695.n4.nabble.com/Hessian-in-GARCH-Models-rugarch-package-tp4371133p4371133.html
Sent from the Rmetrics mailing list archive at Nabble.com.



------------------------------

Message: 3
Date: Thu, 9 Feb 2012 06:30:36 +0000
From: Baranzan Alhamdu Vayin <BAVayin at uclan.ac.uk>
To: "r-sig-finance at r-project.org" <r-sig-finance at r-project.org>
Subject: [R-SIG-Finance] assistance please
Message-ID:
        <7A8CC04960D0F542B25192E38F02289D0D1F7BA1 at AMSPRD0302MB103.eurprd03.prod.outlook.com>

Content-Type: text/plain; charset="us-ascii"

Download data for the same period for FTSE all shares (^FTAS) as well as the
gilts index BG06.L. Using the last two as proxies for the overall market return and the riskfree
return, calculate the appropriate continuously compounded (logarithmic) returns.
1. Provide the appropriate time series regression tests for the CAPM.
2. Estimate the Single Index model for your stock and discuss the differences to the CAPM
estimates.
3. Provide a rolling analysis of the time series CAPM regression. Select appropriate window
and briefly justify your choice. Draw appropriate conclusions from your analysis. pls, could you help me with some clue on how to go about this work. thanks for your anticipated response
________________________________________
From: r-sig-finance-bounces at r-project.org [r-sig-finance-bounces at r-project.org] on behalf of r-sig-finance-request at r-project.org [r-sig-finance-request at r-project.org]
Sent: 07 February 2012 11:00
To: r-sig-finance at r-project.org
Subject: R-SIG-Finance Digest, Vol 93, Issue 6

Send R-SIG-Finance mailing list submissions to
        r-sig-finance at r-project.org

To subscribe or unsubscribe via the World Wide Web, visit
        https://stat.ethz.ch/mailman/listinfo/r-sig-finance
or, via email, send a message with subject or body 'help' to
        r-sig-finance-request at r-project.org

You can reach the person managing the list at
        r-sig-finance-owner at r-project.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of R-SIG-Finance digest..."


Today's Topics:

   1. Volatility forecast (Papa Senyo)
   2. Re: Volatility forecast (alexios)
   3. Re: Volatility forecast (Papa Senyo)
   4. Re: Volatility forecast (alexios)


----------------------------------------------------------------------

Message: 1
Date: Mon, 6 Feb 2012 12:42:09 +0000 (GMT)
From: Papa Senyo <papa.senyo at yahoo.it>
To: "r-sig-finance at r-project.org" <r-sig-finance at r-project.org>
Subject: [R-SIG-Finance] Volatility forecast
Message-ID:
        <1328532129.67245.YahooMailNeo at web132301.mail.ird.yahoo.com>
Content-Type: text/plain

Dear ALL,
Please, how does one get volatility forecast using rugarch package?
kind regards
Papa
        [[alternative HTML version deleted]]



------------------------------

Message: 2
Date: Mon, 06 Feb 2012 12:45:03 +0000
From: alexios <alexios at 4dscape.com>

Cc: "r-sig-finance at r-project.org" <r-sig-finance at r-project.org>
Subject: Re: [R-SIG-Finance] Volatility forecast
Message-ID: <4F2FCB4F.4090202 at 4dscape.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

By reading the documentation.
Alexios

On 06/02/2012 12:42, Papa Senyo wrote:
> Dear ALL,
> Please, how does one get volatility forecast using rugarch package?
> kind regards
> Papa
>       [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>



------------------------------

Message: 3
Date: Mon, 6 Feb 2012 13:35:14 +0000 (GMT)

To: alexios <alexios at 4dscape.com>
Cc: "r-sig-finance at r-project.org" <r-sig-finance at r-project.org>
Subject: Re: [R-SIG-Finance] Volatility forecast
Message-ID:
        <1328535314.32659.YahooMailNeo at web132303.mail.ird.yahoo.com>
Content-Type: text/plain

Please, keep in mind that I have cannot do anything without the documentation. It is a nice package and trying to know the nitty gritty ?of it. There is a forecast for GARCH model and also the report on the loss functions. What i really want to understand is if the loss function are calculated based on the volatility or the mean returns.
kind regards,
papa


________________________________
 Da: alexios <alexios at 4dscape.com>

Cc: "r-sig-finance at r-project.org" <r-sig-finance at r-project.org>
Inviato: Luned? 6 Febbraio 2012 13:45
Oggetto: Re: [R-SIG-Finance] Volatility forecast

By reading the documentation.
Alexios

On 06/02/2012 12:42, Papa Senyo wrote:
> Dear ALL,
> Please, how does one get volatility forecast using rugarch package?
> kind regards
> Papa
> ??? [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>
        [[alternative HTML version deleted]]



------------------------------

Message: 4
Date: Mon, 06 Feb 2012 13:50:28 +0000
From: alexios <alexios at 4dscape.com>

Cc: "r-sig-finance at r-project.org" <r-sig-finance at r-project.org>
Subject: Re: [R-SIG-Finance] Volatility forecast
Message-ID: <4F2FDAA4.7060204 at 4dscape.com>
Content-Type: text/plain

[[elided Yahoo spam]]

The 'fpm' method (I assume this is what you refer to as 'report')
returns some summary
forecast performance measures on the fitted return series AND assuming
you have used
the out.sample option in the fit routine (the 'fpm' requires at least 6
out.sample points to
compute the statistics). Because forecasts can be a combination of both
rolling and unconditional
type, the extractor methods (as.array, as.data.frame etc) have a host of
options to indicate
what exactly you want to extract from the object. If you go through the
tests in the 'inst' folder
of the source package, you will see many examples of how this works.

Finally, it would not be possible to return such measures on the
volatility as that is not observed (although
you could very roughly proxy it with abs or squared returns...).
However, if you have your own realized volatility series then surely it
is quite simple to compute
those yourself.

Alexios

On 06/02/12 13:35, Papa Senyo wrote:
> Please, keep in mind that I have cannot do anything without the
> documentation. It is a nice package and trying to know the nitty
> gritty  of it. There is a forecast for GARCH model and also the report
> on the loss functions. What i really want to understand is if the loss
> function are calculated based on the volatility or the mean returns.
> kind regards,
> papa
>
> ------------------------------------------------------------------------
> *Da:* alexios <alexios at 4dscape.com>

> *Cc:* "r-sig-finance at r-project.org" <r-sig-finance at r-project.org>
> *Inviato:* Luned? 6 Febbraio 2012 13:45
> *Oggetto:* Re: [R-SIG-Finance] Volatility forecast
>
> By reading the documentation.
> Alexios
>
> On 06/02/2012 12:42, Papa Senyo wrote:
> > Dear ALL,
> > Please, how does one get volatility forecast using rugarch package?
> > kind regards
> > Papa
> >     [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at r-project.org <mailto:R-SIG-Finance at r-project.org>
> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > -- Subscriber-posting only. If you want to post, subscribe first.
> > -- Also note that this is not the r-help list where general R
> questions should go.
> >
>
>
>


        [[alternative HTML version deleted]]



------------------------------

_______________________________________________
R-SIG-Finance mailing list
R-SIG-Finance at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-finance


End of R-SIG-Finance Digest, Vol 93, Issue 6
********************************************




------------------------------

_______________________________________________
R-SIG-Finance mailing list
R-SIG-Finance at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-finance


End of R-SIG-Finance Digest, Vol 93, Issue 7
********************************************


________________________________________
From: Brian G. Peterson [brian at braverock.com]
Sent: 09 February 2012 12:30
To: Baranzan Alhamdu Vayin
Cc: r-sig-finance at r-project.org
Subject: Re: CAPM homework (was RE: assistance please)

On Thu, 2012-02-09 at 06:30 +0000, Baranzan Alhamdu Vayin wrote:
> Download data for the same period for FTSE all shares (^FTAS) as well as the
> gilts index BG06.L. Using the last two as proxies for the overall market return and the riskfree
> return, calculate the appropriate continuously compounded (logarithmic) returns.
> 1. Provide the appropriate time series regression tests for the CAPM.
> 2. Estimate the Single Index model for your stock and discuss the differences to the CAPM
> estimates.
> 3. Provide a rolling analysis of the time series CAPM regression. Select appropriate window
> and briefly justify your choice. Draw appropriate conclusions from your analysis.

> pls, could you help me with some clue on how to go about this work. thanks for your anticipated response

It's not polite to hit 'reply' to a digest post.  If your email reader
can't handle digests correctly (as Microsoft Outlook and many others
cannot), then change your list preferences to receive individual emails,
and use your mail client's filter capabilities to move posts from this
list into a folder.

This looks shockingly like a homework question, so I'm not going to give
you the answers, only a bit of direction.

>From the R console:

install.packages(quantmod)
install.packages(PerformanceAnalytics)
require(quantmod)
require(PerformanceAnalytics)
?getSymbols
?Return.calculate
?CAPM.utils
?chart.RollingRegression

In the future, please follow the posting guide[1] and esr[2] and *try*
first.  Search on rseek.org, which would likely have found all the
functions described above.  Try some things.  *Then* post on r-help or
here (depending on whether your question is general to R or specific to
finance, respectively) showing what you've already tried, and being
specific about where you are having difficulty.

Regards,

    - Brian

Ref:
[1] http://www.r-project.org/posting-guide.html
[2] http://catb.org/esr/faqs/smart-questions.html

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock




-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list