avatarMunesh Lakhey

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5662

Abstract

ecalculating, “%Change“ e.t.c</figcaption></figure><figure id="a6c3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*2ABRb2weIMM7juUvYYcplQ.png"><figcaption>Data frame containing returns on each assets</figcaption></figure><figure id="26b2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Mm1assFk5GDIeb0GRVqJuA.png"><figcaption>Covariance matrix</figcaption></figure><p id="dd5d">While standard deviation tells how much asset can fluctuate every day on average, VaR represents how much asset can fall a single day during a highly volatile period. VaR is expressed as a potential loss of asset (e.g. 4% of the total asset) with a certain probability (e.g. 2% chance or 98% confidence) within a certain time frame (e.g. daily/weekly). Being aware of VaR allows us to keep the right-sized asset or the right mix of assets such that we can avoid unexpected and huge losses. The image below shows that return could be as worse as or worse than -1.64 standard deviation 1 out of 20 trading days (5% probability).</p><figure id="65cd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nNOW-hjVOkglUw19x0Wn7g.png"><figcaption>Value at Risk lies at negative side of distribution. Events 1.64 SD away from the center occur with frequency of 5% in average assuming normal distribution.</figcaption></figure><p id="7f99">It is obvious that we would want a portfolio and with minimum VaR as possible for any given return. Next, we will find such a portfolio with an optimal combination of weights that has a minimum variance for a specific return. Alternatively, portfolio optimization will help us to maximize our return for any selected risk level.</p><figure id="8fd2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*6mLTxhiBndsU4LzemmEiPQ.png"><figcaption></figcaption></figure><p id="b764"><b><i>Example: For some random positions(weights) in 4 stocks [0.08, 0.2, 0.58, 0.14] calculated portfolio variance is 0.005, and standard deviation is 0.022 or 2.2% daily std. Assuming a normal distribution of returns, the worst 5% outcomes lie -1.64 SD away from the center. That is returns can be worse than -0.0367 or -3.67% (-1.645*0.022) in 1/20 trading days (5% chance). If we have 100000 invested, our Var at 5% probability is 3670. In other words, we tend to lose $3670 (or more) once in every 20 trading days.</i></b></p><p id="821a"><b>Part 2. Portfolio Optimization</b></p><p id="7c0e">We will gradually discover the portfolio we want in few steps. We shall start with <b>completely random allocation</b> (i.e. use random weights). Some of these random weight portfolios have better <b>risk-return trade-off</b>, hence are <b>dominant portfolios</b>. Those lying most upper/outer in the risk-return plot make the <b>efficient frontier</b>. Next, we shall find the <b>Market portfolio</b> by comparing the Sharpe ratio for each random portfolio. Needless to say, the Market portfolio lies on the efficient frontier and has the highest/maximum Sharpe ratio. The line tangent to this point and passing through risk-free asset is the <b>Capital Market Line</b>. The slope and intercept to this line are Sharpe ratio and risk-free rate respectively. <b><i>Ideally, all investors select their portfolio somewhere on this line depending upon their risk/return preferences.</i></b></p><p id="22c0">A. Random allocation of weights: We will visualize the effect of random allocation weights. 1000s of portfolios are taken with random weights, respective mean and variance are calculated (and scattered for visualization). Some combination weight has better risk-return characteristics and therefore are dominating/desirable portfolios.</p><p id="6eb2">Portfolio return:</p><figure id="6dfb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QNMzfpBjjJw9WandOq0H3Q.png"><figcaption></figcaption></figure><figure id="c02c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*7Shu8GY-KzPWvYddfrhvkg.png"><figcaption></figcaption></figure><p id="727b">Portfolio risk:</p><figure id="d905"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*0cdPD92HDYMJgWiNsDF1rg.png"><figcaption></figcaption></figure><p id="a177">B. Market portfolio: Among all possible combinations of dominating portfolios in the efficient frontier, one which gives the highest return for taking additional risk beyond a risk-free rate is designated as a Market portfolio. we can calculate the Sharpe ratio for all portfolios given a risk-free rate and the winning portfolio with the highest Sharpe ratio is the Market Portfolio. All the rational investors gravitate towards to this best performing portfolio. This model of combining risk-free assets with the Market portfolio is known as the Capital asset pricing model (CAPM). All portfolios in CML is optimally compensated for risk above the risk-free rate. Mathematically CAPM is expressed as:</p><figure id="d2b3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Rrc_NqkRoCw78JV-HPSAqw.png"><figcaption>Rp -portfolio return after combining risk free asset, Rm -market return , Rf -Risk free rate. Sigma_m -standard deviation of market returns, Sigma_p -standard deviation of portfolio returns. Note risk for Risk free asset is 0</figcaption></figure><p id="70b3">Rearranging, we have Sharpe ratio for combined portfolio :</p><figure id="e2d0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*4dGE5Z7GffRIl2RmoeCaxQ.png"><figcaption>Sharpe ratio for combined portfolio is equal to market risk premium divided by market portfolio risk.</figcaption></figure><p id="2fff">Using Rm and Sigma_m calculated from s

