Principles of Corporate Finance - by F. Allen, R. A. Brealey, & S. Myers
Financial Risk

How is Portfolio Risk Calculated?
In the previous entry, I discussed the essence of diversification. Now let’s take a look at specific formulas, as shown in the image.
Why do we need these formulas? The point is that when placing any two assets in one portfolio, there are optimal proportions that provide maximum returns with minimal risk.
Let’s assume a theoretical situation where you can only store your savings in gold and silver. From diversification, we understand that it is better to distribute savings between rather than choosing just one. But the question arises: how much gold and how much silver? This choice depends not only on the range of their price fluctuations (variation) but also on the correlation (and consequently covariance) between their prices.
These formulas can be extended to multiple assets, theoretically to a large number, but it becomes so complex that we might need a quantum computer to analyze all the stocks on the market. However, I have good news: this is not necessary :). Firstly, the effect of diversification decreases and is almost negligible after purchasing 30 different assets. Secondly, you can simply buy an index or shares of other diversified funds.
If you are very fond of mathematics and want to build your model in Excel, check out this link:
P.S.
Note that these formulas do not tell us anything about market risk. They only address specific risks of the assets. Market risk is additionally measured by the β (beta) coefficient, which I will write about next.
Source:
Principles of Corporate Finance, – By Richard Brealey, Stewart Myers and Franklin Allen