## Task

Create a variance matrix given a matrix of returns. (There can be more assets than time points, and missing values are allowed.)

## Preparation

- return matrix
`BurStFin`

package

You need a return matrix — see “Prices to returns”. The columns must be for assets and the rows for times.

You also need the `BurStFin`

package available in the session:

require(BurStFin)

If that package is not installed on your machine, then you can get it from CRAN:

install.packages("BurStFin")

### Doing the example

`pprobeData`

package

To do the example, the `pprobeData`

package must be loaded in the session:

require(pprobeData)

If that package is not installed on your machine, see “Using R packages”.

## Doing it

We present a choice of:

- Ledoit-Wolf shrinkage estimation
- statistical factor model

### Ledoit-Wolf shrinkage

xaLWvar06 <- var.shrink.eqcor(xassetLogReturns[1:250,])

There are additional controls on the estimation if you are keen.

### Statistical factor model

xaSFvar06 <- factor.model.stat(xassetLogReturns[1:250,])

There are additional controls on the estimation if you are keen.

## Explanation

Each of `var.shrink.eqcor`

and `factor.model.stat`

from the `BurStFin`

package take a matrix of returns and return an estimate of the variance matrix that is positive definite even if there are more assets than times. (Having a positive-definite variance matrix is very important for optimization, it is probably not much of an issue when generating random portfolios.)

We extract the first 250 returns from the full return matrix — these are the daily returns from 2006.

## See also

To produce a return matrix, see “Prices to returns”.

Once you have a variance matrix, you might want to add a benchmark to it: “Add benchmark to variance matrix”

There have been blog posts on Ledoit-Wolf shrinkage and on statistical factor models.

## Navigate

- Back to “Data Basics”
- Back to the top level of “Portfolio Probe Cookbook”