Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active October 12, 2015 14:28
Show Gist options
  • Save joshbode/4041186 to your computer and use it in GitHub Desktop.
Save joshbode/4041186 to your computer and use it in GitHub Desktop.
Beta Distribution
library(gtools)
step_length = 0.01
x = seq(0.0, 1.0, step_length)
a = 20.0
b = 2.0
beta_mean = a / (a + b)
print(beta_mean)
print(qbeta(beta_mean, a, b))
y = dbeta(x, a, b)
y_max = max(y)
k = binsearch(function(x) { x - beta_mean / step_length }, x / step_length)$where
k = k[length(k)] + 1
plot(smooth.spline(100.0 * x, y), type='l', xlab="LGD", ylab="Density")
polygon(100.0 * c(0.0, x[1:k], x[k]), c(0.0, y[1:k], 0.0), col='gray')
# add average line
lines(100.0 * x[k] * c(1.0, 1.0), c(0.0, y_max), lty=2)
text(100.0 * x[k], y_max + 0.1, labels=c('mean'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment