[Returnanalytics-commits] r2343 - in pkg/FactorAnalytics: . R data man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 17 18:31:32 CEST 2013
Author: chenyian
Date: 2013-06-17 18:31:32 +0200 (Mon, 17 Jun 2013)
New Revision: 2343
Added:
pkg/FactorAnalytics/R/.Rhistory
pkg/FactorAnalytics/R/factorModelPerformanceAttribution.r
pkg/FactorAnalytics/R/plot.FM.attribution.r
pkg/FactorAnalytics/R/summary.FM.attribution.r
pkg/FactorAnalytics/data/.Rhistory
pkg/FactorAnalytics/man/factorModelPerformanceAttribution.Rd
pkg/FactorAnalytics/man/plot.FM.attribution.Rd
pkg/FactorAnalytics/man/plot.FundamentalFactorModel.Rd
pkg/FactorAnalytics/man/plot.MacroFactorModel.Rd
pkg/FactorAnalytics/man/plot.StatFactorModel.Rd
pkg/FactorAnalytics/man/print.MacroFactorModel.Rd
pkg/FactorAnalytics/man/print.StatFactorModel.Rd
pkg/FactorAnalytics/man/summary.FM.attribution.Rd
pkg/FactorAnalytics/man/summary.MacroFactorModel.Rd
Removed:
pkg/FactorAnalytics/FactorAnalytics old/
pkg/FactorAnalytics/tests/
Modified:
pkg/FactorAnalytics/
pkg/FactorAnalytics/DESCRIPTION
pkg/FactorAnalytics/NAMESPACE
pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
pkg/FactorAnalytics/R/fitMacroeconomicFactorModel.R
pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r
pkg/FactorAnalytics/R/plot.MacroFactorModel.r
pkg/FactorAnalytics/R/plot.StatFactorModel.r
pkg/FactorAnalytics/R/print.StatFactorModel.r
pkg/FactorAnalytics/man/CornishFisher.Rd
pkg/FactorAnalytics/man/factorModelCovariance.Rd
pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd
pkg/FactorAnalytics/man/fitMacroeconomicFactorModel.Rd
pkg/FactorAnalytics/man/fitStatisticalFactorModel.Rd
pkg/FactorAnalytics/man/modifiedPortfolioVaRDecomposition.Rd
pkg/FactorAnalytics/man/normalEsReport.Rd
pkg/FactorAnalytics/man/normalVaRReport.Rd
Log:
merge factorAnalyticsUW into factorAnalytics under returnanalytics/pkg/FactorAnalytics
Property changes on: pkg/FactorAnalytics
___________________________________________________________________
Added: svn:ignore
+ FactorAnalytics old
tests
Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION 2013-06-17 13:13:39 UTC (rev 2342)
+++ pkg/FactorAnalytics/DESCRIPTION 2013-06-17 16:31:32 UTC (rev 2343)
@@ -1,11 +1,11 @@
-Package: factorAnalytics
+Package: factorAnalyticsUW
Type: Package
Title: factor analysis
-Version: 1.0
+Version: 1.1
Date: 2011-07-22
Author: Eric Zivot and Yi-An Chen
Maintainer: Yi-An Chen <chenyian at uw.edu>
Description: An R package for estimation and risk analysis of linear factor models for asset returns and portfolios. It contains three major fitting method for the factor models: fitting macroeconomic factor model, fitting fundamental factor model and fitting statistical factor model and some risk analysis tools like VaR, ES to use the result of the fitting method. It also provides the different type of distribution to fit the fat-tail behavior of the financial returns, including edgeworth expansion type distribution.
License: GPL-2
-Depends: robust, robustbase, leaps, lars, zoo, MASS, PerformanceAnalytics, ff, sn, tseries
+Depends: robust, robustbase, leaps, lars, zoo, MASS, PerformanceAnalytics, ff, sn, tseries, strucchange
LazyLoad: yes
\ No newline at end of file
Modified: pkg/FactorAnalytics/NAMESPACE
===================================================================
--- pkg/FactorAnalytics/NAMESPACE 2013-06-17 13:13:39 UTC (rev 2342)
+++ pkg/FactorAnalytics/NAMESPACE 2013-06-17 16:31:32 UTC (rev 2343)
@@ -1,5 +1,7 @@
-export(chart.Style)
-export(chart.RollingStyle)
-export(table.RollingStyle)
-export(style.fit)
-export(style.QPfit)
\ No newline at end of file
+exportPattern("^[^\\.]")
+S3method(print, MacroFactorModel)
+S3method(print, StatFactorModel)
+S3method(plot, MacroFactorModel)
+S3method(plot, StatFactorModel)
+S3method(plot, FundamentalFactorModel)
+S3method(summary, MacroFactorModel)
\ No newline at end of file
Added: pkg/FactorAnalytics/R/.Rhistory
===================================================================
--- pkg/FactorAnalytics/R/.Rhistory (rev 0)
+++ pkg/FactorAnalytics/R/.Rhistory 2013-06-17 16:31:32 UTC (rev 2343)
@@ -0,0 +1,512 @@
+eigenvector <- eigen(cov(data))$vectors
+eigenvalues <- eigen(cov(data))$values
+abline(a=0,b=eigenvector[2,1]/eigenvector[1,1],col="red")
+abline(a=0,b=eigenvector[2,2]/eigenvector[1,2],col="red")
+covvar <- cbind(c(2,1),c(1,1))
+data <- rmvnorm(100,mean=c(0,0),sigma=covvar)
+eigenvector <- eigen(cov(data))$vectors
+eigenvector
+eigenvalues <- eigen(cov(data))$values
+eigenvalues
+plot(data)
+abline(a=0,b=eigenvector[2,1]/eigenvector[1,1],col="red")
+abline(a=0,b=eigenvector[2,2]/eigenvector[1,2],col="red")
+eigenvector <- eigen(cor(data))$vectors
+eigenvector
+eigenvalues <- eigen(cor(data))$values
+eigenvalues
+plot(data)
+abline(a=0,b=eigenvector[2,1]/eigenvector[1,1],col="red")
+abline(a=0,b=eigenvector[2,2]/eigenvector[1,2],col="red")
+covvar <- cbind(c(2,-1),c(-1,1))
+data <- rmvnorm(100,mean=c(0,0),sigma=covvar)
+eigenvector <- eigen(cor(data))$vectors
+eigenvector
+eigenvalues <- eigen(cor(data))$values
+eigenvalues
+plot(data)
+abline(a=0,b=eigenvector[2,1]/eigenvector[1,1],col="red")
+abline(a=0,b=eigenvector[2,2]/eigenvector[1,2],col="red")
+covvar <- cbind(c(0,-1),c(-1,0))
+data <- rmvnorm(100,mean=c(0,0),sigma=covvar)
+covvar <- cbind(c(0,1),c(1,0))
+data <- rmvnorm(100,mean=c(0,0),sigma=covvar)
+eigen(covvar)
+covvar <- cbind(c(0,1,0),c(1,0,1),c(0,1,0))
+covvar
+eigen(covvar)
+covvar <- cbind(c(1,1,0),c(1,1,1),c(0,1,1))
+covvar
+eigen(covvar)
+covvar <- cbind(c(2,1,0),c(1,1,1),c(0,1,1))
+covvar
+eigen(covvar)
+cor(covvar)
+covvar <- cbind(c(1,1,0),c(1,1,1),c(0,1,1))
+covvar
+eigen(covvar)
+cor(covvar)
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+p <- 0.9
+q <- 0.1
+r <- 0.8
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+is.positive.definite(chol)
+eigen(covvar)
+covvar
+chol <- cbind(c(2,p,q),c(0,1,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+is.positive.definite(chol)
+eigen(covvar)
+chol <- cbind(c(20,p,q),c(0,1,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+eigen(covvar)
+data <- rmvnorm(100,mean=c(0,0,0),sigma=covvar)
+install.packages("scatterplot3d")
+library(mvtnorm)
+library(scatterplot3d)
+? scatterplot3d
+scatterplot3d(data)
+trans3d(data)
+trans3d(data[,1],data[,2],data[,3])
+scatterplot3d(data,highlight.3d=TRUE, col.axis="blue",
+col.grid="lightblue", main="scatterplot3d - 1", pch=20)
+eigen(covvar)
+eigenvector <- eigen(cor(data))$vectors
+eigenvector
+eigenvector <- eigen(cov(data))$vectors
+eigenvector
+p <- 10
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+p <- 1
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,0,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+p <- .1
+chol <- cbind(c(1,p,q),c(0,0,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+p <- .1
+p <- .1
+q <- 0.1
+r <- 0.8
+chol <- cbind(c(1,p,q),c(0,0,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,1))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,0.7))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.65))
+covvar <- chol%*%t(chol)
+covvar
+is.positive.definite(chol)
+eigen(covvar)
+r <- 10
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.65))
+covvar <- chol%*%t(chol)
+covvar
+r <- 1
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.65))
+covvar <- chol%*%t(chol)
+covvar
+r <- 0.1
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.65))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.9))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.98))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.999))
+covvar <- chol%*%t(chol)
+covvar
+eigen(covvar)
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.997))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.9999))
+covvar <- chol%*%t(chol)
+covvar
+r <- 0.5
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.9999))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.8))
+covvar <- chol%*%t(chol)
+covvar
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.9))
+covvar <- chol%*%t(chol)
+covvar
+eigen(covvar)
+chol <- cbind(c(1,p,q),c(0,1,r),c(0,0,.9))
+data <- rmvnorm(100,mean=c(0,0,0),sigma=covvar)
+pc <- princomp(data)
+summary(pc)
+plot(pc)
+loadings(pc)
+eigen(cov(data))
+data <- rmvnorm(100,mean=c(1,0,0),sigma=covvar)
+eigen(cov(data))
+pc <- princomp(data)
+summary(pc)
+loadings(pc)
+data <- rmvnorm(100,mean=c(0,0,0),sigma=covvar)
+# download data small scale experiment
+# using the finance sector provided by CNN money
+library(quantmod)
+library(PerformanceAnalytics)
+symbol.vec=c("AAN","AB","ACAS","ACY","AFL","AIG","AMG","AXP","BAC","BGCP",
+"C","CCNE","DB","GS","HCC","IHC","JPM","KEY","PLFE","TCHC")
+getSymbols(symbol.vec, from ="2000-01-03", to = "2012-05-10")
+# extract monthly adjusted closing prices
+l <- length(symbol.vec)
+db.m.price <- to.monthly(AAN)[, "AAN.Adjusted", drop=FALSE]
+colnames(db.m.price) <- "AAN"
+db.m.ret <- CalculateReturns(db.m.price, method="compound")[-1,]
+for (i in (2:l)) {
+name.price <- paste(symbol.vec[i],"m","price",sep=".")
+stock <- as.name(symbol.vec[i])
+db.m.new <- to.monthly(eval(stock))[,"eval(stock).Adjusted",drop=FALSE]
+colnames(db.m.new) <- symbol.vec[i]
+db.m.price <- cbind(db.m.price,db.m.new)
+# calculate log-returns
+db.m.ret.new <- CalculateReturns(db.m.new, method="compound")[-1,]
+db.m.ret <- cbind(db.m.ret,db.m.ret.new)
+}
+head(db.m.price)
+dim(db.m.price)
+dim(db.m.ret)
+corr.m <- cor(db.m.ret)
+corr.m.inv <- solve(corr.m)
+db.pc <- princomp(db.m.ret)
+summary(db.pc)
+centrality <- loadings(db.pc)[,1]
+centrality
+eigen(corr.m.inv)
+centrality
+centrality.inv <- eigen(corr.m.inv)$vectors[,1]
+eigen(corr.m)$vectors[,1]
+cov.m.inv <- solve(cov(db.m.ret))
+centrality.inv <- eigen(cov.m.inv)$vectors[,1]
+centrality.inv
+eigen(cov(db.m.ret))$vectors[,1]
+centrality
+centrality.inv
+head(db.m.ret)
+names(centrality.inv) <- colnames(db.m.ret)
+centrality.inv
+covvar <- cbind(c(0.8,0.1,0.1),c(0.8,0.1,.1),c(.8,.1,.1))
+covvar
+covvar <- cbind(c(0.8,0.8,0.8),c(0.1,0.1,.1),c(.1,.1,.1))
+covvar
+eigen(covvar)
+covvar <- cbind(c(0.5,0.5,0.5),c(0.4,0.4,.4),c(.1,.1,.1))
+covvar
+eigen(covvar)
+sum(eigen(covvar)$vectors[,1]^2)
+eigen(covvar)$vectors[,1]^2
+sd(eigen(covvar)$vectors[,1])
+covvar <- cbind(replicate(3,c(.33,.33,.33))
+covvar <- cbind(replicate(3,c(.33,.33,.33)))
+covvar
+covvar <- cbind(replicate(3,c(.33,.33,.33)))
+covvar
+eigen(covvar)
+sd(eigen(covvar)$vectors[,1])
+covvar <- cbind(c(0.5,0.4,0.5),c(0.4,0.5,.4),c(.1,.1,.1))
+covvar
+eigen(covvar)
+covvar <- cbind(c(0.5,0.4,0.3),c(0.4,0.5,.6),c(.1,.1,.1))
+covvar
+eigen(covvar)
+sd(eigen(covvar)$vectors[,1])
+covvar <- cbind(c(0.5,0.4,0.3),c(0.4,0.5,.5),c(.1,.1,.2))
+covvar
+eigen(covvar)
+sd(eigen(covvar)$vectors[,1])
+covvar <- cbind(c(0,0.7,0.5),c(0.9,0,.5),c(.1,0.3,0))
+covvar
+eigen(covvar)
+eigen(t(covvar)
+eigen(t(covvar))
+eigen(t(covvar))
+covvar
+t(covvar)
+eigen(t(covvar))
+covvar <- cbind(c(0.1,0.7,0.5),c(0.8,0.1,.2),c(.1,0.2,0.3))
+covvar
+t(covvar)
+eigen(t(covvar))
+sd(eigen(covvar)$vectors[,1])
+eigen(covvar)
+t(covvar)
+eigen(t(covvar))
+covvar <- cbind(c(0.8,0.8,0.8),c(0.1,0.1,.1),c(.1,0.1,0.1))
+covvar
+eigen(covvar)
+t(covvar)
+eigen(t(covvar))
+sd(eigen(covvar)$vectors[,1])
+sd(eigen(t(covvar))$vectors[,1])
+t(covvar)
+library("rmgarch")
+? dcc
+? DCC.fit
+? dccfit
+eigen(diag(2))
+eigen(matrix(rep(1,4),nrow=2))
+eigen(matrix(c(1,-1,-1,1),nrow=2))
+covvar <- matrix(rep(1,9),nrow=3)
+n <- length(covvar[1,])
+alpha <- eigen(cov(covvar))$values[1] -10^(-3)
+solve(diag(n)-alpha*cov(covvar))%*%rep(1,n)
+alpha <- eigen(covvar)$values[1] -10^(-3)
+solve(diag(n)-alpha*cov(covvar))%*%rep(1,n)
+eigen(covvar)$values
+alpha <- eigen(covvar)$values[1] -10^(-3)
+solve(diag(n)-alpha*cov(covvar))%*%rep(1,n)
+kalz <- function(covvar) {
+n <- length(covvar[1,])
+alpha <- eigen(covvar)$values[1] -10^(-3)
+kalz.ec <- solve(diag(n)-alpha*cov(covvar))%*%rep(1,n)
+return(kalz.ec)
+}
+kalz(covvar)
+covvar2 <- matrix(rep(.1,9),nrow=3)
+kalz(covvar2)
+kalz <- function(covvar) {
+n <- length(covvar[1,])
+alpha <- eigen(covvar)$values[1] -10^(-1)
+kalz.ec <- solve(diag(n)-alpha*cov(covvar))%*%rep(1,n)
+return(kalz.ec)
+}
+# example of all 1 matrix
+covvar <- matrix(rep(1,9),nrow=3)
+kalz(covvar)
+# example of all .1 matrix
+covvar2 <- matrix(rep(.1,9),nrow=3)
+kalz(covvar2)
+kalz <- function(covvar) {
+n <- length(covvar[1,])
+alpha <- eigen(covvar)$values[1] -10^(-2)
+kalz.ec <- solve(diag(n)-alpha*cov(covvar))%*%rep(1,n)
+return(kalz.ec)
+}
+# example of all 1 matrix
+covvar <- matrix(rep(1,9),nrow=3)
+kalz(covvar)
+# example of all .1 matrix
+covvar2 <- matrix(rep(.1,9),nrow=3)
+kalz(covvar2)
+covvar3 <- eigen(matrix(c(1,0,0,0,1,0,0,0,1),nrow=3))
+covvar3
+covvar3 <- matrix(c(1,0,0,0,1,0,0,0,1),nrow=3)
+covvar3
+kalz(covvar3)
+covvar
+covvar2
+covvar2 <- matrix(c(1,0.1,0.1,0.1,1,0.1,0.1,0.1,1),nrow=3)
+kalz(covvar2)
+covvar2 <- matrix(c(1,0.1,0.1,0.1,1,0.1,0.1,0.1,1),nrow=3)
+kalz(covvar2)
+covvar2
+diag(covvar2) <- c(0,0,0)
+covvar2
+kalz(covvar2)
+matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3)
+covvar4 <- matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3)
+kalz(covvar4)
+covvar4
+kalz(matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3))
+matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3)
+kalz(matrix(c(1,1,.1,1,1,.1,.1,.1,1),nrow=3))
+kalz <- function(covvar) {
+n <- length(covvar[1,])
+alpha <- (eigen(covvar)$values[1])^(-1) -10^(-2)
+kalz.ec <- solve(diag(n)-alpha*cov(covvar))%*%rep(1,n)
+return(kalz.ec)
+}
+covvar <- matrix(rep(1,9),nrow=3)
+kalz(covvar)
+covvar2 <- matrix(c(1,0.1,0.1,0.1,1,0.1,0.1,0.1,1),nrow=3)
+kalz(covvar2)
+covvar3 <- matrix(c(1,0,0,0,1,0,0,0,1),nrow=3)
+kalz(covvar3)
+covvar4 <- matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3)
+kalz(covvar4)
+kalz(matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3))
+kalz(matrix(c(1,1,.1,1,1,.1,.1,.1,1),nrow=3))
+covvar4 <- matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3)
+kalz(covvar4)
+covvar4 <- matrix(c(1,1,-.9,1,1,-.9,-.9,-.9,1),nrow=3)
+kalz(covvar4)
+kalz(matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3))
+kalz(matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3))
+kalz(matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3))
+kalz(matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3))
+kalz(matrix(c(1,1,.1,1,1,.1,.1,.1,1),nrow=3))
+matrix(c(1,1,.1,1,1,.1,.1,.1,1),nrow=3)
+kalz(matrix(c(1,1,0,1,1,0,0,0,1),nrow=3)
+kalz(matrix(c(1,1,0,1,1,0,0,0,1),nrow=3))
+###########################################################
+kalz(matrix(c(1,1,0,1,1,0,0,0,1),nrow=3))
+matrix(c(1,1,0,1,1,0,0,0,1),nrow=3)
+###########################################################
+kalz(matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3))
+kalz(matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3))
+kalz(matrix(c(1,1,.5,1,1,.5,.5,.5,1),nrow=3))
+kalz(matrix(c(1,1,-.5,1,1,-.5,-.5,-.5,1),nrow=3))
+kalz(matrix(c(1,1,.9,1,1,.9,.9,.9,1),nrow=3))
+kalz(matrix(c(1,1,-.9,1,1,-.9,-.9,-.9,1),nrow=3))
+library(matrixcalc)
+install.packages("matrixcalc")
+is.positive.definite(matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3))
+is.positive.definite(matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3))
+library(matrixcalc)
+is.positive.definite(matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3))
+is.positive.definite(matrix(c(1,1,-.1,1,1,-.1,-.1,-.1,1),nrow=3))
+is.positive.definite(matrix(c(1,1,.1,1,1,.1,.1,.1,1),nrow=3))
+is.positive.definite(matrix(c(1,1,0,1,1,0,0,0,1),nrow=3))
+is.positive.definite(matrix(c(1,1,0,1,1,0,0,0,1),nrow=3))
+is.positive.definite(diag(3))
+is.positive.definite(matrix(c(1,1,-1,1,1,-1,-1,-1,1),nrow=3))
+library(mvtnorm)
+library(sna)
+library(matrixcalc)
+library(corpcor)
+chol <- cbind(c(1,0.1,0.1,0.1,0.1),c(0,0.99,0.1,0.1,0.2),c(0,0,0.98,0.4,0.5),
+c(0,0,0,0.9,0.5),c(0,0,0,0,0.7))
+covvar <- chol%*%t(chol)
+covvar
+is.positive.definite(covvar)
+eigen(covvar)
+eigen(solve(covvar))
+is.positive.definite(covvar)
+gplot(covvar,gmode="graph",edge.lwd=15,label=c(1,2,3,4,5))
+eigen(covvar)
+? gplot
+install.packages(c("JGR","Deducer","DeducerExtras"))
+library(JGR)
+JGR()
+install.packages("rJava")
+JPR()
+JGR()
+library(JGR)
+plot.lm
+library(leaps)
+library(PerformanceAnalytics)
+library(lars)
+library(robust)
+library(ellipse)
+library(MASS)
+#
+# fitMacroeconomicFactormodel
+#
+# load data from the database
+setwd("C:/Users/Yi-An Chen/Documents/R-project/factoranalytics/pkg/factorAnalytics/data")
+# data(managers.df)
+load("managers.df.rda")
+ret.assets = managers.df[,(1:6)]
+factors = managers.df[,(7:9)]
+# fit the factor model with OLS
+setwd("C:/Users/Yi-An Chen/Documents/R-project/factoranalytics/pkg/factorAnalytics/R")
+source("fitMacroeconomicFactorModel.r")
+source("factorModelCovariance.r")
+source("factorModelSdDecomposition.r")
+source("factorModelEsDecomposition.r")
+source("factorModelVaRDecomposition.r")
+fit.macro <- fitMacroeconomicFactorModel(ret.assets,factors,fit.method="OLS", factor.set = 3,
+variable.selection="all subsets",decay.factor = 0.95)
+source("factorModelPerformanceAttribution.r")
+fm.attr <- factorModelPerformanceAttribution(fit.macro)
+fm.attr[[1]]
+fm.attr[[2]]
+fm.attr[[3]]
+fit <- fitMacroeconomicFactorModel(ret.assets,factors,fit.method="OLS", factor.set = 3,
+variable.selection="all subsets",decay.factor = 0.95)
+fm.attr <- factorModelPerformanceAttribution(fit)
+fit$ret.assets
+factors
+benchmark = managers.df[,8]
+fit$ret.assets - benchmark
+fit = fitMacroeconomicFactorModel(port.ret,fit$factors)
+port.ret = fit$ret.assets - benchmark
+fit = fitMacroeconomicFactorModel(port.ret,fit$factors)
+fit$call
+fit <- fitMacroeconomicFactorModel(ret.assets,factors,fit.method="OLS", factor.set = 3,
+variable.selection="all subsets",decay.factor = 0.95)
+fit$call
+eval(fit$call)
+fit$call
+ret.assets = fit$ret.assets - benchmark
+fit.1 = eval(fit$call)
+eval(fit$call)
+setwd("C:/Users/Yi-An Chen/Documents/R-project/factoranalytics/pkg/factorAnalytics/data")
+# data(managers.df)
+load("managers.df.rda")
+ret.assets = managers.df[,(1:6)]
+factors = managers.df[,(7:9)]
+# fit the factor model with OLS
+setwd("C:/Users/Yi-An Chen/Documents/R-project/factoranalytics/pkg/factorAnalytics/R")
+source("fitMacroeconomicFactorModel.r")
+source("factorModelCovariance.r")
+source("factorModelSdDecomposition.r")
+source("factorModelEsDecomposition.r")
+source("factorModelVaRDecomposition.r")
+fit <- fitMacroeconomicFactorModel(ret.assets,factors,fit.method="OLS", factor.set = 3,
+variable.selection="all subsets",decay.factor = 0.95)
+source("factorModelPerformanceAttribution.r")
+fm.attr <- factorModelPerformanceAttribution(fit)
+fm.attr[[1]]
+source("factorModelPerformanceAttribution.r")
+fm.attr <- factorModelPerformanceAttribution(fit)
+source("factorModelPerformanceAttribution.r")
+fm.attr <- factorModelPerformanceAttribution(fit)
+source("factorModelPerformanceAttribution.r")
+fm.attr <- factorModelPerformanceAttribution(fit)
+benchmark=NULL
+if(benchmark != NULL)
+}
+if(benchmark != NULL) {
+}
+benchmark != NULL
+benchmark[1] != NULL
+class(benchmark)
+as.logic(benchmark)
+? logic
+? as.numeric
+as.logical(benchmark)
+(as.logical(benchmark) != NULL)
+source("factorModelPerformanceAttribution.r")
+fm.attr <- factorModelPerformanceAttribution(fit)
+source("factorModelPerformanceAttribution.r")
+fm.attr <- factorModelPerformanceAttribution(fit)
+fm.attr[[1]]
+fm.attr[[2]]
+fm.attr[[3]]
+benchmark = managers.df[,8]
+fm.attr.b <- factorModelPerformanceAttribution(fit,benchmark=benchmark)
+fm.attr.b[[1]]
+fm.attr[[1]]
+fm.attr[[2]]
+source("plot.FM.attribution.r")
+plot(fm.attr,date="2006-12-30")
+plot(fm.attr.b,date="2006-12-30")
+source("summary.FM.attribution.r")
+summary(fm.attr)
+summary(fm.attr.b)
Added: pkg/FactorAnalytics/R/factorModelPerformanceAttribution.r
===================================================================
--- pkg/FactorAnalytics/R/factorModelPerformanceAttribution.r (rev 0)
+++ pkg/FactorAnalytics/R/factorModelPerformanceAttribution.r 2013-06-17 16:31:32 UTC (rev 2343)
@@ -0,0 +1,251 @@
+# performance attribution
+# Yi-An Chen
+# July 30, 2012
+
+factorModelPerformanceAttribution <-
+ function(fit,benchmark=NULL,...) {
+
+ # input
+ # fit : Class of MacroFactorModel, FundamentalFactorModel and statFactorModel
+ # benchmark: benchmark returns, default is NULL. If benchmark is provided, active returns
+ # is used.
+ # ... : controlled variables for fitMacroeconomicsFactorModel and fitStatisticalFactorModel
+ # output
+ # class of "FMattribution"
+ #
+ # plot.FMattribution
+ # summary.FMattribution
+ # print.FMattribution
+ require(zoo)
+
+ if (class(fit) !="MacroFactorModel" & class(fit) !="FundamentalFactorModel"
+ & class(fit) != "StatFactorModel")
+ {
+ stop("Class has to be MacroFactorModel.")
+ }
+
+ # MacroFactorModel chunk
+
+ if (class(fit) == "MacroFactorModel") {
+
+ # if benchmark is provided
+
+ if (!is.null(benchmark)) {
+ ret.assets = fit$ret.assets - benchmark
+ fit = fitMacroeconomicFactorModel(ret.assets=ret.assets,...)
+ }
+# return attributed to factors
+ cum.attr.ret <- fit$beta.mat
+ cum.spec.ret <- fit$alpha.vec
+ factorName = colnames(fit$beta.mat)
+ fundName = rownames(fit$beta.mat)
+
+ attr.list <- list()
+
+ for (k in fundName) {
+ fit.lm = fit$asset.fit[[k]]
+
+ ## extract information from lm object
+ date <- names(fitted(fit.lm))
+
+ actual.z = zoo(fit.lm$model[1], as.Date(date))
+
+
+# attributed returns
+# active portfolio management p.512 17A.9
+
+ cum.ret <- Return.cumulative(actual.z)
+ # setup initial value
+ attr.ret.z.all <- zoo(, as.Date(date))
+ for ( i in factorName ) {
+
+ if (fit$beta.mat[k,i]==0) {
+ cum.attr.ret[k,i] <- 0
+ attr.ret.z.all <- merge(attr.ret.z.all,zoo(rep(0,length(date)),as.Date(date)))
+ } else {
+ attr.ret.z <- actual.z - zoo(as.matrix(fit.lm$model[i])%*%as.matrix(fit.lm$coef[i]),
+ as.Date(date))
+ cum.attr.ret[k,i] <- cum.ret - Return.cumulative(actual.z-attr.ret.z)
+ attr.ret.z.all <- merge(attr.ret.z.all,attr.ret.z)
+ }
+
+ }
+
+ # specific returns
+ spec.ret.z <- actual.z - zoo(as.matrix(fit.lm$model[,-1])%*%as.matrix(fit.lm$coef[-1]),
+ as.Date(date))
+ cum.spec.ret[k] <- cum.ret - Return.cumulative(actual.z-spec.ret.z)
+ attr.list[[k]] <- merge(attr.ret.z.all,spec.ret.z)
+ colnames(attr.list[[k]]) <- c(factorName,"specific.returns")
+ }
+
+
+ }
+
+if (class(fit) =="FundamentalFactorModel" ) {
+ # if benchmark is provided
+
+ if (!is.null(benchmark)) {
+ stop("use fitFundamentalFactorModel instead")
+ }
+ # return attributed to factors
+ factor.returns <- fit$factor.rets[,-1]
+ factor.names <- names(fit$factor.rets[,-1])
+ dates <- as.character(unique(fit$exposure.data[,"DATE"]))
+ exposure <- fund.fit$exposure.data
+ ticker <- names(fit$resids)
+
+ N <- length(ticker)
+ J <- length(factor.names)
+ t <- length(dates)
+ # array arranges in N X J X t
+ # N is assets names, J is factors, t is time
+ attr.ret <- array(,dim=c(N,J,t),dimnames=list(ticker,factor.names,dates))
+ for (i in dates) {
+ idx = which(exposure[,"DATE"]==i)
+ for (j in factor.names) {
+ attr.ret[,j,i] <- exposure[idx,j]*coredata(factor.returns[as.Date(i)])[,j]
+ }
+ }
+
+ # specific returns
+ # zoo class
+ intercept <- fit$factor.rets[,1]
+ resids <- fit$resids
+ spec.ret.z <- resids + intercept
+
+ #cumulative return attributed to factors
+ cum.attr.ret <- matrix(,nrow=length(ticker),ncol=length(factor.names),
+ dimnames=list(ticker,factor.names))
+ cum.spec.ret <- rep(0,length(ticker))
+ names(cum.spec.ret) <- ticker
+
+ # arrange returns data
+ actual <- fund.fit$returns.data
+ # N <- length(assets.names)
+ # t <- length(dates)
+ # array arranges in N X t
+ # N is assets names, J is factors, t is time
+ actual.ret <- array(,dim=c(N,t),dimnames=list(ticker,dates))
+ for (i in dates) {
+ idx = which(actual[,"DATE"]==i)
+ actual.ret[,i] <- actual[idx,"RETURN"]
+ }
+
+ # make returns as zoo
+ actual.z.all <- zoo(,as.Date(dates))
+ for (k in ticker) {
+ actual.z <- zoo(actual.ret[k,],as.Date(dates))
+ actual.z.all <- merge(actual.z.all,actual.z)
+ }
+ colnames(actual.z.all) <- ticker
+
+
+
+ # make list of every asstes and every list contains return attributed to factors
+ # and specific returns
+ attr.list <- list()
+ for (k in ticker){
+ attr.ret.z.all <- zoo(,as.Date(dates))
+ # cumulative returns
+ cum.ret <- Return.cumulative(actual.z.all[,k])
+ for (j in factor.names) {
+ attr.ret.z <- zoo(attr.ret[k,j,],as.Date(dates) )
+ attr.ret.z.all <- merge(attr.ret.z.all,attr.ret.z)
+ cum.attr.ret[k,j] <- cum.ret - Return.cumulative(actual.z.all[,k]-attr.ret.z)
+ }
+ attr.list[[k]] <- merge(attr.ret.z.all,spec.ret.z[,k])
+ colnames(attr.list[[k]]) <- c(factor.names,"specific.returns")
+ cum.spec.ret[k] <- cum.ret - Return.cumulative(actual.z.all[,k]-spec.ret.z[,k])
+
+ }
+
+}
+
+ if (class(fit) == "StatFactorModel") {
+
+ # if benchmark is provided
+
+ if (!is.null(benchmark)) {
+ x = fit$asset.ret - benchmark
+ fit = fitStatisticalFactorModel(x=x,...)
+ }
+ # return attributed to factors
+ cum.attr.ret <- t(fit$loadings)
+ cum.spec.ret <- fit$r2
+ factorName = rownames(fit$loadings)
+ fundName = colnames(fit$loadings)
+
+ # create list for attribution
+ attr.list <- list()
+ # pca method
+
+ if ( dim(fit$asset.ret)[1] > dim(fit$asset.ret)[2] ) {
+
+
+ for (k in fundName) {
+ fit.lm = fit$asset.fit[[k]]
+
+ ## extract information from lm object
+ date <- names(fitted(fit.lm))
+ # probably needs more general Date setting
+ actual.z = zoo(fit.lm$model[1], as.Date(date))
+
+
+ # attributed returns
+ # active portfolio management p.512 17A.9
+
+ cum.ret <- Return.cumulative(actual.z)
+ # setup initial value
+ attr.ret.z.all <- zoo(, as.Date(date))
+ for ( i in factorName ) {
+
+ attr.ret.z <- actual.z - zoo(as.matrix(fit.lm$model[i])%*%as.matrix(fit.lm$coef[i]),
+ as.Date(date))
+ cum.attr.ret[k,i] <- cum.ret - Return.cumulative(actual.z-attr.ret.z)
+ attr.ret.z.all <- merge(attr.ret.z.all,attr.ret.z)
+
+
+ }
+
+ # specific returns
+ spec.ret.z <- actual.z - zoo(as.matrix(fit.lm$model[,-1])%*%as.matrix(fit.lm$coef[-1]),
+ as.Date(date))
+ cum.spec.ret[k] <- cum.ret - Return.cumulative(actual.z-spec.ret.z)
+ attr.list[[k]] <- merge(attr.ret.z.all,spec.ret.z)
+ colnames(attr.list[[k]]) <- c(factorName,"specific.returns")
+ }
+ } else {
+ # apca method
+# fit$loadings # f X K
+# fit$factors # T X f
+
+ dates <- rownames(fit$factors)
+ for ( k in fundName) {
+ attr.ret.z.all <- zoo(, as.Date(date))
+ actual.z <- zoo(fit$asset.ret[,k],as.Date(dates))
+ cum.ret <- Return.cumulative(actual.z)
+ for (i in factorName) {
+ attr.ret.z <- zoo(fit$factors[,i] * fit$loadings[i,k], as.Date(dates) )
+ attr.ret.z.all <- merge(attr.ret.z.all,attr.ret.z)
+ cum.attr.ret[k,i] <- cum.ret - Return.cumulative(actual.z-attr.ret.z)
+ }
+ spec.ret.z <- actual.z - zoo(fit$factors%*%fit$loadings[,k],as.Date(dates))
+ cum.spec.ret[k] <- cum.ret - Return.cumulative(actual.z-spec.ret.z)
+ attr.list[[k]] <- merge(attr.ret.z.all,spec.ret.z)
+ colnames(attr.list[[k]]) <- c(factorName,"specific.returns")
+ }
+
+
+ }
+
+ }
+
+
+
+ ans = list(cum.ret.attr.f=cum.attr.ret,
+ cum.spec.ret=cum.spec.ret,
+ attr.list=attr.list)
+class(ans) = "FM.attribution"
+return(ans)
+ }
\ No newline at end of file
Modified: pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitFundamentalFactorModel.R 2013-06-17 13:13:39 UTC (rev 2342)
+++ pkg/FactorAnalytics/R/fitFundamentalFactorModel.R 2013-06-17 16:31:32 UTC (rev 2343)
@@ -78,7 +78,7 @@
# [1] 42912 117
# dimnames(fulldata)
# PERMNO" "DATE" "RETURN" "TICKER.x" "BOOK2MARKET" "TICKER.y"
- # check if exposures are numeric, if not, create exposures. factors by dummy variables
+ # check if exposures are numeric, if not, create exposures. factors by dummy variables
which.numeric <- sapply(fulldata[, exposures, drop = FALSE],is.numeric)
exposures.numeric <- exposures[which.numeric]
# industry factor model
@@ -334,8 +334,8 @@
factor.rets = f.hat,
resids = E.hat,
tstats = tstats,
- returns.data = fulldata[,c(datevar, assetvar, returnsvar)],
- exposure.data = fulldata[,c(datevar, assetvar, exposures)],
+ returns.data = fulldata[,c(datevar, assetvar, returnsvar)],
+ exposure.data = fulldata[,c(datevar, assetvar, exposures)],
assets = assets,
tickers = tickers,
call = this.call)
Modified: pkg/FactorAnalytics/R/fitMacroeconomicFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitMacroeconomicFactorModel.R 2013-06-17 13:13:39 UTC (rev 2342)
+++ pkg/FactorAnalytics/R/fitMacroeconomicFactorModel.R 2013-06-17 16:31:32 UTC (rev 2343)
@@ -41,9 +41,8 @@
require(leaps)
require(lars)
require(robust)
- require(ellipse)
require(MASS)
-this.call <- match.call()
+ this.call <- match.call()
if (is.data.frame(ret.assets) & is.data.frame(factors) ) {
manager.names = colnames(ret.assets)
Added: pkg/FactorAnalytics/R/plot.FM.attribution.r
===================================================================
--- pkg/FactorAnalytics/R/plot.FM.attribution.r (rev 0)
+++ pkg/FactorAnalytics/R/plot.FM.attribution.r 2013-06-17 16:31:32 UTC (rev 2343)
@@ -0,0 +1,82 @@
+# plot.FM.attribution.r
+# Yi-An Chen
+# 8/1/2012
+
+plot.FM.attribution <- function(fm.attr, which.plot=c("none","1L","2L","3L"),max.show=6,
+ date,plot.single=FALSE,fundName,
+ which.plot.single=c("none","1L","2L","3L"),...) {
+ # ... for chart.TimeSeries
+ require(PerformanceAnalytics)
+ # plot single assets
+ if (plot.single==TRUE){
+
+ which.plot.single<-which.plot.single[1]
+
+ if (which.plot.single=="none")
+ which.plot.single<-menu(c("attributed cumulative returns",
+ paste("attributed returns","on",date,sep=" "),
+ "Time series of attributed returns"),
+ title="performance attribution plot \nMake a plot selection (or 0 to exit):\n")
+ switch(which.plot.single,
+ "1L" = {
+ bar <- c(fm.attr$cum.spec.ret[fundName],fm.attr$cum.ret.attr.f[fundName,])
+ names(bar)[1] <- "specific.returns"
+ barplot(bar,horiz=TRUE,main="cumulative attributed returns",las=1)
+ },
+ "2L" ={
+ bar <- coredata(fm.attr$attr.list[[fundName]][as.Date(date)])
+ barplot(bar,horiz=TRUE,main=fundName,las=1)
+ },
+ "3L" = {
+ chart.TimeSeries(fm.attr$attr.list[[fundName]],
+ main=paste("Time series of attributed returns of ",fundName,sep=""),... )
+ },
+ invisible())
+ }
+ # plot all assets
+ else {
+ which.plot<-which.plot[1]
+ fundnames <- rownames(fm.attr$cum.ret.attr.f)
+ n <- length(fundnames)
+
+ if(which.plot=='none')
+ which.plot<-menu(c("attributed cumulative returns",
+ paste("attributed returns","on",date,sep=" "),
+ "time series of attributed returns"),
+ title="performance attribution plot \nMake a plot selection (or 0 to exit):\n")
+ if (n >= max.show) {
+ cat(paste("numbers of assets are greater than",max.show,", show only first",
+ max.show,"assets",sep=" "))
+ n <- max.show
+ }
+ switch(which.plot,
+
+ "1L" = {
+ par(mfrow=c(2,n/2))
+ for (i in fundnames[1:n]) {
+ bar <- c(fm.attr$cum.spec.ret[i],fm.attr$cum.ret.attr.f[i,])
+ names(bar)[1] <- "specific.returns"
+ barplot(bar,horiz=TRUE,main=i,las=1)
+ }
+ par(mfrow=c(1,1))
+ },
+ "2L" ={
+ par(mfrow=c(2,n/2))
+ for (i in fundnames[1:n]) {
+ bar <- coredata(fm.attr$attr.list[[i]][as.Date(date)])
+ barplot(bar,horiz=TRUE,main=i,las=1)
+ }
+ par(mfrow=c(1,1))
+ },
+ "3L" = {
+ par(mfrow=c(2,n/2))
+ for (i in fundnames[1:n]) {
+ chart.TimeSeries(fm.attr$attr.list[[i]],main=i)
+ }
+ par(mfrow=c(1,1))
+ },
+ invisible()
+ )
+
+ }
+ }
\ No newline at end of file
Modified: pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r 2013-06-17 13:13:39 UTC (rev 2342)
+++ pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r 2013-06-17 16:31:32 UTC (rev 2343)
@@ -3,15 +3,11 @@
# 7/16/2012
plot.FundamentalFactorModel <-
-function(fund.fit,which.plot=c("none","1L","2L","3L","4L","5L","6L","7L"),max.show=12,
- plot.single=FALSE, fundId, fundName="TBA",
- which.plot.single=c("none","1L","2L","3L","4L","5L","6L","7L","8L",
- "9L","10L","11L","12L","13L") ) {
+function(fund.fit,which.plot=c("none","1L","2L","3L","4L"),max.show=12)
+ {
require(ellipse)
- if (plot.single==TRUE) {
-
- } else {
+
which.plot<-which.plot[1]
if(which.plot=='none')
@@ -51,7 +47,7 @@
invisible()
)
- }
+
}
Modified: pkg/FactorAnalytics/R/plot.MacroFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/plot.MacroFactorModel.r 2013-06-17 13:13:39 UTC (rev 2342)
+++ pkg/FactorAnalytics/R/plot.MacroFactorModel.r 2013-06-17 16:31:32 UTC (rev 2343)
@@ -1,7 +1,7 @@
plot.MacroFactorModel <-
function(fit.macro,colorset=c(1:12),legend.loc=NULL,
which.plot=c("none","1L","2L","3L","4L","5L","6L","7L"),max.show=6,
- plot.single=FALSE, fundId, fundName="TBA",which.plot.single=c("none","1L","2L","3L","4L","5L","6L",
+ plot.single=FALSE, fundName,which.plot.single=c("none","1L","2L","3L","4L","5L","6L",
"7L","8L","9L","10L","11L","12L","13L")) {
require(zoo)
require(PerformanceAnalytics)
@@ -11,9 +11,8 @@
## inputs:
## fit.macro lm object summarizing factor model fit. It is assumed that
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 2343
More information about the Returnanalytics-commits
mailing list