from pyscript import document
import math
from scipy.stats import norm

def calculate_corr(asset_class, pd, s=50):
        # Berechnung der Korrelation R basierend auf der Asset-Klasse
    if asset_class == "corporate":
        # Für Unternehmens-Kredite: PD-abhängige Korrelation für Standardwerte
        R = 0.12 * (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)) + 0.24 * (1 - (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)))
        

    elif asset_class == "SME":
        R = 0.12 * (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)) + 0.24 * (1 - (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)))
        s=max(0,min(s,50))
        firm_size_adjustment = 0.04 * (1 - (s - 5) / 45)
        R -= firm_size_adjustment


    elif asset_class == "sovereign":
        R = 0.12 * (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)) + 0.24 * (1 - (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)))
    
    elif asset_class == "bank":
        R = 0.12 * (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)) + 0.24 * (1 - (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)))
       

    elif asset_class == "large_bank":
        R = 1.25*0.12 * (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)) + 1.25*0.24 * (1 - (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)))
        

    elif asset_class == "retail_residential":
        R = 0.15

    elif asset_class == "retail_revolving":
        R = 0.04

    elif asset_class == "retail_other":
        R = 0.03 * (1 - math.exp(-35 * pd)) / (1 - math.exp(-35)) + 0.16 * (1 - (1 - math.exp(-35 * pd)) / (1 - math.exp(-35)))

    elif asset_class == "specialised_lending":
        R = 0.12 * (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)) + 0.24 * (1 - (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)))

    elif asset_class == "hv_commercial_real_estate":
        R = 0.12 * (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)) + 0.30 * (1 - (1 - math.exp(-50 * pd)) / (1 - math.exp(-50)))

    return R
 

def calculate_MAdj(asset_class, pd, maturity):   
    M = maturity if maturity else 2.5
    M = max(1, min(M, 5))
    b = (0.11852 - 0.05478 * math.log(pd)) ** 2
    if asset_class == "corporate":
        Madj=(1+(M-2.5)*b)/(1-1.5*b)
    
    elif asset_class == "sovereign":
        Madj=(1+(M-2.5)*b)/(1-1.5*b)
    
    elif asset_class == "SME":
        Madj=(1+(M-2.5)*b)/(1-1.5*b)
    
    elif asset_class == "large_bank":
        Madj=(1+(M-2.5)*b)/(1-1.5*b)
        
    
    elif asset_class == "bank":
        Madj=(1+(M-2.5)*b)/(1-1.5*b)

    elif asset_class == "retail_residential":
        Madj=1

    elif asset_class == "retail_revolving":
        Madj=1

    elif asset_class == "retail_other":
        Madj=1

    elif asset_class == "specialised_lending":
        Madj=1

    elif asset_class == "hv_commercial_real_estate":
        Madj=(1+(M-2.5)*b)/(1-1.5*b)

    return Madj
    
def calculate_RWA(asset_class, pd, M, s=50, LGD=0.4, Nominal=1,EAD=1):       
    R=calculate_corr(asset_class, pd, s)
    K = (norm.cdf((norm.ppf(pd) + math.sqrt(R) * norm.ppf(0.999)) / math.sqrt(1 - R)) - pd) * LGD
    print(R)
    print(K)
    Madj = calculate_MAdj(asset_class, pd, M)
    print(Madj)
    rwa = K * 12.5*Madj*EAD/Nominal
    print(rwa)
    
    return rwa

def Basel_calc(event):
    input_text = document.querySelector("#nominal")
    nominal = float(input_text.value)
    input_text = document.querySelector("#pd")
    pd = float(input_text.value)
    input_text = document.querySelector("#ead")
    ead = float(input_text.value)
    input_text = document.querySelector("#lgd")
    lgd = float(input_text.value)
    input_text = document.querySelector("#asset_class")
    asset_class=input_text.value
    input_text = document.querySelector("#M")
    M = float(input_text.value)
    input_text = document.querySelector("#u")
    u = float(input_text.value)
    output_div=document.querySelector("#output")
    output1_div=document.querySelector("#output1")
    output2_div=document.querySelector("#output2")
    a=round(calculate_RWA(asset_class, pd, M, u, lgd, nominal,ead)*100,2)
    b=a*nominal/100
    c=round(calculate_corr(asset_class, pd, u)*100,2)
    output_div.innerText=a
    output1_div.innerText=b
    output2_div.innerText=c