Create a set of scenarios based on a statistical model.
- a vector of expected returns for the assets
- a variance matrix for the assets
pprobeSup package needs to be loaded into your R session:
pprobeSup is not installed on your machine, then you can get it with:
or alternatively with:
install.packages("pprobeSup", repos="https://www.portfolioprobe.com/R", type="source")
Doing the example
You need to have the
pprobeData package loaded into your R session:
This package may be installed from the same repository.
Here we take pretty much the simplest (and not especially useful) model — multivariate normal distribution of log returns.
The multivariate normal requires a vector of mean values. We’ll just use zero for all the means in this example.
It also requires a variance matrix. When there are a lot more time points than assets, then the sample variance matrix is not terrible. That is what is used here:
varMat50 <- var(xassetLogReturns[1:300, 1:50])
Generating the scenarios
We create 100 scenarios of daily price changes on the first 50 assets in the price matrix for 20 days:
normScen <- pp.normalScenarios(xassetPrices[ nrow(xassetPrices),1:50], expected.return=rep(0,50), variance=varMat50, ntimes=20, nscenarios=100)
The result is a three-dimensional array that is 20 (times) by 50 (assets) by 100 (scenarios):
> dim(normScen)  20 50 100
The first argument is the vector of prices to use. The first row of each scenario will have these prices.
mvrnorm function from the
MASS package is used to create the random numbers which are then transformed into prices.
More realistic distributions can — and probably should — be simulated.
- Back to “Scenario optimization”
- Back to “Optimize trades”
- Back to the top level of “Portfolio Probe Cookbook”