Weekly maintenance every saturday 03:00 - 06:00 UTC

# Maximize Sharpe ratio.

There is a lot of ways to optimize the stock portfolio, see for example the references. I will describe here the process of optimizing your portfolio by maximizing its Sharpe ratio with the help of the Asset master website for portfolio optimization and it can be used for free.
The Sharpe ratio can be though of as a measure of return to a risk, or as the measure of the portfolio "effectiveness".

If you are not interested in underlying math, you can skip to the Steps section.

$Sharpe\;Ratio = \frac{R_p - R_f}{\sigma_p}$

where

$R_p$ is the expected excess return of portfolio

$R_f$ is a risk-free rate

$\sigma_p$ is the standard deviation of the portfolio return

Return of the portfolio for holding period can be calculated as

$R = \frac{V_f - V_i}{V_i}$

where

$V_f$ = final value, including dividends and interest

$V_{i}$ = initial value

and the excess return formula is $R_p = R - 1$

Expected return of the asset for a long time period in most cases calculated as the geometric mean of the asset return for a small time periods.

$\overline{R} = \left(\prod _{i=1}^{N}a_{i}\right)^{\frac {1}{N}}={\sqrt[{N}]{a_{1}a_{2}\cdots a_{N}}}$

or, equivalently, as the arithmetic mean in logscale:

$\overline{R} = \exp {\left({{\frac {1}{N}}\sum \limits _{i=1}^{N}\ln a_{i}}\right)}$

where $a_i$ are the asset returns for each period, $N$ is a number of periods.

The variance is the expectation of the squared deviation of a random variable from its population mean or sample mean. It can be calculated as follows:

The formula for biased variance is:

\begin{aligned}\sigma ^{2}&={\frac {1}{N}}\sum _{i=1}^{N}\left(a_{i}-\mu \right)^{2}={\frac {1}{N}}\sum _{i=1}^{N}\left(a_{i}^{2}-2\mu a_{i}+\mu ^{2}\right)\\[5pt]&=\left({\frac {1}{N}}\sum _{i=1}^{N}a_{i}^{2}\right)-2\mu \left({\frac {1}{N}}\sum _{i=1}^{N}a_{i}\right)+\mu ^{2}\\[5pt]&=\left({\frac {1}{N}}\sum _{i=1}^{N}a_{i}^{2}\right)-\mu ^{2}\end{aligned}

where the population mean is

${\displaystyle \mu ={\frac {1}{N}}\sum _{i=1}^{N}a_{i}.}$

The formula for unbiased variance is:

${\displaystyle \sigma ^{2}={\frac {1}{N - 1}}\sum _{i=1}^{N}\left(a_{i}-\mu \right)^{2}. }$

And finally, the standard deviation of the asset (portfolio) return is the square root of the variance.

$\sigma_p = \sqrt{{\sigma}^2}$

It can be seen from these equation that maximizing the Sharpe ratio you could get a portfolio with high expected return and low risk.

### Steps:

- Open the   Asset master website webpage on you PC. (Note that the website isn't working on the mobile, so please use PC or notebook instead.)

- Register on the website (registration is required for portfolio optimization) and download the example data.

- Unzip the archive with downloaded data. Install the LibreOffice if you don't have it installed already.

- Open the file "tickers.csv" with the help of LibreOffice Calc. Click button "OK" in the prompt window to agree with the settings for opening the file.

- Add stock symbols you would like to see in your portfolio to the column of symbols. For example, you can add symbols "MSFT" and "AAPL".

- Delete the rows with the symbols you wouldn't like to be in your portfolio.

- To save your file with symbols choose menu "File" -> "Save As..." on the top left.

- Go to the Optimize page. Click on the button "Read settings..." to load the portfolio settings from a preconfigured settings file maximize_sharpe.json (these are in the folder portfolio_settings/mean_variance_optimization of the unzipped archive), then set the desired number of years, number of months for the time horizon, and choose the tickers.csv file from the uploaded earlier universe_data folder, then click the Optimize button. Note that you should edit the ticker file to choose tickers you want include in your portfolio.

- Click button "Optimize" at the bottom of the page. Take a look at the log messages. while portfolio optimization will take place.

- After you see the message Now you can take a look at your optimization result at the log
window, click the Optimization result... button to view you optimization results.

Tips

Note that you can choose different time horizons for evaluating the expected return and volatility for stocks.

When looking at your optimization results page, take a look at the "Date From" field in the tables, they should be nearly the same for all stocks. In other case, the expected return and volatility for optimization can be estimated incorrectly, because the overall expected return for nearly all the stocks is changing with the time.

Try to use at least 10 years of history data while optimizing portfolio because there are quite big falls of stock prices nearly every 10 years. It is noted in the Economic Cycles theory (Juglar cycle). You can optimize portfolio with up to 150 stock symbols using Daily stock history data for free account type. Also try to use a small (some years maybe) time period of stock quote history, to check if the expected return/volatility for some stocks from the chosen stock set is not degraded recently.

### About the Covariance matrix shrinkage:

This is essential that the number of the stock price data for calculating the covariance matrix should be greater than the overall symbol count in your portfolio, because in the other case the covariance matrix will be singular (i.e. non-invertible) [9]. Even in the case when the number of the stock price data points is not bigger enough than the overall symbol count in your portfolio, you can get a poor estimate for the covariance matrix if you will not use the covariance matrix shrinkage [9], [10]. In that case it is recommended that you will use the shrinkage estimator for the covariance matrix calculation.

### About the stock data timeframe:

It should be clear from above, that the larger the number of the stock historical price samples, the larger stock count can be used to get the good estimation of the covariance matrix. For example if you are using 10 years of the history data with the monthly timeframe, you can get a descent estimation of the covariance matrix for about 10 stocks in your portfolio. For 10 years of stock data with the daily timeframe you can get a good covariance matrix estimation for about 100 stocks in your portfolio.

### The result of the example of Sharpe ratio optimization.

For example, given 396 stocks from S&P 500 which have about 20 years of history price quotes, the optimization of Sharpe ratio gives a quite impressive results:

expected return: 0.35
volatility: 0.218
Sharpe ratio: 1.511

Note that you can also minimize risk for a given target return or maximize return for a given target volatility, or even minimize volatility by choosing the appropriate value in the Choose optimization type for Mean - variance optimization selector.

When observing the optimization result, note that the count of the stock data sample is at least $10$ times larger than the count of the stock symbols, so the estimate of the covariance matrix should be good enough without the covariance matrix shrinkage.