yellowbean / AbsBox

a cashflow engine wrapper for structured finance professionals
https://pypi.org/project/absbox/
Apache License 2.0
37 stars 5 forks source link

Plotting candy function: Asset-vs-Liability #15

Open yellowbean opened 1 year ago

yellowbean commented 1 year ago

y -> time line axis

x -> a pair of two stacks bars over the projection period x ->

yellowbean commented 1 year ago

prioritizing this as coming rating adjust for china. it's good to show the OC level for the senior tranches

yellowbean commented 1 year ago

Prototype

image
yellowbean commented 1 year ago
def plot_bs(x, excludeItems=["FeeDue","IntAccrue","Account"]):
    import matplotlib.pyplot as plt
    import numpy as np
    dates = x.index.to_list() 

    liabilityItems = x['liability'].to_dict(orient='list')
    assetItems = x['asset'].to_dict(orient='list')

    highest_y1 = max([ max(v) for k,v in liabilityItems.items()])
    highest_y2 = max([ max(v) for k,v in assetItems.items()])

    if "FeeDue" in set(excludeItems):
        liabilityItems = {k:v for (k,v) in liabilityItems.items() if (not k.startswith("Fee Due:"))}

    if "IntAccrue" in set(excludeItems):
        liabilityItems = {k:v for (k,v) in liabilityItems.items() if (not k.startswith("Accured Int:")) }

    if "Account" in set(excludeItems):
        assetItems = {k:v for (k,v) in assetItems.items()  if k not in excludeItems }

    balanceItems = {
        'Asset': assetItems,
        'Liability': liabilityItems,
    }

    x = np.arange(len(dates))  # the label locations
    width = 0.40  # the width of the bars
    multiplier = 0

    fig, ax = plt.subplots(layout='constrained')

    for assetClass, assetBreakdown in balanceItems.items():
        offset = width * multiplier
        # plot asset 
        bottom = np.zeros(len(dates))
        for (breakdownsK,breakdowns) in assetBreakdown.items():
            #print(f"plotting bar,data={breakdowns},offset={offset},bottom={bottom}")
            rects = ax.bar(x + offset, breakdowns, width, label=breakdownsK, bottom=bottom)
            bottom += breakdowns

            ax.bar_label(rects, padding=1,label_type="center")
        multiplier += 1

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel('Amount')
    ax.set_title('Projected Captial Structure')
    ax.set_xticks(x + width/2, dates)
    ax.legend(loc='upper right')
    ax.set_ylim(0, max([highest_y1,highest_y2])*1.2)
    plt.show()