def my_portfolio_generator(N, max_months):
    possible_duration = np.array([1, 3, 6, 12, 24, 36, 60, 84])
    coupon_rate = np.array([1, 3, 4, 6, 12])
    Coupons_per_year = np.zeros(N)
    Interest = np.zeros(N)
    P = np.zeros((N, max_months))
    Type = np.zeros(N)

    # loop to guarantee one of each bond type in order
    for x in np.arange(0, 8):
        i = np.random.uniform(0.01, 0.1)
        Interest[x] = i
        tipe = possible_duration[x]
        Type[x] = tipe
        cr = coupon_rate[coupon_rate <= tipe]
        ncp = cr[np.random.randint(np.size(cr))]

        Coupons_per_year[x] = ncp
        P[x] = im.my_bond_generator(max_months, tipe, 1, i, ncp)

    # loop to randomize the rest of the portfolio
    for x in np.arange(8, N):
        i = np.random.uniform(0.01, 0.1)
        Interest[x] = i
        possible_duration = possible_duration[possible_duration <= max_months]
        tipe = possible_duration[np.random.randint(np.size(possible_duration))]
        Type[x] = tipe
        cr = coupon_rate[coupon_rate <= tipe]
        ncp = cr[np.random.randint(np.size(cr))]  # calculate the number of
        # coupon payments in a year
        Coupons_per_year[x] = ncp
        P[x] = im.my_bond_generator(max_months, tipe, 1, i, ncp)

    return P, Type, Coupons_per_year
Beispiel #2
0
def Lgen(N, max_months, possible_duration, coupon_rate):
    Coupons_per_year = np.zeros(N)
    Interest = np.zeros(N)
    Pl = np.zeros((N, max_months))
    r = np.arange(N)
    Typel = np.zeros(N)

    for x in r:
        i = np.random.uniform(0.01, 0.1)
        Interest[x] = i
        possible_duration = possible_duration[possible_duration <= max_months]
        tipe = possible_duration[x]
        Typel[x] = tipe
        cr = coupon_rate[coupon_rate <= tipe]
        ncp = cr[np.random.randint(np.size(cr))]  # calculate the number of
        # coupon payments in a year
        Coupons_per_year[x] = ncp
        Pl[x] = im.my_bond_generator(max_months, tipe, 1, i, ncp)

        L_portfolio = Pl
        L_Type = Typel
        L_CPY = Coupons_per_year
    return L_portfolio, L_Type, L_CPY
def firstMonth(N, Type, possible_types, considered, coupon_rate, max_months,
               monthly_rates, Portfolio_A, Portfolio_L, transaction_cost, I,
               LType, Liability_number, transaction, K):
    # Inputs: N, possible_types, Type, I, considered, coupon_rate, max_months,
    #         monthly_rates, Portfolio_A, Portfolio_L, Liability_number

    # Standard computation
    for y in np.arange(N):
        if Type[y] == 1 & y != 0:
            continue
        pt = possible_types.copy()
        choice = pt[possible_types <= Type[y]][-1]
        LType[y] = choice

        liability_interest = im.my_extract_rates(I, choice)
        LI = liability_interest[considered - 1]
        LI = im.my_monthly_effective_rate(LI)
        li = np.random.uniform(0.01, 0.1)
        cr = coupon_rate[coupon_rate <= choice]
        ncp = cr[np.random.randint(np.size(cr))]
        Portfolio_L[y] = im.my_bond_generator(max_months, choice, 1, li, ncp)

        asset_rate = im.my_extract_rates(monthly_rates, Type[y])
        asset_rate = asset_rate[considered - 1]

        macD_A = im.my_macD(Portfolio_A[y], asset_rate)
        macD_A = 12 * macD_A
        macD_L = im.my_macD(Portfolio_L[y], LI)
        macD_L = 12 * macD_L
        PVA = im.my_present_value(Portfolio_A[y], asset_rate)
        PVL = im.my_present_value(Portfolio_L[y], LI)

        Liability_number[y] = (macD_A * PVA /
                               (1 + asset_rate)) / (macD_L * PVL / (1 + LI))
        net = np.abs(PVA - Liability_number[y] * PVL)
        acquisition = Liability_number[y] * PVL

        transaction[y] = transaction_cost * (net + acquisition)
    # FOR loop end

    # Skip index for krd
    use = np.arange(N)
    index = np.ones(N)
    vuse = np.arange(N)
    vindex = np.ones(N)

    for y in np.arange(N):
        if (Type[y] == 1):
            index[y] = 0
        elif (Type[y] == 1):
            vindex[y] = 0

    use = use[index == 1]
    vuse = vuse[vindex == 1]

    # KRD computation
    NL = K.size + 1
    # one more than key rate durations

    Portfolio_L_krd = Portfolio_L[0:NL]
    interp_rates = krd.rateinterp(monthly_rates, considered, max_months)  # krd
    Qa = np.ones(krd.nbonds(Portfolio_A[use]))
    N2short, err, w = krd.immunize(Portfolio_A[use],
                                   interp_rates,
                                   Qa,
                                   Portfolio_L_krd,
                                   K,
                                   r=1)
    Liability_number_krd = np.negative(N2short)

    transaction_krd = np.zeros(transaction.shape)
    acquisition_krd = np.zeros(transaction.shape)
    Lkrd = np.zeros(transaction.shape)
    #transaction_krd[NL] = krd.portfolio(Portfolio_A[NL:],interp_rates,Qa[NL:],K)[1]
    for y in np.arange(NL):
        acquisition_krd[y] = Liability_number_krd[y] * krd.bond(
            Portfolio_L_krd[y], interp_rates, K)[1]
        Lkrd[y] = Liability_number_krd[y] * krd.bond(Portfolio_L_krd[y],
                                                     interp_rates, K)[1]
    net_krd = np.abs(
        krd.portfolio(Portfolio_A[use], interp_rates, Qa, K)[1] - np.sum(Lkrd))

    transaction_krd = transaction_cost * (net_krd + np.sum(acquisition_krd))

    return transaction, Liability_number, Portfolio_L, transaction_krd, Liability_number_krd, Portfolio_L_krd
Beispiel #4
0
    transaction = np.zeros(N)
    for y in np.arange(N):
        if Type[y] == 1:
            continue
        pt = possible_types.copy()
        choice = pt[possible_types < Type[y]][-1]
        
        # generating libaiblilty portfolio
        liability_interest = im.my_extract_rates(I,choice)
        LI = liability_interest[considered-1]
        LI = im.my_monthly_effective_rate(LI)
        li = np.random.uniform(0.01, 0.1)
        cr = coupon_rate[coupon_rate <= choice]
        ncp = cr[np.random.randint(np.size(cr))]
        Portfolio_L[y] = im.my_bond_generator(max_months, choice, 1, li, ncp)
        
        asset_rate = im.my_extract_rates(monthly_rates, Type[y])
        asset_rate = asset_rate[considered-1]
        
        # getting durations for each position, assests and liabilities
        macD_A = im.my_macD(Portfolio_A[y],asset_rate)
        macD_A = 12*macD_A
        macD_L = im.my_macD(Portfolio_L[y], LI)
        macD_L = 12*macD_L

        # present values
        PVA = im.my_present_value(Portfolio_A[y], asset_rate)
        PVL = im.my_present_value(Portfolio_L[y], LI)

        # computing N as a vector which will be over written