Expectiles are simple to compute
Wednesday, June 11th, 2025
Expectiles are a class of summary statistics generalising the well-known expected value. They have been relatively neglected since their introduction. Perhaps this is because the expectiles of a sample lack a well-known, simple and efficient calculation procedure?
This note derives a simple and efficient algorithm for computing the expectiles of a discrete distribution, or “sample expectiles” of a finite sample from an arbitrary distribution.
Defining expectiles
Expectiles generalise the expected value by introducing an asymmetry parameter , representing a degree of optimism, or, how much to weight better-than-expected data compared to worse-than-expected data.
Formally, given a scalar random variable with finite second moment, the -expectile of , , is the minimiser of an asymmetric version of the expected squared distance, weighting squared positive distances by and squared negative distances by : Here, is a generalised Iverson bracket, evaluating to if is a true proposition, or to otherwise.
For a discrete random variable taking on a finite set of values with respective probabilities , this definition simplifies to
Similarly, if we take a finite sample of size , , where , then we can define the sample expectile as
In the rest of this note, I derive a simple algorithm for computing sample expectiles. This method can easily be extended to computing the expectiles of an arbitrary discrete distribution with finite support.
The sample expectile objective
Computing the sample -expectile given a sample involves finding the value of that minimises the (rescaled) sample expectile objective
The following method is based on three observations:
As a sum of piecewise quadratic functions, the objective is piecewise quadratic. Therefore, its gradient is piecewise linear.
As a sum of functions that are continuously differentiable with respect to , the objective is continuously differentiable with respect to .1 That is, its gradient is a continuous function.
As a sum of strictly convex functions, the objective is strictly convex. Therefore, its gradient is strictly increasing.
In summary, is piecewise linear, continuous, and monotonically increasing in .
Solving for the minimum
The above implies we can find the minimum of by finding the root of , and we can find this root in turn by checking each linear “piece” of to find the one that crosses zero.
Let’s start by finding those linear pieces. Differentiating the objective with respect to yields where
Observe that, while and depend on , the dependence arises only through comparison with the in the summation bounds. Therefore, these functions are piecewise constant with boundaries at each distinct .
This leads to the following efficient algorithm for finding the root of :
Sort . Hence, assume that .
For , compute and . All values can be computed in linear time by sharing partial sums and .
For , compute . Note that the th linear segment of runs from up to .
Check each to identify one such that . This linear segment crosses zero at .
The resulting satisfies . Since is strictly monotonically increasing this implies that this stationary point is a global minimum of the objective and therefore
Python/NumPy implementation
Here is a simplified NumPy implementation of the above algorithm.
import numpy as np
def expectile(sample: np.ndarray, tau: float) -> float:
# step 1: pre-sort the sample
= np.sort(sample)
x
# step 2: compute linear segment coefficients
# i. precompute partial sums at key points
= x.size
N = np.arange(N) + 1
F = np.cumsum(x)
M
# ii. compute the coefficients from these partial sums
= ((1 - tau) * F + tau * (F[-1] - F)) # slopes
A = ((1 - tau) * M + tau * (M[-1] - M)) # offsets
B
# step 3: compute starting point of each segment
= A * x - B
G
# step 4: find the segment crossing zero and its root
# i. find the segment with G_i <= 0 and G_i+1 > 0
= (G <= 0)[:-1]
start_below_0 = (G > 0)[1:]
stop_above_0 = np.nonzero(start_below_0 & stop_above_0)[0][0]
i
# ii. interpolate to get the root
= B[i]/A[i]
eps
# done!
return eps
This implementation is meant for pedagogical purposes. A version that
vectorises the tau
input and is more careful about corner
cases and potential numerical issues is available in this
repository.
Algorithmic efficiency
An advantage of the above method is that it doesn’t involve resorting to numerical methods for minimising the expectile objective. It’s not quite a closed-form solution as it still involves sorting the data and searching iteratively for the segment of the gradient that crosses zero.
The above implementation is pretty efficient. However, it could be made even more efficient.2
If we are computing only a single expectile for the given sample, notice that we’re just looking for the segment that crosses zero, and we don’t really care about the order of the other segments. Often this kind of “searching” operation only actually requires partial sorting. In this case, we can adapt a method like quickselect to find the crossing segment in linear time. Efficiently evaluating the coefficients requires some care, but it seems it can be done.
If we are computing many different expectiles from the given sample, pre-sorting makes sense, as the cost can be amortised over the entire batch. This saves us from the complexity of a selection algorithm. However, that’s not all—we can also take advantage of a further opportunity to save time by using binary search rather than linear search to find the crossing segment. This requires a logarithmic number of operations per value.
Related work
I derived this algorithm in 2020 during a coursework research project. Back then, I remember being disappointed that the implementation I saw for computing sample expectiles worked by applying generic root finding algorithms to the gradient, without taking advantage of its special structure. I also looked around and found that standard statistics libraries (SciPy, R) didn’t appear to have tools for computing sample expectiles.
Since then, there have been some developments in tools for computing sample expectiles. SciPy added a method for computing sample expectiles, though it too uses a generic root finder. More excitingly, Daouia, Stupfler, and Usseglio-Carleve published “An expectile computation cookbook” (submitted 2023), which discusses methods similar to mine for computing exact expectiles for discrete distributions as well as for other kinds of distributions. I’m glad to see expectiles getting some love!
It’s not obvious that the function is continuous or differentiable, let alone continuously differentiable, given the use of (discontinuous) Iverson brackets. However, these brackets always occur in multiplication with a term of the form which is zero and has gradient zero at the discontinuity of the Iverson bracket.↩︎
I have to thank Gemini 2.5 Pro (preview), which pointed out these directions for efficiency improvements when I showed it an earlier draft of this post that conjectured prematurely that the above method was optimal.↩︎