Mean - variance optimization. The diversification of the portfolio with the help of $L_2$ regularization.

The mean-variance optimization process often ends with most of the stock weights to be negligibly small. But in order to have a diversified portfolio, it should include a definite number of assets. It can be achieved with the help of the technique known as $L_2$ regularization [1]. To make it happen, we add a small - weight penalty to our objective function - the $\gamma \mathbf{w}^T \mathbf{w}$ (i.e a sum of square of the weights) member. Here $\gamma \gt 0$ is a constant parameter of regularization, the more the value of the $\gamma$, the more regularization (or diversification) effect we will have. This term reduces the number of negligible small weights, because it has a minimum value when all the weights are equally distributed, and the maximum value in the case where the only weight is nonzero and equal to $1$.

For example, to minimize the portfolio volatility using the $L_2$ regularization we should change the objective function as follows: $$ \underset{\mathbf{w}}{\text{minimize}} ~ \left(\mathbf{w}^T \Sigma \mathbf{w} \right) ~~~ \longrightarrow ~~~ \underset{\mathbf{w}}{\text{minimize}} ~ \left(\mathbf{w}^T \Sigma \mathbf{w} + \gamma \mathbf{w}^T \mathbf{w} \right) $$

References:

[1] Boyd, S.; Vandenberghe, L. Convex Optimization (2004).