Options

tock return and covariance Sharpe ratio for each random portfolio is calculated, one with the highest Sharpe ratio is the market/optimal portfolio. C. Capital Market Line (CML): Capital Market line represents all combinations of the risk-free asset and the optimal market portfolio. It’s easy to see that the slope of this line is the Sharpe ratio itself and the intercept is a risk-free rate. Given this information, CML can be readily drawn as seen below.</p><figure id="5fbe"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*b4X97eXS1NAD5utU5TZhoQ.png"><figcaption>Using Sharpe Ratio (SR) and risk free rate (Rf), portfolio return (Y) for any portfolio risk (X) can be calculated.</figcaption></figure><figure id="a9ca"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*O_Z9Wr_bJoE8V9rfKD8DxA.png"><figcaption>CML in green passes trough optimal market portfolio (red dot) and risk free rate. This line is tangent to the efficient frontier.</figcaption></figure><p id="c922">Code for constructing random portfolios, finding a market portfolio, and drawing CML line.</p> <figure id="6127"> <div> <div>

            <iframe class="gist-iframe" src="/gist/LakheyM/7a1e99d36ff6be732d8a1ed219dfd139.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="4ad3"><b>Part 3. Solvers</b></p><p id="9083">Random initialization of weights can work for a portfolio with few assets but when assets grow large, the cost of computing and time it takes can be forbidding. However, solving for the best portfolio is an optimization problem with constraints. We can use Solvers from python either for getting single best return (or risk) or get the entire efficient frontier. These are solved as convex problems and the global optimum is always reached.</p><p id="90ff">The optimization objective is expressed as:</p><figure id="5b14"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*z8TWw_fzbFMmVB_25cWF7g.png"><figcaption></figcaption></figure><p id="2281">Here weights are represented by x and we are solving for optimal x. Objectives and constraints are written in matrix form. As a rule objective function is stated as a minimization problem. All the inequalities are similarly stated as lesser than some value.</p><p id="9208">Inputs for Solvers:</p><p id="7305">The algorithm solves for ‘x’ with the help of inputs</p><p id="d0b2">P or quadratic part (here it’s sigma or variance-covariance matrix, cov)</p><p id="1948">q or linear part of the objective function (here represented by a vector of returns r or ER_stocks, above)</p><p id="7527">G and h make the lower bound for weights i.e 0. We can input G as an identity matrix of size n x n (n: number of weights/stocks). Stated as matmul(G, x) &lt; h, hence h is a vector of 0s of size n. <b><i>while weights are non-negative, while solving they are stated as: -weights &lt; 0.</i></b></p><p id="6b6e">A and b make the equality part of the constraint. For our purpose the sum of all weights must equal 1. Hence A is a vector of 1s of size n. matmul(A, r_transpose) = 1, or b is simply 1.</p><p id="27ee">We can get the frontier and plot along with random portfolios:</p><figure id="e66e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Aagnzxn0d2eGVXqJbHRBNw.png"><figcaption>Efficient frontier plotted along with random portfolios and the Capital Market Line</figcaption></figure><p id="b482">Code to solve using Solvers.</p>
    <figure id="f994">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/LakheyM/63684b8910af5ede4064fc22af1a0d04.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="11c4"><b>Limitations and Conclusion</b></p><p id="855b">The biggest limitations of this model are the assumption of a normal distribution of returns and the validity of covariance. Actual returns are rarely normal and in fact, highly negative and positive returns are much more common giving rise to so-called fat-tailed distribution. As such the model underestimates the risks. Portfolio variance/risk is very sensitive to variance and covariance pairs i.e. the covariance matrix. Covariance rarely remains the same for a long time, hence finding a robust/valid covariance matrix is challenging.</p><p id="6a76">Despite some limitations, risk modeling with VaR is a very helpful and probably most used indicator. Portfolio optimization leads to the most efficient use of our resources and any allocation decisions should be based upon these considerations. Solvers can be used to quickly find the efficient frontier or market portfolio has given return series for each stock.</p><p id="8ad8">You can read the earlier post here:</p><div id="1918" class="link-block">
      <a href="https://readmedium.com/algorithmic-trading-simplified-b871b101cf66">
        <div>
          <div>
            <h2>Algorithmic trading simplified</h2>
            <div><h3>In this post, I will build and evaluate a trading plan which is the key step in any trading strategy, particularly…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*_s6qg0gzz0xaOyBCQxYFdg.png)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Algorithmic trading simplified: Value at Risk and Portfolio Optimization

