Learn R Programming

stockPortfolio (version 1.2)

testPort: Test a portfolio on a data set

Description

Test a portfolio allocation on a new data set. This function is useful for comparing portfolios under different data scenarios.

Usage

testPort(theData, model = NULL, X = NULL, newestFirst = TRUE, isReturns = NULL)

Arguments

theData
The data set to be used. This may be an object of class "stockReturns", a vector of 1 plus the returns on each stock, or a matrix of stock returns where rows are ordered observations and columns represent individual stocks (see also argument newestFirst). The matrix may also be stock prices, in which case see argument isReturns.
model
An object of class "stockModel" or of class "optimalPortfolio". The allocation will be set as the optimal portfolio's allocation. To set a different allocation, leave this argument as NULL and use argument X.
X
The stock allocation of the portfolio, where element i corresponds to stock i in argument theData. If model is given, this argument is ignored.
newestFirst
If argument theData is a matrix of stock returns or stock prices, and the rows run from oldest (row 1) to most recent (last row), set newestFirst=FALSE.
isReturns
If argument theData is a matrix of stock prices and not stock returns, set this argument as FALSE.

Value

testPort outputs an object of class "testPort", which consists of the following items:
X
The allocation used.
sumRet
Summary of the returns for each stock.
change
The value of the portfolio if it started at 1.
returns
Return data, if provided.

Details

When the argument X is used or if theData is not from getReturns, provide column names to theData that correspond with the names of the elements of X. If theData is a vector of one plus the returns of each stock, then this vector should have its element names corresponding to those elements in X.

If theData is an object of class "stockReturns" or is a matrix of returns or prices, then this will allow the resulting object of class "testPort" to be plotted. See the examples for details.

See Also

getReturns, stockModel, optimalPort, portReturn

Examples

Run this code
#===> build two single index models <===#
data(stock99)
data(stock94Info)
non <- stockModel(stock99, drop=25, model='none', industry=stock94Info$industry)
sim <- stockModel(stock99, model='SIM', industry=stock94Info$industry, index=25)
ccm <- stockModel(stock99, drop=25, model='CCM', industry=stock94Info$industry)
mgm <- stockModel(stock99, drop=25, model='MGM', industry=stock94Info$industry)

#===> build optimal portfolios <===#
opNon <- optimalPort(non)
opSim <- optimalPort(sim)
opCcm <- optimalPort(ccm)
opMgm <- optimalPort(mgm)

#===> test portfolios on 2004-9 <===#
data(stock04)
tpEqu <- testPort(stock04[,-25], X=rep(1,24)/24)
tpNon <- testPort(stock04, opNon)
tpSim <- testPort(stock04, opSim)
tpCcm <- testPort(stock04, opCcm)
tpMgm <- testPort(stock04, opMgm)
print(tpEqu)
summary(tpEqu)

#===> compare performances <===#
plot(tpEqu, ylim=c(1, 3))
lines(tpNon, col=2, lty=2)
lines(tpSim, col=3, lty=3)
lines(tpCcm, col=4, lty=4)
# a sample of how to use points on an object of
# class "testPort", however, its use makes the
# plot somewhat ugly
points(tpMgm, col=5, lty=5, type='b')
legend('topleft', col=1:5, lty=1:5, legend=c('equal all.', 'none', 'SIM', 'CCM', 'MGM'), pch=c(NA, NA, NA, NA, 1))

Run the code above in your browser using DataLab