Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbeque/daae5bed01531996f9c6 to your computer and use it in GitHub Desktop.
Save barbeque/daae5bed01531996f9c6 to your computer and use it in GitHub Desktop.
/* Expected exposure is the expected (average) credit exposure conditional on positive market values */
def expectedExposure(marketValues: Seq[Double]): Double = {
val exposures = marketValues.filter(_ > 0f)
if (exposures.size == 0) 0.0
else exposures.sum / exposures.size
}
/* A high percentile (95%) of the distribution of exposures at any particular future date. Also called Peak Exposure (PE) */
def potentialFutureExposure(marketValues: Seq[Double], confidenceLevel: Double): Double = {
val exposures = marketValues.filter(_ > 0)
val size = exposures.size
if (size > 0) {
val sortedVector = exposures.sorted
val indexR = (size * ((100 - confidenceLevel) / 100)) - 1
val upper = math.ceil(indexR).toInt.max(0).min(size - 1)
val lower = math.floor(indexR).toInt.max(0).min(size - 1)
if (lower == upper)
sortedVector.apply(upper)
else /* interpolate if necessary */
((upper - indexR) * sortedVector(lower)) + ((indexR - lower) * sortedVector(upper))
}
else 0.0
}
// Maximum Potential future exposure (PFE) is the maximum PFE across all dates
def maxPotentialFutureExposure(pfeProfile: Seq[Double]): Double = {
pfeProfile.max
}
/* Expected Positive Exposure (EPE) means the weighted average over time of expected exposures */
def expectedPositiveExposure(eeProfile: Seq[Double], timeIntervals: Seq[Double]) =
timeIntervals.zip(eeProfile).map(x => (x._1 * x._2)).sum / timeIntervals.sum
/* Effective Expected Exposure (EEE) is the maximum expected exposure which occurs over the exposure horizon time interval. */
def effectiveExpectedExposure(eeProfiles: Seq[(Double, Double)]) = eeProfiles.sortBy(_._1).scanLeft((0.0, 0.0)) { (a, b) => (b._1, math.max(a._2, b._2)) }.drop(1)
/* Effective Expected Positive Exposure (EEPE) is the weighted average over time of effective expected exposure */
def effectiveExpectedPositiveExposure(eeeProfile: Seq[Double], timeIntervals: Seq[Double]) =
eeeProfile.zip(timeIntervals).map(x => (x._1 * x._2)).sum / timeIntervals.sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment