EXPECTILE
Overview
The EXPECTILE function computes the expectile of a dataset at a specified level, providing a flexible measure of central tendency and tail behavior. Expectiles are a generalization of the expected value (mean) in the same way that quantiles are a generalization of the median. At level α = 0.5, the expectile equals the arithmetic mean; lower values of α weight observations below the expectile more heavily, while higher values emphasize observations above it.
This implementation uses the scipy.stats.expectile function from the SciPy library. The expectile at level α is defined as the unique solution t to the equation:
\alpha \sum_{i=1}^{n} w_i (a_i - t)_+ = (1 - \alpha) \sum_{i=1}^{n} w_i (t - a_i)_+
where (x)_+ = \max(0, x) denotes the positive part of x, a_i are the data values, and w_i are optional weights. Equivalently, expectiles can be characterized as the minimizer of an asymmetric least squares loss function, which differentiates them from quantiles that minimize an asymmetric absolute deviation loss.
Expectiles have applications in financial risk management, econometrics, and robust statistics. They provide a coherent risk measure and are particularly useful for analyzing tail risk in distributions. The concept was introduced by Newey and Powell in their 1987 paper “Asymmetric Least Squares Estimation and Testing” published in Econometrica. For additional theoretical background, see the Wikipedia article on expectiles.
The function accepts optional weights, allowing for weighted expectile calculations where each data point can have a different influence on the result. An integer-valued weight acts like repeating the corresponding observation that many times.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=EXPECTILE(data, alpha, weights)
data(list[list], required): A 2D column of numeric data.alpha(float, optional, default: 0.5): The expectile to compute, a value between 0 and 1.weights(list[list], optional, default: null): A 2D column of weights for each data point. Must have the same length as data.
Returns (float): The calculated expectile, or an error message (str) if input is invalid.
Examples
Example 1: Median expectile (alpha=0.5) without weights
Inputs:
| data | alpha |
|---|---|
| 1 | 0.5 |
| 2 | |
| 3 | |
| 4 | |
| 5 |
Excel formula:
=EXPECTILE({1;2;3;4;5}, 0.5)
Expected output:
3
Example 2: Low alpha (0.2) expectile without weights
Inputs:
| data | alpha |
|---|---|
| 1 | 0.2 |
| 2 | |
| 3 | |
| 4 | |
| 5 |
Excel formula:
=EXPECTILE({1;2;3;4;5}, 0.2)
Expected output:
2.182
Example 3: High alpha (0.8) expectile with uniform weights
Inputs:
| data | alpha | weights |
|---|---|---|
| 1 | 0.8 | 1 |
| 2 | 1 | |
| 3 | 1 | |
| 4 | 1 | |
| 5 | 1 |
Excel formula:
=EXPECTILE({1;2;3;4;5}, 0.8, {1;1;1;1;1})
Expected output:
3.818
Example 4: Skewed weights emphasizing larger values
Inputs:
| data | alpha | weights |
|---|---|---|
| 1 | 0.5 | 1 |
| 2 | 1 | |
| 3 | 1 | |
| 4 | 1 | |
| 10 | 4 |
Excel formula:
=EXPECTILE({1;2;3;4;10}, 0.5, {1;1;1;1;4})
Expected output:
6.25
Python Code
from scipy.stats import expectile as scipy_expectile
def expectile(data, alpha=0.5, weights=None):
"""
Calculates the expectile of a dataset using scipy.stats.expectile.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.expectile.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): A 2D column of numeric data.
alpha (float, optional): The expectile to compute, a value between 0 and 1. Default is 0.5.
weights (list[list], optional): A 2D column of weights for each data point. Must have the same length as data. Default is None.
Returns:
float: The calculated expectile, or an error message (str) if input is invalid.
"""
def to2d(x):
return [[x]] if not isinstance(x, list) else x
data = to2d(data)
if not isinstance(data, list) or not all(isinstance(row, list) for row in data) or not data:
return "Invalid input: data must be a non-empty 2D list."
a = []
for row in data:
for x in row:
if not isinstance(x, (int, float)):
return "Invalid input: data must contain only numeric values."
a.append(float(x))
if not a:
return "Invalid input: data must contain at least one numeric value."
if not isinstance(alpha, (int, float)):
return "Invalid input: alpha must be between 0 and 1."
alpha = float(alpha)
if not (0.0 <= alpha <= 1.0):
return "Invalid input: alpha must be between 0 and 1."
w = None
if weights is not None:
weights = to2d(weights)
if not isinstance(weights, list) or not all(isinstance(row, list) for row in weights):
return "Invalid input: weights must be a 2D list."
flat_w = []
for row in weights:
for x in row:
if not isinstance(x, (int, float)):
return "Invalid input: weights must contain only numeric values."
if x < 0:
return "Invalid input: weights must be non-negative."
flat_w.append(float(x))
if len(flat_w) != len(a):
return "Invalid input: weights must have the same number of values as data."
w = flat_w
try:
result = scipy_expectile(a=a, alpha=alpha, weights=w)
return float(result)
except Exception as e:
return f"An error occurred during calculation: {e}"