EXPECTILE
This function computes an expectile at level \alpha for a numeric dataset, optionally using observation weights.
The expectile t is the solution of the asymmetric balance equation:
\alpha \sum_{i=1}^{n} w_i (x_i - t)_+ = (1-\alpha) \sum_{i=1}^{n} w_i (t - x_i)_+
where (u)_+ = \max(u, 0) and w_i=1 when no weights are supplied. At \alpha=0.5, the expectile equals the (weighted) mean.
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.
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.18182
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.81818
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
try:
data = to2d(data)
if not isinstance(data, list) or not all(isinstance(row, list) for row in data) or not data:
return "Error: 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 "Error: Invalid input: data must contain only numeric values."
a.append(float(x))
if not a:
return "Error: Invalid input: data must contain at least one numeric value."
if not isinstance(alpha, (int, float)):
return "Error: Invalid input: alpha must be between 0 and 1."
alpha = float(alpha)
if not (0.0 <= alpha <= 1.0):
return "Error: 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 "Error: 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 "Error: Invalid input: weights must contain only numeric values."
if x < 0:
return "Error: Invalid input: weights must be non-negative."
flat_w.append(float(x))
if len(flat_w) != len(a):
return "Error: Invalid input: weights must have the same number of values as data."
w = flat_w
result = scipy_expectile(a=a, alpha=alpha, weights=w)
return float(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
A 2D column of numeric data.
The expectile to compute, a value between 0 and 1.
A 2D column of weights for each data point. Must have the same length as data.