Python Forum

Full Version: another alternative to np.interp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am doing finance programming and i need another solution instead of using numpy's np.interp. I have a yield curve table which shows interest rates for bonds, for the respective tenure.

i am new to python

the tenures are 1 month, 2 months, 3 months.... up to 30 years, i converted to fraction and whole numbers for calculation purpose. there are 12 tenure and 12 interest rate respectively.

tenure = [1/12, 2/12, 3/12, 6/12, 1, 2, 3, 5, 7, 10, 20, 30]
interest_rates = [2.40, 2.40, 2.42, 2.51, 2.60, 2.50, 2.47, 2.49, 2.56, 2.66, 2.83, 2.97]

i understand that one way i can do is to use np.interp

calculate_interest_rate = np.interp (year, tenure, interest_rates)
year = x (my input depending which year i am interested in)

i need to make a user defined function to automate the calculation of the interest rate for the respective tenure. for example, a 1.5 (input) year bond will return 2.55% of interest rate.

is there another another way i can get the same outcome but not using np.interp? something like a for loop or something? i really have no idea.

also, i am given a mathematical formula for calculation of the interest rate, the required inputs in for formula are the tenure and interest rate before and after the tenure i want to calculate. is there a way i can get the numbers with python function, and then use the numbers i obtain to sub into my mathematical formula?

for example, if i want to calculate the interest rate at 1.5 years
the inputs i need in the mathematical function are:
1 year (tenure) - 2.60% (interest rates)
2 year (tenure) - 2.50% (interest rates)