[R-SIG-Finance] fPortfolio and R/Rmetrics

Charles Ward cwrward at gmail.com
Sun Jun 7 22:34:04 CEST 2009


Could someone please suggest ways of improving the following two
functions for use in fPortfolio?

(fPortfolio is an extremely powerful and comprehensive package for
constructing and testing portfolios.See the Rmetrics ebook, entitled
'Portfolio Optimization with R/Rmetrics'.
http://www.rmetrics.org/ebook.htm)
The first function aims to replace the matrix of returns used in
running fPortfolio by a matrix of returns in which the mean of each
asset is equal to a forecast specifed by the user.  It works but is
incredibly slow, mainly I think because of the loops used.

 forec=function(x,forc)
{
newx=x
if (length(forc)!=ncol(x)) stop("Forecast columns do not match up with data")
cm=colMeans(x)
nc=ncol(x)
nr=nrow(x)
for (j in 1:nc){for (i in 1:nr)
newx[i,j]=x[i,j]-cm[j]+forc[j]}
newx
}

#Used as in this example:
library(fPortfolio)
lppAssets=100*LPP2005.RET[,c("SBI","SPI","LMI","MPI")]
forc=c(.1,.2,.1,.2)
newAss=forec(lppAssets,forc)
colMeans(lppAssets)
colMeans(newAss)

The second addresses a problem which is common in real estate asset
returns which arises because of the way in which the returns are
generated (through appraisals rather than through market prices). The
function is designed to "de-smooth" the appraisal-based returns. For
references to the problem see, the summary of a report for the
Investment Property Forum in the UK
https://members.ipf.org.uk/membersarealive/downloads/listings1.asp?pid=292

or google "desmoothing real estate returns"

The function requires a column of asset returns  and effectively
removes the AR(1) component.The alpha is the AR(1) coefficient but
users can enter a lower coefficient if preferred for reasons that are
discussed in the literature!
Again improvements would be welcome.e.g reading in a matrix rather
than a single column and specifying say a standard deviation rather
than the alpha.
Thank you for any comments
Charles Ward

desmooth<-function(x1,alpha=0,plot=TRUE,print=TRUE)
{
x1=ts(x1)
x2=x1
tab01=matrix(ncol=4,nrow=4)
rnd<-function(x) {x=round(100000*x)/100000;x}
r1=pacf(x1,plot=FALSE)
if (alpha==0) alpha=r1$ac[1]
for (i in 2:length(x1)) x2[i]=(x1[i]-alpha*x1[i-1])/(1-alpha)
if (plot==TRUE) {plot(x2,col="red",type="l");lines(x1,col="black");
	legend("topleft",c("Original","De-smoothed"), col=1:5,lty=1:1)}
if (print==TRUE) {
	tab01[1,]=c("", "Original","De-Smoothed","Alpha");
	tab01[2,1]="Mean";
	tab01[3,1]="Std. Dev.";
	tab01[2,2]=rnd(mean(x1));tab01[2,3]=rnd(mean(x2));
	tab01[3,2]=rnd(sd(x1));tab01[3,3]=rnd(sd(x2));
	tab01[2,4]="";	
	tab01[3,4]=rnd(alpha);
	dimnames(tab01)<-list(rep("",3),rep("",4));
	print(tab01,quote=FALSE)}
	x2
}

Used as in this example

library(fPortfolio)
PropInd=c(.0093,.0085,.009,.0098,.0084,.01294,.0136,.0165,.0235,.0150,.0178,.0131,
.0233,.0156,.0258,.026,.0284,.0261,.0230,.0235
 ,.0209,.0218,.0241,.0152,.0161)
DesPropInd=desmooth(PropInd)
#or to take a less Efficient Market line
DesPropInd=desmooth(PropInd,alpha=0.4)



More information about the R-SIG-Finance mailing list