1. Custom moment functions
As you learned in the previous video, there are several different methods and techniques for estimating moments.
2. Custom moment functions
While PortfolioAnalytics supports several methods for estimating moments, it is not feasible or possible to support all methods and models so the package supports custom moment functions. A custom moment function is any user defined function that is a valid R function. There are a few guidelines to follow for naming arguments and the type of object that must be returned. The function should accept an argument R for the asset returns, portfolio for the portfolio specification object. Any other named arguments can be used that are specific to estimating the moments. The function must return a named list where the elements of the list represent the moments. This is important because the optimization routines in PortfolioAnalytics expect the moment estimates represented in this format.
3. Example: custom moment function
In this example, you will define a custom moment function to estimate the variance-covariance matrix using a robust estimator. The MASS package has the cov-dot-rob function that supports the "mve" and "mcd" methods for a robust estimate of the variance-covariance matrix. "mve" stands for minimum volume ellipsoid and "mcd" stands for minimimum covariance determinant. Intuitively, this estimates the covariance of the "good" part of the data and lessens the impact of outliers. Following the guidelines in the previous slide, you define the function to accept R and portfolio arguments. Additionally, you also want the ability to specify the method for cov-dot-rob so you add a rob_method argument as well. You will note that the portfolio argument is not used in this custom moment function. In some cases, you may want to use information in the portfolio specification in your custom moment function so it is available if you wish to use it. This custom moment function only estimates the variance-covariance matrix, the second moment, so you return a named list with "sigma". You use the custom moment function in the optimization by specifying the name of your custom function for the momentFUN argument in optimize-dot-portfolio. Also, you can pass in the named argument, for example rob_method = "mcd", to specify the method used to estimate the variance-covariance matrix.
4. Let's practice!
Let's move on to exercises.