Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mata (command mm_root) within a Stata loop does not work

    Dear Statalist members,

    I'm trying to solve the unknown Lambda using mm_root function of the moremata package. The original formula can be found in O'Kane & Turnbull (2003): Valuation of CDS, Lehman Brothers, page 13 (http://www.javaquant.net/papers/ValuationCDSLehman.pdf)

    Following the post of Attaullah Shah, I could fix it for one company over 48 months (http://www.statalist.org/forums/forum/general-stata-discussion/mata/1345852-mm_root-function-to-solve-for-unknows). However, I need to find the unknown variable lambda for 30 companies over 48 months.

    Here is the code for one company (I have just created a fake dataset in order to reduce the complexity).
    Code:
    clear all
    set obs 3 // number of months (in my dataset 48)
    gen fraction_0 = 0
    gen fraction_1 = 0.25
    gen fraction_2 = 0.5
    gen fraction_3 = 0.75
    gen fraction_4 = 1
    gen Rec = 0.4
    gen Gen_fraction = 0.25
    gen CDS_1Y = 0.021
    gen lambda = .
    
    mata
    v = J(1,1,.)
    st_view(v, . , "lambda CDS_1Y fraction_0 fraction_1 fraction_2 fraction_3 fraction_4 Rec Gen_fraction")
    function y(lambda, a, b, c, d, e, f, g, h) {
    return (a/(1-g)*((h*0.99822*exp(-lambda*c))+(h*0.99648*exp(-lambda*d))+(h*0.99476*exp(-lambda*e))+(h*0.99301*exp(-lambda*f)))-(0.99822*(exp(-lambda*b)-exp(-lambda*c))+0.99648*(exp(-lambda*c)-exp(-lambda*d))+0.99476*(exp(-lambda*d)-exp(-lambda*e))+0.99301*(exp(-lambda*e)-exp(-lambda*f))))
    }
    for (i=1;i<=rows(v);i++) {
    r=mm_root(lambda=., &y(),0,1,0.01,1000,v[i,2], v[i,3], v[i,4], v[i,5], v[i,6], v[i,7], v[i,8], v[i,9])
    v[i,1] = lambda
    }
    end
    l, noo
    I have tried to write a loop in Stata, which includes the Mata calculation, but it doesn't work.
    The idea with curly brackets around mata code was originated from Niels Henrik Bruun (http://www.statalist.org/forums/foru...ile-loop-error).

    Here is my Stata-loop

    Code:
    clear all
    local currentfile C:\...
    cd "`currentfile'"
    foreach name company1 company2 company3 company4 ... {
    use "`currentfile'\OriginalDataset.dta", clear
    collapse CDS_1Y_`name' fraction_0 fraction_1 fraction_2 fraction_3 fraction_4 Rec Gen_fraction, by(month)
    rename CDS_1Y_`name' CDS_1Y // that was done for the mata-code to avoid any changes in the mata code above.
    gen lambda = .
    mata {
    ... code above (but without the "end" command)
    }
    rename CDS_1Y CDS_1Y_`name'
    rename lambda lambda_`name' // lambda for each company can be uniquely identified
    merge 1:1 month using "`currentfile'\OriginalDataset.dta", nogen
    save "`currentfile'\OriginalDataset.dta", replace
    }
    The error message is as follows.
    Code:
    'function' found where almost anything else expected
    r(3000)
    As I mentioned above, the code works perfectly for one company, however, I don't know how to modify the code, that it will work for all of my companies.
    Additionally, I read some proposals to put every calculation step in Mata and call the Mata function within the Stata loop - but again, I wasn't able to apply it for my problem.

    I'm looking forward to hearing from you!

    Thank you very much in advance!
    Kind regards from Germany, Marco
Working...
X