[Uwgarp-commits] r7 - in pkg/GARPFRM: . R man sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Nov 18 22:45:03 CET 2013
Author: tfillebeen
Date: 2013-11-18 22:45:02 +0100 (Mon, 18 Nov 2013)
New Revision: 7
Added:
pkg/GARPFRM/R/capm.R
pkg/GARPFRM/man/capm.tstats.Rd
pkg/GARPFRM/sandbox/test_capm.R
Modified:
pkg/GARPFRM/
pkg/GARPFRM/DESCRIPTION
pkg/GARPFRM/NAMESPACE
Log:
Adding capm files
Property changes on: pkg/GARPFRM
___________________________________________________________________
Added: svn:ignore
+ .Rproj.user
.Rhistory
.RData
Modified: pkg/GARPFRM/DESCRIPTION
===================================================================
--- pkg/GARPFRM/DESCRIPTION 2013-11-18 17:50:31 UTC (rev 6)
+++ pkg/GARPFRM/DESCRIPTION 2013-11-18 21:45:02 UTC (rev 7)
@@ -1,4 +1,4 @@
-Package: GARP_FRM
+Package: GARPFRM
Type: Package
Title: Global Association of Risk Professionals: Financial Risk Manager
Version: 0.1.0
@@ -13,4 +13,5 @@
Suggests:
quadprog
License: GPL
-
+Collate:
+ 'capm.R'
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2013-11-18 17:50:31 UTC (rev 6)
+++ pkg/GARPFRM/NAMESPACE 2013-11-18 21:45:02 UTC (rev 7)
@@ -1 +1 @@
-exportPattern("^[[:alpha:]]+")
+export(capm.tstats)
Added: pkg/GARPFRM/R/capm.R
===================================================================
--- pkg/GARPFRM/R/capm.R (rev 0)
+++ pkg/GARPFRM/R/capm.R 2013-11-18 21:45:02 UTC (rev 7)
@@ -0,0 +1,16 @@
+#' CAPM Function
+#'
+#' Description for CAPM
+#' @param rf risk-free rate
+#' @param mkrt
+#' @return this is what the function returns
+#' @export
+capm.tstats = function(rf,mkrt) {
+ # Fiting CAPM
+ capm.fit = lm(rf~mkrt)
+ # Extract summary info
+ capm.summary = summary(capm.fit)
+ # Retrieve t-stat
+ t.stat = coef(capm.summary)[1,3]
+ t.stat
+}
\ No newline at end of file
Added: pkg/GARPFRM/man/capm.tstats.Rd
===================================================================
--- pkg/GARPFRM/man/capm.tstats.Rd (rev 0)
+++ pkg/GARPFRM/man/capm.tstats.Rd 2013-11-18 21:45:02 UTC (rev 7)
@@ -0,0 +1,18 @@
+\name{capm.tstats}
+\alias{capm.tstats}
+\title{CAPM Function}
+\usage{
+ capm.tstats(rf, mkrt)
+}
+\arguments{
+ \item{rf}{risk-free rate}
+
+ \item{mkrt}{}
+}
+\value{
+ this is what the function returns
+}
+\description{
+ Description for CAPM
+}
+
Added: pkg/GARPFRM/sandbox/test_capm.R
===================================================================
--- pkg/GARPFRM/sandbox/test_capm.R (rev 0)
+++ pkg/GARPFRM/sandbox/test_capm.R 2013-11-18 21:45:02 UTC (rev 7)
@@ -0,0 +1,57 @@
+# Efficient Frontier w/ constraints options: short-sale & borrowing
+# Standard Capital Asset Pricing Model
+
+# CAPM Assumptions
+#1. Identical investors who are price takers
+#2. Investment over the same time horizon
+#3. No transaction costs or taxes
+#4. Can borrow and lend at risk-free rate
+#5. Investors only care about portfolio expected return and variance
+#6. Market consists of all publicly traded assets
+
+# Load Libraries
+library(zoo)
+library("PerformanceAnalytics")
+options(digits=3)
+
+# Read returns from .csv file
+stock.df <- read.csv("~/Documents/R_ FRM EXAM/Stocks_data.csv")
+colnames(SP_data)
+
+# Estimate a zooreg object: regularly spaced zoo object
+stock.z = zooreg(stock.df[,-1], start=c(1993, 1), end=c(2013,11), frequency=12)
+index(stock.z) = as.yearmon(index(stock.z))
+# Summarize Start, End, and Number of Rows
+start(stock.z)
+end(stock.z)
+nrow(stock.z)
+
+# Estimate excess returns: subtracting off risk-free rate
+# To strip off the dates and just return a plain vector/matrix coredata() can be used.
+# as.data.frame to check if an object is a data frame, or coerce it if possible.
+returns.mat = as.matrix(coredata(stock.z))
+exReturns.mat = returns.mat - returns.mat[,"RFREE"]
+exReturns.df = as.data.frame(exReturns.mat)
+
+# Run CAPM regression for AAPL (AAPL) using first 4 years
+# 48 months divided by 12 months in a years = 4 years
+capm.fit = lm(AAPL~MARKET,data=exReturns.df,subset=1:48)
+summary(capm.fit)
+
+# plot data and regression line
+plot(exReturns.df$MARKET,exReturns.df$AAPL, main="CAPM for AAPL",
+ ylab="Excess Return: AAPL",
+ xlab="Excess Return: MARKET")
+# Plot CAPM regression estimate
+abline(capm.fit)
+# Create Axis
+abline(h=0,v=0,lty=3)
+
+# Use a capm.tstats function:
+# Estimating CAPM with alpha=0 for asset using first 4 years of data
+
+
+# Testing CAPM function
+tmp = capm.tstats(exReturns.mat[1:60,1],
+ exReturns.mat[1:60,"MARKET"])
+tmp
More information about the Uwgarp-commits
mailing list