Photo by Markus Spiske (left) and Jamie Street (right) on Unsplash

In the last post, we saw how actual algorithms are developed and tested. In this post, we will figure out the level of exposure so that can always protect our investments from significant downturns. And find the best performing portfolio given several assets.

Why quantify the risk?

To invest or trade with confidence. Knowing Value at Risk (VaR) helps us to gauge the potential loss of equity, both in scale and frequency. Not managing this volatility can lead to significant (and avoidable) losses.

Why Optimize Portfolio?

We are all risk-averse and we want to maximize our profits. Diversification can potentially achieve both of these goals. We can select assets with the best returns that are less correlated with each other. Portfolio return and risk however depend upon final mix or weight allocation to each asset. Not getting allocation right will lead to lower returns/higher risks. We will use mean-variance optimization to find desired weights to construct the best performing portfolio(modern portfolio theory by Markowitz).

Short explanation/definition of key terms

Value at Risk (VaR): Maximum dollar amount (or percentage) expected to be lost over a given time horizon, at a pre-defined confidence/probability level.

Portfolio optimization and efficient frontier: Finding best weights for selected assets such that the expected return is highest for a given risk or vice versa. An efficient frontier is simply a collection of all such dominant/optimized portfolios for all possible risk level.

Sharpe ratio: Sharpe ratio is the measure of risk-adjusted return of a portfolio. Degree of excess return beyond risk-free asset for taking extra risk.

Capital Market Line (CML): Theoretical concept that gives optimal combinations of a risk-free asset and the market portfolio (portfolio of risky assets).

Convex optimization: Solving a mean-variance problem as convex functions, here with the help of Solvers.

Part 1: Value at Risk (VaR)

VaR represents portfolio variance at one extreme (of the distribution). Portfolio risk is calculated as portfolio variance (equivalently standard deviation). Portfolio variance plays a major role in determining the risk characteristic of asset as a whole. Portfolio variance depends upon variance of each asset within the portfolio as well their covariance. Thus when assets are uncorrelated (do not tend to move together) portfolio risks are lower compared to portfolio of assets are highly positively correlated.

For a 2 asset portfolio, portfolio variance is given by following equation where w1, w2 are weights, sigma1, sigma2 are standard deviation of each asset and Cov12 denotes covariance between them.

For multiple asset portfolio, position vector and covariance matrix make it much easier to express portfolio variance rather than writing a long summation.

W represents the position vector containing allocated weight to each asset. Sigma represents the covariance matrix.

Following is an expanded view of the matrix operation.

With python (Pandas/Numpy or other tools) it is straight forward to calculate the covariance matrix for the whole asset given the series of returns. Matrix multiplication is then used to get portfolio variance. For illustration, I have taken stocks from the Nepal Stock Exchange (NEPSE). 4 highly traded stocks from different sectors were selected. The process involves loading data, calculating return for each stock, computing covariance, and finally calculating portfolio variance as given below.

Data, returns and covariance can be seen in following tables.

Raw data for one asset(stock with symbol SIC). Note: raw data will often need some processing to remove duplicates, verifying or recalculating, “%Change“ e.t.c
Data frame containing returns on each assets
Covariance matrix

While standard deviation tells how much asset can fluctuate every day on average, VaR represents how much asset can fall a single day during a highly volatile period. VaR is expressed as a potential loss of asset (e.g. 4% of the total asset) with a certain probability (e.g. 2% chance or 98% confidence) within a certain time frame (e.g. daily/weekly). Being aware of VaR allows us to keep the right-sized asset or the right mix of assets such that we can avoid unexpected and huge losses. The image below shows that return could be as worse as or worse than -1.64 standard deviation 1 out of 20 trading days (5% probability).

Value at Risk lies at negative side of distribution. Events 1.64 SD away from the center occur with frequency of 5% in average assuming normal distribution.

It is obvious that we would want a portfolio and with minimum VaR as possible for any given return. Next, we will find such a portfolio with an optimal combination of weights that has a minimum variance for a specific return. Alternatively, portfolio optimization will help us to maximize our return for any selected risk level.

Example: For some random positions(weights) in 4 stocks [0.08, 0.2, 0.58, 0.14] calculated portfolio variance is 0.005, and standard deviation is 0.022 or 2.2% daily std. Assuming a normal distribution of returns, the worst 5% outcomes lie -1.64 SD away from the center. That is returns can be worse than -0.0367 or -3.67% (-1.645*0.022) in 1/20 trading days (5% chance). If we have $100000 invested, our Var at 5% probability is $3670. In other words, we tend to lose $3670 (or more) once in every 20 trading days.

Part 2. Portfolio Optimization

