Skip to content

Commit

Permalink
version 0.2-0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Enos authored and gaborcsardi committed Jan 1, 2006
1 parent ead0eda commit dbe6aef
Show file tree
Hide file tree
Showing 31 changed files with 1,096 additions and 147 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
@@ -1,10 +1,11 @@
Package: portfolio
Title: Analysing equity portfolios
Version: 0.1-0
Date: 2005-11-16
Version: 0.2-0
Date: 2006-01-01
Author: Jeff Enos <jeff@kanecap.com> and David Kane <dave@kanecap.com>
Description: Classes for analysing and implementing portfolios.
Maintainer: Jeff Enos <jeff@kanecap.com>
Depends: R (>= 2.0.0), methods
Depends: R (>= 2.0.0), methods, graphics, grid, lattice
Suggests: MatchIt
License: GPL version 2 or later.
Packaged: Thu Nov 17 10:34:00 2005; enos
Packaged: Sun Jan 1 17:51:33 2006; enos
51 changes: 31 additions & 20 deletions NAMESPACE
@@ -1,7 +1,11 @@
export(weight
importFrom("graphics", plot)

export(
"weight"
)

exportClasses("portfolioBasic",
exportClasses(
"portfolioBasic",
"portfolio",
"objectHistory",
"exposure",
Expand All @@ -14,24 +18,31 @@ exportClasses("portfolioBasic",
)

exportMethods(
show,
summary,
mean,

initialize,
create,
calcWeights,
calcShares,
scaleWeights,

balance,
exposure,
performance,
contribution,

add,
"show",
"summary",
"mean",
"plot",


"initialize",
"create",
"calcWeights",
"calcShares",
"scaleWeights",

"balance",
"exposure",
"performance",
"contribution",

"add",

"portfolioDiff",
"securityInfo",

"matching",

"+"

portfolioDiff,
securityInfo
)

2 changes: 2 additions & 0 deletions R/AllClasses.R
Expand Up @@ -41,6 +41,7 @@ setClass("portfolioBasic",

type = "character",
size = "characterOrNumeric",
weight.range = "numeric",
sides = "character",

## Weights storage. Contains data frame with
Expand All @@ -67,6 +68,7 @@ setClass("portfolioBasic",

type = "equal",
size = "quintile",
weight.range = c(0, Inf),
sides = c("long","short"),

weights = data.frame()
Expand Down
3 changes: 3 additions & 0 deletions R/AllGenerics.R
Expand Up @@ -44,6 +44,9 @@ if(!isGeneric("mvLong"))
if(!isGeneric("mvShort"))
setGeneric("mvShort", function(object) standardGeneric("mvShort"))

if(!isGeneric("matching"))
setGeneric("matching", function(object, covariates, ...) standardGeneric("matching"))

## Class portfolioHistory

if(!isGeneric("add"))
Expand Down
22 changes: 16 additions & 6 deletions R/contribution.R
Expand Up @@ -9,7 +9,6 @@
setMethod("show",
signature(object = "contribution"),
function(object){
cat(paste("An object of class \"", class(object), "\"\n", sep = ""))
for(v in names(object@data)){
cat(v, "\n")
show(object@data[[v]])
Expand All @@ -21,11 +20,22 @@ setMethod("show",
setMethod("summary",
signature(object = "contribution"),
function(object){
for(v in names(object@data)){
cat(v, "\n")
show(object@data[[v]])
cat("\n")
}
show(object)
}
)

setMethod("plot",
signature(x = "contribution", y = "missing"),
function(x, ...){

tlist <- list()
for(var in names(x@data)){
title <- paste("roic by", var)
data <- x@data[[var]]
data$variable <- factor(data$variable, levels = rev(unique(data$variable)))
tlist[[var]] <- barchart(variable ~ roic, data = data, origin = 0, main = title)
}

portfolio:::.trellis.multiplot(tlist)
}
)
41 changes: 30 additions & 11 deletions R/contributionHistory.R
Expand Up @@ -17,20 +17,16 @@ setMethod("show",
names(x@data)
}))), collapse = " "),
"\n"))


## Don't show data for now.

## cat("Data:\n\n")

## for(v in names(object@data)){
## cat(v, "\n")
## show(object@data[[v]])
## cat("\n")
## }
}
)

setMethod("summary",
signature(object = "contributionHistory"),
function(object){
cat("Mean contribution:\n")
show(mean(object))
}
)

setMethod("mean",
signature(x = "contributionHistory"),
Expand All @@ -57,3 +53,26 @@ setMethod("mean",
}
}
)

