def runModelSuite(N, M, P, C, alpha, nu, myRho, rhoTarget, tenor, modelType): startTime = time.time() if modelType == 0: # Binomial (independent-default) model el, ul, var, es = bp.independentBinomialSimulation(N, M, P, C, alpha) simTime = (time.time() - startTime) elif modelType == 1: # Gaussian threshold model el, ul, var, es = th.oneFactorThresholdModel(N, M, P, C, myRho, nu, alpha, 0) simTime = (time.time() - startTime) elif modelType == 2: # Beta-binomial mixture model myP = np.mean(P) a, b = mix.betaCalibrate(myP, rhoTarget) M1 = mix.betaMoment(a, b, 1) M2 = mix.betaMoment(a, b, 2) print("a, b parameters are %0.1f and %0.1f." % (a, b)) print("Targeted: %0.4f and calibrated: %0.4f default probability." % (myP, M1)) print("Targeted: %0.3f and calibrated: %0.3f default correlation." % (rhoTarget, np.divide(M2 - M1**2, M1 - M1**2))) el, ul, var, es = mix.betaBinomialSimulation(N, M, C, a, b, alpha) simTime = (time.time() - startTime) elif modelType == 3: # t-distributed threshold model el, ul, var, es = th.oneFactorThresholdModel(N, M, P, C, myRho, nu, alpha, 1) simTime = (time.time() - startTime) elif modelType == 4: # Basel IRB approach mAdjustedC = np.multiply(C, getMaturityAdjustment(tenor, P)) el = np.dot(P, mAdjustedC) var = getBaselRiskCapital(P, tenor, C, alpha) ul = np.sum(P * (1 - P) * mAdjustedC) es = var simTime = (time.time() - startTime) elif modelType == 5: # Asymptotic Gaussian threshold model (ASRF) meanP = np.maximum(0.0009, np.median(P)) a, b = th.getAsrfMoments(meanP, myRho) el = np.sum(C) * a ul = np.sum(C) * b pdf, cdf, var, es = th.asrfModel(meanP, myRho, C, alpha) simTime = (time.time() - startTime) return el, ul, var, es, simTime
import varianceReduction as vr plt.close("all") # Model input and parameters c = np.load(expFile) p = np.load(dpFile) N = len(c) alpha = np.array([0.95, 0.97, 0.99, 0.995, 0.999, 0.9997, 0.9999]) myP = np.mean(p) # See ex04.py for the calibration of the t-threshold model rhoT = 0.06339367516538337 nu = 8.0269146874964417 M = 1000000 S = 10 # Caution, not very many. print("Running t-THRESHOLD MODEL. Caution: it's a bit slow!") el, ul, var, es = th.oneFactorThresholdModel(N, M, p, c, rhoT, nu, alpha, 1) VaRC = vc.myVaRCYT(var[-1], p, c, rhoT, nu, 6) print("Printing RAW MONTE-CARLO RESULTS") C, V, E = vc.mcThresholdTDecomposition(N, M, S, p, c, rhoT, nu, 1, alpha[-1]) mcVaRC = np.mean(C, 1)[:, 0] print("Printing IMPORTANCE-SAMPLING RESULTS") Mis = 24000 eps = 5 tailProb = np.zeros([S]) varTarget = np.mean(V) isContributions = np.zeros([N, S]) for s in range(0, S): print("Running IS Estimator: %d" % (s + 1)) testIS, thetaZStar, pZ, qZ, cgf, rnDerivative = vr.isThresholdContr( N, Mis, p, c, varTarget, rhoT, nu, -0.2) L_is = np.dot(c, testIS)
def findAlphaGaussian(a, N, M, p, c, l, myRho): elTemp, ulTemp, varTemp, esTemp = th.oneFactorThresholdModel( N, M, p, c, myRho, 0, np.array([a]), 0) return 1e4 * (l - esTemp[0])**2
importlib.reload(vc) importlib.reload(vr) plt.close('all') # Model input and parameters c = np.load(expFile) p = np.load(dpFile) N = len(c) alpha = np.array([0.95,0.97,0.99,0.995,0.999,0.9997,0.9999]) myP =np.mean(p) # See ex04.py for the calibration of the t-threshold model rhoT = 0.06339367516538337 nu = 8.0269146874964417 M = 1000000 S = 10 # Caution, not very many. print("Running t-THRESHOLD MODEL. Caution: it's a bit slow!") el,ul,var,es = th.oneFactorThresholdModel(N,M,p,c,rhoT,nu,alpha,1) VaRC = vc.myVaRCYT(var[-1],p,c,rhoT,nu,6) print("Printing RAW MONTE-CARLO RESULTS") C,V,E = vc.mcThresholdTDecomposition(N,M,S,p,c,rhoT,nu,1,alpha[-1]) mcVaRC = np.mean(C,1)[:,0] print("Printing IMPORTANCE-SAMPLING RESULTS") Mis = 24000 eps = 5 tailProb = np.zeros([S]) varTarget = np.mean(V) isContributions = np.zeros([N,S]) for s in range(0,S): print("Running IS Estimator: %d" % (s+1)) testIS,thetaZStar,pZ,qZ,cgf,rnDerivative = \ vr.isThresholdContr(N,Mis,p,c,varTarget,rhoT,nu,-0.2) L_is = np.dot(c,testIS)
rhoG = resultG.x # (b) Simulate startTime = time.time() el[0], ul[0], var[:, 0], es[:, 0] = th.oneFactorGaussianModel( N, M, p, c, rhoG, alpha) cTime[0] = (time.time() - startTime) print("Running t THRESHOLD MODEL") # (a) Calibrate tModel = scipy.optimize.fsolve(th.tCalibrate, np.array([startRho, startNu]), args=(myP, rhoTarget, tDependenceTarget)) rhoT = tModel[0] nu = tModel[1] # (b) Simulate startTime = time.time() el[1], ul[1], var[:, 1], es[:, 1] = th.oneFactorThresholdModel( N, M, p, c, rhoT, nu, alpha, 1) cTime[1] = (time.time() - startTime) print("Running VARIANCE-GAMMA MODEL") # (a) Calibrate kTarget = 6 vgModel = scipy.optimize.fsolve(th.nvmCalibrate, np.array([0.2, 1]), args=(myP, rhoTarget, kTarget, 0)) rhoVG = vgModel[0] vgA = vgModel[1] nvmOutcome = th.nvmCalibrate(np.array([rhoVG, vgA]), myP, 0, 0, 0) # (b) Simulate startTime = time.time() el[2], ul[2], var[:, 2], es[:, 2] = th.oneFactorNVMModel(N, M, p, c, rhoVG, vgA, alpha, 0)