Skip to content

Instantly share code, notes, and snippets.

@agabrielsen
agabrielsen / jarquebera.R
Created June 19, 2016 22:06
Jarque-Bera Test for Normality
jarquebera <- function(x){
#' Test for normality Jarque-Bera, Bowman-Shenton(1977))
#'
#' Calculates Jarque-Bera Normality test & corresponding p-value
#' @param x a Nx1 vector
#' @return Jarque-Bera statistic
#' @export
#' @author Alexandros Gabrielsen
T=length(x)
@agabrielsen
agabrielsen / bintest.R
Created June 19, 2016 00:29
Binomial Test: Upper One/Two Tailed Upper Bounds Assuming Independence/Asset Correlation
bintest = function(ci, pds, freq, obspds, statistic) {
# ---------------------------------------------------------------
# PURPOSE:
# Binomial Test: Upper One/Two Tailed Upper Bounds Assuming Independence
# ---------------------------------------------------------------
# INPUS:
# ci Confidence Interval
# pds Expected PDs
# freq Frequency of ratings
# obspds Observed PDs
@agabrielsen
agabrielsen / Gini.R
Created June 19, 2016 00:28
PD Accuracy Ratio (Gini)
Gini = function(freq, default, saveplot, showplot) {
# ---------------------------------------------------------------
# PURPOSE:
# PD Accuracy Ration (Gini)
# Basel Committee on Banking Supervision
# Working Paper No. 14
# Studies on the Validation of Internal Rating Systems
# ---------------------------------------------------------------
# INPUTS:
# freq Frequency
@agabrielsen
agabrielsen / HosmerLemeshow.R
Created June 19, 2016 00:27
Hosmer Lemeshow Test
HosmerLemeshow = function(freq,obspds,ci) {
# ---------------------------------------------------------------
# PURPOSE:
# Hosmer Lemeshow Test
# Basel Committee on Banking Supervision
# Working Paper No. 14
# Studies on the Validation of Internal Rating Systems
# ---------------------------------------------------------------
# INPUTS:
# freq Frequency
@agabrielsen
agabrielsen / KendallTau.R
Created June 19, 2016 00:27
Kendall Tau
KendallTau = function(X,Y,type) {
# ---------------------------------------------------------------
# PURPOSE:
# Kendall Tau
# Basel Committee on Banking Supervision
# Working Paper No. 14
# Studies on the Validation of Internal Rating Systems
#
# ---------------------------------------------------------------
# INPUTS:
@agabrielsen
agabrielsen / PSI.R
Created June 19, 2016 00:26
Population Distribution Index (PSI) Studies on the Validation of Internal Rating Systems
PSI = function(X,Y,saveplot,showplot,ratings,psinames) {
# ---------------------------------------------------------------
# PURPOSE:
# Population Distribution Index (PSI)
# Basel Committee on Banking Supervision
# Working Paper No. 14
# Studies on the Validation of Internal Rating Systems
# ---------------------------------------------------------------
# INPUTS:
# X Array of (%) of Frequency
@agabrielsen
agabrielsen / plutotashe.R
Created June 19, 2016 00:25
Pluto and Tasche, 2005, Estimating Probabilities of Default for Low Default Portfolios
---------------------------------------------------------------
# PURPOSE:
# Pluto and Tasche, 2005, Estimating Probabilities of Default for
# Low Default Portfolios
# Basel Committee on Banking Supervision
# Working Paper No. 14
# Studies on the Validation of Internal Rating Systems
# ---------------------------------------------------------------
# INPUTS:
# n number of obligors for a particular grade
@agabrielsen
agabrielsen / VaRLR.R
Created June 19, 2016 00:22
Value-at-Risk backtesting for long and short positions using the unconditional coverage, independence and conditional coverage family of tests
VaRLR <-function(fdata, VaR, alpha, position) {
#
#-----------------------------------------------------------------------
# PURPOSE:
# Value-at-Risk backtesting for long and short positions using
# the unconditional coverage, independence and conditional coverage family
# of tests
#-----------------------------------------------------------------------
# USAGE:
# results = VaRLR(fdata, VaR, alpha, position, options)
@agabrielsen
agabrielsen / ewma.R
Created June 19, 2016 00:21
Exponentially Weighted Moving Average (Univariate & Multivariate)
# Exponentially Weighted Moving Average
# ----------------------------------------------------
# PURPOSE:
# Estimates the Exponentially Weighted Moving Average.
#---------------------------------------------------
# USAGE:
# [M, V, H, C] = ewma(data,lambda, method)
#
# INPUTS:
# data = ( M x N ) vector