## Task

Create a set of scenarios based on historical price movements.

## Preparation

You need:

- a matrix of the historical prices — times (in order) in the rows and assets in the columns.
- 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

We create 100 scenarios of monthly price changes on the first 50 assets in the price matrix:

monScen <- pp.historyScenarios(xassetPrices[, 1:50], template=c(0,21), number=100)

The result is a three-dimensional array that is 2 (*times*) by 50 (*assets*) by 100 (*scenarios*):

> dim(monScen) [1] 2 50 100

## Explanation

The `template`

argument specifies the time relation of data that will go into the scenarios. In this example the first row of the first scenario will be some row of the price matrix and the second row of the first scenario will be the row of the price matrix that is 21 rows later.

`template`

normally starts with a zero and is strictly increasing.

The final task that `pp.historyScenarios`

performs is to adjust all of the scenarios so that they all correspond to a specific (current) price. If a `prices`

argument is not given, then it defaults to the last row of the price matrix. Thus the first row of all scenarios will be the same.

## Further details

#### time sequences

If your utility needs to estimate values over time (as the four moments utility does), then `template`

should be a sequence of numbers:

dqScen <- pp.historyScenarios(xassetPrices[, 1:50], template=0:62, number=100)

The result is a 63 by 50 by 100 array.

#### short histories

The `number`

argument says how many scenarios to generate based on a random selection of all the possible starting places. If you have a short history, that is going to be silly. You will want to generate one of each possible scenario. To do that use the `which`

argument instead of the `number`

argument:

shortHistory <- xassetPrices[1:36, 1:50] shortScen <- pp.historyScenarios(shortHistory, template=0:1, which=1:(nrow(shortHistory)-1))

The result is a 2 by 50 by 35 array. There are 35 possible scenarios and we get each one.

## See also

## Navigation

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