Currently we compute portfolio risk and return via our own C# program. Historical data is stored in a SQL database. We want to compute the risk and return parameters - given a portfolio (i.e. not computing the efficient frontier). It's the R syntax that we're not familiar with (Vs the theory of computing the risk-return).

So, how would one go about computing portfolio risk-return in R?


There are a lot of code in Eric Zivots recent class in computational finance.

  1. http://spark-public.s3.amazonaws.com/compfinance/R%20code/portfolio.r
  2. http://spark-public.s3.amazonaws.com/compfinance/R%20code/testport.r
  3. http://spark-public.s3.amazonaws.com/compfinance/R%20code/rollingPortfolios.r

Also, you can google some slides in his class where he provides a lot of examples:


Sample Code:

Standard Deviation of Return series:

sd(x)  #where x = portfolio return series

Rolling Analysis

rollapplyr(x,days,function) #rolling analysis given function

Calculate Return

require(PerformanceAnalytics)    #heaps of functions for portfolio analytics
require(TTR)     #package with indicator functions
ROC(x,days)      #given equity series, get log return
ROC(x,days,type="discrete") #given equity series, get discrete return series
findDrawdowns(R) #find drawdown for time series
Return.annualized(R,n)   #R = return series, N = number of periods in year
  • $\begingroup$ Those are all for determining the efficient frontier (and then the min or max variance within it). I'm looking for the risk-return for a portfolio; given a portfolio (I.e. portfolio weights are fixed, assets are known as is the historical data behind the assets) $\endgroup$ Dec 29 '12 at 4:36
  • 1
    $\begingroup$ I am assuming you are defining risk in traditional metrics. Given weights and historical data, wouldn't a simple weighted return of each asset give the return series of the portfolio? From there wouldn't you just go about calculating the standard deviation and annualized return of the portfolio return series? In R, you just use "sd(return.series)" to get risk. and return just use PerformanceAnalytics functions to get return $\endgroup$ Dec 29 '12 at 14:56
  • $\begingroup$ Yup, it is simple in theory and I was expecting it to be simple in code too. I was actually expecting 3-4 lines of R code as the answer :) ... It's not the theory, it's the R syntax we're unfamiliar with (as well as integrating it with our C# based systems). But those more complex issue are questions for another day. Thanks and happy new year! $\endgroup$ Dec 29 '12 at 22:33

or http://blog.streeteye.com/blog/2012/01/portfolio-optimization-and-efficient-frontiers-in-r/

or via RMetrics: http://www.statistik.wiso.uni-erlangen.de/lehre/bachelor/datenanalyse/Refcard3.pdf

It cannot get that much easier. You would have found those yourself faster on google than the time it took to post your question here. Plus there are a dozen duplicate questions you could have gotten similar information

  • $\begingroup$ Thanks. Google did give several results but the overall options were quite ... diverse? So I thought I'd ask fellow folks here. $\endgroup$ Dec 29 '12 at 0:50
  • $\begingroup$ Actually, none of your answers answer the question. I'm looking for risk-return GIVEN a portfolio - not plotting an efficient frontier. The system won't let me undo my vote up $\endgroup$ Dec 29 '12 at 4:27
  • $\begingroup$ @Sid , then you did not carefully go through the links. Each single link I provided calculates, as the most basic building blocks, portfolio risk and portfolio returns. $\endgroup$
    – Matt
    Dec 29 '12 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.