[Uwgarp-commits] r40 - in pkg/GARPFRM: . R man sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jan 3 21:20:14 CET 2014


Author: rossbennett34
Date: 2014-01-03 21:20:14 +0100 (Fri, 03 Jan 2014)
New Revision: 40

Added:
   pkg/GARPFRM/man/CAPM.Rd
   pkg/GARPFRM/man/getAlphas.Rd
   pkg/GARPFRM/man/getBetas.Rd
   pkg/GARPFRM/man/getStatistics.Rd
   pkg/GARPFRM/sandbox/test_capm_rb.R
Modified:
   pkg/GARPFRM/NAMESPACE
   pkg/GARPFRM/R/capm.R
Log:
Adding capm functions and test in sandbox

Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE	2014-01-03 05:29:42 UTC (rev 39)
+++ pkg/GARPFRM/NAMESPACE	2014-01-03 20:20:14 UTC (rev 40)
@@ -1 +1,7 @@
-export(capm.tstats)
+export(CAPM)
+export(getAlphas)
+export(getBetas)
+export(getStatistics)
+S3method(getAlphas,capm_uv)
+S3method(getBetas,capm_uv)
+S3method(getStatistics,capm_uv)

Modified: pkg/GARPFRM/R/capm.R
===================================================================
--- pkg/GARPFRM/R/capm.R	2014-01-03 05:29:42 UTC (rev 39)
+++ pkg/GARPFRM/R/capm.R	2014-01-03 20:20:14 UTC (rev 40)
@@ -1,22 +1,94 @@
-#' CAPM Function
+# CAPM Function 
+# Description for CAPM
+# @param r risk-free rate
+# @param mkrt market return
+# @return the function returns tstat upon default & pvalue when specified
+# @export
+# capm.tstats = function(r,mkrt,type = FALSE) {
+#   # Fiting CAPM and retrieve alpha specific tstats or pvalues
+#   capm.fit = lm(r~mkrt)    
+#   # Extract summary info
+#   capm.summary = summary(capm.fit) 
+#   if(is.null(type) | type=="pvalue"){
+#     # Retrieve p-value if specified
+#     p.value = coef(capm.summary)[1,4]  
+#     p.value
+#   }else{
+#     # Otherwise retrieve t-stat if specified or on default
+#     t.stat = coef(capm.summary)[1,3]  
+#     t.stat
+#   }
+# }
+
+#' Capital Asset Pricing Model
 #' 
-#' Description for CAPM
-#' @param r risk-free rate
-#' @param mkrt market return
-#' @return the function returns tstat upon default & pvalue when specified
+#' Description of CAPM
+#' 
+#' @param R asset returns
+#' @param Rmkt market returns
 #' @export
-capm.tstats = function(r,mkrt,type = FALSE) {
-  # Fiting CAPM and retrieve alpha specific tstats or pvalues
-  capm.fit = lm(r~mkrt)    
-  # Extract summary info
-  capm.summary = summary(capm.fit) 
-  if(is.null(type) | type=="pvalue"){
-    # Retrieve p-value if specified
-    p.value = coef(capm.summary)[1,4]  
-    p.value
-  }else{
-    # Otherwise retrieve t-stat if specified or on default
-    t.stat = coef(capm.summary)[1,3]  
-    t.stat
-  }
-}
\ No newline at end of file
+CAPM <- function(R, Rmkt){
+  # We should have a capm_uv class for a univariate capm (i.e. R is the returns
+  # of a single asset) and capm_mv for a multivariate capm (i.e. R is the returns
+  # for multiple assets)
+  
+  # if(ncol(R) > 1){
+  #   multivariate capm
+  # } else if(ncol(R) == 1){
+  #   univariate capm
+  # }
+  
+  # For now, assume that R is the returns of a single asset
+  capm_fit <- lm(R ~ Rmkt)
+  class(capm_fit) <- c("capm_uv", "lm")
+  return(capm_fit)
+}
+
+#' CAPM alphas
+#' 
+#' Description of CAPM alphas
+#' 
+#' @param object a capm object created by \code{\link{CAPM}}
+#' @export
+getAlphas <- function(object){
+  UseMethod("getAlphas")
+}
+
+#' @method getAlphas capm_uv
+#' @S3method getAlphas capm_uv
+getAlphas.capm_uv <- function(object){
+  return(coef(object)[1])
+}
+
+#' CAPM betas
+#' 
+#' Description of CAPM betas
+#' 
+#' @param object a capm object created by \code{\link{CAPM}}
+#' @export
+getBetas <- function(object){
+  UseMethod("getBetas")
+}
+
+#' @method getBetas capm_uv
+#' @S3method getBetas capm_uv
+getBetas.capm_uv <- function(object){
+  return(coef(object)[2])
+}
+
+#' CAPM statistics
+#' 
+#' Description of CAPM statistics (standard error, t-values, and p-values)
+#' @param object a capm object created by \code{\link{CAPM}}
+#' @export
+getStatistics <- function(object){
+  UseMethod("getStatistics")
+}
+
+#' @method getStatistics capm_uv
+#' @S3method getStatistics capm_uv
+getStatistics.capm_uv <- function(object){
+  tmp_sm <- summary.lm(object)
+  # gets the standard error, t-value, and p-value of model
+  return(coef(tmp_sm)[,2:4])
+}

