Matrixwise Calculation Firm Asset Value, Volatility, Debt Value, Spread, Default Prob, ExpRecovery
Calculates the Value of Firm Assets, Volatility of Firm Assets,
DebtValue, CreditSpread, Default Probability and Recovery Rate as per
Merton's Structural Credit Model. The value and volatility of firm assets
are found by Bivariate Newton RootFinding Method of the Merton
Simultaneous Equations. The Newton Method is carried out matrixwise
(i.e. fully vectorised) in a 3d Jacobian so that bivariate ranges of
(E_t,sig_E,K,T) values may simultaneously calculated. (See Examples)
Function requires mtimesx.m available on the Matlab File Exchange at
http://www.mathworks.com/matlabcentral/fileexchange/25977mtimesxfastmatrixmultiplywithmultidimensionalsupport
Outputs
A_t: Value of Firm's Assets [A_t = Call(K,sig_A,A_t,t,T,r)]
sig_A: Volatility of Firm's Assets
D_t: Value of Firm Debt [D_t = pv(K)  Put(K,sig_A,A_t,t,T,r)]
s: Credit Spread
p: Default Probability
R: Expected Recovery
d: BlackScholes Parameter Anonymous Function
Inputs
E_t: Value of Equity
sig_E: Equity Volatility
K: Debt Barrier
t: Estimation Time (Years)
T: Maturity Time (Years)
r: RiskfreeRate
Example 1
T = 5;
t = 0;
K = 500;
sig_E = 0.5;
r = 0.05;
E_t = 1200;
[A_t,sig_A,D_t,s,p,R,d1] = calcMertonModel(E_t,sig_E,K,t,T,r);
Example 2: Variates (sig_E,E_t)
t = 0; r = 0.05;
sig_E = (0.05:0.05:0.8)'; E_t = (100:100:2000)';
[sig_E,E_t] = meshgrid(sig_E,E_t);
K = repmat(600,size(sig_E)); T = repmat(5,size(sig_E));
[A_t,sig_A,D_t,s,p,R,d1] = calcMertonModel(E_t,sig_E,K,t,T,r);
Example 3: Variates (K,T)
t = 0; r = 0.05;
K = (100:100:4000)'; T = (0.1:0.1:10)';
[K,T] = meshgrid(K,T);
sig_E = repmat(0.4,size(K)); E_t = repmat(1300,size(K));
[A_t,sig_A,D_t,s,p,R,d1] = calcMertonModel(E_t,sig_E,K,t,T,r);
1.5  Removed fsolve dependency (Optim Toolbox) for efficiency increase (even in scalar inputs case) Full Code refactorization to facilitate matrixwise calculation of bivariate ranges of {E_t,sig_E,K,T} values using 3d Newton Jacobian solution. 

1.4  Added the BlackScholes Parameter Anonymous Function Handle as an Output to allow for further analysis (sensitivity, greeks etc) d = @(z,A_t,sig_A,T,t,K,r)((1/(sig_A*sqrt(Tt)))*(log(A_t/K) + (r + (z)*0.5*sig_A^2)*(Tt))); z = +1/1 

1.3  Minor code refactoring, code returns the BlackScholes Parameter to allow for further sensitivity analysis & calculation of greeks d = @(z,A_t,sig_A,T,t,K,r)


1.1  Added ExpectedRecovery calclulation [A_t,sig_A,D_t,s,p,R] = calcMertonModel(E_t,sig_E,K,t,T,r); 
Inspired by: MTIMESX  Fast Matrix Multiply with MultiDimensional Support
Trang Ngo (view profile)
Hi Mark, first of all I would like to thank you very much for the code as I have been searching for it so long. I just want to ask that when I paste example 1 in my command window, it appears like that: Undefined function or variable 'calcMertonModel'.
I have downloaded both the calcMertonModel.m as well as mtimesx.m already :( Please help me on this issue. I really appreciate it.
Hong Xu (view profile)
I guess we could add the distance to default in addtion to the default probability.
DTD = norminv(p, 0, 1)
Hong Xu (view profile)
Thanks a lot Mark.
Mark Whirdy (view profile)
Hi Hong
You can replace mtimesx with one of the other methods here.
http://www.mathworks.com/matlabcentral/answers/62382matrixmultiplyslicesof3dmatricies
Best
Mark
Hong Xu (view profile)
hi, thanks for the post. This is great.
Is there a way to go around the error due to not having SDK or C/C++ compiler?
Thanks a lot.
Hong
Nick (view profile)
It works fine. The thing was that ''size'' was used by another function. I reformulated to refer to matrix dimension. Thank you very much again.
Mark Whirdy (view profile)
Hi Panagiotis,
Thanks for your comments.
I redownloaded mtimesx and calcMertonModel and recompiled, but am not getting this issue. Its quite odd since line 61 is (as you say) just the pedestrian "size(E_t)" function call. Difficult for me to troubleshoot from here but if you could try
1. Create the variables from the first example. i.e. T = 5;
t = 0;
K = 500;
sig_E = 0.5;
r = 0.05;
E_t = 1200;
and then run "size(E_t);" at the commandline. Does it work?
2. Pls put a breakpoint at 61, call "calcMertonModel(E_t,sig_E,K,t,T,r);", hover over E_t variable at 61 at tell me what value it has. Assuming that it is "E_t", then step through 61, Does it work?
3. is there any possibility you have another function called "size" in folder on your pathdef (or in current directory) which is being preferentially called above the normal function?
Nick (view profile)
Hello.
First of all, you did an excellent work. Welldone.
I downloaded SDK 7.1, I set up the compiler and then I ran mtimesx_build (... mex mtimesx.c build completed ... you may now use mtimesx).
If I understand right, the next step is to run calcMertonModel.m as it is, without making any adjustments to the script. However, when I run it using the data from example 1 I get this:
Error using calcMertonModel (line 61)
Not enough input arguments.
Line 61 is the command [n,m] = size(E_t), which I guess creates a matrix of dimension equal to the market value of equity. Any help on how to proceed?
Mark Whirdy (view profile)
There is a bug with Microsoft's SDK 7.1 which Matlab have provided a troubleshoot for at
http://www.mathworks.co.uk/matlabcentral/answers/101105howdoiinstallmicrosoftwindowssdk71
Hope this helps
Mark Whirdy (view profile)
Hi Joacim  possible incompatability between the compiler you installed and the version of matlab?
http://www.mathworks.co.uk/support/sysreq/previous_releases.html
Joacim (view profile)
Im trying to follow the instructions in a previous comment, setting up mtimesx, but it wont work. It seems like Matlab can't find any compliers even though I installed them, any suggestions on what to do?
Hannes (view profile)
userfriendly and fast calculating implementation of the Merton model.
fast adaptable and convenient for sensitivty analysis
Jung (view profile)
Mark Whirdy (view profile)
To get mtimesx working, type
mex setup
Select a compiler, [1] is the C compiler shipped with Matlab and will do
Then run
mtimesx_build
which will create a .mex32 file in your path. This is Matlabs "interface" to the C code
Henok Tewolde (view profile)
I really don't know why I am getting this error
Error using mtimesx_build (line 169)
A C/C++ compiler has not been selected with mex setup
Jaeho (view profile)
This model is excellent! Very flexible and userfriendly. It's been very useful for calculating credit spreads of bonds with different asset values, and volatilities.