# Convex Combinations

2020-06-21

2 minutes

A while back I came across convex combinations in this paper that outlines the math of how to detect who’s guarding whom in basketball player tracking data. I then used them in a Stan case study.

A convex combination helps you define any point inside a shape using a vector of coefficients that sum to one. So say you have a vector of coefficients `alpha`

, and x-coordinates `x`

and y-coordinates `y`

. Then then `x_new = alpha * x`

and `y_new = alpha * y`

will always be within the shape defined by the coordinates `x, y`

.

Here’s a brief Python script that simulates this result.

```
# convex.py
import numpy as np
import matplotlib.pyplot as plt
k = [1,1,1]
N = 1000
x = [-0.5, 0.8, -0.3]
y = [-0.5, 0.6, 0.7]
# convex combination
alpha = np.random.dirichlet(alpha=k, size=N)
x_new = np.matmul(alpha, x)
y_new = np.matmul(alpha, y)
plt.plot(x + [x[0]], y + [y[0]], color='#000000', marker='o', lw=2)
plt.ylim((-1,1))
plt.xlim((-1,1))
plt.scatter(x_new, y_new, color='#76d7c450', edgecolor='none', s=10)
plt.savefig('./convex.svg', format='svg')
plt.close()
```

Here we generate `alpha`

from the Dirichlet distribution since realizations from this distribution have the property of summing to to one. The resulting plot of a single run of the script looks something like the figure below. Each green point is a convex combination of the coordinates (-0.5, -0.5), (0.8, 0.6), and (-0.3, 0.7).

219 Words