Git Product home page Git Product logo

bond_valuations_python's Introduction

Bond Valuations With Python

Calculate bond valuations given a par value, coupon rate, time to maturity, and yield Implemented using Python, NumPy, Pandas, and PrettyPrint

Import

import pandas as pd
import numpy as np
from pprint import pprint

Bond Price

def bond_price(par_val, coup_rate, ttm, yld):

#Create empty Dictionary to pass into Pandas DataFrame
    data = {'Cash_Flow': [],
                 'Time': [],
            'PV_Factor': [],
                   'PV': []
           }
    #Create DataFrame
    df = pd.DataFrame(data, columns=['Cash_Flow', 'Time', 'PV_Factor', 'PV'])
    #Generate Cash_Flow content
    cash_flow = np.array(np.repeat(int(par_val*coup_rate), ttm-1))
    #Push Cash_Flow records into DataFrame
    df.Cash_Flow = np.append(cash_flow, int(par_val*(1+coup_rate)))
    #Create Time record representing years, add to DataFrame
    df.Time = df.index+1
    #Create PV Factor, add to DataFrame
    df.PV_Factor = np.array(1/(1+yld)**df['Time'])
    #Create PV record and add to DataFrame
    df.PV = np.array(df.Cash_Flow * df.PV_Factor)
    #Set index to Time(Years)
    df.set_index('Time', inplace=True)
    #Return DataFrame and sum of PV column
    return df, sum(df.PV)

Example set

Conditions: par value = $100, coupon rate = %5, time to maturity = 5 years, and yield = %6

dataset, bondval = bond_price(100, 0.05, 5, 0.06)
pprint(dataset)
print("Total Bond Price: $" + str(bondval))

bond_valuations_python's People

Contributors

ajh1143 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

cnxtech karagul

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.