setMethod("plot",
signature(x = "contributionHistory", y = "missing"),
function(x){

tlist <- list()
for(var in unique(unlist(lapply(x@data, function(x){ names(x@data) })))){
title <- paste("roic by", var)
data <- do.call(rbind, lapply(x@data, function(x){ x@data[[var]] }))

if("rank" %in% names(data)){
data$variable <- data$rank
}

## Display by variable value alphabetically.

data$variable <- factor(data$variable, levels = rev(sort(unique(as.character(data$variable)))))
tlist[[var]] <- bwplot(variable ~ roic, data = data, main = title)
}

portfolio:::.trellis.multiplot(tlist)
}
)
23 changes: 17 additions & 6 deletions R/exposure.R
Expand Up @@ -9,8 +9,6 @@
setMethod("show",
signature(object = "exposure"),
function(object){
cat(paste("An object of class \"", class(object), "\"\n", sep = ""))

for(v in names(object@data)){
cat(v, "\n")
show(object@data[[v]])
Expand All @@ -23,10 +21,23 @@ setMethod("show",
setMethod("summary",
signature(object = "exposure"),
function(object){
for(v in names(object@data)){
cat(v, "\n")
show(object@data[[v]])
cat("\n")
show(object)
}
)

setMethod("plot",
signature(x = "exposure", y = "missing"),
function(x, ...){

tlist <- list()
for(var in names(x@data)){
title <- paste(var, "exposure")
data <- x@data[[var]]
data$variable <- factor(data$variable, levels = rev(unique(data$variable)))
tlist[[var]] <- barchart(variable ~ exposure, data = data, origin = 0, main = title)
}

portfolio:::.trellis.multiplot(tlist)
}

)
34 changes: 26 additions & 8 deletions R/exposureHistory.R
Expand Up @@ -18,15 +18,14 @@ setMethod("show",
}))), collapse = " "),
"\n"))

## Don't show data for now.

## cat("Data:\n\n")
}
)

## for(v in names(object@data)){
## cat(v, "\n")
## show(object@data[[v]])
## cat("\n")
## }
setMethod("summary",
signature(object = "exposureHistory"),
function(object){
cat("Mean exposure:\n")
show(mean(object))
}
)

Expand Down Expand Up @@ -58,3 +57,22 @@ setMethod("mean",

}
)

setMethod("plot",
signature(x = "exposureHistory", y = "missing"),
function(x){

tlist <- list()
for(var in unique(unlist(lapply(x@data, function(x){ names(x@data) })))){
title <- paste(var, "exposure")
data <- do.call(rbind, lapply(x@data, function(x){ x@data[[var]] }))

## Display by variable value alphabetically.

data$variable <- factor(data$variable, levels = rev(sort(unique(as.character(data$variable)))))
tlist[[var]] <- bwplot(variable ~ exposure, data = data, main = title)
}

portfolio:::.trellis.multiplot(tlist)
}
)
47 changes: 34 additions & 13 deletions R/performance.R
Expand Up @@ -9,18 +9,6 @@
setMethod("show",
signature(object = "performance"),
function(object){
cat(paste("An object of class \"", class(object), "\"\n", sep = ""))
if(length(object@ret) > 0)
cat(paste("ret: ", round(object@ret, 4), "\n"))
cat("ret.detail (at most 10 records):\n")
show(object@ret.detail[1:min(10, nrow(object@ret.detail)),])
}
)

setMethod("summary",
signature(object = "performance"),
function(object){
cat("Performance summary:\n\n")
if(length(object@ret) > 0){

ret <- object@ret
Expand All @@ -44,7 +32,40 @@ setMethod("summary",
}
}
else{
cat("No data.\n")
cat("Object of class performance with no return data.\n")
}
}
)

setMethod("summary",
signature(object = "performance"),
function(object){
show(object)
}
)

setMethod("plot",
signature(x = "performance", y = "missing"),
function(x){
ret <- x@ret
ret.tag <- ifelse(abs(ret) > 0.01, "%", "bps")
ret <- ifelse(ret.tag == "%", ret * 100, ret * 100 * 100)
ret <- round(ret, digits = 2)

if(nrow(x@ret.detail) > 0){
y <- x@ret.detail
y <- y[order(y$contrib, na.last = NA),]
y$id <- factor(y$id, levels = unique(y$id))
if(nrow(y) > 10){
y <- rbind(head(y, n = 5),
tail(y, n = 5))
}

print(barchart(id ~ contrib, data = y, origin = 0, main = paste("Performance: ", ret, ret.tag, sep = "")))

}
else{
stop("Nothing to plot")
}
}
)
29 changes: 29 additions & 0 deletions R/performanceHistory.R
Expand Up @@ -59,3 +59,32 @@ setMethod("summary",
cat("\n")
}
)

setMethod("plot",
signature(x = "performanceHistory", y = "missing"),
function(x){
p.ret <- data.frame(date = as.Date(names(x@data)),
ret = unlist(lapply(x@data, function(x) { x@ret })))
p.ret <- p.ret[order(p.ret$date),]

p.ret$date <- as.POSIXct(as.character(p.ret$date))

p.ret$nav <- cumprod(1 + p.ret$ret)
p.ret$tret <- p.ret$nav - 1

p.ret$ret <- p.ret$ret * 100
p.ret$tret <- p.ret$tret * 100

date.fmt.axis <- "%y-%m-%d"

main <- paste("Performance:",
paste(range(as.POSIXct(names(x@data))), collapse = " -- "))

## For now, plot all data points.

print(xyplot(tret ~ date, data = p.ret, type = "l",
scales = list(cex = 0.8, x = list(at = p.ret$date)),
main = main))

}
)

0 comments on commit dbe6aef

Please sign in to comment.