Dear Statalist members,
would be great to have some help! I am relatively new to Stata.
I have built a function cdsP in Mata. I would like to save the results of the function in "cds_model".
I am using st_store, but my code does not seem to save the results of the function (price)
Any help would be greatly appreciated!
Thanks
Luca
would be great to have some help! I am relatively new to Stata.
I have built a function cdsP in Mata. I would like to save the results of the function in "cds_model".
I am using st_store, but my code does not seem to save the results of the function (price)
Any help would be greatly appreciated!
Thanks
Luca
Code:
mata
mata clear
real scalar cdsP(real scalar alpha1, real scalar beta, real scalar sigma, real scalar ni, real scalar rr, real scalar lambda, real scalar X)
{
real matrix A, B, C, D, G, H, D1, CDS_model_timesteps, GG
real scalar phi, K, price
A = J(1,20,0)
B = J(1,20,0)
C = J(1,20,0)
D = J(1,20,0)
G = J(1,20,0)
H = J(1,20,0)
D1 = st_data(.,("c2-c241"))
Num = J(1,20,0)
Dem = J(1,20,0)
for (z = 1; z<=20; z++) {
phi = (2*sigma^2 + beta^2)^0.5
K = (beta + phi)/(beta - phi)
C[1, z] = exp((ni^2 * (z*0.25)^3 )/ 6)
B[1, z] = (beta - phi)/sigma^2 + (2*phi)/(sigma^2 * ((1 - K)* exp(phi * (z*0.25))))
A[1, z] = exp(((alpha1*(beta + phi))/(sigma^2)) * (z*0.25))*((1 - K)/((1 -K) * exp(phi*(z*0.25))))^((2*alpha1)/sigma^2)
H[1, z] = exp((alpha1*(beta+ phi)+ phi* sigma^2)/ sigma^2 *(z*0.25))*((1 - K)/(1 - K* exp(phi*(z*0.25))))^((2*alpha1)/sigma^2 +2)
D[1, z] = D1[X,z+1]
G[1, z] = (alpha1/phi) * (exp(phi * (z*0.25))-1) * exp((alpha1 * (beta+phi))/(sigma^2) * (z*0.25))*((1 - K)/((1 - K) * exp(phi*(z*0.25))))^((2 * alpha1)/(sigma^2) +1)
Num[1,z] = (exp(B[1,z]*lambda)*D[1,z] *(G[1,z] + H[1,z] * lambda))
Dem[1,z] = (A[1,z]*exp(B[1,z] *lambda)*D[1,z])
}
CDS_model_timesteps = ((1-rr)*Num):/Dem
price = rowsum(CDS_model_timesteps)
return(price)
_st_store(X,"cds_model",price)
}
end

Comment