Class TailRisk

java.lang.Object
com.quantfinlib.risk.TailRisk

public final class TailRisk extends Object
Empirical value-at-risk and expected shortfall of a loss sample, with one tail definition shared by every class in the library that reports them.

Convention: inputs are losses, positive = loss. Confidence c is the one-sided level (0.975 = 97.5%). With losses sorted ascending, L(1) <= ... <= L(n):

  • VaR is the lower empirical quantile L(ceil(c·n)), the smallest loss whose empirical CDF reaches c.
  • ES is the Acerbi-Tasche (2002, eq. 4.4 / Prop. 4.1) estimator of the tail mean over the worst w = (1−c)·n observations, with the fractional boundary weight:
      ES = ( L(n) + ... + L(n−k+1) + (w − k)·L(n−k) ) / w,   k = floor(w)
    It is coherent, it is the sample analogue of (1/(1−c))·∫_c^1 VaR_u du, and it never falls below VaR. When w < 1 it is the worst loss.

Example: losses 1, 2, ..., 1000 at c = 0.975 give w = 25, VaR = 975 and ES = mean(976..1000) = 988.0. The estimator that also averages the VaR observation itself (26 points, 987.5) is biased low by about 1/(w+1) of the tail spread, which is 17% at a 250-day window.

c·n and (1−c)·n are snapped to the nearest integer when within 1e-9 of it, so 0.975·1000 is treated as 975 and not 975.0000000001. Results are not clamped: a sample whose quantile is a gain returns a negative VaR. Callers that report "0 if a gain" clamp themselves.

Reference: C. Acerbi and D. Tasche, "On the coherence of expected shortfall", Journal of Banking and Finance 26 (2002) 1487-1503.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    VaR and ES of one sample, both in loss units.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    expectedShortfallSorted(double[] sortedLosses, double confidence)
    Acerbi-Tasche expected shortfall of an ascending sample.
    of(double[] losses, double confidence)
    VaR and ES of an unsorted loss sample.
    ofSorted(double[] sortedLosses, double confidence)
    VaR and ES of a loss sample already sorted ascending.
    static double
    varSorted(double[] sortedLosses, double confidence)
    Lower empirical quantile L(ceil(c·n)) of an ascending sample.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      public static TailRisk.Tail of(double[] losses, double confidence)
      VaR and ES of an unsorted loss sample. The input is not modified.
    • ofSorted

      public static TailRisk.Tail ofSorted(double[] sortedLosses, double confidence)
      VaR and ES of a loss sample already sorted ascending.
    • varSorted

      public static double varSorted(double[] sortedLosses, double confidence)
      Lower empirical quantile L(ceil(c·n)) of an ascending sample.
    • expectedShortfallSorted

      public static double expectedShortfallSorted(double[] sortedLosses, double confidence)
      Acerbi-Tasche expected shortfall of an ascending sample.