Task
Compute the returns for a portfolio over one or more time periods, and compute the realized volatility.
Preparation
This presumes that you have or can create a portfolio object. For example, that you have mastered “Passive, no benchmark (minimum variance)”.
- portfolio object
- Portfolio Probe
You need a portfolio object with which to create valuations.
You also need to have the Portfolio Probe package loaded into your R session:
require(PortfolioProbe)
If you don’t have Portfolio Probe, see “Demo or Buy”.
Doing the example
opMinVar
object from the “Passive, no benchmark (minimum variance)” examplepprobeData
package
You need to have the package loaded into your R session:
require(pprobeData)
Doing it
We do these tasks:
- compute the returns for a portfolio object
- get the realized volatility of the portfolio
Compute the returns for a portfolio object
We give the valuation
function a price matrix that covers the period in which we are interested, and we specify which type of returns we want (the default is, of course, to return valuations rather than returns):
retOpMinVarDaily07 <- valuation(opMinVar, xassetPrices[251:502,], returns="log")
The prices given are the daily closing prices during 2007.
The result is a vector of the log return of the portfolio for each day:
> summary(retOpMinVarDaily07 * 100) Min. 1st Qu. Median Mean 3rd Qu. -2.533000 -0.331700 0.080790 0.007378 0.398000 Max. 2.908000
Get the realized volatility of the portfolio
Now that we have returns for the portfolio, we can compute its realized volatility over that time frame.
Volatility is merely the annualized standard deviation (and often expressed in percent):
volOpMinVarDaily07 <- sd(retOpMinVarDaily07) * sqrt(252) * 100
The result is:
> volOpMinVarDaily07 [1] 11.57053
One annual return
We want to get the simple return for the entire year of 2007 for our portfolio. The command to do that is:
sretOpMinVarAnn07 <- valuation(opMinVar, xassetPrices[c(251, 502),], returns="simple")
There are two small changes from the creation of retOpMinVarDaily07
above. The row subscript of the prices matrix changes from ‘251:502
‘ to ‘c(251, 502)
‘. The colon operator gives us all the integers from 251 to 502 while we get just the extremes with the second version. With this second version we get just one return.
The other change is of course that we are asking for simple returns instead of log returns.
The result is:
> sretOpMinVarAnn07 2007-12-31 0.01869028 attr(,"timestamp") [1] "Thu Sep 27 16:53:37 2012" attr(,"call") valuation.portfolBurSt(x = opMinVar, prices = xassetPrices[c(251, 502), ], returns = "simple")
This has some attributes that give information about the object, but the heart of it is just one number (about 1.87%).
Explanation
The valuation
function is multi-talented.
Further details
We have given the valuation
function an object that was the result of a call to trade.optimizer
. But we didn’t need to. We would get the same thing if we gave it a vector that described the portfolio — that is, a vector of the number of units of each asset where the names are the asset identifiers.
In particular, these commands give us the same answer:
retOpt <- valuation(opMinVar, xassetPrices[500:503,], returns="simple") retVec <- valuation(opMinVar$new.portfolio, xassetPrices[500:503,], returns="simple")
We compare the two return vectors by binding them into a matrix, and show the vector that we give to valuation
in the second instance:
> rbind(retOpt, retVec) 2007-12-28 2007-12-31 2008-01-02 retOpt 0.001850554 -0.006919563 -0.009832901 retVec 0.001850554 -0.006919563 -0.009832901 > opMinVar$new.portfolio XA105 XA280 XA298 XA643 XA675 XA709 XA731 XA778 2659 17920 2906 20192 7182 6485 4166 6840 XA891 XA966 5200 6247
See also
Navigate
- Back to “Optimize Trades”
- Back to the top level of “Portfolio Probe Cookbook”