vba - modifying function cohort/matrix of ratings migration -




i have function trying modify.

the function below calculates migration frequencies of ids moving 1 rating on period of 1 calendar year restricts computation cohorts formed @ end of year (ystart) , transitions occuring until end of year (yend). if not specified transition matrix estimated year end following first rating year end preceding last rating action.

my problem if have monthly data can calculate transition matrices within calendar year. however, estimating rolling transitions such instance feb-2002 feb-2003, mar-2002 mar-2003. function, such written limits year end year end. can modify compute rolling 12-month frequencies?

function cohort(id, dat, rat, _ optional classes integer, optional ystart, optional yend) 'function written data sorted according issuers , rating dates                     (ascending), 'rating classes numbered 1 classes, last rating class=default, not     rated has number "0"  if ismissing(ystart) ystart =         year(application.worksheetfunction.min(dat)) if ismissing(yend) yend = year(application.worksheetfunction.max(dat))     - 1 if classes = 0 classes = application.worksheetfunction.max(rat)  dim obs long, k long, kn long, integer, j integer, t         integer dim ni() long, nij() long, pij() double, newrat integer redim nij(1 classes - 1, 0 classes), ni(0 classes) obs = id.rows.count  k = 1 obs 'earliest cohort observation can belong year: t = application.max(ystart, year(dat(k)))  'loop through cohorts observation k can belong while t < yend 'is there rating same year? if id(k + 1) = id(k, 1) , year(dat(k + 1)) <= t , k <> obs exit end if 'is issuer in default or not rated? if rat(k) = classes or rat(k) = 0 exit  'add number of issuers in cohort ni(rat(k)) = ni(rat(k)) + 1  'determine rating end of next year (=y+1) 'rating stayed constant if id(k + 1) <> id(k) or year(dat(k + 1)) > t + 1 or k = obs newrat = rat(k) 'rating changed else kn = k + 1 while year(dat(kn + 1)) = year(dat(kn)) , id(kn + 1) = id(kn) if rat(kn) = classes exit 'default absorbing! kn = kn + 1 loop newrat = rat(kn) end if  'add number of transitions nij(rat(k), newrat) = nij(rat(k), newrat) + 1 'exit if observation k cannot belong cohort of y+1 if newrat <> rat(k) exit t = t + 1 loop next k  redim pij(1 classes - 1, 1 classes + 1)  'compute transition frequencies pij=nij/ni = 1 classes - 1 j = 1 classes if ni(i) > 0 pij(i, j) = nij(i, j) / ni(i) next j next  'nr category end = 1 classes - 1 if ni(i) > 0 pij(i, classes + 1) = nij(i, 0) / ni(i) next  cohort = pij  end function 





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -