# Convex combination
## Introduction

In convex geometry, a convex combination is a linear combination of points (which can be vectors, scalars, or more generally points in an affine space) where all coefficients are non-negative and sum to 1.

## Definition - Convex Combination

More formally, given a finite number of points $x_{1},x_{2},\dots ,x_{n}$ in a real vector space, a convex combination of these points is a point of the form
    $\lambda _{1}x_{1}+\lambda _{2}x_{2}+\cdots +\lambda _{n}x_{n}$
where the real numbers $\lambda _{i}$ satisfy $\lambda _{i}\geq 0$ and $\lambda _{1}+\lambda _{2}+\cdots +\lambda _{n}=1.$

### Remark - outside of convex hull

In the animation of the tetrahedron there are examples of points that
* fulfill $\lambda _{1}+\lambda _{2}+\cdots +\lambda _{n}=1$, but
* violate the condition $\lambda _{i}\geq 0$.
In case that on $\lambda _{i}$ is negative, then linear combination
    $\lambda _{1}x_{1}+\lambda _{2}x_{2}+\cdots +\lambda _{n}x_{n}$
is a point outside the tetrahedron (see animation and consider, when values become negative.

## Convex combinations in the plane

If we consider a convex combinations in the plane, then the underlying vector space is the two-dimensional space $V:=\mathbb {R} ^{2}$. First, we consider convex combinations of two vectors in $\mathbb {R} ^{2}$. Later we transfer that to infinite dimensional vector spaces of functions and visualize convex combination as GIF animation with Open Source Geogebra. By the condition $\lambda _{1}+\lambda _{2}=1$, scalars are interdependent. For example, if we define $t\in [0,1]$, then we can set $\lambda _{1}:=(1-t)$ and $\lambda _{2}:=t$.

## Convex combinations as mappings into the vector space
### Convex combination of two functions

Geogebra: Interaktives Applet - Download: Geogebra-File

### Convex Combination as a Mapping

Now we consider the convex combination as a mapping $K\colon [0,1]\to V$ into the underlying vector space. Due to the fact that the equation can generally represent 1st order convex combinations of 2 vectors $v_{1},v_{2}\in V$ as follows over the mapping $K$:
    $K(t):=(1-t)\cdot v_{1}+t\cdot v_{2}$

### Convex Combination of 2 Points

As a particular example, every convex combination of two points lies on the line segment between the points.

### Convex Combination of 2 Functions as Vectors

Let $[a,b]=[4,7]$ be an interval and as the first function is defined as a polynomial $f:[a,b]\to \mathbb {R}$.
    $f(x):={\frac {3}{10}}\cdot x^{2}-2$
As the second function a trigonometric function $g:[a,b]\to \mathbb {R}$ was chosen for the convex combination in the vector space of continuous functions.
    $g(x):=2\cdot cos(x)+1$
The animation above illustrates the convex combination $K(t):=(1-t)\cdot f+t\cdot g$.

### Remark - Vector space of continuous functions

Both functions $f,g$ and $K(t)$ for all $t\in [0,1]$ are elements of the vector space of continuous functions from $[a,b]$ to $\mathbb {R}$ (i.e. $f,g,K(t)\in {\mathcal {C}}([a,b],\mathbb {R} )$ ).

### Remark - Deformation

If the first function $f$ describes the initial shape and $g$ the target shape. A convex combinations of those functions can describe (e.g. in computer graphics) a continuous deformation of an initial shape into a target shape.

### Convex combination of more than 2 Points

The figure above, the point $P$ is a convex combination of the three points, while $Q$ is not. ($Q$ is however an affine combination of the three points, as their affine hull is the entire plane.)]]

### Convex combination of n points

A set is convex if it contains all convex combinations of its points. The convex hull of a given set of points is identical to the set of all their convex combinations.
    ${\begin{array}{rcl}v&=&\lambda _{1}v_{1}+\lambda _{2}v_{2}+\dotsb +\lambda _{n}v_{n}\\&=&\sum _{i=1}^{n}\lambda _{i}v_{i},\\&{\mbox{mit}}&0\leq \lambda _{i}\leq 1,\quad \sum _{i=1}^{n}\lambda _{i}=1\,.\end{array}}$

### Linear Combinations and Convex Combinations

There exist subsets of a vector space that are not closed under linear combinations but are closed under convex combinations. For example, the interval $[0,1]$ is convex but generates the real-number line under linear combinations. Another example is the convex set of probability distributions, as linear combinations preserve neither non-negativity nor affinity (i.e., having total integral one).

## Convex combinations of convex combinations

In the animation above you can see a convex combination of 2 vectors in the plane or in a function space. If one uses three points then one can create a 1st order convex combination between every two points. We will now consider higher order convex combinations by constructing e.g. a 2nd order convex combination generated from two 1st order convex combinations. Generally from 2 convex combinations of order $n$ you can create a convex combination of order $n+1$.

### Convex hull

The set of all convex combinations of a given set of vectors is called a convex hull (see also p-convex hull).

### Video convex combinations in the plane

Geogebra: Interactive applet - Download:' Geogebra-File

### Remarks video about convex combinations of order 1, 2 and 3 in Geogebra

In the video you can see convex combinations of the
* 1st order between $A_{1}$ and $B_{1}$ without auxiliary points,
* 2nd order between $A_{2}$ and $B_{2}$ with auxiliary point $S_{1}$,
* 3rd order between $A_{3}$ and $B_{3}$ with auxiliary points $H_{1},H_{2}$,

## Convex combinations as polynomials of t

Convex combinations can be conceived as polynomials where the coefficients come from a vector space $(V,+,\cdot ,\mathbb {R} )$ (see also Polynomial Algebra). For example, if one chooses $V:=\mathbb {R} ^{n}$, one can take a convex combination $K$ to be an element of algebra of polynomial $V[t]$.

### 3D convex combination - 1st order

For example, choosing $n=3$ and $V:=\mathbb {R} ^{3}$, a 1st order convex combination is defined as follows.
    $A={\begin{pmatrix}1\\2\\4\end{pmatrix}},\,B={\begin{pmatrix}4\\1\\0\end{pmatrix}},\,\,K(t):=(1-t)\cdot A+t\cdot B=(B-A)\cdot t+A$
Thus, a 1st order convex combination yields a polynomial of degree 1. with argument $t$. Represent the convex combination in Geogebra 3D with $t\in [0,1]$ (see also Representation of a Straight Line by Direction Vector and Location Vector).

### 3D convex combination - 2nd order

Choosing again $n=3$ and $V:=\mathbb {R} ^{3}$ with an auxiliary point $H_{1}\in V$, two 1st order convex combinations yield 2nd order convex combinations.
    $H_{1}={\begin{pmatrix}2\\2\\2\end{pmatrix}},{\begin{array}{rcl}K_{(1,1)}(t)&:=&(H_{1}-A)\cdot t+A\\K_{(1,2)}(t)&:=&(B-H_{1})\cdot t+H_{1}\\K_{2}(t)&:=&((H_{1}-A)\cdot t+A)\cdot (1-t)+((B-H_{1})\cdot t+H_{1})\cdot t\end{array}}$
Represent $K_{2}$ as a polynomial $K_{2}(t)=P_{2}\cdot t^{2}+P_{1}\cdot t^{1}+P_{0}\cdot t^{0}$ and calculate for $n=2$ ($n=3,4,...$) the coefficients in $P_{k}\in V=\mathbb {R} ^{3}$.

### Bernstein polynomial - order 1

    ${\begin{array}{rcl}K_{1}(t)&:=&A\cdot (1-t)+B\cdot t\\&=&A\cdot (1-t)^{1}\cdot t^{0}+B\cdot (1-t)^{0}\cdot t^{1}\\\end{array}}$

### Calculation of the polynomial - order 2

    ${\begin{array}{rcl}K_{2}(t)&:=&((H_{1}-A)\cdot t+A)\cdot (1-t)+((B-H_{1})\cdot t+H_{1})\cdot t\\&=&(H_{1}\cdot t-A\cdot t+A)-(H_{1}\cdot t^{2}-A\cdot t^{2}+A\cdot t)\\&&+(B\cdot t^{2}-H_{1}\cdot t^{2}+H_{1}\cdot t)\\&=&(B-H_{1}+A)\cdot t^{2}+2\cdot (H_{1}-A)\cdot t+A\end{array}}$

### Bernstein polynomial - order 2

    ${\begin{array}{rcl}K_{2}(t)&:=&((H_{1}-A)\cdot t+A)\cdot (1-t)+((B-H_{1})\cdot t+H_{1})\cdot t\\&=&A\cdot (1-t)^{2}+2\cdot H_{1}\cdot t\cdot (1-t)+B\cdot t^{2}\end{array}}$

### Bernstein polynomial - order 3

    ${\begin{array}{rcl}K_{3}(t)&:=&A\cdot (1-t)^{3}+3\cdot H_{1}\cdot (1-t)^{2}\cdot t+3\cdot H_{2}\cdot (1-t)\cdot t^{2}+B\cdot t^{3}\end{array}}$

## Convex combination as a mapping

A convex combination can be used to interpolate points $A=v_{1}$ and $B=v_{n}$. Furthermore, if the auxiliary points $H_{1}=v_{2}$,....$H_{n_{1}}=v_{n-1}$ are given for a convex combination $n$-th order. The convex combinations can be generally thought of as mapping from the interval $[0,1]$ to $\mathbb {R} ^{n}$ as follows:
    ${\begin{array}{rcl}K_{n}:&[0,1]&\rightarrow &\mathbb {R} ^{n}\\&t&\mapsto &\displaystyle \sum _{k=0}^{n}{n \choose k}(1-t)^{n-k}\cdot t^{k}\cdot v_{k+1}\\&&&={n \choose 0}(1-t)^{n}v_{1}+{n \choose 1}(1-t)^{n-1}tv_{2}+\ldots +{n \choose n}t^{n}v_{n}\end{array}}$

## Interpolations

Convex combinations can also be used to interpolate polynomials. Start first with first order interpolations by interpolating the points with straight lines of the form $f_{k}(x):=m_{k}\cdot x+b_{k}$. Here, the points $\mathbb {D} :=\{(x_{0},y_{0}),\ldots ,(x_{n},y_{n})\}$ are given data points that are interpolated piecewise using the functions $f_{k}(x):=m_{k}\cdot x+b_{k}$. Compute from the convex combinations $P_{k}(t)\in \mathbb {R} ^{2}$ the functional representation $f_{k}:[x_{k-1},x_{k}]\to \mathbb {R}$ with $f_{k}(x):=m_{k}\cdot x+b_{k}$:
    $P_{k}(t):=(1-t)\cdot {\begin{pmatrix}x_{k-1}\\y_{k-1}\end{pmatrix}}+t\cdot {\begin{pmatrix}x_{k}\\y_{k}\end{pmatrix}}$

### Calculation of t as a function of x

Given $x\in [x_{k-1},x_{k}]\subset \mathbb {R}$. We now compute the corresponding $t\in [0,1]$ for the convex combination with the preliminary consideration that $t=0$ for $x=x_{k-1}$ and $t=1$ for $x=x_{k}$. The following figure takes the linear transformation $T:[x_{k-1},x_{k}]\to [0,1]$.
    $T(x):={\frac {x-x_{k-1}}{x_{k}-x_{k-1}}}$

### Calculation of the function value at x

The convex combination
    $P_{k}(t):=(1-t)\cdot {\begin{pmatrix}x_{k-1}\\y_{k-1}\end{pmatrix}}+t\cdot {\begin{pmatrix}x_{k}\\y_{k}\end{pmatrix}}$
gives the interpolation point of the graph. However, we only need the y-coordinate of the corresponding interpolation point $P_{k}(t)={\begin{pmatrix}(1-t)\cdot x_{k-1}+t\cdot x_{k}\\(1-t)\cdot y_{k-1}+t\cdot y_{k}\end{pmatrix}}$. So we use the following term: $(1-t)\cdot y_{k-1}+t\cdot y_{k}$.

### Functional representation

Substituting for $t\in [0,1]$ gives the linear interpolation function $f_{k}:[x_{k-1},x_{k}]\to \mathbb {R}$ over:
    $f_{k}(x):={\bigg1-\underbrace {\frac {x-x_{k-1}}{x_{k}-x_{k-1}}} _{=t}{\bigg )}\cdot y_{k-1}+{\bigg\underbrace {\frac {x-x_{k-1}}{x_{k}-x_{k-1}}} _{=t}{\bigg )}\cdot y_{k}$.

### Learning Tasks

* Calculate the coefficients $m_{k},b_{k}\in \mathbb {R}$ of the function $f_{k}:[x_{k-1},x_{k}]\to \mathbb {R}$ with $f_{k}(x):=m_{k}\cdot x+b_{k}$!
* Transfer this interpolation to convex combination of order 3 and consider how, depending on the data points, you must choose the two auxiliary points of the interpolation so that the interpolation is differentiable and generates differentiable transitions between the interpolation points in the plot.
* What geometric properties must auxiliary points between two adjacent interpolation intervals have for differentiability.

### Interpolation with convex combination of order 3

Geogebra: Interactive Applet - Download: Geogebra-File
Geogebra: Interactive Applet - Download: Geogebra-File
Develop a mathematical/algebraic description by terms for the following:
* The green stippled lines are 1st order convex combinations,
* At the vertices of the open Polygon course, create an angle bisector (constructively, this can be implemented by a rhombus, where two sides and a vertex are defined by two adjacent lines in the polygon course).
* Create an orthogonal through the connection point of two adjacent lines in the polygon course.
* Analyze the figure above and determine the next steps for defining the two auxiliary points for a 3rd order convex combination. The procedure is not clear especially at the boundary points of the polygon course. What speaks to your choice of mathematical implementation?
Use altogether methods from linear algebra (e.g. scalar product,... for the vectorial description of the above geometric procedure.

## Morphing and the use of convex combinations

In the following section we will consider transformations of images in the context of convex combinations. In Morphing there are different mathematical tools. Here we will only consider aspects in the context of convex combinations.
* Look at the above GIF animation and first take two different black and white images transform the first image pixel by pixel to the second image by applying a convex combination from gray level values (black=0,...255=white) of a pixel in image 1 to a pixel in image 2 (implementation e.g. in Octave Image Processing v7.3.0 or Octave Image Processing v5.2.0. Note that the convex combinations yields real values of brightness in the gray levels, which must rounded to integer values (e.g. 232.423 to 232 = approximately white). This is necessary due to that the fact that brightness encoding is done with 256 values (Byte).
* Transfer the procedure to color images only, similarly transferring color values for the primary colors from grayscale values to color values.
* In the above morphing animation, however, not only static pixel-by-pixel convex combinations are made, but for fixedly defined points, such as eyes, a spatial transformation process also takes place. Consider how, for example, the center of the iris in the eye is spatially shifted from image1 to image2.
* now connect spatial transformation processes with a color adjustment of the pixels, so that a pixel moves from the location $(x_{1},y_{1})$ in the image matrix to $(x_{2},y_{2})$ and on the way from $(x_{1},y_{1})$ to $(x_{2},y_{2})$ the color changes from yellow to blue.

## CAS4Wiki

With the following CAS4Wiki commands you can play around with the definition of curves in $\mathbb {R} ^{2}$
    Curves in $\mathbb {R} ^{2}$ and $\mathbb {R} ^{3}$

## Other objects

* Similarly, a convex combination $X$ of random variables $Y_{i}$ is a weighted sum (where $\alpha _{i}$ satisfy the same constraints as above) of its component probability distributions, often called a finite mixture distribution, with probability density function:
    $f_{X}(x)=\sum _{i=1}^{n}\lambda _{i}f_{Y_{i}}(x)$

## Related constructions

* Linear Combination
* A conical combination is a linear combination with nonnegative coefficients. When a point $x$ is to be used as the reference origin for defining displacement vectors, then $x$ is a convex combination of $n$ points $x_{1},x_{2},\dots ,x_{n}$ if and only if the zero displacement is a non-trivial conical combination of their $n$ respective displacement vectors relative to $x$.
* Weighted means are functionally the same as convex combinations, but they use a different notation. The coefficients (weights) in a weighted mean are not required to sum to 1; instead the weighted linear combination is explicitly divided by the count of the weights.
* Affine combinations are like convex combinations, but the coefficients are not required to be non-negative. Hence affine combinations are defined in vector spaces over any field.