Skip to content
All library documents

Matching Covariance and Weight Dimensions in a Minimum Variance Portfolio

Article Quant Q&A · Author: user57440

Summary

The document sets up a long-only minimum variance portfolio in Python using a covariance matrix estimated from a table of asset returns. The optimization minimizes portfolio variance subject to weights summing to one and each weight being nonnegative. The reported solver error comes from a mismatch between the number of portfolio weights and the size of the covariance matrix: the sample data has five columns, so its covariance matrix is five by five, while the weight vector is created with ten entries.

The accepted response identifies this dimension mismatch and recommends constructing the data with ten asset columns so the covariance matrix matches the ten-dimensional weight vector. The key implementation principle is that the covariance matrix must have one row and column per asset represented by the portfolio weights. The example does not discuss estimation quality, alternative constraints, solver behavior beyond this error, or portfolio performance; its focus is resolving the input-shape issue.

Key ideas

  • The minimum variance objective uses the covariance matrix of asset returns and a vector of portfolio weights.
  • The covariance matrix must have the same number of asset dimensions as the weight vector.
  • The example creates five return columns but defines ten portfolio weights, causing the dimension error.
  • A feasible long-only portfolio is constrained to nonnegative weights that sum to one.
  • The example addresses matrix dimensions, not the quality of the covariance estimates or the portfolio’s performance.

Tags

Full text
# Minimum variance portfolio in Python


# Minimum variance portfolio in Python












I have a portfolio or $N$ assets in $t=10$ days.

```
import numpy as np
import pandas as pd
n= 10
A = pd.DataFrame([np.random.randn(5) for i in range(n)],columns=['a', 'b', 'c', 'd', 'e'])
A  

T = A.shape[0]
k = A.shape[1]
print(T,k)
```

The covariance matrix

```
Σ = A.cov().to_numpy()
Sigma = Σ
print(Sigma)
```

I want to minimize the variance with convex optimization in Python.

Actually I want to solve the

$$\min \quad (1/2) w^T \Sigma w$$ s.t $$w_{i}\geq 0,\sum_{i=1}^{n}w_{I} =1$$

So I do :

```
import cvxpy as cp
w = cp.Variable(n)

# Defining risk objective
risk = cp.quad_form(w,Sigma)
objective = cp.Minimize((1/2)*risk)

# Budget and weights constraints
constraints = [cp.sum(w) == 1, 
                w >= 0]
# Solver 
prob = cp.Problem(objective, constraints)
prob.solve()
```

but I receive an error:

```
Exception: Invalid dimensions for arguments.
```

what is my mistake here ? Anybody ?

## Answer by lehalle (score 0, accepted)

https://quant.stackexchange.com/a/69283

your `Sigma` matrix is 5x5 and not 10x10, try this

```
A = pd.DataFrame(
      [np.random.randn(n) for i in range(5*n)],
      columns=[chr(65+i) for i in range(n)]
   )
```

it will work.

[ADDITION following a remark] I assumed that you expected the portfolio to be of dimension 10 (because you write `n=10;w = cp.Variable(n)`), hence your covariance matrix should have the dimension of the portfolio, ie 10.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.