We will gradually discover the portfolio we want in few steps. We shall start with completely random allocation (i.e. use random weights). Some of these random weight portfolios have better risk-return trade-off, hence are dominant portfolios. Those lying most upper/outer in the risk-return plot make the efficient frontier. Next, we shall find the Market portfolio by comparing the Sharpe ratio for each random portfolio. Needless to say, the Market portfolio lies on the efficient frontier and has the highest/maximum Sharpe ratio. The line tangent to this point and passing through risk-free asset is the Capital Market Line. The slope and intercept to this line are Sharpe ratio and risk-free rate respectively. Ideally, all investors select their portfolio somewhere on this line depending upon their risk/return preferences.

A. Random allocation of weights: We will visualize the effect of random allocation weights. 1000s of portfolios are taken with random weights, respective mean and variance are calculated (and scattered for visualization). Some combination weight has better risk-return characteristics and therefore are dominating/desirable portfolios.

Portfolio return:

Portfolio risk:

B. Market portfolio: Among all possible combinations of dominating portfolios in the efficient frontier, one which gives the highest return for taking additional risk beyond a risk-free rate is designated as a Market portfolio. we can calculate the Sharpe ratio for all portfolios given a risk-free rate and the winning portfolio with the highest Sharpe ratio is the Market Portfolio. All the rational investors gravitate towards to this best performing portfolio. This model of combining risk-free assets with the Market portfolio is known as the Capital asset pricing model (CAPM). All portfolios in CML is optimally compensated for risk above the risk-free rate. Mathematically CAPM is expressed as:

Rp -portfolio return after combining risk free asset, Rm -market return , Rf -Risk free rate. Sigma_m -standard deviation of market returns, Sigma_p -standard deviation of portfolio returns. Note risk for Risk free asset is 0

Rearranging, we have Sharpe ratio for combined portfolio :

Sharpe ratio for combined portfolio is equal to market risk premium divided by market portfolio risk.

Using Rm and Sigma_m calculated from stock return and covariance Sharpe ratio for each random portfolio is calculated, one with the highest Sharpe ratio is the market/optimal portfolio. C. Capital Market Line (CML): Capital Market line represents all combinations of the risk-free asset and the optimal market portfolio. It’s easy to see that the slope of this line is the Sharpe ratio itself and the intercept is a risk-free rate. Given this information, CML can be readily drawn as seen below.

Using Sharpe Ratio (SR) and risk free rate (Rf), portfolio return (Y) for any portfolio risk (X) can be calculated.
CML in green passes trough optimal market portfolio (red dot) and risk free rate. This line is tangent to the efficient frontier.

Code for constructing random portfolios, finding a market portfolio, and drawing CML line.

Part 3. Solvers

Random initialization of weights can work for a portfolio with few assets but when assets grow large, the cost of computing and time it takes can be forbidding. However, solving for the best portfolio is an optimization problem with constraints. We can use Solvers from python either for getting single best return (or risk) or get the entire efficient frontier. These are solved as convex problems and the global optimum is always reached.

The optimization objective is expressed as:

Here weights are represented by x and we are solving for optimal x. Objectives and constraints are written in matrix form. As a rule objective function is stated as a minimization problem. All the inequalities are similarly stated as lesser than some value.

Inputs for Solvers:

The algorithm solves for ‘x’ with the help of inputs

P or quadratic part (here it’s sigma or variance-covariance matrix, cov)

q or linear part of the objective function (here represented by a vector of returns r or ER_stocks, above)

G and h make the lower bound for weights i.e 0. We can input G as an identity matrix of size n x n (n: number of weights/stocks). Stated as matmul(G, x) < h, hence h is a vector of 0s of size n. while weights are non-negative, while solving they are stated as: -weights < 0.

A and b make the equality part of the constraint. For our purpose the sum of all weights must equal 1. Hence A is a vector of 1s of size n. matmul(A, r_transpose) = 1, or b is simply 1.

We can get the frontier and plot along with random portfolios:

Efficient frontier plotted along with random portfolios and the Capital Market Line

Code to solve using Solvers.

Limitations and Conclusion

The biggest limitations of this model are the assumption of a normal distribution of returns and the validity of covariance. Actual returns are rarely normal and in fact, highly negative and positive returns are much more common giving rise to so-called fat-tailed distribution. As such the model underestimates the risks. Portfolio variance/risk is very sensitive to variance and covariance pairs i.e. the covariance matrix. Covariance rarely remains the same for a long time, hence finding a robust/valid covariance matrix is challenging.

Despite some limitations, risk modeling with VaR is a very helpful and probably most used indicator. Portfolio optimization leads to the most efficient use of our resources and any allocation decisions should be based upon these considerations. Solvers can be used to quickly find the efficient frontier or market portfolio has given return series for each stock.

You can read the earlier post here:

Trading
Algorithmic Trading
Value At Risk
Portfolio Optimization
Solver
Recommended from ReadMedium