human
I am working on the file "JupyterFiles/CDOPricingNEW.ipynb".
The first task is:
Large Homogenous Portfolio (LHP) Model
The LHP tranche pricing model is given by the following equations:
$$\begin{aligned}
E[\min (L(T), K)] &= (1-R) \Phi_{2}(C(T), -A(K), - \beta) + K \Phi(A(K)) \
A(K) &= \frac{1}{\beta} \left( C(T) - \sqrt{1 - \beta^{2}} \Phi^{-1} \left( \frac{K}{1-R} \right) \right) \
C(T) &= \Phi^{-1} (1 - Q(T))
\end{aligned}$$
Here:
* $\Phi_{2}$ is the cdf of the bivariate normal distribution. The third perameter being correlation.
* $\beta^{2}$ the correlation
* $R$ is the recovery rate.
* $Q(T)$ is the survival curve
Can you write Python code for it?
 Can you provide a the Python code you requested?
assistant
#Here is the Python code you requested.

from scipy.stats import norm, mvn #normal and bivariate normal
from numpy       import sqrt

def Q_lhp(t, K1, K2, R, beta, Q):
    """Calculates the Tranche survival curve for the LHP model.
    
    Args:
        T (datetime.date): Should be given in "days" (no hours, minutes, etc.)
        K1 (float): The starting value of the tranche. Its value should be 
            between 0 & 1.
        K2 (float): The final value of the tranche. 
        beta (float): Correlation perameter. Its value should be between 0 and 1.
        R (float): Recovery rate. Usually between 0 and 1.
        Q (callable function): A function Q that takes in a dateime.date and
            outputs a float from 0 to 1. It is assumed that Q(0) = 1 and Q is 
            decreasing. Represents survival curve of each credit.
    """
    if Q(t) == 1:
        return 1 # prevents infinity
    def emin(K):
    # Calculates E[min(L(T), K)] in LHP model
        print(Q(t))
        C = norm.ppf(1 - Q(t))
        A = (1 / beta) * (C - sqrt(1 - beta * beta) * norm.ppf(K / (1 - R)))
        return (1 - R) * mvn.mvndst(upper = [C, -1 * A],
                                    lower = [0,0],
                                    infin = [0,0], # set lower bounds = -infty
                                    correl =  -1 * beta)[1] + K * norm.cdf(A)
    return 1 - (emin(K2) - emin(K1)) / (K2 - K1)