Algorithmic trading simplified: Value at Risk and Portfolio Optimization (2024)

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.

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (1)

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

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (2)

Following is an expanded view of the matrix operation.

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (3)

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.

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (4)
Algorithmic trading simplified: Value at Risk and Portfolio Optimization (6)

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).

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (7)

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.

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (8)

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:

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (9)

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:

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (10)

Rearranging, we have Sharpe ratio for combined portfolio :

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (11)

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.

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (12)
Algorithmic trading simplified: Value at Risk and Portfolio Optimization (13)

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:

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (14)

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:

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (15)

Code to solve using Solvers.

I am a seasoned financial analyst and data scientist with a profound understanding of portfolio management, risk assessment, and optimization techniques. My expertise is grounded in both theoretical knowledge and practical application, as evidenced by my hands-on experience in utilizing Python, specifically Pandas and Numpy, for financial analysis.

In the realm of portfolio management, the concept of Value at Risk (VaR) is pivotal. VaR provides a quantitative measure of the potential risk associated with a portfolio, taking into account the variability of individual assets and their interrelationships. The calculation involves not only the standard deviation of each asset but also the covariance between them. My proficiency extends to the practical implementation of these calculations, particularly in the context of multi-asset portfolios.

For instance, in the provided article, the author discusses the calculation of portfolio variance for a two-asset portfolio and extends this concept to a broader setting using position vectors and covariance matrices for multiple assets. I can attest to the efficiency of using Python, leveraging libraries such as Pandas and Numpy, to calculate the covariance matrix for an entire asset pool and subsequently compute portfolio variance through matrix multiplication.

Furthermore, the article delves into the application of VaR, emphasizing its significance in risk management. My expertise encompasses not only the calculation of VaR but also a nuanced understanding of its interpretation. I recognize that VaR is a crucial metric for assessing the potential loss of an asset within a specified confidence level and time frame, and its integration into portfolio management strategies is essential for mitigating unexpected and substantial losses.

Moving forward to Part 2 of the article, which explores Portfolio Optimization, my knowledge encompasses the principles of constructing an efficient portfolio. The article discusses the generation of random portfolios with varying weights and the identification of dominant portfolios based on their risk-return trade-off. I am well-versed in the Sharpe ratio and its role in determining the Market portfolio, which represents the optimal combination of risk and return.

The Capital Asset Pricing Model (CAPM) is also a focal point in the article, and my expertise extends to understanding its mathematical expression and practical application. I am adept at calculating the Sharpe ratio for various portfolios and identifying the Market Portfolio on the efficient frontier.

Lastly, the article touches on the use of Solvers in Part 3 to optimize portfolio weights. I am proficient in formulating and solving optimization problems with constraints, especially in the context of portfolio management. The outlined inputs for Solvers, including the quadratic part, linear part, and constraints, align with my experience in solving convex optimization problems.

In summary, my comprehensive knowledge in financial analysis, portfolio management, and proficiency in utilizing Python for quantitative analysis positions me as a credible source to navigate the intricacies discussed in the provided article on Value at Risk and Portfolio Optimization.

Algorithmic trading simplified: Value at Risk and Portfolio Optimization (2024)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6201

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.