<jupyter_start><jupyter_text>import the packages :<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>load data<jupyter_code>#usd ois curve
#deposite
df1 = pd.DataFrame({
    "Tenor" : ['ON','TN','S/N','1W','2W','3W','1M','2M','3M','6M','9M'],
    "Rate Used":[1.60,1.59,1.59,1.59,1.59,1.59,1.59,1.58,1.57,1.51,1.45]
})
#swap
df2 = pd.DataFrame({
    "Tenor" : ['1Y','2Y','3Y','4Y','5Y','6Y','7Y','8Y','9Y','10Y','12Y','15Y','20Y','25Y','30Y'],
    "Rate Used":[1.38,1.22,1.12,1.14,1.15,1.16,1.18,1.21,1.24,1.27,1.31,1.37,1.43,1.45,1.46]
})

#usd 3m libor
df3 = pd.DataFrame({
    "Tenor" : ['ON','TN','S/N','1W','2W','3W','1M','2M','3M'],
    "Rate Used":[1.75] * 9
})
df4 = pd.DataFrame({
    "Tenor" : ['F1','F2','F3','F4','F5','F6','F7','F8'],
    "Rate Used":[98.35,98.47,98.60,98.64,98.75,98.79,98.81,98.78]
})

df5 = pd.DataFrame({
    "Tenor" : ['3Y','4Y','5Y','6Y','7Y','8Y','9Y','10Y','12Y','15Y','20Y','25Y','30Y'],
    "Rate Used":[1.34,1.33,1.33,1.35,1.37,1.40,1.43,1.46,1.51,1.57,1.63,1.65,1.66]
}) 

#usd 1m/3m libor
df6 = pd.DataFrame({
    "Tenor" : ['ON','TN','S/N','1W','2W','3Y','4Y','5Y','7Y','10Y','12Y','15Y','20Y','25Y','30Y'],
    "Rate Used":[15,12,10,9] + [8] * 11
}) 

#usd 3m/6m libor
df7 = pd.DataFrame({
    "Tenor" : ['ON','TN','S/N','1W','2W','3Y','4Y','5Y','7Y','10Y','12Y','15Y','20Y','25Y','30Y'],
    "Rate Used":[20,15,12,11] + [10] * 11
})
# to see what is df , in fact it's just the same as slids 30
# this cell can be delete before uploading . 

df = [df1,df2,df3,df4,df5,df6,df7]
df<jupyter_output><empty_output><jupyter_text>define the functions<jupyter_code>def oiscurve(df1,df2):
    """use the first table to get the ois curve"""
    return yieldcurve

def libor3m(ois_curve,df3,df4,df5):
    """use the got ois curve and the info in table2 to get the 3mlibor"""
    return yieldcurve
    
def libor1m(ois_curve,libor_curve_3m,df6):
    """use 3mliborcurve , oiscurve , and 1m/3m spread , to calculate 1mliborcurve"""
    return yieldcurve

def libor6m(ois_curve,libor_curve_3m,df7):
    """use 3mliborcurve , oiscurve , and 6m/3m spread , to calculate 6mliborcurve"""

def priced(curve):
    """calculate deposite rate from a curve"""
    return df1

def prices(curve):
    """calculate swap rate from a curve"""
    return df2

def pricef(curve):
    """calculate future price from a curve"""
    return df4

def franpv(notional,start,end,ois_curve,rate):
    """calculate notional of start * end ATM rate frome oiscurve"""
    return npv

def swapnpv(notional,start,end,ois_curve,rate):
    """calculate notional of start * end ATM swap rate frome oiscurve"""
    return npv

def spreadnpv(ois_curve,libor_curve_3m,spread):
    """calculate spreat = 1 , what is the npv of the 5th instrument of the portfolio"""
    return npv<jupyter_output><empty_output><jupyter_text>calculation bootstrap multi-yield curve<jupyter_code>ois_curve = oiscurve(df1,df2)
libor_curve_3m = libor3m(ois_curve,df3,df4,df5)
libor_curve_1m = libor1m(ois_curve,df6)
libor_curve_1m = libor6m(ois_curve,df7)

#plot this 4 curve in 1 photo
#plt.show()<jupyter_output><empty_output><jupyter_text>confirm self consistent<jupyter_code>df1r = priced(ois_curve)
df2r = prices(ois_curve)
df3r = priced(libor_curve_3m)
# ...<jupyter_output><empty_output><jupyter_text>ATM rate<jupyter_code>#A * r + B = npv
#r = 1 , npvp = A + B
#r = -1 , npvm = -A + B
#A * r0 + B = 0 , r0 = (npvp+npvm)/(npvm-npvp)
npvp = franpv(200,9,12,ois_curve,1)
npvm = franpv(200,9,12,ois_curve,-1)
r1 = (npvp+npvm)/(npvm-npvp)

#r2 = fra(-150,9,12,ois_curve)
#r3 = swap(300,10,10,ois_curve)
#r4 = swap(-100,5,5,ois_curve)

pd.DataFrame({
    "instrument No.":[1,2,3,4]
    "ATM rate":[r1,r2,r3,r4]
})<jupyter_output><empty_output><jupyter_text>Spread<jupyter_code>p = spreadnpv(ois_curve,libor_curve_3m,1)
m = spreadnpv(ois_curve,libor_curve_3m,-1)

(m+p)/(m-p)<jupyter_output><empty_output><jupyter_text>risk report<jupyter_code>def portfolio(ois_curve,libor_curve_3m):
    npv = np.array([franpv(200,9,12,ois_curve,r1),
                    franpv(-150,6,12,ois_curve,r2),
                    swapnpv(300,10,10,ois_curve,r3),
                    swapnpv(-100,10,10,ois_curve,r4),
                    spreadnpv(ois_curve,libor_curve_3m,r5)
                   ]).sum()
    return npv

for change in ??:
    df[change] = df[change] + 0.01
    port[change] = portfolio(oicurve(??),libor3m(??))
    
port<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20522052205220523, 'fraction_numerical': 0.09698469846984699, 'mean_word_length': 4.825688073394495, 'pattern_counts': {'":': 9, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '258082', 'license': 'MIT', 'n_tokens_mistral': 2227, 'n_tokens_neox': 1989, 'n_words': 311, 'path': 'proj.ipynb', 'repo_name': 'liyusu-tsinghua/5203proj1', 'size': '4444'}