Added: pkg/GARPFRM/man/CAPM.Rd
===================================================================
--- pkg/GARPFRM/man/CAPM.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/CAPM.Rd	2014-01-03 20:20:14 UTC (rev 40)
@@ -0,0 +1,15 @@
+\name{CAPM}
+\alias{CAPM}
+\title{Capital Asset Pricing Model}
+\usage{
+  CAPM(R, Rmkt)
+}
+\arguments{
+  \item{R}{asset returns}
+
+  \item{Rmkt}{market returns}
+}
+\description{
+  Description of CAPM
+}
+

Added: pkg/GARPFRM/man/getAlphas.Rd
===================================================================
--- pkg/GARPFRM/man/getAlphas.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/getAlphas.Rd	2014-01-03 20:20:14 UTC (rev 40)
@@ -0,0 +1,14 @@
+\name{getAlphas}
+\alias{getAlphas}
+\title{CAPM alphas}
+\usage{
+  getAlphas(object)
+}
+\arguments{
+  \item{object}{a capm object created by
+  \code{\link{CAPM}}}
+}
+\description{
+  Description of CAPM alphas
+}
+

Added: pkg/GARPFRM/man/getBetas.Rd
===================================================================
--- pkg/GARPFRM/man/getBetas.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/getBetas.Rd	2014-01-03 20:20:14 UTC (rev 40)
@@ -0,0 +1,14 @@
+\name{getBetas}
+\alias{getBetas}
+\title{CAPM betas}
+\usage{
+  getBetas(object)
+}
+\arguments{
+  \item{object}{a capm object created by
+  \code{\link{CAPM}}}
+}
+\description{
+  Description of CAPM betas
+}
+

Added: pkg/GARPFRM/man/getStatistics.Rd
===================================================================
--- pkg/GARPFRM/man/getStatistics.Rd	                        (rev 0)
+++ pkg/GARPFRM/man/getStatistics.Rd	2014-01-03 20:20:14 UTC (rev 40)
@@ -0,0 +1,15 @@
+\name{getStatistics}
+\alias{getStatistics}
+\title{CAPM statistics}
+\usage{
+  getStatistics(object)
+}
+\arguments{
+  \item{object}{a capm object created by
+  \code{\link{CAPM}}}
+}
+\description{
+  Description of CAPM statistics (standard error, t-values,
+  and p-values)
+}
+

Added: pkg/GARPFRM/sandbox/test_capm_rb.R
===================================================================
--- pkg/GARPFRM/sandbox/test_capm_rb.R	                        (rev 0)
+++ pkg/GARPFRM/sandbox/test_capm_rb.R	2014-01-03 20:20:14 UTC (rev 40)
@@ -0,0 +1,25 @@
+
+library(GARPFRM)
+data(crsp.short)
+
+head(largecap.ts)
+
+Rf <- largecap.ts[, "t90"]
+R <- largecap.ts[, "CAT"] - Rf
+MKT <- largecap.ts[, "market"] - Rf
+
+# Fit the CAPM model
+tmp <- CAPM(R=R, Rmkt=MKT)
+
+class(tmp)
+
+# Internally, R is calling print.lm and summary.lm because tmp is of class lm
+# You can define your own print.capm_uv and summary.capm_uv for control over
+# what values are calculated and the output that is displayed.
+print(tmp)
+summary(tmp)
+
+# Demonstrate extractor functions
+getAlphas(tmp)
+getBetas(tmp)
+getStatistics(tmp)



More information about the Uwgarp-commits mailing list