[Returnanalytics-commits] r3403 - in pkg/PortfolioAnalytics: . R man src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jun 3 21:00:07 CEST 2014
Author: rossbennett34
Date: 2014-06-03 21:00:06 +0200 (Tue, 03 Jun 2014)
New Revision: 3403
Added:
pkg/PortfolioAnalytics/R/stat.factor.model.R
pkg/PortfolioAnalytics/man/center.Rd
pkg/PortfolioAnalytics/man/cokurtosisMF.Rd
pkg/PortfolioAnalytics/man/cokurtosisSF.Rd
pkg/PortfolioAnalytics/man/coskewnessMF.Rd
pkg/PortfolioAnalytics/man/coskewnessSF.Rd
pkg/PortfolioAnalytics/man/covarianceMF.Rd
pkg/PortfolioAnalytics/man/covarianceSF.Rd
pkg/PortfolioAnalytics/man/extract.cokurtosis.Rd
pkg/PortfolioAnalytics/man/extract.coskewness.Rd
pkg/PortfolioAnalytics/man/extract.covariance.Rd
pkg/PortfolioAnalytics/man/statistical.factor.model.Rd
pkg/PortfolioAnalytics/src/
pkg/PortfolioAnalytics/src/residualcokurtosisMF.c
pkg/PortfolioAnalytics/src/residualcokurtosisSF.c
Modified:
pkg/PortfolioAnalytics/DESCRIPTION
pkg/PortfolioAnalytics/NAMESPACE
Log:
Adding moment estimates using statistical factor model based on Boudt paper
Modified: pkg/PortfolioAnalytics/DESCRIPTION
===================================================================
--- pkg/PortfolioAnalytics/DESCRIPTION 2014-05-28 23:31:44 UTC (rev 3402)
+++ pkg/PortfolioAnalytics/DESCRIPTION 2014-06-03 19:00:06 UTC (rev 3403)
@@ -66,3 +66,4 @@
'inverse.volatility.weight.R'
'utils.R'
'chart.concentration.R'
+ 'stat.factor.model.R'
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2014-05-28 23:31:44 UTC (rev 3402)
+++ pkg/PortfolioAnalytics/NAMESPACE 2014-06-03 19:00:06 UTC (rev 3403)
@@ -3,6 +3,7 @@
export(applyFUN)
export(box_constraint)
export(CCCgarch.MM)
+export(center)
export(chart.Concentration)
export(chart.EfficientFrontier)
export(chart.EfficientFrontierOverlay)
@@ -21,6 +22,9 @@
export(diversification_constraint)
export(diversification)
export(equal.weight)
+export(extract.cokurtosis)
+export(extract.coskewness)
+export(extract.covariance)
export(extractEfficientFrontier)
export(extractGroups)
export(extractObjectiveMeasures)
@@ -69,6 +73,7 @@
export(set.portfolio.moments_v1)
export(set.portfolio.moments_v2)
export(set.portfolio.moments)
+export(statistical.factor.model)
export(trailingFUN)
export(transaction_cost_constraint)
export(turnover_constraint)
Added: pkg/PortfolioAnalytics/R/stat.factor.model.R
===================================================================
--- pkg/PortfolioAnalytics/R/stat.factor.model.R (rev 0)
+++ pkg/PortfolioAnalytics/R/stat.factor.model.R 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,570 @@
+###############################################################################
+# R (http://r-project.org/) Numeric Methods for Optimization of Portfolios
+#
+# Copyright (c) 2004-2014 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: charts.DE.R 3378 2014-04-28 21:43:21Z rossbennett34 $
+#
+###############################################################################
+
+# Note that many of these functions were provided by Kris Boudt and modified
+# only slightly to work with this package
+
+#' Statistical Factor Model
+#'
+#' Fit a statistical factor model using Principal Component Analysis (PCA)
+#'
+#' @details
+#' The statistical factor model is fitted using \code{prcomp}. The factor
+#' loadings, factor realizations, and residuals are computed and returned
+#' given the number of factors used for the model.
+#'
+#' @param R xts of asset returns
+#' @param k number of factors to use
+#' @param \dots additional arguments passed to \code{prcomp}
+#' @return
+#' #' \itemize{
+#' \item{factor_loadings}{ N x k matrix of factor loadings (i.e. betas)}
+#' \item{factor_realizations}{ m x k matrix of factor realizations}
+#' \item{residuals}{ m x N matrix of model residuals representing idiosyncratic
+#' risk factors}
+#' }
+#' Where N is the number of assets, k is the number of factors, and m is the
+#' number of observations.
+#' @export
+statistical.factor.model <- function(R, k=1, ...){
+ if(!is.xts(R)){
+ R <- try(as.xts(R))
+ if(inherits(R, "try-error")) stop("R must be an xts object or coercible to an xts object")
+ }
+ # dimensions of R
+ m <- nrow(R)
+ N <- ncol(R)
+
+ # checks for R
+ if(m < N) stop("fewer observations than assets")
+ x <- coredata(R)
+
+ # Make sure k is an integer
+ if(k <= 0) stop("k must be a positive integer")
+ k <- as.integer(k)
+
+ # Fit a statistical factor model using Principal Component Analysis (PCA)
+ fit <- prcomp(x, ...=...)
+
+ # Extract the betas
+ # (N x k)
+ betas <- fit$rotation[, 1:k]
+
+ # Compute the estimated factor realizations
+ # (m x N) %*% (N x k) = (m x k)
+ f <- x %*% betas
+
+ # Compute the residuals
+ # These can be computed manually or by fitting a linear model
+ tmp <- x - f %*% t(betas)
+ b0 <- colMeans(tmp)
+ res <- tmp - matrix(rep(b0, m), ncol=N, byrow=TRUE)
+
+ # Compute residuals via fitting a linear model
+ # tmp.model <- lm(x ~ f)
+ # tmp.beta <- coef(tmp.model)[2:(k+1),]
+ # tmp.resid <- resid(tmp.model)
+ # all.equal(t(tmp.beta), betas, check.attributes=FALSE)
+ # all.equal(res, tmp.resid)
+
+ # structure and return
+ # stfm = *st*atistical *f*actor *m*odel
+ structure(list(factor_loadings=betas,
+ factor_realizations=f,
+ residuals=res,
+ m=m,
+ k=k,
+ N=N),
+ class="stfm")
+}
+
+
+#' Center
+#'
+#' Center a matrix
+#'
+#' This function is used primarily to center a time series of asset returns or
+#' factors. Each column should represent the returns of an asset or factor
+#' realizations. The expected value is taken as the sample mean.
+#'
+#' x.centered = x - mean(x)
+#'
+#' @param x matrix
+#' @return matrix of centered data
+#' @export
+center <- function(x){
+ if(!is.matrix(x)) stop("x must be a matrix")
+ n <- nrow(x)
+ p <- ncol(x)
+ meanx <- colMeans(x)
+ x.centered <- x - matrix(rep(meanx, n), ncol=p, byrow=TRUE)
+ x.centered
+}
+
+##### Single Factor Model Comoments #####
+
+#' Covariance Matrix Estimate
+#'
+#' Estimate covariance matrix using a single factor statistical factor model
+#'
+#' @details
+#' This function estimates an (N x N) covariance matrix from a single factor
+#' statistical factor model with k=1 factors, where N is the number of assets.
+#'
+#' @param beta vector of length N or (N x 1) matrix of factor loadings
+#' (i.e. the betas) from a single factor statistical factor model
+#' @param stockM2 vector of length N of the variance (2nd moment) of the
+#' model residuals (i.e. idiosyncratic variance of the stock)
+#' @param factorM2 scalar value of the 2nd moment of the factor realizations
+#' from a single factor statistical factor model
+#' @return (N x N) covariance matrix
+covarianceSF <- function(beta, stockM2, factorM2){
+ # Beta of the stock with the factor index
+ beta = as.numeric(beta)
+
+ N = length(beta)
+
+ # Idiosyncratic variance of the stock
+ stockM2 = as.numeric(stockM2)
+
+ if(length(stockM2) != N) stop("dimensions do not match for beta and stockM2")
+
+ # Variance of the factor
+ factorM2 = as.numeric(factorM2)
+
+ # Coerce beta to a matrix
+ beta = matrix(beta, ncol = 1)
+
+ # Compute estimate
+ # S = (beta %*% t(beta)) * factorM2
+ S = tcrossprod(beta) * factorM2
+ D = diag(stockM2)
+ return(S + D)
+}
+
+#' Coskewness Matrix Estimate
+#'
+#' Estimate coskewness matrix using a single factor statistical factor model
+#'
+#' @details
+#' This function estimates an (N x N^2) coskewness matrix from a single factor
+#' statistical factor model with k=1 factors, where N is the number of assets.
+#'
+#' @param beta vector of length N or (N x 1) matrix of factor loadings
+#' (i.e. the betas) from a single factor statistical factor model
+#' @param stockM3 vector of length N of the 3rd moment of the model residuals
+#' @param factorM3 scalar of the 3rd moment of the factor realizations from a
+#' single factor statistical factor model
+#' @return (N x N^2) coskewness matrix
+coskewnessSF <- function(beta, stockM3, factorM3){
+ # Beta of the stock with the factor index
+ beta = as.numeric(beta)
+ N = length(beta)
+
+ # Idiosyncratic third moment of the stock
+ stockM3 = as.numeric(stockM3)
+
+ if(length(stockM3) != N) stop("dimensions do not match for beta and stockM3")
+
+ # Third moment of the factor
+ factorM3 = as.numeric(factorM3)
+
+ # Coerce beta to a matrix
+ beta = matrix(beta, ncol = 1)
+
+ # Compute estimate
+ # S = ((beta %*% t(beta)) %x% t(beta)) * factorM3
+ S = (tcrossprod(beta) %x% t(beta)) * factorM3
+ D = matrix(0, nrow=N, ncol=N^2)
+ for(i in 1:N){
+ col = (i - 1) * N + i
+ D[i, col] = stockM3[i]
+ }
+ return(S + D)
+}
+
+#' Cokurtosis Matrix Estimate
+#'
+#' Estimate cokurtosis matrix using a single factor statistical factor model
+#'
+#' @details
+#' This function estimates an (N x N^3) cokurtosis matrix from a statistical
+#' factor model with k factors, where N is the number of assets.
+#'
+#' @param beta vector of length N or (N x 1) matrix of factor loadings
+#' (i.e. the betas) from a single factor statistical factor model
+#' @param stockM2 vector of length N of the 2nd moment of the model residuals
+#' @param stockM4 vector of length N of the 4th moment of the model residuals
+#' @param factorM2 scalar of the 2nd moment of the factor realizations from a
+#' single factor statistical factor model
+#' @param factorM4 scalar of the 4th moment of the factor realizations from a
+#' single factor statistical factor model
+#' @return (N x N^3) cokurtosis matrix
+cokurtosisSF <- function(beta, stockM2, stockM4, factorM2, factorM4){
+ # Beta of the stock with the factor index
+ beta = as.numeric(beta)
+ N = length(beta)
+
+ # Idiosyncratic second moment of the stock
+ stockM2 = as.numeric(stockM2)
+
+ if(length(stockM2) != N) stop("dimensions do not match for beta and stockM2")
+
+ # Idiosyncratic fourth moment of the stock
+ stockM4 = as.numeric(stockM4)
+
+ if(length(stockM4) != N) stop("dimensions do not match for beta and stockM4")
+
+ # Second moment of the factor
+ factorM2 = as.numeric(factorM2)
+
+ # Fourth moment of the factor
+ factorM4 = as.numeric(factorM4)
+
+ # Compute estimate
+ # S = ((beta %*% t(beta)) %x% t(beta) %x% t(beta)) * factorM4
+ S = (tcrossprod(beta) %x% t(beta) %x% t(beta)) * factorM4
+ D = .residualcokurtosisSF(NN=N, sstockM2=stockM2, sstockM4=stockM4, mfactorM2=factorM2, bbeta=beta)
+ return(S + D)
+}
+
+# Wrapper function to compute the residual cokurtosis matrix of a statistical
+# factor model with k = 1.
+# Note that this function was orignally written in C++ (using Rcpp) by
+# Joshua Ulrich and re-written using the C API by Ross Bennett
+.residualcokurtosisSF <- function(NN, sstockM2, sstockM4, mfactorM2, bbeta){
+ # NN : integer
+ # sstockM2 : vector of length NN
+ # sstockM4 : vector of length NN
+ # mfactorM2 : double
+ # bbeta : vector of length NN
+
+ # Should I add checks here? These are passed from cokurtosisSF which already has checks
+ .Call('residualcokurtosisSF', NN, sstockM2, sstockM4, bbeta, PACKAGE="PortfolioAnalytics")
+}
+
+##### Multiple Factor Model Comoments #####
+
+#' Covariance Matrix Estimate
+#'
+#' Estimate covariance matrix using a statistical factor model
+#'
+#' @details
+#' This function estimates an (N x N) covariance matrix from a statistical
+#' factor model with k factors, where N is the number of assets.
+#'
+#' @param beta (N x k) matrix of factor loadings (i.e. the betas) from a
+#' statistical factor model
+#' @param stockM2 vector of length N of the variance (2nd moment) of the
+#' model residuals (i.e. idiosyncratic variance of the stock)
+#' @param factorM2 (k x k) matrix of the covariance (2nd moment) of the factor
+#' realizations from a statistical factor model
+#' @return (N x N) covariance matrix
+covarianceMF <- function(beta, stockM2, factorM2){
+ # Formula for covariance matrix estimate
+ # Sigma = beta %*% factorM2 %*% beta**T + Delta
+ # Delta is a diagonal matrix with the 2nd moment of residuals on the diagonal
+
+ # N = number of assets
+ # k = number of factors
+
+ # Use the dimensions of beta for checks of stockM2 and factorM2
+ # beta should be an (N x k) matrix
+ if(!is.matrix(beta)) stop("beta must be a matrix")
+ N <- nrow(beta)
+ k <- ncol(beta)
+
+ # stockM2 should be a vector of length N
+ stockM2 <- as.numeric(stockM2)
+ if(length(stockM2) != N) stop("dimensions do not match for beta and stockM2")
+
+ # factorM2 should be a (k x k) matrix
+ if(!is.matrix(factorM2)) stop("factorM2 must be a matrix")
+ if((nrow(factorM2) != k) | (ncol(factorM2) != k)){
+ stop("dimensions do not match for beta and factorM2")
+ }
+
+ # Compute covariance matrix
+ S <- beta %*% tcrossprod(factorM2, beta)
+ D <- diag(stockM2)
+ return(S + D)
+}
+
+#' Coskewness Matrix Estimate
+#'
+#' Estimate coskewness matrix using a statistical factor model
+#'
+#' @details
+#' This function estimates an (N x N^2) coskewness matrix from a statistical
+#' factor model with k factors, where N is the number of assets.
+#'
+#' @param beta (N x k) matrix of factor loadings (i.e. the betas) from a
+#' statistical factor model
+#' @param stockM3 vector of length N of the 3rd moment of the model residuals
+#' @param factorM3 (k x k^2) matrix of the 3rd moment of the factor
+#' realizations from a statistical factor model
+#' @return (N x N^2) coskewness matrix
+coskewnessMF <- function(beta, stockM3, factorM3){
+ # Formula for coskewness matrix estimate
+ # Phi = beta %*% factorM3 %*% (beta**T %x% beta**T) + Omega
+ # %x% is the kronecker product
+ # Omega is the (N x N^2) matrix matrix of zeros except for the i,j elements
+ # where j = (i - 1) * N + i, which is corresponding to the expected third
+ # moment of the idiosyncratic factors
+
+ # N = number of assets
+ # k = number of factors
+
+ # Use the dimensions of beta for checks of stockM2 and factorM2
+ # beta should be an (N x k) matrix
+ if(!is.matrix(beta)) stop("beta must be a matrix")
+ N <- nrow(beta)
+ k <- ncol(beta)
+
+ # stockM3 should be a vector of length N
+ stockM3 <- as.numeric(stockM3)
+ if(length(stockM3) != N) stop("dimensions do not match for beta and stockM3")
+
+ # factorM3 should be an (k x k^2) matrix
+ if(!is.matrix(factorM3)) stop("factorM3 must be a matrix")
+ if((nrow(factorM3) != k) | (ncol(factorM3) != k^2)){
+ stop("dimensions do not match for beta and factorM3")
+ }
+
+ # Compute coskewness matrix
+ beta.t <- t(beta)
+ S <- (beta %*% factorM3) %*% (beta.t %x% beta.t)
+ D <- matrix(0, nrow=N, ncol=N^2)
+ for(i in 1:N){
+ col <- (i - 1) * N + i
+ D[i, col] <- stockM3[i]
+ }
+ return(S + D)
+}
+
+#' Cokurtosis Matrix Estimate
+#'
+#' Estimate cokurtosis matrix using a statistical factor model
+#'
+#' @details
+#' This function estimates an (N x N^3) cokurtosis matrix from a statistical
+#' factor model with k factors, where N is the number of assets.
+#'
+#' @param beta (N x k) matrix of factor loadings (i.e. the betas) from a
+#' statistical factor model
+#' @param stockM2 vector of length N of the 2nd moment of the model residuals
+#' @param stockM4 vector of length N of the 4th moment of the model residuals
+#' @param factorM2 (k x k) matrix of the 2nd moment of the factor
+#' realizations from a statistical factor model
+#' @param factorM4 (k x k^3) matrix of the 4th moment of the factor
+#' realizations from a statistical factor model
+#' @return (N x N^3) cokurtosis matrix
+cokurtosisMF <- function(beta , stockM2 , stockM4 , factorM2 , factorM4){
+
+ # Formula for cokurtosis matrix estimate
+ # Psi = beta %*% factorM4 %*% (beta**T %x% beta**T %x% beta**T) + Y
+ # %x% is the kronecker product
+ # Y is the residual matrix.
+ # see Asset allocation with higher order moments and factor models for
+ # definition of Y
+
+ # N = number of assets
+ # k = number of factors
+
+ # Use the dimensions of beta for checks of stockM2 and factorM2
+ # beta should be an (N x k) matrix
+ if(!is.matrix(beta)) stop("beta must be a matrix")
+ N <- nrow(beta)
+ k <- ncol(beta)
+
+ # stockM2 should be a vector of length N
+ stockM2 <- as.numeric(stockM2)
+ if(length(stockM2) != N) stop("dimensions do not match for beta and stockM2")
+
+ # stockM4 should be a vector of length N
+ stockM4 <- as.numeric(stockM4)
+ if(length(stockM4) != N) stop("dimensions do not match for beta and stockM4")
+
+ # factorM2 should be a (k x k) matrix
+ if(!is.matrix(factorM2)) stop("factorM2 must be a matrix")
+ if((nrow(factorM2) != k) | (ncol(factorM2) != k)){
+ stop("dimensions do not match for beta and factorM2")
+ }
+
+ # factorM4 should be a (k x k^3) matrix
+ if(!is.matrix(factorM4)) stop("factorM4 must be a matrix")
+ if((nrow(factorM4) != k) | (ncol(factorM4) != k^3)){
+ stop("dimensions do not match for beta and factorM4")
+ }
+
+ # Compute cokurtosis matrix
+ beta.t <- t(beta)
+ S <- (beta %*% factorM4) %*% (beta.t %x% beta.t %x% beta.t)
+ # betacov
+ betacov <- as.numeric(beta %*% tcrossprod(factorM2, beta))
+ # Compute the residual cokurtosis matrix
+ D <- .residualcokurtosisMF(NN=N, sstockM2=stockM2, sstockM4=stockM4, bbetacov=betacov)
+ return( S + D )
+}
+
+# Wrapper function to compute the residual cokurtosis matrix of a statistical
+# factor model with k > 1.
+# Note that this function was orignally written in C++ (using Rcpp) by
+# Joshua Ulrich and re-written using the C API by Ross Bennett
+.residualcokurtosisMF <- function(NN, sstockM2, sstockM4, bbetacov){
+ # NN : integer, number of assets
+ # sstockM2 : numeric vector of length NN
+ # ssstockM4 : numeric vector of length NN
+ # bbetacov : numeric vector of length NN * NN
+
+ # Should I add checks here? These are passed from cokurtosisSF which already has checks
+ .Call('residualcokurtosisMF', NN, sstockM2, sstockM4, bbetacov, PACKAGE="PortfolioAnalytics")
+}
+
+##### Extract Moments #####
+
+#' Covariance Estimate
+#'
+#' Extract the covariance matrix estimate from a statistical factor model
+#'
+#' @param model statistical factor model estimated via
+#' \code{\link{statistical.factor.model}}
+#' @param \dots not currently used
+#' @return covariance matrix estimate
+#' @seealso \code{\link{statistical.factor.model}}
+#' @author Ross Bennett
+#' @export
+extractCovariance <- function(model, ...){
+ if(!inherits(model, "stfm")) stop("model must be of class 'stfm'")
+
+ # Extract elements from the model
+ beta <- model$factor_loadings
+ f <- model$factor_realizations
+ res <- model$residuals
+ m <- model$m
+ k <- model$k
+ N <- model$N
+
+ # Residual moments
+ denom <- m - k - 1
+ stockM2 <- colSums(res^2) / denom
+
+ # Factor moments
+ factorM2 <- cov(f)
+
+ # Compute covariance estimate
+ if(k == 1){
+ out <- covarianceSF(beta, stockM2, factorM2)
+ } else if(k > 1){
+ out <- covarianceMF(beta, stockM2, factorM2)
+ } else {
+ # invalid k
+ message("invalid k, returning NULL")
+ out <- NULL
+ }
+ return(out)
+}
+
+#' Coskewness Estimate
+#'
+#' Extract the coskewness matrix estimate from a statistical factor model
+#'
+#' @param model statistical factor model estimated via
+#' \code{\link{statistical.factor.model}}
+#' @param \dots not currently used
+#' @return coskewness matrix estimate
+#' @seealso \code{\link{statistical.factor.model}}
+#' @author Ross Bennett
+#' @export
+extractCoskewness <- function(model, ...){
+ if(!inherits(model, "stfm")) stop("model must be of class 'stfm'")
+
+ # Extract elements from the model
+ beta <- model$factor_loadings
+ f <- model$factor_realizations
+ res <- model$residuals
+ m <- model$m
+ k <- model$k
+ N <- model$N
+
+ # Residual moments
+ denom <- m - k - 1
+ stockM3 <- colSums(res^3) / denom
+
+ # Factor moments
+ # f.centered <- center(f)
+ # factorM3 <- M3.MM(f.centered)
+ factorM3 <- PerformanceAnalytics:::M3.MM(f)
+
+ # Compute covariance estimate
+ if(k == 1){
+ # Single factor model
+ out <- coskewnessSF(beta, stockM3, factorM3)
+ } else if(k > 1){
+ # Multi-factor model
+ out <- coskewnessMF(beta, stockM3, factorM3)
+ } else {
+ # invalid k
+ message("invalid k, returning NULL")
+ out <- NULL
+ }
+ return(out)
+}
+
+#' Cokurtosis Estimate
+#'
+#' Extract the cokurtosis matrix estimate from a statistical factor model
+#'
+#' @param model statistical factor model estimated via
+#' \code{\link{statistical.factor.model}}
+#' @param \dots not currently used
+#' @return cokurtosis matrix estimate
+#' @seealso \code{\link{statistical.factor.model}}
+#' @author Ross Bennett
+#' @export
+extractCokurtosis <- function(model, ...){
+ if(!inherits(model, "stfm")) stop("model must be of class 'stfm'")
+
+ # Extract elements from the model
+ beta <- model$factor_loadings
+ f <- model$factor_realizations
+ res <- model$residuals
+ m <- model$m
+ k <- model$k
+ N <- model$N
+
+ # Residual moments
+ denom <- m - k - 1
+ stockM2 <- colSums(res^2) / denom
+ stockM4 <- colSums(res^4) / denom
+
+ # Factor moments
+ factorM2 <- cov(f)
+ # f.centered <- center(f)
+ # factorM4 <- M4.MM(f.centered)
+ factorM4 <- PerformanceAnalytics:::M4.MM(f)
+
+ # Compute covariance estimate
+ if(k == 1){
+ # Single factor model
+ out <- cokurtosisSF(beta, stockM2, stockM4, factorM2, factorM4)
+ } else if(k > 1){
+ # Multi-factor model
+ out <- cokurtosisMF(beta, stockM2, stockM4, factorM2, factorM4)
+ } else {
+ # invalid k
+ message("invalid k, returning NULL")
+ out <- NULL
+ }
+ return(out)
+}
+
Added: pkg/PortfolioAnalytics/man/center.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/center.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/center.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,24 @@
+\name{center}
+\alias{center}
+\title{Center}
+\usage{
+ center(x)
+}
+\arguments{
+ \item{x}{matrix}
+}
+\value{
+ matrix of centered data
+}
+\description{
+ Center a matrix
+}
+\details{
+ This function is used primarily to center a time series
+ of asset returns or factors. Each column should represent
+ the returns of an asset or factor realizations. The
+ expected value is taken as the sample mean.
+
+ x.centered = x - mean(x)
+}
+
Added: pkg/PortfolioAnalytics/man/cokurtosisMF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/cokurtosisMF.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/cokurtosisMF.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,35 @@
+\name{cokurtosisMF}
+\alias{cokurtosisMF}
+\title{Cokurtosis Matrix Estimate}
+\usage{
+ cokurtosisMF(beta, stockM2, stockM4, factorM2, factorM4)
+}
+\arguments{
+ \item{beta}{(N x k) matrix of factor loadings (i.e. the
+ betas) from a statistical factor model}
+
+ \item{stockM2}{vector of length N of the 2nd moment of
+ the model residuals}
+
+ \item{stockM4}{vector of length N of the 4th moment of
+ the model residuals}
+
+ \item{factorM2}{(k x k) matrix of the 2nd moment of the
+ factor realizations from a statistical factor model}
+
+ \item{factorM4}{(k x k^3) matrix of the 4th moment of the
+ factor realizations from a statistical factor model}
+}
+\value{
+ (N x N^3) cokurtosis matrix
+}
+\description{
+ Estimate cokurtosis matrix using a statistical factor
+ model
+}
+\details{
+ This function estimates an (N x N^3) cokurtosis matrix
+ from a statistical factor model with k factors, where N
+ is the number of assets.
+}
+
Added: pkg/PortfolioAnalytics/man/cokurtosisSF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/cokurtosisSF.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/cokurtosisSF.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,38 @@
+\name{cokurtosisSF}
+\alias{cokurtosisSF}
+\title{Cokurtosis Matrix Estimate}
+\usage{
+ cokurtosisSF(beta, stockM2, stockM4, factorM2, factorM4)
+}
+\arguments{
+ \item{beta}{vector of length N or (N x 1) matrix of
+ factor loadings (i.e. the betas) from a single factor
+ statistical factor model}
+
+ \item{stockM2}{vector of length N of the 2nd moment of
+ the model residuals}
+
+ \item{stockM4}{vector of length N of the 4th moment of
+ the model residuals}
+
+ \item{factorM2}{scalar of the 2nd moment of the factor
+ realizations from a single factor statistical factor
+ model}
+
+ \item{factorM4}{scalar of the 4th moment of the factor
+ realizations from a single factor statistical factor
+ model}
+}
+\value{
+ (N x N^3) cokurtosis matrix
+}
+\description{
+ Estimate cokurtosis matrix using a single factor
+ statistical factor model
+}
+\details{
+ This function estimates an (N x N^3) cokurtosis matrix
+ from a statistical factor model with k factors, where N
+ is the number of assets.
+}
+
Added: pkg/PortfolioAnalytics/man/coskewnessMF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/coskewnessMF.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/coskewnessMF.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,29 @@
+\name{coskewnessMF}
+\alias{coskewnessMF}
+\title{Coskewness Matrix Estimate}
+\usage{
+ coskewnessMF(beta, stockM3, factorM3)
+}
+\arguments{
+ \item{beta}{(N x k) matrix of factor loadings (i.e. the
+ betas) from a statistical factor model}
+
+ \item{stockM3}{vector of length N of the 3rd moment of
+ the model residuals}
+
+ \item{factorM3}{(k x k^2) matrix of the 3rd moment of the
+ factor realizations from a statistical factor model}
+}
+\value{
+ (N x N^2) coskewness matrix
+}
+\description{
+ Estimate coskewness matrix using a statistical factor
+ model
+}
+\details{
+ This function estimates an (N x N^2) coskewness matrix
+ from a statistical factor model with k factors, where N
+ is the number of assets.
+}
+
Added: pkg/PortfolioAnalytics/man/coskewnessSF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/coskewnessSF.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/coskewnessSF.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,31 @@
+\name{coskewnessSF}
+\alias{coskewnessSF}
+\title{Coskewness Matrix Estimate}
+\usage{
+ coskewnessSF(beta, stockM3, factorM3)
+}
+\arguments{
+ \item{beta}{vector of length N or (N x 1) matrix of
+ factor loadings (i.e. the betas) from a single factor
+ statistical factor model}
+
+ \item{stockM3}{vector of length N of the 3rd moment of
+ the model residuals}
+
+ \item{factorM3}{scalar of the 3rd moment of the factor
+ realizations from a single factor statistical factor
+ model}
+}
+\value{
+ (N x N^2) coskewness matrix
+}
+\description{
+ Estimate coskewness matrix using a single factor
+ statistical factor model
+}
+\details{
+ This function estimates an (N x N^2) coskewness matrix
+ from a single factor statistical factor model with k=1
+ factors, where N is the number of assets.
+}
+
Added: pkg/PortfolioAnalytics/man/covarianceMF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/covarianceMF.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/covarianceMF.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,31 @@
+\name{covarianceMF}
+\alias{covarianceMF}
+\title{Covariance Matrix Estimate}
+\usage{
+ covarianceMF(beta, stockM2, factorM2)
+}
+\arguments{
+ \item{beta}{(N x k) matrix of factor loadings (i.e. the
+ betas) from a statistical factor model}
+
+ \item{stockM2}{vector of length N of the variance (2nd
+ moment) of the model residuals (i.e. idiosyncratic
+ variance of the stock)}
+
+ \item{factorM2}{(k x k) matrix of the covariance (2nd
+ moment) of the factor realizations from a statistical
+ factor model}
+}
+\value{
+ (N x N) covariance matrix
+}
+\description{
+ Estimate covariance matrix using a statistical factor
+ model
+}
+\details{
+ This function estimates an (N x N) covariance matrix from
+ a statistical factor model with k factors, where N is the
+ number of assets.
+}
+
Added: pkg/PortfolioAnalytics/man/covarianceSF.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/covarianceSF.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/covarianceSF.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,32 @@
+\name{covarianceSF}
+\alias{covarianceSF}
+\title{Covariance Matrix Estimate}
+\usage{
+ covarianceSF(beta, stockM2, factorM2)
+}
+\arguments{
+ \item{beta}{vector of length N or (N x 1) matrix of
+ factor loadings (i.e. the betas) from a single factor
+ statistical factor model}
+
+ \item{stockM2}{vector of length N of the variance (2nd
+ moment) of the model residuals (i.e. idiosyncratic
+ variance of the stock)}
+
+ \item{factorM2}{scalar value of the 2nd moment of the
+ factor realizations from a single factor statistical
+ factor model}
+}
+\value{
+ (N x N) covariance matrix
+}
+\description{
+ Estimate covariance matrix using a single factor
+ statistical factor model
+}
+\details{
+ This function estimates an (N x N) covariance matrix from
+ a single factor statistical factor model with k=1
+ factors, where N is the number of assets.
+}
+
Added: pkg/PortfolioAnalytics/man/extract.cokurtosis.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extract.cokurtosis.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/extract.cokurtosis.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,26 @@
+\name{extract.cokurtosis}
+\alias{extract.cokurtosis}
+\title{Cokurtosis Estimate}
+\usage{
+ extract.cokurtosis(model, ...)
+}
+\arguments{
+ \item{model}{statistical factor model estimated via
+ \code{\link{statistical.factor.model}}}
+
+ \item{\dots}{not currently used}
+}
+\value{
+ cokurtosis matrix estimate
+}
+\description{
+ Extract the cokurtosis matrix estimate from a statistical
+ factor model
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{statistical.factor.model}}
+}
+
Added: pkg/PortfolioAnalytics/man/extract.coskewness.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extract.coskewness.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/extract.coskewness.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,26 @@
+\name{extract.coskewness}
+\alias{extract.coskewness}
+\title{Coskewness Estimate}
+\usage{
+ extract.coskewness(model, ...)
+}
+\arguments{
+ \item{model}{statistical factor model estimated via
+ \code{\link{statistical.factor.model}}}
+
+ \item{\dots}{not currently used}
+}
+\value{
+ coskewness matrix estimate
+}
+\description{
+ Extract the coskewness matrix estimate from a statistical
+ factor model
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{statistical.factor.model}}
+}
+
Added: pkg/PortfolioAnalytics/man/extract.covariance.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/extract.covariance.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/extract.covariance.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,26 @@
+\name{extract.covariance}
+\alias{extract.covariance}
+\title{Covariance Estimate}
+\usage{
+ extract.covariance(model, ...)
+}
+\arguments{
+ \item{model}{statistical factor model estimated via
+ \code{\link{statistical.factor.model}}}
+
+ \item{\dots}{not currently used}
+}
+\value{
+ covariance matrix estimate
+}
+\description{
+ Extract the covariance matrix estimate from a statistical
+ factor model
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{statistical.factor.model}}
+}
+
Added: pkg/PortfolioAnalytics/man/statistical.factor.model.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/statistical.factor.model.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/statistical.factor.model.Rd 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,33 @@
+\name{statistical.factor.model}
+\alias{statistical.factor.model}
+\title{Statistical Factor Model}
+\usage{
+ statistical.factor.model(R, k = 1, ...)
+}
+\arguments{
+ \item{R}{xts of asset returns}
+
+ \item{k}{number of factors to use}
+
+ \item{\dots}{additional arguments passed to
+ \code{prcomp}}
+}
+\value{
+ #' \itemize{ \item{factor_loadings}{ N x k matrix of
+ factor loadings (i.e. betas)} \item{factor_realizations}{
+ m x k matrix of factor realizations} \item{residuals}{ m
+ x N matrix of model residuals representing idiosyncratic
+ risk factors} } Where N is the number of assets, k is the
+ number of factors, and m is the number of observations.
+}
+\description{
+ Fit a statistical factor model using Principal Component
+ Analysis (PCA)
+}
+\details{
+ The statistical factor model is fitted using
+ \code{prcomp}. The factor loadings, factor realizations,
+ and residuals are computed and returned given the number
+ of factors used for the model.
+}
+
Added: pkg/PortfolioAnalytics/src/residualcokurtosisMF.c
===================================================================
--- pkg/PortfolioAnalytics/src/residualcokurtosisMF.c (rev 0)
+++ pkg/PortfolioAnalytics/src/residualcokurtosisMF.c 2014-06-03 19:00:06 UTC (rev 3403)
@@ -0,0 +1,159 @@
+
+#include <R.h>
+#include <Rinternals.h>
+
+SEXP residualcokurtosisMF_C(SEXP NN, SEXP sstockM2, SEXP sstockM4, SEXP bbetacov){
+ /*
+ arguments
+ NN : integer, number of assets
+ sstockM2 : vector of length NN, 2nd moment of the model residuals
+ sstockM4 : vector of length NN, 4th moment of the model residuals
+ bbetacov : vector of length NN * NN, beta and factor covariance
+
+ Note that this function was orignally written in C++ (using Rcpp) by
+ Joshua Ulrich and re-written using the C API by Ross Bennett
+ */
+
+ // // declare pointers for the vectors
+ double *stockM2, *stockM4, *betacov;
+
+ // Do I need to protect these if they are function arguments?
+ // use REAL() to access the C array inside the numeric vector passed in from R
+ stockM2 = REAL(sstockM2);
+ stockM4 = REAL(sstockM4);
+ betacov = REAL(bbetacov);
+
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 3403
More information about the Returnanalytics-commits
mailing list