This is a credit risk calculator based on a simplified version of the Merton model.
One of my assignments for MAT1856 was to compare different credit risk models, the Merton model being one of them.
I thought the math behind it was fairly interesting, and so I decided to make a write-up and public implementation for it.
The Merton model is based on the Black-Scholes formula for option prices,
and views a company's equity as a call option on its assets.
Section I is the calculator itself.
The background for the Merton model can be found in section II.
Section III contains the optimizer used for finding asset and asset volatility.
Appendix A contains more detailed calculations.
For software packages, I am using Math.js for matrix calculations and CDF evaluations, and MathJax for rendering LaTeX in HTML.
You can see all my code at the GitHub repository.
If you are interested in learning more about this, feel free to watch my professor's lecture.
I. Calculator
If you need help finding these values, you can go to the following sites (after choosing a company):
Go to Financials -> Balance Sheet -> choose "Total stockholders' equity"
Compute $(\text{number of outstanding shares}) \times (\text{stock price})$. Stock price should be obvious, shares outstanding can be found in the Statistics tab.
Equity Volatility: Go to Yahoo! Finance, extract daily historical stock prices,
and compute the annualized historical volatility
following the method found here.
Debt: Go to Yahoo! Finance -> Financials -> Balance Sheet -> choose either "Current Debt" or "Total Liabilities".
Risk-Free Rate: Go to Bank of Canada and choose the latest 3-month treasury bill yield.
There are a lot of variables in this equation, so here is a legend for what they represent.
$E$ is the call option price (company's equity)
$V$ is the underlying price (company's assets)
$K$ is the strike price (company's debt)
$r$ is the risk-free rate
$t$ is the time to maturity (of the debt)
$\sigma$ is the volatility of the underlying instrument.
$N$ is the normal cumulative distribution function with mean $0$ and standard deviation $1$.
The Greeks
Buying options is risky. To quantify this risk, we use "the Greeks" which are essentially names given to varying derivatives of the BSF with respect to its inputs.
The Merton model uses a few of them, and the derivation of their simplified values can be found in Appendix A.
Delta ($\Delta$): $\partial_V E$
Vega ($\nu$): $\partial_\sigma E$
Gamma ($\Gamma$): $\partial_V^2 E$
Vanna: $\partial_\sigma \Delta$
II.2 Merton Model
As stated before, the Merton model looks at a companies equity ($E$) as a call option on its assets ($V$) with the strike price as its debt ($K$). That is, $E = \max(V - K, 0)$.
When we exercise this option, it means the company has not defaulted and we find $E = V - K$. It can be shown the probability of this happening is $N(d_2)$.
Thus, the probability of default is given by $1 - N(d_2) = N(-d_2)$. So, if we can find $d_2$, we are good to go! However, there is a slight caveat.
Frequently, a companies assets and asset volatility cannot be directly observed. So, we need to derive them from other known values.
One value that is frequently observed is equity. The equity of a company can (rougly) be calculated as $E = (\text{number of outstanding shares}) \times (\text{stock price})$.
By going to Yahoo! Finance, you can get the daily values for $E$. With the daily values, you can also get the equity volatility.
Equity volatility and asset volatility are related via:
\begin{equation}
\sigma_E E = \sigma_V V \partial_V E = \sigma_V V \Delta \tag{2}
\end{equation}
Let's summarize two facts we know so far:
The calculated equity, $E$, must equal the observed equity $E_0$.
The actual values $(V, \sigma_V)$ must satisfy (2).
Thus, we can create a root-finding problem to find $(V, \sigma_V)$, which takes the following form.
To find the roots of $f$, I used Powell's Dog Leg Method (PDL from now on). PDL is the method used by SciPy's fsolve (which calls MINPACK's hybrd/hybrj).
Powell's dog leg method essentially combines the Gauss-Newton algorithm and the gradient descent algorithm.
I think the Wikipedia page does a good enough job explaining how it works.
My 2D specific implementation can be found in the function findPDLStep.
Appendix A: Derivations
In this section, I go through some derivations that simplify computations and code. We will denote the PDF of the CDF $N(\cdot)$ as $n(\cdot) = \frac{1}{\sqrt{2\pi}} e^{-(\cdot)^2/2}$.
Note that $\partial_V d_1$ and $\partial d_2 d_2$ are given by:
Here, we will compute simplified expressions for the Greeks.
A.1.1 Delta
First, let's compute the arbitrary quantity $n(d_2)/n(d_1)$. First, we note that we can rewrite $d_1 = \frac{\log \frac{V}{Ke^{-rt}} + \frac{\sigma^2 t}{2}}{\sigma\sqrt{t}} = A + B$ with $d_2 = A - B$.
To compute the Jacobian used in the PDL, we need to find the Jacobian of $f = (f_1, f_2)$.
The first function, $f_1$ is easy as it is just $E - E_0$ so that $\partial_V f_1 = \Delta$ and $\partial_\sigma f_1 = \nu$.
The second function is a little more involved, but still not that difficult with the representations we've built so far.
\begin{align}
\partial_V f_2 &= (\sigma_S - \sigma_V)\Delta - \sigma_V V \Gamma \\
\partial_\sigma f_2 &= \sigma_S \nu - V \Delta - \sigma_V V M
\end{align}