Find the weighted average return for each stock.
Stock A: (0.10)(-5) + (0.30)(-2) + (0.50)(10) + (0.10)(0.31) = 7%.
Stock B: (0.10)(4) + (0.30)(8) + (0.50)(10) + (0.10)(0.12) = 9%.
Next, multiply the differences of the two stocks by each other, multiply by the probability of the event occurring, and sum. This is the covariance between the returns of the two stocks.
[(-0.05 − 0.07) × (0.04 − 0.09)] (0.1) + [(-0.02 − 0.07) × (0.08 − 0.09)](0.3) + [(0.10 − 0.07) × (0.10 − 0.09)](0.5) + [ (0.31 − 0.07) × (0.12 − 0.09)](0.1) = 0.0006 + 0.00027 + 0.00015 + 0.00072 = 0.00174.