print("%d\t %s\t %0.1f\t %0.1f\t %0.1f\t %0.1f\t %0.1f\t" % (n+1, rating[n], A[n], K[n], 1e4*defaultProb[n], 1e2*sigma[n], 1e2*mu[n])) # Asset moments and other important inputs EA = mert.getExpA(mu,dt,A) VA = mert.getVarA(mu,sigma,dt,A,K) SA = np.sqrt(VA) C = mert.generateCorrelationMatrix(N,myRho) CA = np.zeros([N,N]) myOmega = np.zeros([N,N]) for n in range(0,N): for m in range(0,N): CA[n,m] = mert.getCorAB(sigma[n],sigma[m],C[n,m],dt) myOmega[n,m] = mert.getCovAB(A[n],A[m],mu[n],mu[m], sigma[n],sigma[m],CA[n,m],dt) print("ASSET correlation matrix") util.printMatrix(CA,'%0.2f') DC = np.zeros([N,N]) for n in range(0,N): for m in range(0,N): DC[n,m] = mert.getDefaultCorAB(A[n],A[m], mu[n],mu[m],sigma[n],sigma[m],CA[n,m],dt,K[n],K[m]) print("DEFAUL correlation matrix") util.printMatrix(DC,'%0.2f') print("Running MERTON MODEL") V = np.diag(sigmaA) OmegaA = np.dot(np.dot(V,CA),V) hatA,Omega = mert.computeAssetValueMoments(N,A,mu,sigma,CA,dt) startTime = time.clock() el[0],ul[0],var[:,0],es[:,0] = mert.mertonDirectSimulation(N,M,K,hatA,OmegaA,c6,alpha) cTime[0] = (time.clock() - startTime) # =====================
np.linspace(0, K - 1, K).astype(int), np.linspace(0, K - 1, K).astype(int)) plt.xlabel('Rating Notch Change') plt.ylabel('Proportion (Per cent)') # Compute the empirical number of transitions M = mc.getTransitionCount(K, T, N, D) print("We expect to observe %d transitions." % (N * (T - 1))) print("We observe, however, %d transitions." % (np.sum(M))) print("And, %d default(s)." % (np.sum(D[:, -1] == 4))) print("Counted default(s) are %d." % (M[-1, -1])) print("Default span %d periods." % (np.sum(D == 4) - np.sum(D[:, -1] == 4))) print("+++++ Counts +++++++") # Estimate the transition matrix with cohort approach Phat = mc.estimateTransitionMatrix(K, T, N, D, 1, 0) print("+++++ Estimated P +++++++") util.printMatrix(Phat, "%0.4f") print("+++++ True P +++++++") util.printMatrix(P, "%0.4f") print("+++++ Error +++++++") util.printMatrix(np.abs(P - Phat), "%0.4f") print("The average error is %0.2f" % (1e2 * np.mean(np.abs(P[0:-1, :] - Phat[0:-1, :])))) # Compute SE by the observed Fisher information xTrue = np.reshape(P, K * K) x = np.reshape(Phat, K * K) mVector = np.reshape(M, len(M)**2) H1 = -np.diag(np.divide(mVector, x**2)) seAnl = np.sqrt(-np.diag(anp.inv(H1[0:-K, 0:-K]))) # Examine analytic results print("Printing FISHER-INFORMATION CONFIDENCE BOUNDS") print("p\t pHat\t SE\t CI")
# ============================================= # 1/ Simple (1-parameter) MLE Example # ============================================= # Load pre-simulated data X = np.load(XFile) Y = np.load(YFile) pMap = np.load(pMapFile) allP = np.load(allPFile) X0 = np.load(X0File) # To generate your own data, merely use the following function call # X,Y,pMap,allP,X0 = ac.createRatingData(K,N,T,P,wStart,myRho,nu,isT) # Extract key elements from the data pVec, nVec, kVec = ac.getSimpleEstimationData(T, X, allP) Phat = mc.estimateTransitionMatrix(K, T, N, X, 1, 0) print("==== True Transition Matrix ==========") util.printMatrix(P, '%0.4f') print("==== Estimated Transition Matrix ==========") util.printMatrix(Phat, '%0.4f') print("==== Difference ==========") util.printMatrix(P - Phat, '%0.4f') # Grid search gridSize = 100 rhoRange = np.linspace(0.001, 0.999, gridSize) logL = np.zeros(gridSize) cTime = np.zeros(gridSize) for m in range(0, gridSize): if np.mod(m + 1, 25) == 0: print("Running ITERATION %d" % (m + 1)) startTime = time.time() logL[m] = -ac.logLSimple(rhoRange[m], T, pVec, nVec, kVec) cTime[m] = time.time() - startTime
) # Asset moments and other important inputs EA = mert.getExpA(mu, dt, A) VA = mert.getVarA(mu, sigma, dt, A, K) SA = np.sqrt(VA) C = mert.generateCorrelationMatrix(N, myRho) CA = np.zeros([N, N]) myOmega = np.zeros([N, N]) for n in range(0, N): for m in range(0, N): CA[n, m] = mert.getCorAB(sigma[n], sigma[m], C[n, m], dt) myOmega[n, m] = mert.getCovAB( A[n], A[m], mu[n], mu[m], sigma[n], sigma[m], CA[n, m], dt ) print("ASSET correlation matrix") util.printMatrix(CA, "%0.2f") DC = np.zeros([N, N]) for n in range(0, N): for m in range(0, N): DC[n, m] = mert.getDefaultCorAB( A[n], A[m], mu[n], mu[m], sigma[n], sigma[m], CA[n, m], dt, K[n], K[m] ) print("DEFAUL correlation matrix") util.printMatrix(DC, "%0.2f") print("Running MERTON MODEL") V = np.diag(sigmaA) OmegaA = np.dot(np.dot(V, CA), V) hatA, Omega = mert.computeAssetValueMoments(N, A, mu, sigma, CA, dt) startTime = time.perf_counter() el[0], ul[0], var[:, 0], es[:, 0] = mert.mertonDirectSimulation( N, M, K, hatA, OmegaA, c6, alpha
# ============================================= # 1/ Simple (1-parameter) MLE Example # ============================================= # Load pre-simulated data X = np.load(XFile) Y = np.load(YFile) pMap = np.load(pMapFile) allP = np.load(allPFile) X0 = np.load(X0File) # To generate your own data, merely use the following function call # X,Y,pMap,allP,X0 = ac.createRatingData(K,N,T,P,wStart,myRho,nu,isT) # Extract key elements from the data pVec, nVec, kVec = ac.getSimpleEstimationData(T, X, allP) Phat = mc.estimateTransitionMatrix(K, T, N, X, 1, 0) print("==== True Transition Matrix ==========") util.printMatrix(P, "%0.4f") print("==== Estimated Transition Matrix ==========") util.printMatrix(Phat, "%0.4f") print("==== Difference ==========") util.printMatrix(P - Phat, "%0.4f") # Grid search gridSize = 100 rhoRange = np.linspace(0.001, 0.999, gridSize) logL = np.zeros(gridSize) cTime = np.zeros(gridSize) for m in range(0, gridSize): if np.mod(m + 1, 25) == 0: print("Running ITERATION %d" % (m + 1)) startTime = time.time() logL[m] = -ac.logLSimple(rhoRange[m], T, pVec, nVec, kVec) cTime[m] = time.time() - startTime