## Task

Create a set of scenarios based on a statistical model.

## Preparation

You need:

- a vector of expected returns for the assets
- a variance matrix for the assets
- the
`pprobeSup`

package

The `pprobeSup`

package needs to be loaded into your R session:

require(pprobeSup)

If `pprobeSup`

is not installed on your machine, then you can get it with:

install.packages("pprobeSup", repos="http://www.portfolioprobe.com/R")

or alternatively with:

install.packages("pprobeSup", repos="http://www.portfolioprobe.com/R", type="source")

### Doing the example

You need to have the `pprobeData`

package loaded into your R session:

require(pprobeData)

This package may be installed from the same repository.

## Doing it

Here we take pretty much the simplest (and not especially useful) model — multivariate normal distribution of log returns.

### Preparation

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) [1] 20 50 100

## Explanation

The first argument is the vector of prices to use. The first row of each scenario will have these prices.

The `mvrnorm`

function from the `MASS`

package is used to create the random numbers which are then transformed into prices.

## Further details

More realistic distributions can — and probably should — be simulated.

## See also

## Navigation

- Back to “Scenario optimization”
- Back to “Optimize trades”
- Back to the top level of “Portfolio Probe Cookbook”