def getPy(p,y,p1,p2,whichModel,v=0): if whichModel==0: # Gaussian threshold return th.computeP(p,p1,y) elif whichModel==1: # beta return y*np.ones(len(p)) elif whichModel==2: # CreditRisk+ v = p*(1-p1+p1*y) return np.maximum(np.minimum(1-np.exp(-v),0.999),0.0001) elif whichModel==3: # logit return np.reciprocal(1+np.exp(-(p1+p2*y))) elif whichModel==4: # probit return norm.ppf(p1+p2*y) elif whichModel==5: # Weibull return np.maximum(np.minimum(1-np.exp(-y),0.999),0.0001)*np.ones(len(p)) if whichModel==6: # t threshold return th.computeP_t(p,p1,y,v,p2)
def isThreshold(N, M, p, c, l, myRho, nu, shiftMean, isT, invVector=0): mu = 0.0 gamma = 0.0 if shiftMean == 1: mu = getOptimalMeanShift(c, p, l, myRho) theta = np.zeros(M) cgf = np.zeros(M) qZ = np.zeros([M, N]) G = np.transpose(np.tile(np.random.normal(mu, 1, M), (N, 1))) e = np.random.normal(0, 1, [M, N]) if isT == 1: gamma = -2 W = np.random.chisquare(nu, M) myV = W / (1 - 2 * gamma) V = np.transpose(np.sqrt(np.tile(myV, (N, 1)) / nu)) num = (1 / V) * myT.ppf(p, nu) * np.ones( (M, 1)) - np.multiply(np.sqrt(myRho), G) pZ = norm.cdf(np.divide(num, np.sqrt(1 - myRho))) elif isT == 2: V = np.transpose( np.sqrt(np.tile(np.random.gamma(nu, 1 / nu, M), (N, 1)))) num = (1 / V) * invVector * np.ones( (M, 1)) - np.multiply(np.sqrt(myRho), G) pZ = norm.cdf(np.divide(num, np.sqrt(1 - myRho))) else: pZ = th.computeP(p, myRho, G) for n in range(0, M): theta[n] = vc.getSaddlePoint(pZ[n, :], c, l, 0.0) qZ[n, :] = getQ(theta[n], c, pZ[n, :]) cgf[n] = vc.computeCGF(theta[n], pZ[n, :], c) I = np.transpose(1 * np.less(e, norm.ppf(qZ))) L = np.dot(c, I) if isT == 1: rnChi = np.exp(-gamma * myV - (nu / 2) * np.log(1 - 2 * gamma)) else: rnChi = np.ones(M) if shiftMean == 1: rn = computeRND(theta, L, cgf) * np.exp(-mu * G[:, 0] + 0.5 * (mu**2)) * rnChi else: rn = computeRND(theta, L, cgf) * rnChi tailProb = np.mean(np.multiply(L > l, rn)) eShortfall = np.mean(np.multiply(L * (L > l), rn)) / tailProb return tailProb, eShortfall
def jointIntegrandRegion(g, p, q, rhoVec): p1 = th.computeP(p, rhoVec[0], g) p2 = th.computeP(q, rhoVec[1], g) density = util.gaussianDensity(g, 0, 1) f = p1 * p2 * density return f
def integrateGaussianMoment(g, r, myP, myMoment): integrand = np.power(th.computeP(myP, r, g), myMoment) return integrand * util.gaussianDensity(g, 0, 1)
def jointIntegrand(g, p, q, myRho): p1 = th.computeP(p, myRho, g) p2 = th.computeP(q, myRho, g) f = p1 * p2 * util.gaussianDensity(g, 0, 1) return f
def getProdCMF(g, myRho, myP, myN, myK): pg = th.computeP(myP, myRho, g) return np.multiply(util.getBC(myN, myK), np.power(pg, myK) * np.power(1 - pg, myN - myK))
def getCMF(g, myRho, myP, myN, myK): pg = th.computeP(myP, myRho, g) f = util.getBC(myN, myK) * np.power(pg, myK) * np.power(1 - pg, myN - myK) cmf = f * util.gaussianDensity(g, 0, 1) return cmf
def meanShiftOF(mu, c, p, l, myRho): pZ = th.computeP(p, myRho, mu) theta = vc.getSaddlePoint(pZ, c, l, 0.0) f_l = -theta * l + vc.computeCGF(theta, pZ, c) return -(f_l - 0.5 * np.dot(mu, mu))
def getBaselK(p_n, tenor, alpha): g = norm.ppf(1 - alpha) rhoBasel = getRho(p_n) ma = getMaturityAdjustment(tenor, p_n) pG = th.computeP(p_n, rhoBasel, g) return np.multiply(pG - p_n, ma)
def getW(myP, myA, myRho, myAlpha): num = th.computeP(myP, myRho, norm.ppf(1 - myAlpha)) - myP den = myP * (gamma.ppf(myAlpha, myA, 0, 1 / myA) - 1) return np.divide(num, den)
def dnu(myAlpha, myP, myC, myRho): pn = th.computeP(myP, myRho, norm.ppf(1 - myAlpha)) ratio = norm.ppf(pn) constant = np.sqrt(np.divide(myRho, 1 - myRho)) return -constant * np.dot(norm.pdf(ratio) * np.power(myC, 2), 1 - 2 * pn)
def nu(myAlpha, myP, myC, myRho): pn = th.computeP(myP, myRho, norm.ppf(1 - myAlpha)) return np.dot(np.power(myC, 2), pn * (1 - pn))
def d2mu(myAlpha, myP, myC, myRho): constant = np.divide(myRho, 1 - myRho) ratio = norm.ppf(th.computeP(myP, myRho, norm.ppf(1 - myAlpha))) return -constant * np.dot(ratio * myC, norm.pdf(ratio))
def mu(myAlpha, myP, myC, myRho): pn = th.computeP(myP, myRho, norm.ppf(1 - myAlpha)) return np.dot(myC, pn)