This tutorial will teach you about how to use R for portfolio analysis. We will be using various financial packages from R that will help us perform portfolio analysis. Let’s look at these packages:
Quantmod is a very powerful package that is designed for quant traders to explore and build quantitative trading models. For our work related to portfolio analysis, it will primarily be used to download relevant stock data, although it has further functionality for advanced techniques. It comes with both the xts and zoo packages, both of which are excellent programmes for manipulating time series data. The quantmod package can be used for three important things: 1) downloading data, 2) creating charts and 3) technical indicators and other functions.
The second package that is used is PortfolioAnalytics. This package is designed especially to optimise portfolios according to specific criteria, for example, Markowitz mean-variance portfolio optimisation. Using the PortfolioAnalytics package, you can get solutions and visualizations for portfolio problems with complex objectives and constraints. It allows you to specify a portfolio with assets, constraints and objectives that are solver agnostic. It supports various objective types such as: return, risk, risk budget, and weight concentration. It has solid functions for optimization problems. It can also be used for creating charts such as risk budgets and efficient frontier.
The PortfolioAnalytics package also comes with the PerformanceAnalytics package, which is useful for calculating portfolio returns. It is used for performance and risk analysis of financial instruments or portfolios. It includes various functions to calculate various metrics such as Conditional Value at Risk (CVAR), Standard Deviation, Expected Tail Loss (ETL), and Expected Shortfall (ES).
Finally, DerivMkts is used for the valuation of options and is specifically used in this tutorial to calculate implied volatility for portfolio optimisation. The package contains a set of pricing and expository functions that are useful in teaching financial derivatives.
Note that this is not an exhaustive list. There are many financial packages, however, for the purpose of portfolio analysis we will be using these packages.